mainwindow.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. import os
  10. from PyQt5.QtGui import QIcon
  11. from PyQt5.QtWidgets import QMainWindow, QAction, QWidget, QLabel, QStackedWidget, QFileDialog, QMessageBox
  12. from . import design, content, docsetdialog
  13. class MainWindow(QMainWindow):
  14. def __init__(self, core):
  15. super(MainWindow, self).__init__()
  16. # load core class
  17. self.core = core
  18. self.setWindowTitle("Cascade")
  19. self.setWindowIcon(QIcon(os.path.join(self.core.appcwd,'assets/images/icon.png')))
  20. self.resize(self.core.mw_size)
  21. self.move(self.core.mw_pos)
  22. self.initMenuBar()
  23. self.initMainStack()
  24. self.show()
  25. # __ ___ ____
  26. # / |/ /__ ____ __ __/ __ )____ ______
  27. # / /|_/ / _ \/ __ \/ / / / __ / __ `/ ___/
  28. # / / / / __/ / / / /_/ / /_/ / /_/ / /
  29. # /_/ /_/\___/_/ /_/\__,_/_____/\__,_/_/
  30. def initMenuBar(self):
  31. # menu bar
  32. bar = self.menuBar()
  33. file = bar.addMenu("&File")
  34. new = QAction("&New Project",self)
  35. new.setShortcut("Ctrl+n")
  36. file.addAction(new)
  37. open = QAction("&Open",self)
  38. open.setShortcut("Ctrl+o")
  39. file.addAction(open)
  40. self.save_action = QAction("&Save Project as",self)
  41. self.save_action.setShortcut("Ctrl+Shift+s")
  42. file.addAction(self.save_action)
  43. self.save_action = QAction("&Save Project as",self)
  44. self.save_action.setShortcut("Ctrl+Shift+s")
  45. file.addAction(self.save_action)
  46. self.docset_action = QAction("&Document Settings",self)
  47. self.docset_action.setShortcut("Ctrl+d")
  48. file.addAction(self.docset_action)
  49. self.pdf_action = QAction("&PDF",self)
  50. self.pdf_action.setShortcut("Ctrl+p")
  51. file.addAction(self.pdf_action)
  52. self.quit_action = QAction("&Quit",self)
  53. self.quit_action.setShortcut("Ctrl+q")
  54. file.addAction(self.quit_action)
  55. file.triggered[QAction].connect(self.onfilemenutrigger)
  56. # edit menu
  57. edit = bar.addMenu("&Edit")
  58. # edit.addAction("&copy")
  59. # edit.addAction("&paste")
  60. edit.addAction("&build")
  61. self.reload_action = QAction("&Reload",self)
  62. self.reload_action.setShortcut("Ctrl+r")
  63. edit.addAction(self.reload_action)
  64. edit.addAction("&preferences")
  65. edit.triggered[QAction].connect(self.oneditmenutrigger)
  66. # view menu
  67. view = bar.addMenu("&View")
  68. designview = QAction("&Design",self)
  69. designview.setShortcut("F1")
  70. view.addAction(designview)
  71. contentview = QAction("&Content",self)
  72. contentview.setShortcut("F2")
  73. view.addAction(contentview)
  74. versionview = QAction("&Version",self)
  75. versionview.setShortcut("F3")
  76. view.addAction(versionview)
  77. view.triggered[QAction].connect(self.onviewmenutrigger)
  78. # about menu
  79. about = bar.addMenu("About")
  80. about.addAction("&Website")
  81. def onfilemenutrigger(self, q):
  82. print(q.text()+" is triggered")
  83. if q.text() == "&New Project":
  84. self.newprojectdialogue()
  85. elif q.text() == "&Open":
  86. self.openprojectdialogue()
  87. elif q.text() == "&Save Project as":
  88. self.saveprojectdialogue()
  89. elif q.text() == "&Document Settings":
  90. self.onDocSettings()
  91. elif q.text() == "&PDF":
  92. self.genPDF()
  93. elif q.text() == "&Quit":
  94. self.quit()
  95. def openprojectdialogue(self):
  96. print("open")
  97. dialog = QFileDialog()
  98. dialog.setFileMode(QFileDialog.Directory)
  99. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  100. options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly
  101. folder = dialog.getExistingDirectory(
  102. self,
  103. 'Open Project',
  104. self.core.dialog_path,
  105. options
  106. )
  107. try:
  108. head, tail = os.path.split(folder)
  109. self.core.dialog_path = head
  110. # TODO: check if is cascade folder
  111. print(folder)
  112. if os.path.isdir(folder):
  113. self.core.openproject(folder)
  114. else:
  115. print("folder doesn't exists")
  116. except Exception as e:
  117. print('Exception', e)
  118. pass
  119. def newprojectdialogue(self):
  120. dialog = QFileDialog()
  121. dialog.setFileMode(QFileDialog.Directory)
  122. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  123. projectname = dialog.getSaveFileName(
  124. self,
  125. 'New Project',
  126. self.core.dialog_path
  127. )[0]
  128. # TODO: no file type
  129. try:
  130. head, tail = os.path.split(projectname)
  131. self.core.dialog_path = head
  132. if not os.path.isdir(projectname):
  133. self.core.initnewproject(projectname)
  134. else:
  135. print("folder already exists")
  136. # TODO: check if is cascade folder
  137. except Exception as e:
  138. print('Exception', e)
  139. pass
  140. def saveprojectdialogue(self, quit=False):
  141. dialog = QFileDialog()
  142. dialog.setFileMode(QFileDialog.Directory)
  143. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  144. projectname = dialog.getSaveFileName(
  145. self,
  146. 'Save Project',
  147. self.core.dialog_path
  148. )[0]
  149. # TODO: no file type
  150. try:
  151. head, tail = os.path.split(projectname)
  152. self.core.dialog_path = head
  153. if not os.path.isdir(projectname):
  154. self.core.saveproject(projectname)
  155. if quit:
  156. self.quit()
  157. else:
  158. print("folder already exists")
  159. # TODO: check if is cascade folder
  160. except Exception as e:
  161. print('Exception', e)
  162. pass
  163. def onDocSettings(self):
  164. d = docsetdialog.DocsetDialog(self)
  165. d.exec_()
  166. self.core.recordDocSettings({
  167. "ho":d.headerOdd.text(),
  168. "he":d.headerEven.text(),
  169. "np":d.np.text(),
  170. "pw":d.pw.text(),
  171. "ph":d.ph.text(),
  172. "mt":d.mt.text(),
  173. "mb":d.mb.text(),
  174. "me":d.me.text(),
  175. "mi":d.mi.text(),
  176. "cs":d.cs.text(),
  177. "bs":d.bs.text(),
  178. "cn":d.cn.text(),
  179. "cg":d.cg.text(),
  180. "rn":d.rn.text(),
  181. "rg":d.rg.text(),
  182. "lh":d.lh.text()
  183. })
  184. def genPDF(self):
  185. print('PDF')
  186. self.designstack.webkitview.ongenPDF()
  187. def quit(self):
  188. print("Quit")
  189. if self.core.tempcwd:
  190. 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)
  191. if buttonReply == QMessageBox.Yes:
  192. self.saveprojectdialogue(quit=True)
  193. if buttonReply == QMessageBox.No:
  194. self.core.quit()
  195. else:
  196. self.core.quit()
  197. def oneditmenutrigger(self, q):
  198. print(q.text()+" is triggered")
  199. if q.text() == "&Reload":
  200. self.designstack.webkitview.reload()
  201. def onviewmenutrigger(self, q):
  202. print(q.text()+" is triggered")
  203. if q.text() == "&Design":
  204. self.mainstack.setCurrentIndex(0)
  205. elif q.text() == "&Content":
  206. self.mainstack.setCurrentIndex(1)
  207. elif q.text() == "&Version":
  208. self.mainstack.setCurrentIndex(2)
  209. # __ ___ _ _____ __ __
  210. # / |/ /___ _(_)___ / ___// /_____ ______/ /__
  211. # / /|_/ / __ `/ / __ \\__ \/ __/ __ `/ ___/ //_/
  212. # / / / / /_/ / / / / /__/ / /_/ /_/ / /__/ ,<
  213. # /_/ /_/\__,_/_/_/ /_/____/\__/\__,_/\___/_/|_|
  214. def initMainStack(self):
  215. self.mainstack = QStackedWidget()
  216. self.designstack = design.DesignStack(self.core)
  217. self.contentstack = content.ContentStack(self.core)
  218. self.versionstack = QLabel("Version (git).")
  219. self.mainstack.addWidget(self.designstack)
  220. self.mainstack.addWidget(self.contentstack)
  221. self.mainstack.addWidget(self.versionstack)
  222. self.mainstack.setCurrentIndex(self.core.mw_curstack)
  223. # TODO: add an app console window (show sass compilation errors for example)
  224. self.setCentralWidget(self.mainstack)