mainwindow.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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
  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. quit = QAction("&Quit",self)
  39. quit.setShortcut("Ctrl+q")
  40. file.addAction(quit)
  41. file.triggered[QAction].connect(self.onfilemenutrigger)
  42. # edit menu
  43. edit = bar.addMenu("&Edit")
  44. # edit.addAction("&copy")
  45. # edit.addAction("&paste")
  46. edit.addAction("&build")
  47. self.reload_action = QAction("&Reload",self)
  48. self.reload_action.setShortcut("Ctrl+r")
  49. edit.addAction(self.reload_action)
  50. edit.addAction("&preferences")
  51. edit.triggered[QAction].connect(self.oneditmenutrigger)
  52. # view menu
  53. view = bar.addMenu("&View")
  54. designview = QAction("&Design",self)
  55. designview.setShortcut("F1")
  56. view.addAction(designview)
  57. contentview = QAction("&Content",self)
  58. contentview.setShortcut("F2")
  59. view.addAction(contentview)
  60. versionview = QAction("&Version",self)
  61. versionview.setShortcut("F3")
  62. view.addAction(versionview)
  63. view.triggered[QAction].connect(self.onviewmenutrigger)
  64. # about menu
  65. about = bar.addMenu("About")
  66. about.addAction("&Website")
  67. def onfilemenutrigger(self, q):
  68. print(q.text()+" is triggered")
  69. if q.text() == "&New Project":
  70. self.newprojectdialogue()
  71. elif q.text() == "&Open":
  72. self.openprojectdialogue()
  73. elif q.text() == "&Save Project as":
  74. self.saveprojectdialogue()
  75. elif q.text() == "&Quit":
  76. self.quit()
  77. def openprojectdialogue(self):
  78. print("open")
  79. def newprojectdialogue(self):
  80. dialog = QFileDialog()
  81. dialog.setFileMode(QFileDialog.Directory)
  82. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  83. projectname = dialog.getSaveFileName(
  84. self,
  85. 'New Project',
  86. self.core.dialog_path
  87. )[0]
  88. # TODO: no file type
  89. try:
  90. head, tail = os.path.split(projectname)
  91. self.core.dialog_path = head
  92. if not os.path.isdir(projectname):
  93. self.core.initnewproject(projectname)
  94. else:
  95. print("folder already exists")
  96. # TODO: check if is cascade folder
  97. except Exception as e:
  98. print('Exception', e)
  99. pass
  100. def saveprojectdialogue(self, quit=False):
  101. dialog = QFileDialog()
  102. dialog.setFileMode(QFileDialog.Directory)
  103. dialog.setAcceptMode(QFileDialog.AcceptOpen)
  104. projectname = dialog.getSaveFileName(
  105. self,
  106. 'Save Project',
  107. self.core.dialog_path
  108. )[0]
  109. # TODO: no file type
  110. try:
  111. head, tail = os.path.split(projectname)
  112. self.core.dialog_path = head
  113. if not os.path.isdir(projectname):
  114. self.core.saveproject(projectname)
  115. if quit:
  116. self.quit()
  117. else:
  118. print("folder already exists")
  119. # TODO: check if is cascade folder
  120. except Exception as e:
  121. print('Exception', e)
  122. pass
  123. def quit(self):
  124. print("Quit")
  125. if self.core.tempcwd:
  126. 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)
  127. if buttonReply == QMessageBox.Yes:
  128. self.saveprojectdialogue(quit=True)
  129. if buttonReply == QMessageBox.No:
  130. self.core.quit()
  131. else:
  132. self.core.quit()
  133. def oneditmenutrigger(self, q):
  134. print(q.text()+" is triggered")
  135. if q.text() == "&Reload":
  136. self.designstack.webkitview.reload()
  137. def onviewmenutrigger(self, q):
  138. print(q.text()+" is triggered")
  139. if q.text() == "&Design":
  140. self.mainstack.setCurrentIndex(0)
  141. elif q.text() == "&Content":
  142. self.mainstack.setCurrentIndex(1)
  143. elif q.text() == "&Version":
  144. self.mainstack.setCurrentIndex(2)
  145. def initMainStack(self):
  146. self.mainstack = QStackedWidget()
  147. self.designstack = design.DesignStack(self.core)
  148. self.contentstack = content.ContentStack(self.core)
  149. self.versionstack = QLabel("Version (git).")
  150. self.mainstack.addWidget(self.designstack)
  151. self.mainstack.addWidget(self.contentstack)
  152. self.mainstack.addWidget(self.versionstack)
  153. self.mainstack.setCurrentIndex(self.core.mw_curstack)
  154. self.setCentralWidget(self.mainstack)