design.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. self.hbox.addWidget(self.gotopage)
  137. self.page = QSpinBox(self)
  138. # TODO: action
  139. self.hbox.addWidget(self.page)
  140. self.addpage = QPushButton("&Add Page", self)
  141. self.addpage.clicked.connect(self.onAddPage)
  142. self.hbox.addWidget(self.addpage)
  143. self.rmpage = QPushButton("Re&move Page", self)
  144. self.rmpage.clicked.connect(self.onRmPage)
  145. self.hbox.addWidget(self.rmpage)
  146. #
  147. self.hbox.addStretch()
  148. #
  149. self.reload = QPushButton("&Reload", self)
  150. # self.reload.setShortcut('Ctrl+Shift+r')
  151. # TODO: how to define same shortcut in different places
  152. # self.reload.setIcon(Icon(ico)))
  153. self.reload.clicked.connect(self.onReload)
  154. self.hbox.addWidget(self.reload)
  155. self.genpdf = QPushButton("&PDF", self)
  156. # self.genpdf.setShortcut('Ctrl+Shift+r')
  157. # TODO: how to define same shortcut in different places
  158. # self.genpdf.setIcon(Icon(ico)))
  159. self.genpdf.clicked.connect(self.onGenPDF)
  160. self.hbox.addWidget(self.genpdf)
  161. self.setLayout(self.hbox)
  162. # def onCheckboxAction(self, box):
  163. # self.parent.webkitview.toggleDocClass(box, self[box].isChecked())
  164. # self.recToolbarState(box, self[box].isChecked())
  165. def onPreview(self):
  166. print('Toolbar : onPreview', self.preview.isChecked())
  167. self.parent.webkitview.toggleDocClass('preview', self.preview.isChecked())
  168. self.recToolbarState('preview', self.preview.isChecked())
  169. def onDebug(self):
  170. self.parent.webkitview.toggleDocClass('debug', self.debug.isChecked())
  171. self.recToolbarState('debug', self.debug.isChecked())
  172. def onGrid(self):
  173. self.parent.webkitview.toggleDocClass('grid', self.grid.isChecked())
  174. self.recToolbarState('grid', self.grid.isChecked())
  175. def onSpread(self):
  176. self.parent.webkitview.toggleDocClass('spread', self.spread.isChecked())
  177. self.recToolbarState('spread', self.spread.isChecked())
  178. def onFacing(self):
  179. self.parent.webkitview.toggleDocClass('facing', self.facing.isChecked())
  180. self.recToolbarState('facing', self.facing.isChecked())
  181. def onAddPage(self):
  182. # print("onAddPage")
  183. self.parent.core.addPage()
  184. # self.refreshNumberPage()
  185. def onRmPage(self):
  186. # print("onAddPage")
  187. self.parent.core.rmPage()
  188. # self.refreshNumberPage()
  189. # def refreshNumberPage(self):
  190. def onReload(self):
  191. # print("onReload")
  192. self.parent.webkitview.reload()
  193. def onGenPDF(self):
  194. print("onGenPDF")
  195. self.parent.webkitview.ongenPDF()
  196. def recToolbarState(self, prop, val):
  197. # print('recToolbarState : '+prop, val)
  198. settings = QSettings('FiguresLibres', 'Cascade')
  199. settings.setValue('design/toolbar/'+prop, val)
  200. # print('recToolbarState after : '+prop, settings.value('design/toolbar/'+prop))
  201. def onRefresh(self):
  202. # apply precedent toolbar state
  203. settings = QSettings('FiguresLibres', 'Cascade')
  204. self.preview.setChecked(bool(settings.value('design/toolbar/preview', False, type=bool)))
  205. self.debug.setChecked(bool(settings.value('design/toolbar/debug', False, type=bool)))
  206. self.grid.setChecked(bool(settings.value('design/toolbar/grid', False, type=bool)))
  207. self.spread.setChecked(bool(settings.value('design/toolbar/spread', False, type=bool)))
  208. self.facing.setChecked(bool(settings.value('design/toolbar/facing', False, type=bool)))
  209. # trigger webview changes
  210. self.parent.webkitview.toggleDocClass('preview', self.preview.isChecked())
  211. self.parent.webkitview.toggleDocClass('debug', self.debug.isChecked())
  212. self.parent.webkitview.toggleDocClass('grid', self.grid.isChecked())
  213. self.parent.webkitview.toggleDocClass('spread', self.spread.isChecked())
  214. self.gotopage.setText("Go to Page: /"+str(self.parent.core.docsettings['np']))
  215. # ______ ___ __
  216. # / ____/___/ (_) /_____ _____
  217. # / __/ / __ / / __/ __ \/ ___/
  218. # / /___/ /_/ / / /_/ /_/ / /
  219. # /_____/\__,_/_/\__/\____/_/
  220. class CodeEditor(QPlainTextEdit):
  221. def __init__(self, parent, core, tabs, file, mode):
  222. super(CodeEditor, self).__init__()
  223. self.parent = parent
  224. self.core = core
  225. self.tabs = tabs
  226. self.file = file
  227. self.hl= highlighter.Highlighter(self.document(),mode)
  228. self.setText()
  229. self.setTabStopWidth(15)
  230. self.textChanged.connect(self.onTextChanged)
  231. self.shortcut = QShortcut(QKeySequence("Ctrl+s"), self)
  232. self.shortcut.activated.connect(self.save)
  233. def setText(self):
  234. # try:
  235. # self.textChanged.disconnect(self.onTextChanged)
  236. # except Exception as e:
  237. # print(e)
  238. self.filepath = os.path.join(self.core.cwd,self.file)
  239. self.clear()
  240. self.insertPlainText(open(self.filepath, 'r').read())
  241. self.changed = False
  242. font = QFont()
  243. font.setFamily("Droid Sans Mono")
  244. font.setFixedPitch(True)
  245. font.setPointSize(12)
  246. self.setFont(font)
  247. def onTextChanged(self):
  248. print('textChanged')
  249. # print(self.toPlainText())
  250. # open(self.filepath, 'w').write(self.toPlainText())
  251. if not self.changed:
  252. self.changed = True
  253. i = self.tabs.currentIndex()
  254. # self.tabs.setTabText(i, re.sub(r'^\**\s', '', self.tabs.tabText(i)))
  255. self.tabs.setTabText(i, "* "+self.tabs.tabText(i))
  256. # TODO: indicate that webview needs to be reloaded
  257. def save(self):
  258. if self.changed:
  259. open(self.filepath, 'w').write(self.toPlainText())
  260. i = self.tabs.currentIndex()
  261. self.tabs.setTabText(i, re.sub(r'^\**\s', '', self.tabs.tabText(i)))
  262. self.parent.reloadView()
  263. self.changed = False
  264. # TODO: how to combine file save and project save
  265. class Editor(QWidget):
  266. def __init__(self, parent):
  267. super(Editor, self).__init__()
  268. self.parent = parent
  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(self, self.parent.core, self.tabs, 'assets/css/styles.scss', "scss")
  274. self.jstab = CodeEditor(self, self.parent.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. def reloadView(self):
  285. self.parent.webkitview.reload()
  286. # _____ __ __
  287. # / ___// /_____ ______/ /__
  288. # \__ \/ __/ __ `/ ___/ //_/
  289. # ___/ / /_/ /_/ / /__/ ,<
  290. # /____/\__/\__,_/\___/_/|_|
  291. class DesignStack(QWidget):
  292. def __init__(self, core):
  293. super(DesignStack, self).__init__()
  294. self.core = core
  295. # self.grid = QGridLayout()
  296. self.hbox = QHBoxLayout()
  297. self.hbox.setContentsMargins(0,0,0,0)
  298. self.setLayout(self.hbox)
  299. self.webview = QWidget()
  300. self.webview.vbox = QVBoxLayout()
  301. self.webview.setLayout(self.webview.vbox)
  302. self.webview.vbox.setContentsMargins(0,0,0,0)
  303. # toolbar
  304. self.webviewtoolbar = WebViewToolBar(self)
  305. self.webview.vbox.addWidget(self.webviewtoolbar)
  306. # webkitview
  307. self.webkitview = WebkitView(self, core)
  308. # webkitinspector
  309. self.webkitinspector = WebkitInspector(self, self.webkitview)
  310. shortcut = QShortcut(self)
  311. shortcut.setKey("F12")
  312. shortcut.activated.connect(self.toggleInspector)
  313. self.webkitinspector.setVisible(False)
  314. # V layout
  315. self.vsplitter = QSplitter(QtCore.Qt.Vertical)
  316. self.vsplitter.addWidget(self.webkitview)
  317. self.vsplitter.addWidget(self.webkitinspector)
  318. self.vsplitter.splitterMoved.connect(self.movedSplitter)
  319. self.webview.vbox.addWidget(self.vsplitter)
  320. # H layout
  321. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  322. self.hsplitter.addWidget(self.webview)
  323. # editor
  324. self.editor = Editor(self)
  325. self.hsplitter.addWidget(self.editor)
  326. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  327. self.hbox.addWidget(self.hsplitter)
  328. self.restorePrefs()
  329. def toggleInspector(self):
  330. self.webkitinspector.setVisible(not self.webkitinspector.isVisible())
  331. def restorePrefs(self):
  332. settings = QSettings('FiguresLibres', 'Cascade')
  333. print(settings.value('design/vsplitter/sizes', self.vsplitter.sizes()))
  334. vals = settings.value('design/vsplitter/sizes', None)
  335. if vals:
  336. sizes = []
  337. for size in vals: sizes.append(int(size))
  338. self.vsplitter.setSizes(sizes)
  339. vals = settings.value('design/hsplitter/sizes', None)
  340. if vals:
  341. sizes = []
  342. for size in vals: sizes.append(int(size))
  343. self.hsplitter.setSizes(sizes)
  344. def movedSplitter(self):
  345. settings = QSettings('FiguresLibres', 'Cascade')
  346. # print(self.vsplitter.sizes())
  347. settings.setValue('design/vsplitter/sizes', self.vsplitter.sizes())
  348. settings.setValue('design/hsplitter/sizes', self.hsplitter.sizes())
  349. def refresh(self):
  350. self.editor.refresh()
  351. self.webkitview.refresh()