mainwindow.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. # @Last modified by: bach
  7. # @Last modified time: 03-06-2017
  8. # @License: GPL-V3
  9. from __future__ import absolute_import, print_function, division, unicode_literals
  10. import os, asyncio
  11. from PyQt5.QtCore import Qt
  12. from PyQt5.QtGui import QIcon, QKeySequence
  13. from PyQt5.QtWidgets import (QMainWindow, QAction, QWidget,
  14. QLabel, QStackedWidget,
  15. QFileDialog, QMessageBox,
  16. QShortcut)
  17. from . import design, content, docsetdialog
  18. class MainWindow(QMainWindow):
  19. def __init__(self, core):
  20. super(MainWindow, self).__init__()
  21. # load core class
  22. self.core = core
  23. self.setWindowTitle("Libriis")
  24. self.setWindowIcon(QIcon(os.path.join(self.core.appcwd,'assets/images/icon.png')))
  25. self.resize(self.core.mw_size)
  26. self.move(self.core.mw_pos)
  27. self.initMenuBar()
  28. self.initMainStack()
  29. self.show()
  30. # __ ___ ____
  31. # / |/ /__ ____ __ __/ __ )____ ______
  32. # / /|_/ / _ \/ __ \/ / / / __ / __ `/ ___/
  33. # / / / / __/ / / / /_/ / /_/ / /_/ / /
  34. # /_/ /_/\___/_/ /_/\__,_/_____/\__,_/_/
  35. def initMenuBar(self):
  36. # menu bar
  37. bar = self.menuBar()
  38. file = bar.addMenu("&File")
  39. new = QAction("&New Project",self)
  40. new.setShortcut("Ctrl+n")
  41. file.addAction(new)
  42. open = QAction("&Open",self)
  43. open.setShortcut("Ctrl+o")
  44. file.addAction(open)
  45. self.save_action = QAction("&Save Project as",self)
  46. self.save_action.setShortcut("Ctrl+Shift+s")
  47. file.addAction(self.save_action)
  48. self.save_action = QAction("&Save Project as",self)
  49. self.save_action.setShortcut("Ctrl+Shift+s")
  50. file.addAction(self.save_action)
  51. self.docset_action = QAction("&Document Settings",self)
  52. self.docset_action.setShortcut("Ctrl+d")
  53. file.addAction(self.docset_action)
  54. self.pdf_action = QAction("&PDF",self)
  55. self.pdf_action.setShortcut("Ctrl+p")
  56. file.addAction(self.pdf_action)
  57. self.quit_action = QAction("&Quit",self)
  58. self.quit_action.setShortcut("Ctrl+q")
  59. file.addAction(self.quit_action)
  60. file.triggered[QAction].connect(self.onfilemenutrigger)
  61. # edit menu
  62. edit = bar.addMenu("&Edit")
  63. # edit.addAction("&copy")
  64. # edit.addAction("&paste")
  65. edit.addAction("&build")
  66. self.reload_action = QAction("&Reload",self)
  67. self.reload_action.setShortcut("Ctrl+r")
  68. edit.addAction(self.reload_action)
  69. edit.addAction("&preferences")
  70. edit.triggered[QAction].connect(self.oneditmenutrigger)
  71. # view menu
  72. view = bar.addMenu("&View")
  73. designview = QAction("&Design",self)
  74. designview.setShortcut("F1")
  75. view.addAction(designview)
  76. contentview = QAction("&Content",self)
  77. contentview.setShortcut("F2")
  78. view.addAction(contentview)
  79. versionview = QAction("&Version",self)
  80. versionview.setShortcut("F3")
  81. view.addAction(versionview)
  82. view.triggered[QAction].connect(self.onviewmenutrigger)
  83. # about menu
  84. about = bar.addMenu("About")
  85. about.addAction("&Website")
  86. def onfilemenutrigger(self, q):
  87. print(q.text()+" is triggered")
  88. if q.text() == "&New Project":
  89. self.newprojectdialogue()
  90. elif q.text() == "&Open":
  91. self.openprojectdialogue()
  92. elif q.text() == "&Save Project as":
  93. self.saveprojectdialogue()
  94. elif q.text() == "&Document Settings":
  95. self.onDocSettings()
  96. elif q.text() == "&PDF":
  97. self.genPDF()
  98. elif q.text() == "&Quit":
  99. self.quit()
  100. def openprojectdialogue(self):
  101. print("open")
  102. dialog = QFileDialog()
  103. dialog.setFileMode(QFileDialog.Directory)
  104. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  105. options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly
  106. folder = dialog.getExistingDirectory(
  107. self,
  108. 'Open Project',
  109. self.core.dialog_path,
  110. options
  111. )
  112. try:
  113. head, tail = os.path.split(folder)
  114. self.core.dialog_path = head
  115. # TODO: check if is libriis folder
  116. print(folder)
  117. if os.path.isdir(folder):
  118. self.core.openproject(folder)
  119. else:
  120. print("folder doesn't exists")
  121. except Exception as e:
  122. print('Exception', e)
  123. pass
  124. def newprojectdialogue(self):
  125. dialog = QFileDialog()
  126. dialog.setFileMode(QFileDialog.Directory)
  127. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  128. projectname = dialog.getSaveFileName(
  129. self,
  130. 'New Project',
  131. self.core.dialog_path
  132. )[0]
  133. # TODO: no file type
  134. try:
  135. head, tail = os.path.split(projectname)
  136. self.core.dialog_path = head
  137. if not os.path.isdir(projectname):
  138. self.core.initnewproject(projectname)
  139. else:
  140. print("folder already exists")
  141. # TODO: check if is libriis folder
  142. except Exception as e:
  143. print('Exception', e)
  144. pass
  145. def saveprojectdialogue(self, quit=False):
  146. dialog = QFileDialog()
  147. dialog.setFileMode(QFileDialog.Directory)
  148. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  149. projectname = dialog.getSaveFileName(
  150. self,
  151. 'Save Project',
  152. self.core.dialog_path
  153. )[0]
  154. # TODO: no file type
  155. try:
  156. head, tail = os.path.split(projectname)
  157. self.core.dialog_path = head
  158. if not os.path.isdir(projectname):
  159. self.core.saveproject(projectname)
  160. if quit:
  161. self.quit()
  162. else:
  163. print("folder already exists")
  164. # TODO: check if is libriis folder
  165. except Exception as e:
  166. print('Exception', e)
  167. pass
  168. def onDocSettings(self):
  169. d = docsetdialog.DocsetDialog(self)
  170. d.exec_()
  171. self.core.recordDocSettings({
  172. "ho":d.headerOdd.text(),
  173. "he":d.headerEven.text(),
  174. "np":d.np.text(),
  175. "pw":d.pw.text(),
  176. "ph":d.ph.text(),
  177. "mt":d.mt.text(),
  178. "mb":d.mb.text(),
  179. "me":d.me.text(),
  180. "mi":d.mi.text(),
  181. "cs":d.cs.text(),
  182. "bs":d.bs.text(),
  183. "cn":d.cn.text(),
  184. "cg":d.cg.text(),
  185. "rn":d.rn.text(),
  186. "rg":d.rg.text(),
  187. "lh":d.lh.text()
  188. })
  189. def genPDF(self):
  190. print('PDF')
  191. self.designstack.webkitview.ongenPDF()
  192. def quit(self):
  193. print("Quit")
  194. if self.core.tempcwd:
  195. buttonReply = QMessageBox.question(self, 'Project Not Saved', "Do you want to save your current project before quiting?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Cancel)
  196. if buttonReply == QMessageBox.Yes:
  197. self.saveprojectdialogue(quit=True)
  198. if buttonReply == QMessageBox.No:
  199. self.core.quit()
  200. else:
  201. self.core.quit()
  202. def oneditmenutrigger(self, q):
  203. print(q.text()+" is triggered")
  204. if q.text() == "&Reload":
  205. self.designstack.webkitview.reload()
  206. def onviewmenutrigger(self, q):
  207. print(q.text()+" is triggered")
  208. if q.text() == "&Design":
  209. self.mainstack.setCurrentIndex(0)
  210. elif q.text() == "&Content":
  211. self.mainstack.setCurrentIndex(1)
  212. elif q.text() == "&Version":
  213. self.mainstack.setCurrentIndex(2)
  214. # __ ____ ______ __
  215. # / /_____ __ __/ __ \________ __________/ ____/ _____ ____ / /_
  216. # / //_/ _ \/ / / / /_/ / ___/ _ \/ ___/ ___/ __/ | | / / _ \/ __ \/ __/
  217. # / ,< / __/ /_/ / ____/ / / __(__ |__ ) /___ | |/ / __/ / / / /_
  218. # /_/|_|\___/\__, /_/ /_/ \___/____/____/_____/ |___/\___/_/ /_/\__/
  219. # /____/
  220. # def keyPressEvent(self, e):
  221. # print('keyPressEvent', e.key())
  222. # # print('Qt.Key_Alt', Qt.Key_Alt)
  223. # super(MainWindow, self).keyPressEvent(e)
  224. # self.checkToggleMenu('pressed', e.key() == Qt.Key_Alt)
  225. #
  226. # def keyReleaseEvent(self, e):
  227. # print('keyReleaseEvent', e.key())
  228. # # print('Qt.Key_Alt', Qt.Key_Alt)
  229. # super(MainWindow, self).keyReleaseEvent(e)
  230. # self.checkToggleMenu('released', e.key() == Qt.Key_Alt)
  231. # def checkToggleMenu(self, act='released', alt=True):
  232. # print("-- -- checkToggleMenu : "+act+" | alt :"+str(alt))
  233. # # if alt is pressed, other key pressevent is not fired, only release :(
  234. # # keyPressEvent is waiting for toggleMenu o_O which is supposed to be asynchro ...
  235. # if not alt:
  236. # self.key_pressed_after_alt = True
  237. # if alt and act=="pressed":
  238. # self.key_pressed_after_alt = False
  239. # if act == 'released' and alt:
  240. # loop = asyncio.get_event_loop()
  241. # loop.run_until_complete(self.toggleMenu())
  242. # # loop.close()
  243. # async def toggleMenu(self):
  244. # print('toggleMenu')
  245. # await asyncio.sleep(2)
  246. # if not self.key_pressed_after_alt:
  247. # print('!! !! TOGGLE !! !!')
  248. # # self.menuBar().setVisible(not self.menuBar().isVisible())
  249. # __ ___ _ _____ __ __
  250. # / |/ /___ _(_)___ / ___// /_____ ______/ /__
  251. # / /|_/ / __ `/ / __ \\__ \/ __/ __ `/ ___/ //_/
  252. # / / / / /_/ / / / / /__/ / /_/ /_/ / /__/ ,<
  253. # /_/ /_/\__,_/_/_/ /_/____/\__/\__,_/\___/_/|_|
  254. def initMainStack(self):
  255. self.mainstack = QStackedWidget()
  256. self.designstack = design.DesignStack(self.core)
  257. self.contentstack = content.ContentStack(self.core)
  258. self.versionstack = QLabel("Version (git).")
  259. self.mainstack.addWidget(self.designstack)
  260. self.mainstack.addWidget(self.contentstack)
  261. self.mainstack.addWidget(self.versionstack)
  262. self.mainstack.setCurrentIndex(self.core.mw_curstack)
  263. # TODO: add an app console window (show sass compilation errors for example)
  264. self.setCentralWidget(self.mainstack)