| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- from PyQt5 import QtWidgets
- from PyQt5.QtCore import pyqtSignal
- from PyQt5.QtGui import QFont
- from PyQt5.QtWidgets import QTextEdit
- from ui.qt.widgets.frame_notes_ui import Ui_frameNotes
- class FrameNotes(QtWidgets.QFrame):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.createWidgets()
- def createWidgets(self):
- self.ui = Ui_frameNotes()
- self.ui.setupUi(self)
- # Setup the QTextEdit editor configuration
- self.ui.sessionNotes.setAutoFormatting(QTextEdit.AutoAll)
- self.ui.sessionNotes.selectionChanged.connect(self.update_format)
- # Initialize default font size.
- font = QFont('Verdana', 12)
- self.ui.sessionNotes.setFont(font)
- # We need to repeat the size to init the current format.
- self.ui.sessionNotes.setFontPointSize(12)
- self.ui.btnBold.toggled.connect(self.toggleBold)
- self.ui.btnItalic.toggled.connect(self.toggleItalic)
- self.ui.btnUnderline.toggled.connect(self.toggleUnderlined)
- self.ui.btnUndo.clicked.connect(self.ui.sessionNotes.undo)
- self.ui.btnRedo.clicked.connect(self.ui.sessionNotes.redo)
- # A list of all format-related widgets/actions, so we can disable/enable signals when updating.
- self._format_actions = [
- self.ui.btnBold,
- self.ui.btnItalic,
- self.ui.btnUnderline,
- ]
- self.update_format()
- def toggleBold(self, active):
- self.ui.sessionNotes.setFontWeight(QFont.Bold if active else QFont.Normal)
- def toggleItalic(self, active):
- self.ui.sessionNotes.setFontItalic(active)
- def toggleUnderlined(self, active):
- self.ui.sessionNotes.setFontUnderline(active)
- def _block_signals(self, objects, b):
- for o in objects:
- o.blockSignals(b)
- def update_format(self):
- """
- Update the font format toolbar/actions when a new text selection is made. This is neccessary to keep
- toolbars/etc. in sync with the current edit state.
- :return:
- """
- # # Disable signals for all format widgets, so changing values here does not trigger further formatting.
- self._block_signals(self._format_actions, True)
- self.ui.btnItalic.setChecked(self.ui.sessionNotes.fontItalic())
- self.ui.btnItalic.setChecked(self.ui.sessionNotes.fontUnderline())
- self.ui.btnBold.setChecked(self.ui.sessionNotes.fontWeight() == QFont.Bold)
- self._block_signals(self._format_actions, False)
|