design.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, os, re
  4. from PyQt5 import QtCore
  5. from PyQt5.QtCore import QUrl, QSettings
  6. from PyQt5.QtGui import QKeySequence, QFont, QSyntaxHighlighter
  7. from PyQt5.QtWidgets import QWidget, QLabel, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit, QShortcut
  8. from PyQt5.QtWebKit import QWebSettings
  9. from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
  10. class WebkitView(QWebView):
  11. def __init__(self, parent, port):
  12. self.port = port
  13. self.view = QWebView.__init__(self, parent)
  14. self.load(QUrl('http://localhost:'+str(self.port)))
  15. self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
  16. # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  17. class WebkitInspector(QWebInspector):
  18. def __init__(self, parent, webkitview):
  19. super(WebkitInspector, self).__init__(parent)
  20. self.webkitview = webkitview
  21. self.setPage(self.webkitview.page())
  22. self.showMaximized()
  23. # TODO: webkitinspector is disappearing when chaging tabs
  24. class CodeEditor(QPlainTextEdit):
  25. def __init__(self, core, tabs, file=None):
  26. super(CodeEditor, self).__init__()
  27. self.core = core
  28. self.tabs = tabs
  29. # self.file = file
  30. self.file = os.path.join(self.core.cwd,file)
  31. self.insertPlainText(open(self.file, 'r').read())
  32. self.changed = False
  33. self.textChanged.connect(self.onTextChanged)
  34. self.shortcut = QShortcut(QKeySequence("Ctrl+s"), self)
  35. self.shortcut.activated.connect(self.save)
  36. def onTextChanged(self):
  37. # print('textChanged')
  38. # print(self.toPlainText())
  39. # open(self.file, 'w').write(self.toPlainText())
  40. if not self.changed:
  41. self.changed = True
  42. i = self.tabs.currentIndex()
  43. self.tabs.setTabText(i, "* "+self.tabs.tabText(i))
  44. def save(self):
  45. if self.changed:
  46. open(self.file, 'w').write(self.toPlainText())
  47. i = self.tabs.currentIndex()
  48. self.tabs.setTabText(i, re.sub(r'^\*\s', '', self.tabs.tabText(i)))
  49. self.changed = False
  50. # TODO: how to combine file save and project save
  51. class Editor(QWidget):
  52. def __init__(self, parent, core):
  53. super(Editor, self).__init__()
  54. self.core = core
  55. self.layout = QVBoxLayout(self)
  56. self.layout.setContentsMargins(0,0,0,0)
  57. # Initialize tab screen
  58. self.tabs = QTabWidget()
  59. self.scsstab = CodeEditor(core, self.tabs, 'assets/css/styles.scss')
  60. self.jstab = CodeEditor(core, self.tabs, 'assets/js/script.js')
  61. # Add tabs
  62. self.tabs.addTab(self.scsstab,"scss")
  63. self.tabs.addTab(self.jstab,"js")
  64. # Add tabs to widget
  65. self.layout.addWidget(self.tabs)
  66. self.setLayout(self.layout)
  67. # font = QFont()
  68. # font.setFamily('Courier')
  69. # font.setFixedPitch(True)
  70. # font.setPointSize(10)
  71. # self.setFont(font)
  72. # self.highlighter = Highlighter(self.document())
  73. # https://pypi.python.org/pypi/QScintilla/2.9.2
  74. class DesignStack(QWidget):
  75. def __init__(self, core):
  76. super(DesignStack, self).__init__()
  77. # self.grid = QGridLayout()
  78. hbox = QHBoxLayout()
  79. hbox.setContentsMargins(0,0,0,0)
  80. self.setLayout(hbox)
  81. # webviewbox = QVBoxLayout()
  82. self.vsplitter = QSplitter(QtCore.Qt.Vertical)
  83. self.webkitview = WebkitView(self, core.server.port)
  84. self.vsplitter.addWidget(self.webkitview)
  85. self.webkitinspector = WebkitInspector(self, self.webkitview)
  86. self.vsplitter.addWidget(self.webkitinspector)
  87. shortcut = QShortcut(self)
  88. shortcut.setKey("F12")
  89. shortcut.activated.connect(self.toggleInspector)
  90. self.webkitinspector.setVisible(False)
  91. self.vsplitter.splitterMoved.connect(self.movedSplitter)
  92. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  93. self.hsplitter.addWidget(self.vsplitter)
  94. self.editor = Editor(self, core)
  95. self.hsplitter.addWidget(self.editor)
  96. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  97. hbox.addWidget(self.hsplitter)
  98. self.restorePrefs()
  99. def toggleInspector(self):
  100. self.webkitinspector.setVisible(not self.webkitinspector.isVisible())
  101. def restorePrefs(self):
  102. settings = QSettings('FiguresLibres', 'Cascade')
  103. print(settings.value('design/vsplitter/sizes', self.vsplitter.sizes()))
  104. vals = settings.value('design/vsplitter/sizes', None)
  105. if vals:
  106. sizes = []
  107. for size in vals: sizes.append(int(size))
  108. self.vsplitter.setSizes(sizes)
  109. vals = settings.value('design/hsplitter/sizes', None)
  110. if vals:
  111. sizes = []
  112. for size in vals: sizes.append(int(size))
  113. self.hsplitter.setSizes(sizes)
  114. def movedSplitter(self):
  115. settings = QSettings('FiguresLibres', 'Cascade')
  116. # print(self.vsplitter.sizes())
  117. settings.setValue('design/vsplitter/sizes', self.vsplitter.sizes())
  118. settings.setValue('design/hsplitter/sizes', self.hsplitter.sizes())