frame_notes.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from PyQt5 import QtWidgets
  2. from PyQt5.QtCore import pyqtSignal
  3. from PyQt5.QtGui import QFont
  4. from PyQt5.QtWidgets import QTextEdit, QApplication
  5. from core.repositories import SessionRepository
  6. from ui.qt.widgets.frame_notes_ui import Ui_frameNotes
  7. class FrameNotes(QtWidgets.QFrame):
  8. notesChanged = pyqtSignal()
  9. def __init__(self, parent=None):
  10. super().__init__(parent)
  11. self.dirty = False
  12. self.playlist = None
  13. self.createWidgets()
  14. def createWidgets(self):
  15. self.ui = Ui_frameNotes()
  16. self.ui.setupUi(self)
  17. # Setup the QTextEdit editor configuration
  18. self.ui.sessionNotes.setAutoFormatting(QTextEdit.AutoAll)
  19. self.ui.sessionNotes.selectionChanged.connect(self.update_format)
  20. # Initialize default font size.
  21. font = QFont('Verdana', 12)
  22. self.ui.sessionNotes.setFont(font)
  23. # We need to repeat the size to init the current format.
  24. self.ui.sessionNotes.setFontPointSize(12)
  25. self.ui.btnBold.toggled.connect(self.toggleBold)
  26. self.ui.btnItalic.toggled.connect(self.toggleItalic)
  27. self.ui.btnUnderline.toggled.connect(self.toggleUnderlined)
  28. self.ui.btnUndo.clicked.connect(self.ui.sessionNotes.undo)
  29. self.ui.btnRedo.clicked.connect(self.ui.sessionNotes.redo)
  30. self.ui.sessionNotes.textChanged.connect(self.notes_changed)
  31. QtWidgets.qApp.focusChanged.connect(self.focus_changed)
  32. # A list of all format-related widgets/actions, so we can disable/enable signals when updating.
  33. self._format_actions = [
  34. self.ui.btnBold,
  35. self.ui.btnItalic,
  36. self.ui.btnUnderline,
  37. ]
  38. self.update_format()
  39. def toggleBold(self, active):
  40. self.ui.sessionNotes.setFontWeight(QFont.Bold if active else QFont.Normal)
  41. def toggleItalic(self, active):
  42. self.ui.sessionNotes.setFontItalic(active)
  43. def toggleUnderlined(self, active):
  44. self.ui.sessionNotes.setFontUnderline(active)
  45. def _block_signals(self, objects, b):
  46. for o in objects:
  47. o.blockSignals(b)
  48. def update_format(self):
  49. """
  50. Update the font format toolbar/actions when a new text selection is made. This is neccessary to keep
  51. toolbars/etc. in sync with the current edit state.
  52. :return:
  53. """
  54. # # Disable signals for all format widgets, so changing values here does not trigger further formatting.
  55. self._block_signals(self._format_actions, True)
  56. self.ui.btnItalic.setChecked(self.ui.sessionNotes.fontItalic())
  57. self.ui.btnItalic.setChecked(self.ui.sessionNotes.fontUnderline())
  58. self.ui.btnBold.setChecked(self.ui.sessionNotes.fontWeight() == QFont.Bold)
  59. self._block_signals(self._format_actions, False)
  60. def notes(self):
  61. return self.ui.sessionNotes.toHtml()
  62. def set_notes(self, html):
  63. self.ui.sessionNotes.textChanged.disconnect(self.notes_changed)
  64. self.ui.sessionNotes.setHtml(html)
  65. self.ui.sessionNotes.textChanged.connect(self.notes_changed)
  66. def set_playlist(self, playlist):
  67. self.playlist = playlist
  68. self.set_notes(playlist.notes)
  69. def focus_changed(self, old, now):
  70. if old is self.ui.sessionNotes and self.dirty:
  71. self.save()
  72. def notes_changed(self):
  73. self.dirty = True
  74. def save(self):
  75. print("save notes")
  76. notes = self.notes()
  77. session_repo = SessionRepository()
  78. self.playlist.notes = notes
  79. session_repo.commit()
  80. self.dirty = False