design.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os, re
  4. # sys,
  5. from PyQt5 import QtCore
  6. from PyQt5.QtCore import QUrl, QSettings
  7. from PyQt5.QtGui import QKeySequence, QFont
  8. from PyQt5.QtWidgets import QWidget, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit, QShortcut, QPushButton, QCheckBox
  9. from PyQt5.QtWebKit import QWebSettings
  10. from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
  11. class WebkitView(QWebView):
  12. def __init__(self, parent, port):
  13. self.port = port
  14. self.view = QWebView.__init__(self, parent)
  15. self.load(QUrl('http://localhost:'+str(self.port)))
  16. self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
  17. # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  18. class WebkitInspector(QWebInspector):
  19. def __init__(self, parent, webkitview):
  20. super(WebkitInspector, self).__init__(parent)
  21. self.webkitview = webkitview
  22. self.setPage(self.webkitview.page())
  23. self.showMaximized()
  24. # TODO: webkitinspector is disappearing when chaging tabs
  25. class WebViewOptions(QWidget):
  26. def __init__(self, parent):
  27. super(WebViewOptions, self).__init__(parent)
  28. self.parent = parent
  29. self.hbox = QHBoxLayout()
  30. self.hbox.setContentsMargins(0,0,0,0)
  31. self.preview = QCheckBox('&Preview', self)
  32. # self.preview.setShortcut('Ctrl+Shift+p')
  33. self.preview.clicked.connect(self.onPreview)
  34. self.hbox.addWidget(self.preview)
  35. self.debug = QCheckBox('Deb&ug', self)
  36. # self.debug.setShortcut('Ctrl+Shift+u')
  37. self.debug.clicked.connect(self.onDebug)
  38. self.hbox.addWidget(self.debug)
  39. self.grid = QCheckBox('&Grid', self)
  40. # self.grid.setShortcut('Ctrl+Shift+g')
  41. self.grid.clicked.connect(self.onGrid)
  42. self.hbox.addWidget(self.grid)
  43. # spread
  44. self.spread = QCheckBox('&Spread', self)
  45. # self.spread.setShortcut('Ctrl+Shift+g')
  46. self.spread.clicked.connect(self.onSpread)
  47. self.hbox.addWidget(self.spread)
  48. self.hbox.addStretch()
  49. # zoom
  50. # page
  51. self.hbox.addStretch()
  52. self.reload = QPushButton("&Reload", self)
  53. # self.reload.setShortcut('Ctrl+Shift+r')
  54. # TODO: how to define same shortcut in different places
  55. # self.reload.setIcon(Icon(ico)))
  56. self.reload.clicked.connect(self.onReload)
  57. self.hbox.addWidget(self.reload)
  58. self.setLayout(self.hbox)
  59. def onReload(self):
  60. print("onReload")
  61. # self.parent.webkitview.
  62. def onPreview(self):
  63. print('onPreview')
  64. def onDebug(self):
  65. print('onDebug')
  66. def onGrid(self):
  67. print('onGrid')
  68. def onSpread(self):
  69. print('onSpread')
  70. class CodeEditor(QPlainTextEdit):
  71. def __init__(self, core, tabs, file=None):
  72. super(CodeEditor, self).__init__()
  73. self.core = core
  74. self.tabs = tabs
  75. # self.file = file
  76. self.file = os.path.join(self.core.cwd,file)
  77. self.insertPlainText(open(self.file, 'r').read())
  78. self.changed = False
  79. self.textChanged.connect(self.onTextChanged)
  80. self.shortcut = QShortcut(QKeySequence("Ctrl+s"), self)
  81. self.shortcut.activated.connect(self.save)
  82. def onTextChanged(self):
  83. # print('textChanged')
  84. # print(self.toPlainText())
  85. # open(self.file, 'w').write(self.toPlainText())
  86. if not self.changed:
  87. self.changed = True
  88. i = self.tabs.currentIndex()
  89. self.tabs.setTabText(i, "* "+self.tabs.tabText(i))
  90. def save(self):
  91. if self.changed:
  92. open(self.file, 'w').write(self.toPlainText())
  93. i = self.tabs.currentIndex()
  94. self.tabs.setTabText(i, re.sub(r'^\*\s', '', self.tabs.tabText(i)))
  95. self.changed = False
  96. # TODO: how to combine file save and project save
  97. class Editor(QWidget):
  98. def __init__(self, parent, core):
  99. super(Editor, self).__init__()
  100. self.core = core
  101. self.layout = QVBoxLayout(self)
  102. self.layout.setContentsMargins(0,0,0,0)
  103. # Initialize tab screen
  104. self.tabs = QTabWidget()
  105. self.scsstab = CodeEditor(core, self.tabs, 'assets/css/styles.scss')
  106. self.jstab = CodeEditor(core, self.tabs, 'assets/js/script.js')
  107. # Add tabs
  108. self.tabs.addTab(self.scsstab,"scss")
  109. self.tabs.addTab(self.jstab,"js")
  110. # Add tabs to widget
  111. self.layout.addWidget(self.tabs)
  112. self.setLayout(self.layout)
  113. # font = QFont()
  114. # font.setFamily('Courier')
  115. # font.setFixedPitch(True)
  116. # font.setPointSize(10)
  117. # self.setFont(font)
  118. # self.highlighter = Highlighter(self.document())
  119. # https://pypi.python.org/pypi/QScintilla/2.9.2
  120. class DesignStack(QWidget):
  121. def __init__(self, core):
  122. super(DesignStack, self).__init__()
  123. # self.grid = QGridLayout()
  124. self.hbox = QHBoxLayout()
  125. self.hbox.setContentsMargins(0,0,0,0)
  126. self.setLayout(self.hbox)
  127. self.webview = QWidget()
  128. self.webview.vbox = QVBoxLayout()
  129. self.webview.setLayout(self.webview.vbox)
  130. self.webview.vbox.setContentsMargins(0,0,0,0)
  131. self.webkitview = WebkitView(self, core.server.port)
  132. self.webkitinspector = WebkitInspector(self, self.webkitview)
  133. shortcut = QShortcut(self)
  134. shortcut.setKey("F12")
  135. shortcut.activated.connect(self.toggleInspector)
  136. self.webkitinspector.setVisible(False)
  137. self.webviewoptions = WebViewOptions(self)
  138. self.webview.vbox.addWidget(self.webviewoptions)
  139. self.vsplitter = QSplitter(QtCore.Qt.Vertical)
  140. self.vsplitter.addWidget(self.webkitview)
  141. self.vsplitter.addWidget(self.webkitinspector)
  142. self.vsplitter.splitterMoved.connect(self.movedSplitter)
  143. self.webview.vbox.addWidget(self.vsplitter)
  144. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  145. self.hsplitter.addWidget(self.webview)
  146. self.editor = Editor(self, core)
  147. self.hsplitter.addWidget(self.editor)
  148. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  149. self.hbox.addWidget(self.hsplitter)
  150. self.restorePrefs()
  151. def toggleInspector(self):
  152. self.webkitinspector.setVisible(not self.webkitinspector.isVisible())
  153. def restorePrefs(self):
  154. settings = QSettings('FiguresLibres', 'Cascade')
  155. print(settings.value('design/vsplitter/sizes', self.vsplitter.sizes()))
  156. vals = settings.value('design/vsplitter/sizes', None)
  157. if vals:
  158. sizes = []
  159. for size in vals: sizes.append(int(size))
  160. self.vsplitter.setSizes(sizes)
  161. vals = settings.value('design/hsplitter/sizes', None)
  162. if vals:
  163. sizes = []
  164. for size in vals: sizes.append(int(size))
  165. self.hsplitter.setSizes(sizes)
  166. def movedSplitter(self):
  167. settings = QSettings('FiguresLibres', 'Cascade')
  168. # print(self.vsplitter.sizes())
  169. settings.setValue('design/vsplitter/sizes', self.vsplitter.sizes())
  170. settings.setValue('design/hsplitter/sizes', self.hsplitter.sizes())