frame_notes.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from PyQt5 import QtWidgets
  2. from PyQt5.QtCore import pyqtSignal
  3. from PyQt5.QtGui import QFont
  4. from PyQt5.QtWidgets import QTextEdit
  5. from ui.qt.widgets.frame_notes_ui import Ui_frameNotes
  6. class FrameNotes(QtWidgets.QFrame):
  7. def __init__(self, parent=None):
  8. super().__init__(parent)
  9. self.createWidgets()
  10. def createWidgets(self):
  11. self.ui = Ui_frameNotes()
  12. self.ui.setupUi(self)
  13. # Setup the QTextEdit editor configuration
  14. self.ui.sessionNotes.setAutoFormatting(QTextEdit.AutoAll)
  15. self.ui.sessionNotes.selectionChanged.connect(self.update_format)
  16. # Initialize default font size.
  17. font = QFont('Verdana', 12)
  18. self.ui.sessionNotes.setFont(font)
  19. # We need to repeat the size to init the current format.
  20. self.ui.sessionNotes.setFontPointSize(12)
  21. self.ui.btnBold.toggled.connect(self.toggleBold)
  22. self.ui.btnItalic.toggled.connect(self.toggleItalic)
  23. self.ui.btnUnderline.toggled.connect(self.toggleUnderlined)
  24. self.ui.btnUndo.clicked.connect(self.ui.sessionNotes.undo)
  25. self.ui.btnRedo.clicked.connect(self.ui.sessionNotes.redo)
  26. # A list of all format-related widgets/actions, so we can disable/enable signals when updating.
  27. self._format_actions = [
  28. self.ui.btnBold,
  29. self.ui.btnItalic,
  30. self.ui.btnUnderline,
  31. ]
  32. self.update_format()
  33. def toggleBold(self, active):
  34. self.ui.sessionNotes.setFontWeight(QFont.Bold if active else QFont.Normal)
  35. def toggleItalic(self, active):
  36. self.ui.sessionNotes.setFontItalic(active)
  37. def toggleUnderlined(self, active):
  38. self.ui.sessionNotes.setFontUnderline(active)
  39. def _block_signals(self, objects, b):
  40. for o in objects:
  41. o.blockSignals(b)
  42. def update_format(self):
  43. """
  44. Update the font format toolbar/actions when a new text selection is made. This is neccessary to keep
  45. toolbars/etc. in sync with the current edit state.
  46. :return:
  47. """
  48. # # Disable signals for all format widgets, so changing values here does not trigger further formatting.
  49. self._block_signals(self._format_actions, True)
  50. self.ui.btnItalic.setChecked(self.ui.sessionNotes.fontItalic())
  51. self.ui.btnItalic.setChecked(self.ui.sessionNotes.fontUnderline())
  52. self.ui.btnBold.setChecked(self.ui.sessionNotes.fontWeight() == QFont.Bold)
  53. self._block_signals(self._format_actions, False)