Example #1
0
class FontLayout(QGridLayout):
    """Font selection"""
    def __init__(self, value, parent=None):
        QGridLayout.__init__(self)
        if not font_is_installed(value[0]):
            print(f"Warning: Font `{value[0]}` is not installed", file=sys.stderr)
        font = tuple_to_qfont(value)
        assert font is not None
        
        # Font family
        self.family = QFontComboBox(parent)
        self.family.setCurrentFont(font)
        self.addWidget(self.family, 0, 0, 1, -1)
        
        # Font size
        self.size = QComboBox(parent)
        self.size.setEditable(True)
        sizelist = list(range(6, 12)) + list(range(12, 30, 2)) + [36, 48, 72]
        size = font.pointSize()
        if size not in sizelist:
            sizelist.append(size)
            sizelist.sort()
        self.size.addItems([str(s) for s in sizelist])
        self.size.setCurrentIndex(sizelist.index(size))
        self.addWidget(self.size, 1, 0)
        
        # Italic or not
        self.italic = QCheckBox(self.tr("Italic"), parent)
        self.italic.setChecked(font.italic())
        self.addWidget(self.italic, 1, 1)
        
        # Bold or not
        self.bold = QCheckBox(self.tr("Bold"), parent)
        self.bold.setChecked(font.bold())
        self.addWidget(self.bold, 1, 2)
        
    def get_font(self):
        font = self.family.currentFont()
        font.setItalic(self.italic.isChecked())
        font.setBold(self.bold.isChecked())
        font.setPointSize(int(self.size.currentText()))
        return qfont_to_tuple(font)

    def setStyleSheet(self, style):
        self.family.setStyleSheet(style)
        self.size.setStyleSheet(style)
        self.italic.setStyleSheet(style)
        self.bold.setStyleSheet(style)
Example #2
0
class FontToolBar(QToolBar):
    def __init__(self, title="Font Toolbar", parent=None):
        super().__init__(title, parent)
        self._parent = parent
        self.setObjectName("ftoolbar")
        self.setStyleSheet(""" 
            QWidget[objectName^="ftoolbar"]{background-color: #777777;}
            QPushButton{background-color: #777777;}
            QToolButton{background-color: #777777;};
        """)

        font = QFont()
        font.setPointSize(12)
        font.setFamily("Times New Roman")
        self._font_families = QFontComboBox()
        self._font_families.setCurrentFont(font)
        self._font_families.currentFontChanged.connect(self.keepFontSize)

        self._font_sizes = QComboBox()

        self._font_sizes.setEditable(True)
        validator = QIntValidator()
        self._font_sizes.setValidator(validator)
        FONT_SIZES = [
            '8', '9', '11', '12', '14', '16', '18', '20', '22', '24', '26',
            '28', '36', '48', '72'
        ]
        self._font_sizes.addItems(FONT_SIZES)
        self._font_sizes.activated.connect(self.changeFontSize)
        self._font_sizes.setCurrentIndex(3)

        self.addWidget(QLabel(" Font: "))
        self.addWidget(self._font_families)
        self.addWidget(self._font_sizes)

        # Bold Button
        self.bold_action = QAction(
            QIcon(os.path.join("images", "edit-bold.png")), "Bold", self)
        self.bold_action.setStatusTip("Bold")
        self.bold_action.setShortcut(QKeySequence.Bold)
        self.bold_action.setCheckable(True)
        self.addAction(self.bold_action)

        # Italic Button
        self.italic_action = QAction(
            QIcon(os.path.join("images", "edit-italic.png")), "Italic", self)
        self.italic_action.setStatusTip("Italic")
        self.italic_action.setShortcut(QKeySequence.Italic)
        self.italic_action.setCheckable(True)
        self.addAction(self.italic_action)

        # Underline Button
        self.underline_action = QAction(
            QIcon(os.path.join("images", "edit-underline.png")), "Underline",
            self)
        self.underline_action.setStatusTip("Underline")
        self.underline_action.setShortcut(QKeySequence.Underline)
        self.underline_action.setCheckable(True)
        self.addAction(self.underline_action)
        self.addWidget(VBreakLine(self))

        # Font Color Button
        self.font_color_action = QAction(
            QIcon(os.path.join("images", "edit-color.png")), "Font Color",
            self)
        self.font_color_button = HighlighterButton(color="#000000")
        self.font_color_action.setStatusTip("Font Color")
        self.font_color_action.triggered.connect(self.changeFontColor)
        self.font_color_button.clicked.connect(self.changeFontColor)
        self.addAction(self.font_color_action)
        self.addWidget(self.font_color_button)
        self.addWidget(VBreakLine(self))

        # HighLighter Color Button
        self.highlighter_action = QAction(
            QIcon(os.path.join("images", "edit-highlighter.png")),
            "Highlight Color")
        self.highlighter_button = HighlighterButton(color="yellow")
        self.highlighter_action.setStatusTip("Highlighter")
        self.highlighter_action.triggered.connect(self.changeHighlighterColor)
        self.highlighter_button.clicked.connect(self.changeHighlighterColor)
        self.addAction(self.highlighter_action)
        self.addWidget(self.highlighter_button)
        self.addWidget(VBreakLine(self))

    def keepFontSize(self):
        font_size = int(self._font_sizes.currentText())
        if self._font_families.currentFont().pointSize() != font_size:
            font = QFont()
            font.setPointSize(font_size)
            self._font_families.setCurrentFont(font)

    def connectNotepad(self, notedpad):
        self._font_families.currentFontChanged.connect(notedpad.setCurrentFont)
        self.bold_action.toggled.connect(lambda x: notedpad.setFontWeight(
            QFont.Bold if x else QFont.Normal))
        self.italic_action.toggled.connect(notedpad.setFontItalic)
        self.underline_action.toggled.connect(notedpad.setFontUnderline)

    def changeFontSize(self):
        font_format = QTextCharFormat()
        font_size = int(self._font_sizes.currentText())
        font_format.setFontPointSize(font_size)
        cursor = self._parent.activeNotepad().textCursor()
        cursor.mergeBlockCharFormat(font_format)

        self._parent.activeNotepad().setTextCursor(cursor)
        self._parent.activeNotepad().setFontPointSize(font_size)

    def changeFontColor(self):
        color_dialog = QColorDialog()
        color = color_dialog.getColor()
        hex_color = None
        if color.isValid():
            hex_color = color.name()
            self.font_color_button.setColor(hex_color)
            q_color = QColor(hex_color)
            self._parent.activeNotepad().setTextColor(q_color)

    def changeHighlighterColor(self):
        color_dialog = QColorDialog()
        color = color_dialog.getColor()
        hex_color = None
        if color.isValid():
            hex_color = color.name()
            self.highlighter_button.setColor(hex_color)
            q_color = QColor(hex_color)
            self._parent.activeNotepad().setTextBackgroundColor(q_color)
Example #3
0
class FontsTab(QWidget):
    def __init__(self, parent_dialog, parent):
        """Initialize the fonts tab
        """

        super(FontsTab, self).__init__(parent_dialog)

        self.prefs = parent_dialog.prefs
        self.parent = parent

        # Base application font code
        app = QApplication.instance()
        self.font_base = app.font()
        font_label_base = QLabel("Base Application Font:")
        self.font_combo_base = QFontComboBox()
        self.font_combo_base.setMaximumWidth(175)
        self.font_combo_base.setCurrentFont(self.font_base.family())
        self.font_combo_base.currentFontChanged.connect(self.update)
        self.font_size_combo_base = QComboBox()
        points_base = self.font_points(self.font_base)
        self.font_size_combo_base.addItems(points_base)
        self.font_size_combo_base.setMaximumWidth(50)
        self.font_size_combo_base.setCurrentIndex(self.font_size_combo_base.findText(str(self.font_base.pointSize())))
        self.font_size_combo_base.currentIndexChanged.connect(self.update)
        base_layout = QHBoxLayout()
        base_layout.addWidget(font_label_base)
        base_layout.addWidget(self.font_combo_base)
        base_layout.addWidget(self.font_size_combo_base)

        # Class Tree font code
        self.font_classTree = self.parent.classTree.font()
        font_label_classTree = QLabel("Class Tree Font:")
        self.font_combo_classTree = QFontComboBox()
        self.font_combo_classTree.setMaximumWidth(175)
        self.font_combo_classTree.setCurrentFont(self.font_classTree.family())
        self.font_combo_classTree.currentFontChanged.connect(self.update)
        self.font_size_combo_classTree = QComboBox()
        points_classTree = self.font_points(self.font_classTree)
        self.font_size_combo_classTree.addItems(points_classTree)
        self.font_size_combo_classTree.setMaximumWidth(50)
        self.font_size_combo_classTree.setCurrentIndex(self.font_size_combo_classTree.findText(str(self.font_classTree.pointSize())))
        self.font_size_combo_classTree.currentIndexChanged.connect(self.update)
        classTree_layout = QHBoxLayout()
        classTree_layout.addWidget(font_label_classTree)
        classTree_layout.addWidget(self.font_combo_classTree)
        classTree_layout.addWidget(self.font_size_combo_classTree)

        # Class Table font code
        self.font_classTable = self.parent.classTable.font()
        font_label_classTable = QLabel("Class Table Font:")
        self.font_combo_classTable = QFontComboBox()
        self.font_combo_classTable.setMaximumWidth(175)
        self.font_combo_classTable.setCurrentFont(self.font_classTable.family())
        self.font_combo_classTable.currentFontChanged.connect(self.update)
        self.font_size_combo_classTable = QComboBox()
        points_classTable = self.font_points(self.font_classTable)
        self.font_size_combo_classTable.addItems(points_classTable)
        self.font_size_combo_classTable.setMaximumWidth(50)
        self.font_size_combo_classTable.setCurrentIndex(self.font_size_combo_classTable.findText(str(self.font_classTable.pointSize())))
        self.font_size_combo_classTable.currentIndexChanged.connect(self.update)
        classTable_layout = QHBoxLayout()
        classTable_layout.addWidget(font_label_classTable)
        classTable_layout.addWidget(self.font_combo_classTable)
        classTable_layout.addWidget(self.font_size_combo_classTable)

        # Comments view font code
        self.font_comments = self.parent.commentView.font()
        font_label_comments = QLabel("Comments Font:")
        self.font_combo_comments = QFontComboBox()
        self.font_combo_comments.setMaximumWidth(175)
        self.font_combo_comments.setCurrentFont(self.font_comments.family())
        self.font_combo_comments.currentFontChanged.connect(self.update)
        self.font_size_combo_comments = QComboBox()
        points_comments = self.font_points(self.font_comments)
        self.font_size_combo_comments.addItems(points_comments)
        self.font_size_combo_comments.setMaximumWidth(50)
        self.font_size_combo_comments.setCurrentIndex(self.font_size_combo_comments.findText(str(self.font_comments.pointSize())))
        self.font_size_combo_comments.currentIndexChanged.connect(self.update)
        comments_layout = QHBoxLayout()
        comments_layout.addWidget(font_label_comments)
        comments_layout.addWidget(self.font_combo_comments)
        comments_layout.addWidget(self.font_size_combo_comments)

        # Main layout code
        mainLayout = QVBoxLayout()
        mainLayout.addLayout(base_layout)
        mainLayout.addSpacing(10)
        mainLayout.addLayout(classTree_layout)
        mainLayout.addSpacing(10)
        mainLayout.addLayout(classTable_layout)
        mainLayout.addSpacing(10)
        mainLayout.addLayout(comments_layout)
        mainLayout.addStretch(1)
        self.setLayout(mainLayout)

    def update(self):
        self.font_base.setFamily(self.font_combo_base.currentFont().family())
        self.font_base.setPointSize(int(self.font_size_combo_base.currentText()))
        self.font_classTree.setFamily(self.font_combo_classTree.currentFont().family())
        self.font_classTree.setPointSize(int(self.font_size_combo_classTree.currentText()))
        self.font_classTable.setFamily(self.font_combo_classTable.currentFont().family())
        self.font_classTable.setPointSize(int(self.font_size_combo_classTable.currentText()))
        self.font_comments.setFamily(self.font_combo_comments.currentFont().family())
        self.font_comments.setPointSize(int(self.font_size_combo_comments.currentText()))
        self.prefs['base_font'] = self.font_base.toString()
        self.prefs['class_tree_font'] = self.font_classTree.toString()
        self.prefs['class_table_font'] = self.font_classTable.toString()
        self.prefs['comments_font'] = self.font_comments.toString()
        # self.prefs['log_font'] = self.font_base.toString()
        # self.prefs['ref_font'] = self.font_base.toString()
        # self.prefs['info_font'] = self.font_base.toString()
        # self.prefs['undo_font'] = self.font_base.toString()

    def font_points(self, font):
        points = [str(p) for p in QFontDatabase().pointSizes(font.family())]
        if not points:
            points = [str(p) for p in QFontDatabase().standardSizes()]
        return points