mainwindow.py 8.1 KB

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