You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

894 lines
40 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed May 29 00:02:24 2019
  5. @author: ivan
  6. """
  7. import sys
  8. import fitz
  9. import io
  10. import subprocess
  11. import os
  12. import time
  13. from copy import copy
  14. from PyQt5.QtWidgets import QApplication, QAction, QLabel, QDialogButtonBox, QDialog, QFileDialog, QMessageBox, QPushButton, QLineEdit, QCheckBox, QSpinBox, QDoubleSpinBox, QTableWidgetItem, QTabWidget, QComboBox, QWidget, QScrollArea, QMainWindow, QShortcut, QMdiSubWindow
  15. from PyQt5.QtCore import QFile, QObject, Qt, pyqtSlot, QSettings
  16. from PyQt5.QtGui import QPixmap, QImage, QKeySequence
  17. from PyQt5 import uic
  18. from chordsheet.tableView import ChordTableView, BlockTableView
  19. from chordsheet.comboBox import MComboBox
  20. from chordsheet.pdfViewer import PDFViewer
  21. from reportlab.lib.units import mm, cm, inch, pica
  22. from reportlab.lib.pagesizes import A4, A5, LETTER, LEGAL
  23. from reportlab.pdfbase import pdfmetrics
  24. from reportlab.pdfbase.ttfonts import TTFont
  25. from chordsheet.common import scriptDir
  26. from chordsheet.document import Document, Style, Chord, Block, Section
  27. from chordsheet.render import Renderer
  28. from chordsheet.parsers import parseFingering, parseName
  29. from chordsheet.messageBox import UnsavedMessageBox, UnreadableMessageBox, ChordNameWarningMessageBox, SectionNameWarningMessageBox, BlockMustHaveSectionWarningMessageBox, VoicingWarningMessageBox, LengthWarningMessageBox
  30. from chordsheet.dialogs import GuitarDialog, AboutDialog
  31. from chordsheet.panels import DocInfoDockWidget, PageSetupDockWidget, ChordsDockWidget, SectionsDockWidget, BlocksDockWidget, PreviewDockWidget
  32. import _version
  33. # enable automatic high DPI scaling on Windows
  34. QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
  35. QApplication.setOrganizationName("Ivan Holmes")
  36. QApplication.setOrganizationDomain("ivanholmes.co.uk")
  37. QApplication.setApplicationName("Chordsheet")
  38. settings = QSettings()
  39. pdfmetrics.registerFont(
  40. TTFont('FreeSans', os.path.join(scriptDir, 'fonts', 'FreeSans.ttf')))
  41. if sys.platform == "darwin":
  42. pdfmetrics.registerFont(
  43. TTFont('HelveticaNeue', 'HelveticaNeue.ttc', subfontIndex=0))
  44. # dictionaries for combo boxes
  45. pageSizeDict = {'A4': A4, 'A5': A5, 'Letter': LETTER, 'Legal': LEGAL}
  46. # point is 1 because reportlab's native unit is points.
  47. unitDict = {'mm': mm, 'cm': cm, 'inch': inch, 'point': 1, 'pica': pica}
  48. class MainWindow(QMainWindow):
  49. """
  50. Class for the main window of the application.
  51. """
  52. def __init__(self, filename=None):
  53. """
  54. Initialisation function for the main window of the application.
  55. Arguments:
  56. doc -- the Document object for the window to use
  57. style -- the Style object for the window to use
  58. """
  59. super().__init__()
  60. self.UIFileLoader(str(os.path.join(scriptDir, 'ui', 'new.ui')))
  61. self.setCentralWidget(self.window.centralWidget)
  62. self.setMenuBar(self.window.menuBar)
  63. self.setWindowTitle("Chordsheet")
  64. if filename:
  65. try:
  66. self.openFile(filename)
  67. except Exception:
  68. UnreadableMessageBox().exec()
  69. def closeEvent(self, event):
  70. """
  71. Reimplement the built in closeEvent to allow asking the user to save.
  72. """
  73. if not self.window.mdiArea.subWindowList():
  74. self.close()
  75. def UIFileLoader(self, ui_file):
  76. """
  77. Loads the .ui file for this window and connects the UI elements to their actions.
  78. """
  79. ui_file = QFile(ui_file)
  80. ui_file.open(QFile.ReadOnly)
  81. self.window = uic.loadUi(ui_file)
  82. ui_file.close()
  83. self.docinfo = DocInfoDockWidget()
  84. self.psetup = PageSetupDockWidget()
  85. self.chordsw = ChordsDockWidget()
  86. self.sectionsw = SectionsDockWidget()
  87. self.blocksw = BlocksDockWidget()
  88. self.previeww = PreviewDockWidget()
  89. self.addDockWidget(Qt.LeftDockWidgetArea, self.docinfo)
  90. self.addDockWidget(Qt.LeftDockWidgetArea, self.psetup)
  91. self.addDockWidget(Qt.LeftDockWidgetArea, self.previeww)
  92. self.addDockWidget(Qt.RightDockWidgetArea, self.chordsw)
  93. self.addDockWidget(Qt.RightDockWidgetArea, self.sectionsw)
  94. self.addDockWidget(Qt.RightDockWidgetArea, self.blocksw)
  95. # link all the UI elements
  96. self.window.mdiArea.subWindowActivated.connect(self.switchDocument)
  97. self.window.actionAbout.triggered.connect(self.menuFileAboutAction)
  98. self.window.actionNew.triggered.connect(self.menuFileNewAction)
  99. self.window.actionOpen.triggered.connect(self.menuFileOpenAction)
  100. self.window.actionSave.triggered.connect(self.menuFileSaveAction)
  101. self.window.actionSave_as.triggered.connect(self.menuFileSaveAsAction)
  102. self.window.actionSave_PDF.triggered.connect(
  103. self.menuFileSavePDFAction)
  104. self.window.actionPrint.triggered.connect(self.menuFilePrintAction)
  105. self.window.actionClose.triggered.connect(self.menuFileCloseAction)
  106. self.window.actionUndo.triggered.connect(self.menuEditUndoAction)
  107. self.window.actionRedo.triggered.connect(self.menuEditRedoAction)
  108. self.window.actionCut.triggered.connect(self.menuEditCutAction)
  109. self.window.actionCopy.triggered.connect(self.menuEditCopyAction)
  110. self.window.actionPaste.triggered.connect(self.menuEditPasteAction)
  111. self.window.actionNew.setShortcut(QKeySequence.New)
  112. self.window.actionOpen.setShortcut(QKeySequence.Open)
  113. self.window.actionSave.setShortcut(QKeySequence.Save)
  114. self.window.actionSave_as.setShortcut(QKeySequence.SaveAs)
  115. self.window.actionSave_PDF.setShortcut(QKeySequence("Ctrl+E"))
  116. self.window.actionPrint.setShortcut(QKeySequence.Print)
  117. self.window.actionClose.setShortcut(QKeySequence.Close)
  118. self.window.actionUndo.setShortcut(QKeySequence.Undo)
  119. self.window.actionRedo.setShortcut(QKeySequence.Redo)
  120. self.window.actionCut.setShortcut(QKeySequence.Cut)
  121. self.window.actionCopy.setShortcut(QKeySequence.Copy)
  122. self.window.actionPaste.setShortcut(QKeySequence.Paste)
  123. self.psetup.widget().pageSizeComboBox.currentIndexChanged.connect(
  124. self.pageSizeAction)
  125. self.psetup.widget().documentUnitsComboBox.currentIndexChanged.connect(
  126. self.unitAction)
  127. self.psetup.widget().pageSizeComboBox.addItems(list(pageSizeDict.keys()))
  128. self.psetup.widget().pageSizeComboBox.setCurrentText(
  129. list(pageSizeDict.keys())[0])
  130. self.psetup.widget().documentUnitsComboBox.addItems(list(unitDict.keys()))
  131. self.psetup.widget().documentUnitsComboBox.setCurrentText(
  132. list(unitDict.keys())[0])
  133. self.psetup.widget().includedFontCheckBox.stateChanged.connect(
  134. self.includedFontAction)
  135. self.previeww.widget().updatePreviewButton.clicked.connect(self.generateAction)
  136. # update whole document when any tab is selected
  137. # self.window.tabWidget.tabBarClicked.connect(self.tabBarUpdateAction)
  138. self.chordsw.widget().guitarVoicingButton.clicked.connect(
  139. self.guitarVoicingAction)
  140. self.chordsw.widget().addChordButton.clicked.connect(self.addChordAction)
  141. self.chordsw.widget().removeChordButton.clicked.connect(self.removeChordAction)
  142. self.chordsw.widget().updateChordButton.clicked.connect(self.updateChordAction)
  143. # connecting clicked only works for this combo box because it's my own modified version (MComboBox)
  144. self.blocksw.widget().blockSectionComboBox.clicked.connect(
  145. self.blockSectionClickedAction)
  146. self.blocksw.widget().blockSectionComboBox.currentIndexChanged.connect(
  147. self.blockSectionChangedAction)
  148. self.blocksw.widget().addBlockButton.clicked.connect(self.addBlockAction)
  149. self.blocksw.widget().removeBlockButton.clicked.connect(self.removeBlockAction)
  150. self.blocksw.widget().updateBlockButton.clicked.connect(self.updateBlockAction)
  151. self.sectionsw.widget().addSectionButton.clicked.connect(self.addSectionAction)
  152. self.sectionsw.widget().removeSectionButton.clicked.connect(
  153. self.removeSectionAction)
  154. self.sectionsw.widget().updateSectionButton.clicked.connect(
  155. self.updateSectionAction)
  156. self.chordsw.widget().chordTableView.clicked.connect(self.chordClickedAction)
  157. self.sectionsw.widget().sectionTableView.clicked.connect(self.sectionClickedAction)
  158. self.blocksw.widget().blockTableView.clicked.connect(self.blockClickedAction)
  159. # Set the tab widget to Overview tab
  160. # self.window.tabWidget.setCurrentIndex(0)
  161. def UIInitDocument(self, doc):
  162. """
  163. Fills the window's fields with the values from its document.
  164. """
  165. # self.updateTitleBar()
  166. # set all fields to appropriate values from document
  167. self.docinfo.widget().titleLineEdit.setText(doc.title)
  168. self.docinfo.widget().subtitleLineEdit.setText(doc.subtitle)
  169. self.docinfo.widget().composerLineEdit.setText(doc.composer)
  170. self.docinfo.widget().arrangerLineEdit.setText(doc.arranger)
  171. self.docinfo.widget().timeSignatureSpinBox.setValue(doc.timeSignature)
  172. self.docinfo.widget().tempoLineEdit.setText(doc.tempo)
  173. self.chordsw.widget().chordTableView.populate(doc.chordList)
  174. self.sectionsw.widget().sectionTableView.populate(doc.sectionList)
  175. # populate the block table with the first section, account for a document with no sections
  176. self.currentSection = doc.sectionList[0] if len(
  177. doc.sectionList) else None
  178. self.blocksw.widget().blockTableView.populate(
  179. self.currentSection.blockList if self.currentSection else [])
  180. self.updateChordDict(doc)
  181. self.updateSectionDict(doc)
  182. def UIInitStyle(self, style):
  183. """
  184. Fills the window's fields with the values from its style.
  185. """
  186. self.psetup.widget().pageSizeComboBox.setCurrentText(
  187. [k for k, v in pageSizeDict.items() if v==style.pageSize][0])
  188. self.psetup.widget().documentUnitsComboBox.setCurrentText(
  189. [k for k, v in unitDict.items() if v==style.unit][0])
  190. self.psetup.widget().lineSpacingDoubleSpinBox.setValue(style.lineSpacing)
  191. self.psetup.widget().leftMarginLineEdit.setText(str(style.leftMargin))
  192. self.psetup.widget().rightMarginLineEdit.setText(str(style.rightMargin))
  193. self.psetup.widget().topMarginLineEdit.setText(str(style.topMargin))
  194. self.psetup.widget().bottomMarginLineEdit.setText(str(style.bottomMargin))
  195. self.psetup.widget().fontComboBox.setDisabled(True)
  196. self.psetup.widget().includedFontCheckBox.setChecked(True)
  197. self.psetup.widget().beatWidthLineEdit.setText(str(style.unitWidth))
  198. def updateChordDict(self, doc):
  199. """
  200. Updates the dictionary used to generate the Chord menu (on the block tab)
  201. """
  202. self.chordDict = {'None': None}
  203. self.chordDict.update({c.name: c for c in doc.chordList})
  204. self.blocksw.widget().blockChordComboBox.clear()
  205. self.blocksw.widget().blockChordComboBox.addItems(list(self.chordDict.keys()))
  206. def updateSectionDict(self, doc):
  207. """
  208. Updates the dictionary used to generate the Section menu (on the block tab)
  209. """
  210. self.sectionDict = {s.name: s for s in doc.sectionList}
  211. self.blocksw.widget().blockSectionComboBox.clear()
  212. self.blocksw.widget().blockSectionComboBox.addItems(
  213. list(self.sectionDict.keys()))
  214. def switchDocument(self):
  215. if self.window.mdiArea.currentSubWindow() is not None:
  216. self.UIInitDocument(self.window.mdiArea.currentSubWindow().doc)
  217. self.UIInitStyle(self.window.mdiArea.currentSubWindow().style)
  218. def generateAction(self):
  219. if self.window.mdiArea.currentSubWindow() is not None:
  220. self.window.mdiArea.currentSubWindow().updateDocument()
  221. self.window.mdiArea.currentSubWindow().updatePreview()
  222. def removeChordAction(self):
  223. if self.chordsw.widget().chordTableView.selectionModel().hasSelection(): #  check for selection
  224. self.window.mdiArea.currentSubWindow().updateChords()
  225. row = self.chordsw.widget().chordTableView.selectionModel().currentIndex().row()
  226. oldName = self.chordsw.widget().chordTableView.model.item(row, 0).text()
  227. self.window.mdiArea.currentSubWindow().doc.chordList.pop(row)
  228. self.chordsw.widget().chordTableView.populate(self.window.mdiArea.currentSubWindow().doc.chordList)
  229. # remove the chord if any of the blocks have it attached
  230. if self.currentSection is not None:
  231. for s in self.window.mdiArea.currentSubWindow().doc.sectionList:
  232. for b in s.blockList:
  233. if b.chord:
  234. if b.chord.name == oldName:
  235. b.chord = None
  236. self.blocksw.widget().blockTableView.populate(self.currentSection.blockList)
  237. self.clearChordLineEdits()
  238. self.window.mdiArea.currentSubWindow().updateChordDict()
  239. def addChordAction(self):
  240. success = False # initialise
  241. self.window.mdiArea.currentSubWindow().updateChords()
  242. cName = parseName(self.chordsw.widget().chordNameLineEdit.text())
  243. if cName:
  244. self.window.mdiArea.currentSubWindow().doc.chordList.append(Chord(cName))
  245. if self.chordsw.widget().guitarVoicingLineEdit.text() or self.chordsw.widget().pianoVoicingLineEdit.text():
  246. if self.chordsw.widget().guitarVoicingLineEdit.text():
  247. try:
  248. self.window.mdiArea.currentSubWindow().doc.chordList[-1].voicings['guitar'] = parseFingering(
  249. self.chordsw.widget().guitarVoicingLineEdit.text(), 'guitar')
  250. success = True #  chord successfully parsed
  251. except Exception:
  252. VoicingWarningMessageBox().exec() # Voicing is malformed, warn user
  253. if self.chordsw.widget().pianoVoicingLineEdit.text():
  254. try:
  255. self.window.mdiArea.currentSubWindow().doc.chordList[-1].voicings['piano'] = parseFingering(
  256. self.chordsw.widget().pianoVoicingLineEdit.text(), 'piano')
  257. success = True #  chord successfully parsed
  258. except Exception:
  259. VoicingWarningMessageBox().exec() # Voicing is malformed, warn user
  260. else:
  261. success = True #  chord successfully parsed
  262. else:
  263. ChordNameWarningMessageBox().exec() # Chord has no name, warn user
  264. if success == True: # if chord was parsed properly
  265. self.chordsw.widget().chordTableView.populate(self.window.mdiArea.currentSubWindow().doc.chordList)
  266. self.clearChordLineEdits()
  267. self.window.mdiArea.currentSubWindow().updateChordDict()
  268. def updateChordAction(self):
  269. success = False # see comments above
  270. if self.chordsw.widget().chordTableView.selectionModel().hasSelection(): #  check for selection
  271. self.window.mdiArea.currentSubWindow().updateChords()
  272. row = self.chordsw.widget().chordTableView.selectionModel().currentIndex().row()
  273. oldName = self.chordsw.widget().chordTableView.model.item(row, 0).text()
  274. cName = parseName(self.chordsw.widget().chordNameLineEdit.text())
  275. if cName:
  276. self.window.mdiArea.currentSubWindow().doc.chordList[row].name = cName
  277. if self.chordsw.widget().guitarVoicingLineEdit.text() or self.chordsw.widget().pianoVoicingLineEdit.text():
  278. if self.chordsw.widget().guitarVoicingLineEdit.text():
  279. try:
  280. self.window.mdiArea.currentSubWindow().doc.chordList[row].voicings['guitar'] = parseFingering(
  281. self.chordsw.widget().guitarVoicingLineEdit.text(), 'guitar')
  282. success = True #  chord successfully parsed
  283. except Exception:
  284. VoicingWarningMessageBox().exec() # Voicing is malformed, warn user
  285. if self.chordsw.widget().pianoVoicingLineEdit.text():
  286. try:
  287. self.window.mdiArea.currentSubWindow().doc.chordList[row].voicings['piano'] = parseFingering(
  288. self.chordsw.widget().pianoVoicingLineEdit.text(), 'piano')
  289. success = True #  chord successfully parsed
  290. except Exception:
  291. VoicingWarningMessageBox().exec() # Voicing is malformed, warn user
  292. else:
  293. success = True #  chord successfully parsed
  294. else:
  295. ChordNameWarningMessageBox().exec()
  296. if success == True:
  297. self.window.mdiArea.currentSubWindow().updateChordDict()
  298. self.chordsw.widget().chordTableView.populate(self.window.mdiArea.currentSubWindow().doc.chordList)
  299. # update the names of chords in all blocklists in case they've already been used
  300. for s in self.window.mdiArea.currentSubWindow().doc.sectionList:
  301. for b in s.blockList:
  302. if b.chord:
  303. if b.chord.name == oldName:
  304. b.chord.name = cName
  305. if self.currentSection and self.currentSection.blockList:
  306. self.blocksw.widget().blockTableView.populate(self.currentSection.blockList)
  307. self.clearChordLineEdits()
  308. def removeSectionAction(self):
  309. if self.sectionsw.widget().sectionTableView.selectionModel().hasSelection(): #  check for selection
  310. self.window.mdiArea.currentSubWindow().updateSections()
  311. row = self.sectionsw.widget().sectionTableView.selectionModel().currentIndex().row()
  312. self.window.mdiArea.currentSubWindow().doc.sectionList.pop(row)
  313. self.sectionsw.widget().sectionTableView.populate(self.window.mdiArea.currentSubWindow().doc.sectionList)
  314. self.clearSectionLineEdits()
  315. self.window.mdiArea.currentSubWindow().updateSectionDict()
  316. def addSectionAction(self):
  317. self.window.mdiArea.currentSubWindow().updateSections()
  318. sName = self.sectionsw.widget().sectionNameLineEdit.text()
  319. if sName and sName not in [s.name for s in self.window.mdiArea.currentSubWindow().doc.sectionList]:
  320. self.window.mdiArea.currentSubWindow().doc.sectionList.append(Section(name=sName))
  321. self.sectionsw.widget().sectionTableView.populate(self.window.mdiArea.currentSubWindow().doc.sectionList)
  322. self.clearSectionLineEdits()
  323. self.window.mdiArea.currentSubWindow().updateSectionDict()
  324. else:
  325. # Section has no name or non unique, warn user
  326. SectionNameWarningMessageBox().exec()
  327. def updateSectionAction(self):
  328. if self.sectionsw.widget().sectionTableView.selectionModel().hasSelection(): #  check for selection
  329. self.window.mdiArea.currentSubWindow().updateSections()
  330. row = self.sectionsw.widget().sectionTableView.selectionModel().currentIndex().row()
  331. sName = self.sectionsw.widget().sectionNameLineEdit.text()
  332. if sName and sName not in [s.name for s in self.window.mdiArea.currentSubWindow().doc.sectionList]:
  333. self.window.mdiArea.currentSubWindow().doc.sectionList[row].name = sName
  334. self.sectionsw.widget().sectionTableView.populate(self.window.mdiArea.currentSubWindow().doc.sectionList)
  335. self.clearSectionLineEdits()
  336. self.window.mdiArea.currentSubWindow().updateSectionDict()
  337. else:
  338. # Section has no name or non unique, warn user
  339. SectionNameWarningMessageBox().exec()
  340. def removeBlockAction(self):
  341. if self.blocksw.widget().blockTableView.selectionModel().hasSelection(): #  check for selection
  342. self.window.mdiArea.currentSubWindow().updateBlocks(self.currentSection)
  343. row = self.blocksw.widget().blockTableView.selectionModel().currentIndex().row()
  344. self.currentSection.blockList.pop(row)
  345. self.blocksw.widget().blockTableView.populate(self.currentSection.blockList)
  346. def addBlockAction(self):
  347. self.window.mdiArea.currentSubWindow().updateBlocks(self.currentSection)
  348. try:
  349. #  can the value entered for block length be cast as a float
  350. bLength = float(self.blocksw.widget().blockLengthLineEdit.text())
  351. except Exception:
  352. bLength = False
  353. if bLength: # create the block
  354. self.currentSection.blockList.append(Block(bLength,
  355. chord=self.chordDict[self.blocksw.widget().blockChordComboBox.currentText(
  356. )],
  357. notes=(self.blocksw.widget().blockNotesLineEdit.text() if not "" else None)))
  358. self.blocksw.widget().blockTableView.populate(self.currentSection.blockList)
  359. self.clearBlockLineEdits()
  360. else:
  361. # show warning that length was not entered or in wrong format
  362. LengthWarningMessageBox().exec()
  363. def updateBlockAction(self):
  364. if self.blocksw.widget().blockTableView.selectionModel().hasSelection(): #  check for selection
  365. self.window.mdiArea.currentSubWindow().updateBlocks(self.currentSection)
  366. try:
  367. #  can the value entered for block length be cast as a float
  368. bLength = float(self.blocksw.widget().blockLengthLineEdit.text())
  369. except Exception:
  370. bLength = False
  371. row = self.blocksw.widget().blockTableView.selectionModel().currentIndex().row()
  372. if bLength:
  373. self.currentSection.blockList[row] = (Block(bLength,
  374. chord=self.chordDict[self.blocksw.widget().blockChordComboBox.currentText(
  375. )],
  376. notes=(self.blocksw.widget().blockNotesLineEdit.text() if not "" else None)))
  377. self.blocksw.widget().blockTableView.populate(
  378. self.currentSection.blockList)
  379. self.clearBlockLineEdits()
  380. else:
  381. LengthWarningMessageBox().exec()
  382. def pageSizeAction(self, index):
  383. self.pageSizeSelected = self.psetup.widget().pageSizeComboBox.itemText(index)
  384. def unitAction(self, index):
  385. self.unitSelected = self.psetup.widget().documentUnitsComboBox.itemText(index)
  386. def includedFontAction(self):
  387. if self.window.mdiArea.currentSubWindow() is not None:
  388. if self.psetup.widget().includedFontCheckBox.isChecked():
  389. self.window.mdiArea.currentSubWindow().style.useIncludedFont = True
  390. else:
  391. self.window.mdiArea.currentSubWindow().style.useIncludedFont = False
  392. def chordClickedAction(self, index):
  393. # set the controls to the values from the selected chord
  394. self.chordsw.widget().chordNameLineEdit.setText(
  395. self.chordsw.widget().chordTableView.model.item(index.row(), 0).text())
  396. self.chordsw.widget().guitarVoicingLineEdit.setText(
  397. self.chordsw.widget().chordTableView.model.item(index.row(), 1).text())
  398. self.chordsw.widget().pianoVoicingLineEdit.setText(
  399. self.chordsw.widget().chordTableView.model.item(index.row(), 2).text())
  400. def sectionClickedAction(self, index):
  401. # set the controls to the values from the selected section
  402. self.sectionsw.widget().sectionNameLineEdit.setText(
  403. self.sectionsw.widget().sectionTableView.model.item(index.row(), 0).text())
  404. # also set the combo box on the block page to make it flow well
  405. curSecName = self.sectionsw.widget().sectionTableView.model.item(
  406. index.row(), 0).text()
  407. if curSecName:
  408. self.blocksw.widget().blockSectionComboBox.setCurrentText(
  409. curSecName)
  410. def blockSectionClickedAction(self, text):
  411. if text:
  412. self.window.mdiArea.currentSubWindow().updateBlocks(self.sectionDict[text])
  413. def blockSectionChangedAction(self, index):
  414. sName = self.blocksw.widget().blockSectionComboBox.currentText()
  415. if sName:
  416. if self.window.mdiArea.currentSubWindow() is not None:
  417. self.currentSection = self.sectionDict.get(sName, None)
  418. if self.currentSection is not None:
  419. self.blocksw.widget().blockTableView.populate(self.currentSection.blockList)
  420. else:
  421. self.currentSection = None
  422. def blockClickedAction(self, index):
  423. # set the controls to the values from the selected block
  424. bChord = self.blocksw.widget().blockTableView.model.item(index.row(), 0).text()
  425. self.blocksw.widget().blockChordComboBox.setCurrentText(
  426. bChord if bChord else "None")
  427. self.blocksw.widget().blockLengthLineEdit.setText(
  428. self.blocksw.widget().blockTableView.model.item(index.row(), 1).text())
  429. self.blocksw.widget().blockNotesLineEdit.setText(
  430. self.blocksw.widget().blockTableView.model.item(index.row(), 2).text())
  431. def getPath(self, value):
  432. """
  433. Wrapper for Qt settings to return home directory if no setting exists.
  434. """
  435. return str((settings.value(value) if settings.value(value) else os.path.expanduser("~")))
  436. def setPath(self, value, fullpath):
  437. """
  438. Wrapper for Qt settings to set path to open/save from next time from current file location.
  439. """
  440. return settings.setValue(value, os.path.dirname(fullpath))
  441. def menuFileNewAction(self):
  442. dw = DocumentWindow(Document(), Style(), None)
  443. self.window.mdiArea.addSubWindow(dw)
  444. self.UIInitDocument(dw.doc)
  445. dw.show()
  446. def menuFileOpenAction(self):
  447. filePath = QFileDialog.getOpenFileName(self.window, 'Open file', self.getPath(
  448. "workingPath"), "Chordsheet Markup Language files (*.xml *.cml);;Chordsheet Macro files (*.cma)")[0]
  449. if filePath:
  450. dw = DocumentWindow.openFile(filePath)
  451. self.window.mdiArea.addSubWindow(dw)
  452. self.UIInitDocument(dw.doc)
  453. self.UIInitStyle(dw.style)
  454. self.currentSection = None
  455. dw.show()
  456. def menuFileSaveAction(self):
  457. if self.window.mdiArea.currentSubWindow() is not None:
  458. self.window.mdiArea.currentSubWindow().updateDocument()
  459. if self.window.mdiArea.currentSubWindow().currentFilePath:
  460. fileExt = os.path.splitext(self.window.mdiArea.currentSubWindow().currentFilePath)[1].lower()
  461. if fileExt != ".cma":
  462. # Chordsheet Macro files can't be saved at this time
  463. self.window.mdiArea.currentSubWindow().saveFile(self.window.mdiArea.currentSubWindow().currentFilePath)
  464. else:
  465. filePath = QFileDialog.getSaveFileName(self.window, 'Save file', self.getPath(
  466. "workingPath"), "Chordsheet ML files (*.xml *.cml)")[0]
  467. if filePath:
  468. self.window.mdiArea.currentSubWindow().saveFile(filePath)
  469. def menuFileSaveAsAction(self):
  470. if self.window.mdiArea.currentSubWindow() is not None:
  471. self.window.mdiArea.currentSubWindow().updateDocument()
  472. filePath = QFileDialog.getSaveFileName(self.window, 'Save file', self.getPath(
  473. "workingPath"), "Chordsheet ML files (*.xml *.cml)")[0]
  474. if filePath:
  475. self.window.mdiArea.currentSubWindow().saveFile(filePath)
  476. def menuFileSavePDFAction(self):
  477. if self.window.mdiArea.currentSubWindow() is not None:
  478. self.window.mdiArea.currentSubWindow().updateDocument()
  479. self.window.mdiArea.currentSubWindow().updatePreview()
  480. filePath = QFileDialog.getSaveFileName(self.window, 'Save file', self.getPath(
  481. "lastExportPath"), "PDF files (*.pdf)")[0]
  482. if filePath:
  483. self.window.mdiArea.currentSubWindow().renderer.savePDF(filePath)
  484. self.window.mdiArea.currentSubWindow().setPath("lastExportPath", filePath)
  485. def menuFilePrintAction(self):
  486. if sys.platform == "darwin":
  487. pass
  488. # subprocess.call()
  489. else:
  490. pass
  491. @pyqtSlot()
  492. def menuFileCloseAction(self):
  493. self.saveWarning()
  494. def menuFileAboutAction(self):
  495. AboutDialog()
  496. def menuEditUndoAction(self):
  497. try:
  498. QApplication.focusWidget().undo() # see if the built in widget supports it
  499. except Exception:
  500. pass #  if not just fail silently
  501. def menuEditRedoAction(self):
  502. try:
  503. QApplication.focusWidget().redo()
  504. except Exception:
  505. pass
  506. def menuEditCutAction(self):
  507. try:
  508. QApplication.focusWidget().cut()
  509. except Exception:
  510. pass
  511. def menuEditCopyAction(self):
  512. try:
  513. QApplication.focusWidget().copy()
  514. except Exception:
  515. pass
  516. def menuEditPasteAction(self):
  517. try:
  518. QApplication.focusWidget().paste()
  519. except Exception:
  520. pass
  521. def guitarVoicingAction(self):
  522. gdialog = GuitarDialog()
  523. voicing = gdialog.getVoicing()
  524. if voicing:
  525. self.chordsw.widget().guitarVoicingLineEdit.setText(voicing)
  526. def clearChordLineEdits(self):
  527. self.chordsw.widget().chordNameLineEdit.clear()
  528. self.chordsw.widget().guitarVoicingLineEdit.clear()
  529. self.chordsw.widget().pianoVoicingLineEdit.clear()
  530. # necessary on Mojave with PyInstaller (or previous contents will be shown)
  531. self.chordsw.widget().chordNameLineEdit.repaint()
  532. self.chordsw.widget().guitarVoicingLineEdit.repaint()
  533. self.chordsw.widget().pianoVoicingLineEdit.repaint()
  534. def clearSectionLineEdits(self):
  535. self.sectionsw.widget().sectionNameLineEdit.clear()
  536. # necessary on Mojave with PyInstaller (or previous contents will be shown)
  537. self.sectionsw.widget().sectionNameLineEdit.repaint()
  538. def clearBlockLineEdits(self):
  539. self.blocksw.widget().blockLengthLineEdit.clear()
  540. self.blocksw.widget().blockNotesLineEdit.clear()
  541. # necessary on Mojave with PyInstaller (or previous contents will be shown)
  542. self.blocksw.widget().blockLengthLineEdit.repaint()
  543. self.blocksw.widget().blockNotesLineEdit.repaint()
  544. class DocumentWindow(QMdiSubWindow):
  545. def __init__(self, doc, style, filename):
  546. super().__init__()
  547. self.UIFileLoader(str(os.path.join(scriptDir, 'ui', 'document.ui')))
  548. self.doc = doc
  549. self.style = style
  550. self.renderer = Renderer(self.doc, self.style)
  551. self.lastDoc = copy(self.doc)
  552. self.currentFilePath = filename
  553. mw.updateChordDict(self.doc)
  554. mw.updateSectionDict(self.doc)
  555. self.currentSection = None
  556. def UIFileLoader(self, ui_file):
  557. ui_file = QFile(ui_file)
  558. ui_file.open(QFile.ReadOnly)
  559. self.setWidget(uic.loadUi(ui_file))
  560. ui_file.close()
  561. @classmethod
  562. def openFile(cls, filePath):
  563. dw = cls(Document(), Style(), None)
  564. dw.loadFile(filePath)
  565. return dw
  566. def loadFile(self, filePath):
  567. """
  568. Opens a file from a file path and sets up the window accordingly.
  569. """
  570. self.currentFilePath = filePath
  571. fileExt = os.path.splitext(self.currentFilePath)[1].lower()
  572. if fileExt == ".cma":
  573. self.doc.loadCSMacro(self.currentFilePath)
  574. else: # if fileExt in [".xml", ".cml"]:
  575. self.doc.loadXML(self.currentFilePath)
  576. self.lastDoc = copy(self.doc)
  577. mw.setPath("workingPath", self.currentFilePath)
  578. mw.updateChordDict(self.doc)
  579. mw.updateSectionDict(self.doc)
  580. self.updateTitleBar()
  581. self.updatePreview()
  582. def saveFile(self, filePath):
  583. """
  584. Saves a file to given file path and sets up environment.
  585. """
  586. self.currentFilePath = filePath
  587. fileExt = os.path.splitext(self.currentFilePath)[1].lower()
  588. if fileExt == ".cma":
  589. # At this stage we should never get here
  590. pass
  591. else: # if fileExt in [".xml", ".cml"]:
  592. self.doc.saveXML(self.currentFilePath)
  593. self.lastDoc = copy(self.doc)
  594. mw.setPath("workingPath", self.currentFilePath)
  595. self.updateTitleBar() # as we may have a new filename
  596. def saveWarning(self):
  597. """
  598. Function to check if the document has unsaved data in it and offer to save it.
  599. """
  600. self.updateDocument() # update the document to catch all changes
  601. if self.lastDoc == self.doc:
  602. return True
  603. else:
  604. wantToSave = UnsavedMessageBox().exec()
  605. if wantToSave == QMessageBox.Save:
  606. if not self.currentFilePath:
  607. filePath = QFileDialog.getSaveFileName(self.window, 'Save file', str(
  608. os.path.expanduser("~")), "Chordsheet ML files (*.xml *.cml)")
  609. self.currentFilePath = filePath[0]
  610. self.doc.saveXML(self.currentFilePath)
  611. return True
  612. elif wantToSave == QMessageBox.Discard:
  613. return True
  614. else:
  615. return False
  616. def updatePreview(self):
  617. """
  618. Update the preview shown by rendering a new PDF and drawing it to the scroll area.
  619. """
  620. try:
  621. self.currentPreview = self.renderer.stream()
  622. except Exception:
  623. QMessageBox.warning(self, "Preview failed", "Could not update the preview.",
  624. buttons=QMessageBox.Ok, defaultButton=QMessageBox.Ok)
  625. with open('preview.pdf', 'wb') as f:
  626. f.write(self.currentPreview.getbuffer())
  627. self.widget().pdfArea.update_pdf(self.currentPreview)
  628. def updateTitleBar(self):
  629. """
  630. Update the application's title bar to reflect the current document.
  631. """
  632. if self.currentFilePath:
  633. self.widget().setWindowTitle(os.path.basename(self.currentFilePath))
  634. else:
  635. self.widget().setWindowTitle("Unsaved")
  636. def updateChords(self):
  637. """
  638. Update the chord list by reading the table.
  639. """
  640. chordTableList = []
  641. for i in range(mw.chordsw.widget().chordTableView.model.rowCount()):
  642. chordTableList.append(
  643. Chord(parseName(mw.chordsw.widget().chordTableView.model.item(i, 0).text()))),
  644. if mw.chordsw.widget().chordTableView.model.item(i, 1).text():
  645. chordTableList[-1].voicings['guitar'] = parseFingering(
  646. mw.chordsw.widget().chordTableView.model.item(i, 1).text(), 'guitar')
  647. if mw.chordsw.widget().chordTableView.model.item(i, 2).text():
  648. chordTableList[-1].voicings['piano'] = parseFingering(
  649. mw.chordsw.widget().chordTableView.model.item(i, 2).text(), 'piano')
  650. self.doc.chordList = chordTableList
  651. def matchSection(self, nameToMatch):
  652. """
  653. Given the name of a section, this function checks if it is already present in the document.
  654. If it is, it's returned. If not, a new section with the given name is returned.
  655. """
  656. section = None
  657. for s in self.doc.sectionList:
  658. if s.name == nameToMatch:
  659. section = s
  660. break
  661. if section is None:
  662. section = Section(name=nameToMatch)
  663. return section
  664. def updateSections(self):
  665. """
  666. Update the section list by reading the table
  667. """
  668. sectionTableList = []
  669. for i in range(mw.sectionsw.widget().sectionTableView.model.rowCount()):
  670. sectionTableList.append(self.matchSection(
  671. mw.sectionsw.widget().sectionTableView.model.item(i, 0).text()))
  672. self.doc.sectionList = sectionTableList
  673. def updateBlocks(self, section):
  674. """
  675. Update the block list by reading the table.
  676. """
  677. if section is None:
  678. BlockMustHaveSectionWarningMessageBox().exec()
  679. else:
  680. blockTableList = []
  681. for i in range(mw.blocksw.widget().blockTableView.model.rowCount()):
  682. blockLength = float(
  683. mw.blocksw.widget().blockTableView.model.item(i, 1).text())
  684. blockChord = mw.chordDict[(mw.blocksw.widget().blockTableView.model.item(
  685. i, 0).text() if mw.blocksw.widget().blockTableView.model.item(i, 0).text() else "None")]
  686. blockNotes = mw.blocksw.widget().blockTableView.model.item(i, 2).text(
  687. ) if mw.blocksw.widget().blockTableView.model.item(i, 2).text() else None
  688. blockTableList.append(
  689. Block(blockLength, chord=blockChord, notes=blockNotes))
  690. section.blockList = blockTableList
  691. def updateDocument(self):
  692. """
  693. Update the Document object by reading values from the UI.
  694. """
  695. self.doc.title = mw.docinfo.widget().titleLineEdit.text(
  696. ) # Title can be empty string but not None
  697. self.doc.subtitle = (mw.docinfo.widget().subtitleLineEdit.text(
  698. ) if mw.docinfo.widget().subtitleLineEdit.text() else None)
  699. self.doc.composer = (mw.docinfo.widget().composerLineEdit.text(
  700. ) if mw.docinfo.widget().composerLineEdit.text() else None)
  701. self.doc.arranger = (mw.docinfo.widget().arrangerLineEdit.text(
  702. ) if mw.docinfo.widget().arrangerLineEdit.text() else None)
  703. self.doc.tempo = (mw.docinfo.widget().tempoLineEdit.text(
  704. ) if mw.docinfo.widget().tempoLineEdit.text() else None)
  705. self.doc.timeSignature = int(mw.docinfo.widget().timeSignatureSpinBox.value(
  706. )) if mw.docinfo.widget().timeSignatureSpinBox.value() else self.doc.timeSignature
  707. self.style.pageSize = pageSizeDict[mw.pageSizeSelected]
  708. self.style.unit = unitDict[mw.unitSelected]
  709. self.style.leftMargin = float(mw.psetup.widget().leftMarginLineEdit.text(
  710. )) if mw.psetup.widget().leftMarginLineEdit.text() else self.style.leftMargin
  711. self.style.rightMargin = float(mw.psetup.widget().rightMarginLineEdit.text(
  712. )) if mw.psetup.widget().rightMarginLineEdit.text() else self.style.rightMargin
  713. self.style.topMargin = float(mw.psetup.widget().topMarginLineEdit.text(
  714. )) if mw.psetup.widget().topMarginLineEdit.text() else self.style.topMargin
  715. self.style.bottomMargin = float(mw.psetup.widget().bottomMarginLineEdit.text(
  716. )) if mw.psetup.widget().bottomMarginLineEdit.text() else self.style.bottomMargin
  717. self.style.lineSpacing = float(mw.psetup.widget().lineSpacingDoubleSpinBox.value(
  718. )) if mw.psetup.widget().lineSpacingDoubleSpinBox.value() else self.style.lineSpacing
  719. # make sure the unit width isn't too wide to draw!
  720. if mw.psetup.widget().beatWidthLineEdit.text():
  721. if (self.style.pageSize[0] - 2 * self.style.leftMargin * mm) >= (float(mw.psetup.widget().beatWidthLineEdit.text()) * 2 * self.doc.timeSignature * mm):
  722. self.style.unitWidth = float(
  723. mw.psetup.widget().beatWidthLineEdit.text())
  724. else:
  725. maxBeatWidth = (
  726. self.style.pageSize[0] - 2 * self.style.leftMargin * mm) / (2 * self.doc.timeSignature * mm)
  727. QMessageBox.warning(self, "Out of range", "Beat width is out of range. It can be a maximum of {}.".format(
  728. maxBeatWidth), buttons=QMessageBox.Ok, defaultButton=QMessageBox.Ok)
  729. # update chords, sections, blocks
  730. self.updateChords()
  731. self.updateSections()
  732. if mw.currentSection:
  733. self.updateBlocks(mw.currentSection)
  734. self.style.font = (
  735. 'FreeSans' if self.style.useIncludedFont else 'HelveticaNeue')
  736. # something for the font box here
  737. if __name__ == '__main__':
  738. app = QApplication(sys.argv)
  739. # pass first argument as filename
  740. mw = MainWindow()
  741. mw.show()
  742. sys.exit(app.exec_())