design.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # @Author: Bachir Soussi Chiadmi <bach>
  4. # @Date: 23-05-2017
  5. # @Email: bachir@figureslibres.io
  6. # @Filename: design.py
  7. # @Last modified by: bach
  8. # @Last modified time: 03-06-2017
  9. # @License: GPL-V3
  10. import os, re
  11. # sys,
  12. from PyQt5 import QtCore
  13. from PyQt5.QtCore import QUrl, QSettings, QSizeF
  14. from PyQt5.QtGui import QKeySequence, QFont
  15. from PyQt5.QtWidgets import QWidget, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit, QShortcut, QPushButton, QCheckBox, QSpinBox, QLabel
  16. from PyQt5.QtWebKit import QWebSettings
  17. from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
  18. from PyQt5.QtPrintSupport import QPrintPreviewDialog, QPrinter
  19. from classes import highlighter
  20. # _ __ __ _ ___
  21. # | | / /__ / /_| | / (_)__ _ __
  22. # | | /| / / _ \/ __ \ | / / / _ \ | /| / /
  23. # | |/ |/ / __/ /_/ / |/ / / __/ |/ |/ /
  24. # |__/|__/\___/_.___/|___/_/\___/|__/|__/
  25. class WebkitView(QWebView):
  26. def __init__(self, parent, core):
  27. self.parent = parent
  28. self.core = core
  29. self.port = core.server.port
  30. self.view = QWebView.__init__(self, parent)
  31. self.setZoomFactor(1)
  32. self.loadFinished.connect(self.onLoaded)
  33. self.load(QUrl('http://localhost:'+str(self.port)))
  34. self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
  35. # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
  36. self.initPDF()
  37. # self.mainframe = self.page.mainFrame()
  38. # print(self.mainframe)
  39. def onLoaded(self):
  40. print("WebView : onLoaded")
  41. self.parent.webviewtoolbar.onRefresh()
  42. def initPDF(self):
  43. self.printer = QPrinter(QPrinter.HighResolution)
  44. self.printer.setFullPage(True)
  45. # self.printer.setPageMargins(0,0,0,0,QPrinter.Millimeter)
  46. self.printer.setFontEmbeddingEnabled(True)
  47. self.printer.setColorMode(QPrinter.Color)
  48. # TODO: set the page size and orientation from doc settings
  49. # (need to do doc settings before that)
  50. # self.printer.setPageSize(QPrinter.A4)
  51. self.printer.setPaperSize(QSizeF(210, 300), QPrinter.Millimeter)
  52. # self.printer.setOrientation(QPrinter.Portrait)
  53. self.printer.setOutputFormat(QPrinter.PdfFormat)
  54. self.printer.setCreator('Cascade')
  55. self.printer.setDocName(self.core.projectname)
  56. self.printer.setOutputFileName(self.core.projectname+".pdf")
  57. # self.setFixedWidth(1000)
  58. def ongenPDF(self):
  59. # QPrinter::Custom
  60. # dialog = QPrintPreviewDialog(self.printer)
  61. # dialog.setWindowState(Qt.WindowMaximized)
  62. # dialog.paintRequested.connect(self.print_)
  63. # dialog.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint | Qt.WindowContextHelpButtonHint)
  64. # dialog.exec()
  65. # TODO: open a dialogue to ask where to save the pdf
  66. # TODO: reload webview and wait for it before printing
  67. # TODO: addd a progress bar
  68. # self.webview.
  69. self.print_(self.printer)
  70. def refresh(self):
  71. self.initPDF()
  72. self.reload()
  73. def toggleDocClass(self, c="",a=True):
  74. if a :
  75. togg = "add"
  76. else :
  77. togg = "remove"
  78. command = """document.documentElement.classList."""+togg+"""('"""+c+"""')"""
  79. self.evaluateJS(command)
  80. def evaluateJS(self, command):
  81. self.page().mainFrame().evaluateJavaScript(command)
  82. # ____ __
  83. # / _/___ _________ ___ _____/ /_____ _____
  84. # / // __ \/ ___/ __ \/ _ \/ ___/ __/ __ \/ ___/
  85. # _/ // / / (__ ) /_/ / __/ /__/ /_/ /_/ / /
  86. # /___/_/ /_/____/ .___/\___/\___/\__/\____/_/
  87. # /_/
  88. class WebkitInspector(QWebInspector):
  89. def __init__(self, parent, webkitview):
  90. super(WebkitInspector, self).__init__(parent)
  91. self.webkitview = webkitview
  92. self.setPage(self.webkitview.page())
  93. self.showMaximized()
  94. # TODO: webkitinspector is disappearing when chaging tabs
  95. # ______ ______
  96. # /_ __/___ ____ / / __ )____ ______
  97. # / / / __ \/ __ \/ / __ / __ `/ ___/
  98. # / / / /_/ / /_/ / / /_/ / /_/ / /
  99. # /_/ \____/\____/_/_____/\__,_/_/
  100. class WebViewToolBar(QWidget):
  101. def __init__(self, parent):
  102. super(WebViewToolBar, self).__init__(parent)
  103. self.parent = parent
  104. font = QFont()
  105. # font.setFamily("Droid Sans Mono")
  106. # font.setFixedPitch(True)
  107. font.setPointSize(8)
  108. self.setFont(font)
  109. self.hbox = QHBoxLayout()
  110. self.hbox.setContentsMargins(0,0,0,0)
  111. self.preview = QCheckBox('Prev&iew', self)
  112. self.preview.stateChanged.connect(self.onPreview)
  113. self.hbox.addWidget(self.preview)
  114. self.debug = QCheckBox('Deb&ug', self)
  115. self.debug.stateChanged.connect(self.onDebug)
  116. self.hbox.addWidget(self.debug)
  117. self.grid = QCheckBox('&Grid', self)
  118. self.grid.stateChanged.connect(self.onGrid)
  119. self.hbox.addWidget(self.grid)
  120. self.spread = QCheckBox('&Spread', self)
  121. self.spread.stateChanged.connect(self.onSpread)
  122. self.hbox.addWidget(self.spread)
  123. self.facing = QCheckBox('Fa&cing', self)
  124. self.facing.stateChanged.connect(self.onFacing)
  125. self.hbox.addWidget(self.facing)
  126. #
  127. self.hbox.addStretch()
  128. #
  129. # zoom
  130. self.hbox.addWidget(QLabel("Zoom:"))
  131. self.zoom = QSpinBox(self)
  132. # TODO: action
  133. self.hbox.addWidget(self.zoom)
  134. # page
  135. self.gotopage = QLabel("Go to Page: /"+self.parent.core.docsettings['np'])
  136. # TODO: refresh page number on change
  137. self.hbox.addWidget(self.gotopage)
  138. self.page = QSpinBox(self)
  139. # TODO: action
  140. self.hbox.addWidget(self.page)
  141. self.addpage = QPushButton("&Add Page", self)
  142. self.addpage.clicked.connect(self.onAddPage)
  143. self.hbox.addWidget(self.addpage)
  144. self.rmpage = QPushButton("Re&move Page", self)
  145. self.rmpage.clicked.connect(self.onRmPage)
  146. self.hbox.addWidget(self.rmpage)
  147. #
  148. self.hbox.addStretch()
  149. #
  150. self.reload = QPushButton("&Reload", self)
  151. # self.reload.setShortcut('Ctrl+Shift+r')
  152. # TODO: how to define same shortcut in different places
  153. # self.reload.setIcon(Icon(ico)))
  154. self.reload.clicked.connect(self.onReload)
  155. self.hbox.addWidget(self.reload)
  156. self.genpdf = QPushButton("&PDF", self)
  157. # self.genpdf.setShortcut('Ctrl+Shift+r')
  158. # TODO: how to define same shortcut in different places
  159. # self.genpdf.setIcon(Icon(ico)))
  160. self.genpdf.clicked.connect(self.onGenPDF)
  161. self.hbox.addWidget(self.genpdf)
  162. self.setLayout(self.hbox)
  163. # def onCheckboxAction(self, box):
  164. # self.parent.webkitview.toggleDocClass(box, self[box].isChecked())
  165. # self.recToolbarState(box, self[box].isChecked())
  166. def onPreview(self):
  167. print('Toolbar : onPreview', self.preview.isChecked())
  168. self.parent.webkitview.toggleDocClass('preview', self.preview.isChecked())
  169. self.recToolbarState('preview', self.preview.isChecked())
  170. def onDebug(self):
  171. self.parent.webkitview.toggleDocClass('debug', self.debug.isChecked())
  172. self.recToolbarState('debug', self.debug.isChecked())
  173. def onGrid(self):
  174. self.parent.webkitview.toggleDocClass('grid', self.grid.isChecked())
  175. self.recToolbarState('grid', self.grid.isChecked())
  176. def onSpread(self):
  177. self.parent.webkitview.toggleDocClass('spread', self.spread.isChecked())
  178. self.recToolbarState('spread', self.spread.isChecked())
  179. def onFacing(self):
  180. self.parent.webkitview.toggleDocClass('facing', self.facing.isChecked())
  181. self.recToolbarState('facing', self.facing.isChecked())
  182. def onAddPage(self):
  183. print("onAddPage")
  184. self.parent.core.addPage()
  185. # self.refreshNumberPage()
  186. def onRmPage(self):
  187. print("onAddPage")
  188. self.parent.core.rmPage()
  189. # self.refreshNumberPage()
  190. # def refreshNumberPage(self):
  191. def onReload(self):
  192. print("onReload")
  193. self.parent.webkitview.reload()
  194. def onGenPDF(self):
  195. print("onGenPDF")
  196. self.parent.webkitview.ongenPDF()
  197. def recToolbarState(self, prop, val):
  198. print('recToolbarState : '+prop, val)
  199. settings = QSettings('FiguresLibres', 'Cascade')
  200. settings.setValue('design/toolbar/'+prop, val)
  201. print('recToolbarState after : '+prop, settings.value('design/toolbar/'+prop))
  202. def onRefresh(self):
  203. # apply precedent toolbar state
  204. settings = QSettings('FiguresLibres', 'Cascade')
  205. self.preview.setChecked(bool(settings.value('design/toolbar/preview', False, type=bool)))
  206. self.debug.setChecked(bool(settings.value('design/toolbar/debug', False, type=bool)))
  207. self.grid.setChecked(bool(settings.value('design/toolbar/grid', False, type=bool)))
  208. self.spread.setChecked(bool(settings.value('design/toolbar/spread', False, type=bool)))
  209. self.facing.setChecked(bool(settings.value('design/toolbar/facing', False, type=bool)))
  210. # trigger webview changes
  211. self.parent.webkitview.toggleDocClass('preview', self.preview.isChecked())
  212. self.parent.webkitview.toggleDocClass('debug', self.debug.isChecked())
  213. self.parent.webkitview.toggleDocClass('grid', self.grid.isChecked())
  214. self.parent.webkitview.toggleDocClass('spread', self.spread.isChecked())
  215. self.gotopage.setText("Go to Page: /"+str(self.parent.core.docsettings['np']))
  216. # ______ ___ __
  217. # / ____/___/ (_) /_____ _____
  218. # / __/ / __ / / __/ __ \/ ___/
  219. # / /___/ /_/ / / /_/ /_/ / /
  220. # /_____/\__,_/_/\__/\____/_/
  221. class CodeEditor(QPlainTextEdit):
  222. def __init__(self, core, tabs, file, mode):
  223. super(CodeEditor, self).__init__()
  224. self.core = core
  225. self.tabs = tabs
  226. self.file = file
  227. self.setText()
  228. self.setTabStopWidth(15)
  229. self.hl= highlighter.Highlighter(self.document(),mode)
  230. self.shortcut = QShortcut(QKeySequence("Ctrl+s"), self)
  231. self.shortcut.activated.connect(self.save)
  232. def setText(self):
  233. try:
  234. self.textChanged.disconnect(self.onTextChanged)
  235. except Exception as e:
  236. print(e)
  237. self.filepath = os.path.join(self.core.cwd,self.file)
  238. self.clear()
  239. self.insertPlainText(open(self.filepath, 'r').read())
  240. self.changed = False
  241. self.textChanged.connect(self.onTextChanged)
  242. font = QFont()
  243. font.setFamily("Droid Sans Mono")
  244. font.setFixedPitch(True)
  245. font.setPointSize(12)
  246. self.setFont(font)
  247. # self.highlighter = Highlighter(self.document())
  248. # https://pypi.python.org/pypi/QScintilla/2.9.2
  249. def onTextChanged(self):
  250. # print('textChanged')
  251. # print(self.toPlainText())
  252. # open(self.filepath, 'w').write(self.toPlainText())
  253. if not self.changed:
  254. self.changed = True
  255. i = self.tabs.currentIndex()
  256. self.tabs.setTabText(i, "* "+self.tabs.tabText(i))
  257. # TODO: indicate that webview needs to be reloaded
  258. def save(self):
  259. if self.changed:
  260. open(self.filepath, 'w').write(self.toPlainText())
  261. i = self.tabs.currentIndex()
  262. self.tabs.setTabText(i, re.sub(r'^\*\s', '', self.tabs.tabText(i)))
  263. self.changed = False
  264. # TODO: how to combine file save and project save
  265. class Editor(QWidget):
  266. def __init__(self, parent, core):
  267. super(Editor, self).__init__()
  268. self.core = core
  269. self.layout = QVBoxLayout(self)
  270. self.layout.setContentsMargins(0,0,0,0)
  271. # Initialize tab screen
  272. self.tabs = QTabWidget()
  273. self.scsstab = CodeEditor(core, self.tabs, 'assets/css/styles.scss', "scss")
  274. self.jstab = CodeEditor(core, self.tabs, 'assets/js/script.js', 'js')
  275. # Add tabs
  276. self.tabs.addTab(self.scsstab,"scss")
  277. self.tabs.addTab(self.jstab,"js")
  278. # Add tabs to widget
  279. self.layout.addWidget(self.tabs)
  280. self.setLayout(self.layout)
  281. def refresh(self):
  282. self.scsstab.setText()
  283. self.jstab.setText()
  284. # _____ __ __
  285. # / ___// /_____ ______/ /__
  286. # \__ \/ __/ __ `/ ___/ //_/
  287. # ___/ / /_/ /_/ / /__/ ,<
  288. # /____/\__/\__,_/\___/_/|_|
  289. class DesignStack(QWidget):
  290. def __init__(self, core):
  291. super(DesignStack, self).__init__()
  292. self.core = core
  293. # self.grid = QGridLayout()
  294. self.hbox = QHBoxLayout()
  295. self.hbox.setContentsMargins(0,0,0,0)
  296. self.setLayout(self.hbox)
  297. self.webview = QWidget()
  298. self.webview.vbox = QVBoxLayout()
  299. self.webview.setLayout(self.webview.vbox)
  300. self.webview.vbox.setContentsMargins(0,0,0,0)
  301. # toolbar
  302. self.webviewtoolbar = WebViewToolBar(self)
  303. self.webview.vbox.addWidget(self.webviewtoolbar)
  304. # webkitview
  305. self.webkitview = WebkitView(self, core)
  306. # webkitinspector
  307. self.webkitinspector = WebkitInspector(self, self.webkitview)
  308. shortcut = QShortcut(self)
  309. shortcut.setKey("F12")
  310. shortcut.activated.connect(self.toggleInspector)
  311. self.webkitinspector.setVisible(False)
  312. # V layout
  313. self.vsplitter = QSplitter(QtCore.Qt.Vertical)
  314. self.vsplitter.addWidget(self.webkitview)
  315. self.vsplitter.addWidget(self.webkitinspector)
  316. self.vsplitter.splitterMoved.connect(self.movedSplitter)
  317. self.webview.vbox.addWidget(self.vsplitter)
  318. # H layout
  319. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  320. self.hsplitter.addWidget(self.webview)
  321. # editor
  322. self.editor = Editor(self, core)
  323. self.hsplitter.addWidget(self.editor)
  324. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  325. self.hbox.addWidget(self.hsplitter)
  326. self.restorePrefs()
  327. def toggleInspector(self):
  328. self.webkitinspector.setVisible(not self.webkitinspector.isVisible())
  329. def restorePrefs(self):
  330. settings = QSettings('FiguresLibres', 'Cascade')
  331. print(settings.value('design/vsplitter/sizes', self.vsplitter.sizes()))
  332. vals = settings.value('design/vsplitter/sizes', None)
  333. if vals:
  334. sizes = []
  335. for size in vals: sizes.append(int(size))
  336. self.vsplitter.setSizes(sizes)
  337. vals = settings.value('design/hsplitter/sizes', None)
  338. if vals:
  339. sizes = []
  340. for size in vals: sizes.append(int(size))
  341. self.hsplitter.setSizes(sizes)
  342. def movedSplitter(self):
  343. settings = QSettings('FiguresLibres', 'Cascade')
  344. # print(self.vsplitter.sizes())
  345. settings.setValue('design/vsplitter/sizes', self.vsplitter.sizes())
  346. settings.setValue('design/hsplitter/sizes', self.hsplitter.sizes())
  347. def refresh(self):
  348. self.editor.refresh()
  349. self.webkitview.refresh()