mainwindow.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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: 21-04-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 classes 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. def initMenuBar(self):
  26. # menu bar
  27. bar = self.menuBar()
  28. file = bar.addMenu("&File")
  29. new = QAction("&New Project",self)
  30. new.setShortcut("Ctrl+n")
  31. file.addAction(new)
  32. open = QAction("&Open",self)
  33. open.setShortcut("Ctrl+o")
  34. file.addAction(open)
  35. self.save_action = QAction("&Save Project as",self)
  36. self.save_action.setShortcut("Ctrl+Shift+s")
  37. file.addAction(self.save_action)
  38. self.save_action = QAction("&Save Project as",self)
  39. self.save_action.setShortcut("Ctrl+Shift+s")
  40. file.addAction(self.save_action)
  41. self.docset_action = QAction("&Document Settings",self)
  42. self.docset_action.setShortcut("Ctrl+d")
  43. file.addAction(self.docset_action)
  44. self.pdf_action = QAction("&PDF",self)
  45. self.pdf_action.setShortcut("Ctrl+p")
  46. file.addAction(self.pdf_action)
  47. self.quit_action = QAction("&Quit",self)
  48. self.quit_action.setShortcut("Ctrl+q")
  49. file.addAction(self.quit_action)
  50. file.triggered[QAction].connect(self.onfilemenutrigger)
  51. # edit menu
  52. edit = bar.addMenu("&Edit")
  53. # edit.addAction("&copy")
  54. # edit.addAction("&paste")
  55. edit.addAction("&build")
  56. self.reload_action = QAction("&Reload",self)
  57. self.reload_action.setShortcut("Ctrl+r")
  58. edit.addAction(self.reload_action)
  59. edit.addAction("&preferences")
  60. edit.triggered[QAction].connect(self.oneditmenutrigger)
  61. # view menu
  62. view = bar.addMenu("&View")
  63. designview = QAction("&Design",self)
  64. designview.setShortcut("F1")
  65. view.addAction(designview)
  66. contentview = QAction("&Content",self)
  67. contentview.setShortcut("F2")
  68. view.addAction(contentview)
  69. versionview = QAction("&Version",self)
  70. versionview.setShortcut("F3")
  71. view.addAction(versionview)
  72. view.triggered[QAction].connect(self.onviewmenutrigger)
  73. # about menu
  74. about = bar.addMenu("About")
  75. about.addAction("&Website")
  76. def onfilemenutrigger(self, q):
  77. print(q.text()+" is triggered")
  78. if q.text() == "&New Project":
  79. self.newprojectdialogue()
  80. elif q.text() == "&Open":
  81. self.openprojectdialogue()
  82. elif q.text() == "&Save Project as":
  83. self.saveprojectdialogue()
  84. elif q.text() == "&Document Settings":
  85. self.onDocSettings()
  86. elif q.text() == "&PDF":
  87. self.genPDF()
  88. elif q.text() == "&Quit":
  89. self.quit()
  90. def openprojectdialogue(self):
  91. print("open")
  92. dialog = QFileDialog()
  93. dialog.setFileMode(QFileDialog.Directory)
  94. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  95. options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly
  96. folder = dialog.getExistingDirectory(
  97. self,
  98. 'Open Project',
  99. self.core.dialog_path,
  100. options
  101. )
  102. try:
  103. head, tail = os.path.split(folder)
  104. self.core.dialog_path = head
  105. # TODO: check if is cascade folder
  106. print(folder)
  107. if os.path.isdir(folder):
  108. self.core.openproject(folder)
  109. else:
  110. print("folder doesn't exists")
  111. except Exception as e:
  112. print('Exception', e)
  113. pass
  114. def newprojectdialogue(self):
  115. dialog = QFileDialog()
  116. dialog.setFileMode(QFileDialog.Directory)
  117. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  118. projectname = dialog.getSaveFileName(
  119. self,
  120. 'New Project',
  121. self.core.dialog_path
  122. )[0]
  123. # TODO: no file type
  124. try:
  125. head, tail = os.path.split(projectname)
  126. self.core.dialog_path = head
  127. if not os.path.isdir(projectname):
  128. self.core.initnewproject(projectname)
  129. else:
  130. print("folder already exists")
  131. # TODO: check if is cascade folder
  132. except Exception as e:
  133. print('Exception', e)
  134. pass
  135. def saveprojectdialogue(self, quit=False):
  136. dialog = QFileDialog()
  137. dialog.setFileMode(QFileDialog.Directory)
  138. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  139. projectname = dialog.getSaveFileName(
  140. self,
  141. 'Save Project',
  142. self.core.dialog_path
  143. )[0]
  144. # TODO: no file type
  145. try:
  146. head, tail = os.path.split(projectname)
  147. self.core.dialog_path = head
  148. if not os.path.isdir(projectname):
  149. self.core.saveproject(projectname)
  150. if quit:
  151. self.quit()
  152. else:
  153. print("folder already exists")
  154. # TODO: check if is cascade folder
  155. except Exception as e:
  156. print('Exception', e)
  157. pass
  158. def onDocSettings(self):
  159. d = docsetdialog.DocsetDialog(self)
  160. d.exec_()
  161. self.core.recordDocSettings({
  162. "pw":d.pw.text(),
  163. "ph":d.ph.text(),
  164. "mt":d.mt.text(),
  165. "mb":d.mb.text(),
  166. "me":d.me.text(),
  167. "mi":d.mi.text(),
  168. "cs":d.cs.text(),
  169. "bs":d.bs.text()
  170. })
  171. def genPDF(self):
  172. print('PDF')
  173. self.designstack.webkitview.ongenPDF()
  174. def quit(self):
  175. print("Quit")
  176. if self.core.tempcwd:
  177. 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)
  178. if buttonReply == QMessageBox.Yes:
  179. self.saveprojectdialogue(quit=True)
  180. if buttonReply == QMessageBox.No:
  181. self.core.quit()
  182. else:
  183. self.core.quit()
  184. def oneditmenutrigger(self, q):
  185. print(q.text()+" is triggered")
  186. if q.text() == "&Reload":
  187. self.designstack.webkitview.reload()
  188. def onviewmenutrigger(self, q):
  189. print(q.text()+" is triggered")
  190. if q.text() == "&Design":
  191. self.mainstack.setCurrentIndex(0)
  192. elif q.text() == "&Content":
  193. self.mainstack.setCurrentIndex(1)
  194. elif q.text() == "&Version":
  195. self.mainstack.setCurrentIndex(2)
  196. def initMainStack(self):
  197. self.mainstack = QStackedWidget()
  198. self.designstack = design.DesignStack(self.core)
  199. self.contentstack = content.ContentStack(self.core)
  200. self.versionstack = QLabel("Version (git).")
  201. self.mainstack.addWidget(self.designstack)
  202. self.mainstack.addWidget(self.contentstack)
  203. self.mainstack.addWidget(self.versionstack)
  204. self.mainstack.setCurrentIndex(self.core.mw_curstack)
  205. self.setCentralWidget(self.mainstack)