design.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. from PyQt5.QtPrintSupport import QPrintPreviewDialog, QPrinter
  12. class WebkitView(QWebView):
  13. def __init__(self, parent, port):
  14. self.port = port
  15. self.view = QWebView.__init__(self, parent)
  16. self.load(QUrl('http://localhost:'+str(self.port)))
  17. self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
  18. # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  19. self.printer = QPrinter()
  20. self.printer.setPageSize(QPrinter.A4)
  21. self.printer.setOrientation(QPrinter.Portrait)
  22. self.printer.setPageMargins(10,10,10,10,QPrinter.Millimeter)
  23. self.setFixedWidth(1000)
  24. def onPrint(self):
  25. dialog = QPrintPreviewDialog(self.printer)
  26. # dialog.setWindowState(Qt.WindowMaximized)
  27. dialog.paintRequested.connect(self.print_)
  28. # dialog.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint | Qt.WindowContextHelpButtonHint)
  29. dialog.exec()
  30. class WebkitInspector(QWebInspector):
  31. def __init__(self, parent, webkitview):
  32. super(WebkitInspector, self).__init__(parent)
  33. self.webkitview = webkitview
  34. self.setPage(self.webkitview.page())
  35. self.showMaximized()
  36. # TODO: webkitinspector is disappearing when chaging tabs
  37. class WebViewToolBar(QWidget):
  38. def __init__(self, parent):
  39. super(WebViewToolBar, self).__init__(parent)
  40. self.parent = parent
  41. self.hbox = QHBoxLayout()
  42. self.hbox.setContentsMargins(0,0,0,0)
  43. self.preview = QCheckBox('&Preview', self)
  44. # self.preview.setShortcut('Ctrl+Shift+p')
  45. self.preview.clicked.connect(self.onPreview)
  46. self.hbox.addWidget(self.preview)
  47. self.debug = QCheckBox('Deb&ug', self)
  48. # self.debug.setShortcut('Ctrl+Shift+u')
  49. self.debug.clicked.connect(self.onDebug)
  50. self.hbox.addWidget(self.debug)
  51. self.grid = QCheckBox('&Grid', self)
  52. # self.grid.setShortcut('Ctrl+Shift+g')
  53. self.grid.clicked.connect(self.onGrid)
  54. self.hbox.addWidget(self.grid)
  55. # spread
  56. self.spread = QCheckBox('&Spread', self)
  57. # self.spread.setShortcut('Ctrl+Shift+g')
  58. self.spread.clicked.connect(self.onSpread)
  59. self.hbox.addWidget(self.spread)
  60. self.hbox.addStretch()
  61. # zoom
  62. # page
  63. self.hbox.addStretch()
  64. self.reload = QPushButton("&Reload", self)
  65. # self.reload.setShortcut('Ctrl+Shift+r')
  66. # TODO: how to define same shortcut in different places
  67. # self.reload.setIcon(Icon(ico)))
  68. self.reload.clicked.connect(self.onReload)
  69. self.hbox.addWidget(self.reload)
  70. self.imprim = QPushButton("&Print", self)
  71. # self.imprim.setShortcut('Ctrl+Shift+r')
  72. # TODO: how to define same shortcut in different places
  73. # self.imprim.setIcon(Icon(ico)))
  74. self.imprim.clicked.connect(self.onPrint)
  75. self.hbox.addWidget(self.imprim)
  76. self.setLayout(self.hbox)
  77. def onPreview(self):
  78. print('onPreview')
  79. def onDebug(self):
  80. print('onDebug')
  81. def onGrid(self):
  82. print('onGrid')
  83. def onSpread(self):
  84. print('onSpread')
  85. def onReload(self):
  86. print("onReload")
  87. # self.parent.webkitview.
  88. def onPrint(self):
  89. print("onReload")
  90. self.parent.webkitview.onPrint()
  91. class CodeEditor(QPlainTextEdit):
  92. def __init__(self, core, tabs, file=None):
  93. super(CodeEditor, self).__init__()
  94. self.core = core
  95. self.tabs = tabs
  96. self.file = file
  97. self.setText()
  98. self.shortcut = QShortcut(QKeySequence("Ctrl+s"), self)
  99. self.shortcut.activated.connect(self.save)
  100. def setText(self):
  101. try:
  102. self.textChanged.disconnect(self.onTextChanged)
  103. except Exception as e:
  104. print(e)
  105. self.filepath = os.path.join(self.core.cwd,self.file)
  106. self.clear()
  107. self.insertPlainText(open(self.filepath, 'r').read())
  108. self.changed = False
  109. self.textChanged.connect(self.onTextChanged)
  110. def onTextChanged(self):
  111. # print('textChanged')
  112. # print(self.toPlainText())
  113. # open(self.filepath, 'w').write(self.toPlainText())
  114. if not self.changed:
  115. self.changed = True
  116. i = self.tabs.currentIndex()
  117. self.tabs.setTabText(i, "* "+self.tabs.tabText(i))
  118. def save(self):
  119. if self.changed:
  120. open(self.filepath, 'w').write(self.toPlainText())
  121. i = self.tabs.currentIndex()
  122. self.tabs.setTabText(i, re.sub(r'^\*\s', '', self.tabs.tabText(i)))
  123. self.changed = False
  124. # TODO: how to combine file save and project save
  125. class Editor(QWidget):
  126. def __init__(self, parent, core):
  127. super(Editor, self).__init__()
  128. self.core = core
  129. self.layout = QVBoxLayout(self)
  130. self.layout.setContentsMargins(0,0,0,0)
  131. # Initialize tab screen
  132. self.tabs = QTabWidget()
  133. self.scsstab = CodeEditor(core, self.tabs, 'assets/css/styles.scss')
  134. self.jstab = CodeEditor(core, self.tabs, 'assets/js/script.js')
  135. # Add tabs
  136. self.tabs.addTab(self.scsstab,"scss")
  137. self.tabs.addTab(self.jstab,"js")
  138. # Add tabs to widget
  139. self.layout.addWidget(self.tabs)
  140. self.setLayout(self.layout)
  141. # font = QFont()
  142. # font.setFamily('Courier')
  143. # font.setFixedPitch(True)
  144. # font.setPointSize(10)
  145. # self.setFont(font)
  146. # self.highlighter = Highlighter(self.document())
  147. # https://pypi.python.org/pypi/QScintilla/2.9.2
  148. def refresh(self):
  149. self.scsstab.setText()
  150. self.jstab.setText()
  151. class DesignStack(QWidget):
  152. def __init__(self, core):
  153. super(DesignStack, self).__init__()
  154. # self.grid = QGridLayout()
  155. self.hbox = QHBoxLayout()
  156. self.hbox.setContentsMargins(0,0,0,0)
  157. self.setLayout(self.hbox)
  158. self.webview = QWidget()
  159. self.webview.vbox = QVBoxLayout()
  160. self.webview.setLayout(self.webview.vbox)
  161. self.webview.vbox.setContentsMargins(0,0,0,0)
  162. self.webkitview = WebkitView(self, core.server.port)
  163. self.webkitinspector = WebkitInspector(self, self.webkitview)
  164. shortcut = QShortcut(self)
  165. shortcut.setKey("F12")
  166. shortcut.activated.connect(self.toggleInspector)
  167. self.webkitinspector.setVisible(False)
  168. self.webviewtoolbar = WebViewToolBar(self)
  169. self.webview.vbox.addWidget(self.webviewtoolbar)
  170. self.vsplitter = QSplitter(QtCore.Qt.Vertical)
  171. self.vsplitter.addWidget(self.webkitview)
  172. self.vsplitter.addWidget(self.webkitinspector)
  173. self.vsplitter.splitterMoved.connect(self.movedSplitter)
  174. self.webview.vbox.addWidget(self.vsplitter)
  175. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  176. self.hsplitter.addWidget(self.webview)
  177. self.editor = Editor(self, core)
  178. self.hsplitter.addWidget(self.editor)
  179. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  180. self.hbox.addWidget(self.hsplitter)
  181. self.restorePrefs()
  182. def toggleInspector(self):
  183. self.webkitinspector.setVisible(not self.webkitinspector.isVisible())
  184. def restorePrefs(self):
  185. settings = QSettings('FiguresLibres', 'Cascade')
  186. print(settings.value('design/vsplitter/sizes', self.vsplitter.sizes()))
  187. vals = settings.value('design/vsplitter/sizes', None)
  188. if vals:
  189. sizes = []
  190. for size in vals: sizes.append(int(size))
  191. self.vsplitter.setSizes(sizes)
  192. vals = settings.value('design/hsplitter/sizes', None)
  193. if vals:
  194. sizes = []
  195. for size in vals: sizes.append(int(size))
  196. self.hsplitter.setSizes(sizes)
  197. def movedSplitter(self):
  198. settings = QSettings('FiguresLibres', 'Cascade')
  199. # print(self.vsplitter.sizes())
  200. settings.setValue('design/vsplitter/sizes', self.vsplitter.sizes())
  201. settings.setValue('design/hsplitter/sizes', self.hsplitter.sizes())
  202. def refresh(self):
  203. self.editor.refresh()
  204. self.webkitview.reload()