mainwindow.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. # bar.setNativeMenuBar(False)
  39. file = bar.addMenu("&File")
  40. new = QAction("&New Project",self)
  41. new.setShortcut("Ctrl+n")
  42. file.addAction(new)
  43. open = QAction("&Open",self)
  44. open.setShortcut("Ctrl+o")
  45. file.addAction(open)
  46. self.save_action = QAction("&Save Project as",self)
  47. self.save_action.setShortcut("Ctrl+Shift+s")
  48. file.addAction(self.save_action)
  49. self.docset_action = QAction("&Document Settings",self)
  50. self.docset_action.setShortcut("Ctrl+d")
  51. file.addAction(self.docset_action)
  52. self.pdf_action = QAction("&PDF",self)
  53. self.pdf_action.setShortcut("Ctrl+p")
  54. file.addAction(self.pdf_action)
  55. self.quit_action = QAction("&Quit",self)
  56. self.quit_action.setShortcut("Ctrl+q")
  57. file.addAction(self.quit_action)
  58. file.triggered[QAction].connect(self.onfilemenutrigger)
  59. # edit menu
  60. edit = bar.addMenu("&Edit")
  61. # edit.addAction("&copy")
  62. # edit.addAction("&paste")
  63. edit.addAction("&build")
  64. self.reload_action = QAction("&Reload",self)
  65. self.reload_action.setShortcut("Ctrl+r")
  66. edit.addAction(self.reload_action)
  67. edit.addAction("&preferences")
  68. edit.triggered[QAction].connect(self.oneditmenutrigger)
  69. # view menu
  70. view = bar.addMenu("&View")
  71. contentview = QAction("&Content",self)
  72. contentview.setShortcut("F1")
  73. view.addAction(contentview)
  74. designview = QAction("&Design",self)
  75. designview.setShortcut("F2")
  76. view.addAction(designview)
  77. versionview = QAction("&Version",self)
  78. versionview.setShortcut("F3")
  79. view.addAction(versionview)
  80. self.menuview = QAction("&Show Menu", self)
  81. self.menuview.setCheckable(True)
  82. self.menuview.setShortcutContext(Qt.ApplicationShortcut)
  83. self.menuview.setShortcut('F4')
  84. view.addAction(self.menuview)
  85. # self.menuview_shortcut = QShortcut(QKeySequence("F4"), self)
  86. # self.menuview_shortcut.activated.connect(self.toggleMenu)
  87. view.triggered[QAction].connect(self.onviewmenutrigger)
  88. # about menu
  89. about = bar.addMenu("About")
  90. about.addAction("&Website")
  91. self.menuisvisible = True
  92. # TODO: get menu visibility from settings
  93. def onfilemenutrigger(self, q):
  94. print(q.text()+" is triggered")
  95. if q.text() == "&New Project":
  96. self.newprojectdialogue()
  97. elif q.text() == "&Open":
  98. self.openprojectdialogue()
  99. elif q.text() == "&Save Project as":
  100. self.saveprojectdialogue()
  101. elif q.text() == "&Document Settings":
  102. self.onDocSettings()
  103. elif q.text() == "&PDF":
  104. self.genPDF()
  105. elif q.text() == "&Quit":
  106. self.quit()
  107. def openprojectdialogue(self):
  108. print("open")
  109. dialog = QFileDialog()
  110. dialog.setFileMode(QFileDialog.Directory)
  111. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  112. options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly
  113. folder = dialog.getExistingDirectory(
  114. self,
  115. 'Open Project',
  116. self.core.dialog_path,
  117. options
  118. )
  119. try:
  120. head, tail = os.path.split(folder)
  121. self.core.dialog_path = head
  122. # TODO: check if is libriis folder
  123. print(folder)
  124. if os.path.isdir(folder):
  125. self.core.openproject(folder)
  126. else:
  127. print("folder doesn't exists")
  128. except Exception as e:
  129. print('openprojectdialogue :: !!Exception!! :', e)
  130. pass
  131. def newprojectdialogue(self):
  132. dialog = QFileDialog()
  133. dialog.setFileMode(QFileDialog.Directory)
  134. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  135. projectname = dialog.getSaveFileName(
  136. self,
  137. 'New Project',
  138. self.core.dialog_path
  139. )[0]
  140. # TODO: no file type
  141. try:
  142. head, tail = os.path.split(projectname)
  143. print('newprojectdialogue :: projectname', projectname)
  144. print("newprojectdialogue :: head",head)
  145. print("newprojectdialogue :: tail", tail)
  146. self.core.dialog_path = head
  147. if not os.path.isdir(projectname):
  148. self.core.initnewproject(projectname)
  149. else:
  150. print("folder already exists")
  151. # TODO: check if is libriis folder
  152. except Exception as e:
  153. print('newprojectdialogue :: !!Exception!!', e)
  154. pass
  155. def saveprojectdialogue(self, quit=False):
  156. dialog = QFileDialog()
  157. dialog.setFileMode(QFileDialog.Directory)
  158. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  159. projectname = dialog.getSaveFileName(
  160. self,
  161. 'Save Project',
  162. self.core.dialog_path
  163. # TODO: if tempproject open in home
  164. )[0]
  165. # TODO: no file type
  166. try:
  167. head, tail = os.path.split(projectname)
  168. self.core.dialog_path = head
  169. if not os.path.isdir(projectname):
  170. self.core.saveproject(projectname)
  171. if quit:
  172. self.quit()
  173. else:
  174. print("folder already exists")
  175. # TODO: check if is libriis folder
  176. except Exception as e:
  177. print('Exception', e)
  178. pass
  179. def onDocSettings(self):
  180. d = docsetdialog.DocsetDialog(self)
  181. d.exec_()
  182. self.core.recordDocSettings({
  183. "ho":d.headerOdd.text(),
  184. "he":d.headerEven.text(),
  185. "np":d.np.text(),
  186. "pw":d.pw.text(),
  187. "ph":d.ph.text(),
  188. "mt":d.mt.text(),
  189. "mb":d.mb.text(),
  190. "me":d.me.text(),
  191. "mi":d.mi.text(),
  192. "cs":d.cs.text(),
  193. "bs":d.bs.text(),
  194. "cn":d.cn.text(),
  195. "cg":d.cg.text(),
  196. "rn":d.rn.text(),
  197. "rg":d.rg.text(),
  198. "lh":d.lh.text()
  199. })
  200. def genPDF(self):
  201. print('PDF')
  202. self.designstack.webkitview.ongenPDF()
  203. def quit(self):
  204. print("Quit")
  205. if self.core.tempcwd:
  206. 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)
  207. if buttonReply == QMessageBox.Yes:
  208. self.saveprojectdialogue(quit=True)
  209. if buttonReply == QMessageBox.No:
  210. self.core.quit()
  211. else:
  212. self.core.quit()
  213. def oneditmenutrigger(self, q):
  214. print(q.text()+" is triggered")
  215. if q.text() == "&Reload":
  216. self.designstack.webkitview.reload()
  217. def onviewmenutrigger(self, q):
  218. print(q.text()+" is triggered")
  219. if q.text() == "&Content":
  220. self.mainstack.setCurrentIndex(0)
  221. elif q.text() == "&Design":
  222. self.mainstack.setCurrentIndex(1)
  223. elif q.text() == "&Version":
  224. self.mainstack.setCurrentIndex(2)
  225. elif q.text() == "&Show Menu":
  226. self.toggleMenu()
  227. def toggleMenu(self):
  228. self.menuisvisible = not self.menuisvisible
  229. if self.menuisvisible:
  230. print('show menu')
  231. # self.menuBar().adjustSize()
  232. # self.menuBar().show()
  233. # .height(1)
  234. #setVisible(True)
  235. # .setStyleSheet("margin-top:0;")
  236. self.menuview.setChecked(True)
  237. else:
  238. print('hide menu')
  239. # self.menuBar()
  240. # self.menuBar().hide()
  241. # .height(25)
  242. #setVisible(False)
  243. # .setStyleSheet("margin-top:-10px;")
  244. self.menuview.setChecked(False)
  245. # __ ____ ______ __
  246. # / /_____ __ __/ __ \________ __________/ ____/ _____ ____ / /_
  247. # / //_/ _ \/ / / / /_/ / ___/ _ \/ ___/ ___/ __/ | | / / _ \/ __ \/ __/
  248. # / ,< / __/ /_/ / ____/ / / __(__ |__ ) /___ | |/ / __/ / / / /_
  249. # /_/|_|\___/\__, /_/ /_/ \___/____/____/_____/ |___/\___/_/ /_/\__/
  250. # /____/
  251. # def keyPressEvent(self, e):
  252. # print('keyPressEvent', e.key())
  253. # # print('Qt.Key_Alt', Qt.Key_Alt)
  254. # super(MainWindow, self).keyPressEvent(e)
  255. # self.checkToggleMenu('pressed', e.key() == Qt.Key_Alt)
  256. #
  257. # def keyReleaseEvent(self, e):
  258. # print('keyReleaseEvent', e.key())
  259. # # print('Qt.Key_Alt', Qt.Key_Alt)
  260. # super(MainWindow, self).keyReleaseEvent(e)
  261. # self.checkToggleMenu('released', e.key() == Qt.Key_Alt)
  262. # def checkToggleMenu(self, act='released', alt=True):
  263. # print("-- -- checkToggleMenu : "+act+" | alt :"+str(alt))
  264. # # if alt is pressed, other key pressevent is not fired, only release :(
  265. # # keyPressEvent is waiting for toggleMenu o_O which is supposed to be asynchro ...
  266. # if not alt:
  267. # self.key_pressed_after_alt = True
  268. # if alt and act=="pressed":
  269. # self.key_pressed_after_alt = False
  270. # if act == 'released' and alt:
  271. # loop = asyncio.get_event_loop()
  272. # loop.run_until_complete(self.toggleMenu())
  273. # # loop.close()
  274. # async def toggleMenu(self):
  275. # print('toggleMenu')
  276. # await asyncio.sleep(2)
  277. # if not self.key_pressed_after_alt:
  278. # print('!! !! TOGGLE !! !!')
  279. # # self.menuBar().setVisible(not self.menuBar().isVisible())
  280. # __ ___ _ _____ __ __
  281. # / |/ /___ _(_)___ / ___// /_____ ______/ /__
  282. # / /|_/ / __ `/ / __ \\__ \/ __/ __ `/ ___/ //_/
  283. # / / / / /_/ / / / / /__/ / /_/ /_/ / /__/ ,<
  284. # /_/ /_/\__,_/_/_/ /_/____/\__/\__,_/\___/_/|_|
  285. def initMainStack(self):
  286. self.mainstack = QStackedWidget()
  287. self.contentstack = content.ContentStack(self.core)
  288. self.designstack = design.DesignStack(self.core)
  289. self.versionstack = QLabel("Version (git).")
  290. self.mainstack.addWidget(self.contentstack)
  291. self.mainstack.addWidget(self.designstack)
  292. self.mainstack.addWidget(self.versionstack)
  293. self.mainstack.setCurrentIndex(self.core.mw_curstack)
  294. # TODO: add an app console window (show scss compilation errors for example)
  295. self.setCentralWidget(self.mainstack)