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 setFontBox(self): vbox = QVBoxLayout() fontComboBox = QFontComboBox() fontComboBox.setFontFilters(QFontComboBox.MonospacedFonts) # Configure le type de fonts présentes dans la combo vbox.addWidget(fontComboBox) self.setLayout(vbox)
def toolbar_setup(self): """Create toolbar """ self.toolbar = QToolBar("test", self) self.addToolBar(self.toolbar) current_size = str(self.font().pointSize()) lst = [str(i) for i in range(6, 14)] lst.extend([str(i) for i in range(14, 40, 2)]) index = lst.index(current_size) self.fontsize_combo = QComboBox() self.fontsize_combo.addItems(lst) self.fontsize_combo.setCurrentIndex(index) self.fontsize_combo.currentTextChanged.connect(self.slot.set_fontsize) self.fontsize_label = QLabel("Font size") self.fontsize_label.setFrameShape(QFrame.Box) self.comb = QFontComboBox() self.toolbar.addWidget(self.save_button) self.toolbar.addWidget(self.stop_button) self.toolbar.addWidget(self.rec_button) self.toolbar.addWidget(self.close_button) self.toolbar.addWidget(self.theme_button) self.toolbar.addWidget(self.help_button) self.toolbar.addWidget(self.fontsize_label) self.toolbar.addWidget(self.fontsize_combo) self.toolbar.setStyleSheet( """ QToolBar {spacing:5px;} """ )
def addTextToolBar(self): """Create toolbar to house text actions""" tb = QToolBar() tb.setWindowTitle("Text Actions") self.fontCombo = fontCombo = QFontComboBox(tb) tb.addWidget(self.fontCombo) self.addToolBar(tb) font = self.font() fontCombo.setFont(font) fontCombo.setCurrentFont(font)
def testSetStyle(self): '''All this test have to do is not break with some invalid Python wrapper.''' def setStyleHelper(widget, style): widget.setStyle(style) widget.setPalette(style.standardPalette()) for child in widget.children(): if isinstance(child, QWidget): setStyleHelper(child, style) container = QWidget() # QFontComboBox is used because it has an QLineEdit created in C++ inside it, # and if the QWidget.setStyle(style) steals the ownership of the style # for the C++ originated widget everything will break. fontComboBox = QFontComboBox(container) label = QLabel(container) label.setText('Label') style = QStyleFactory.create(QStyleFactory.keys()[0]) setStyleHelper(container, style)
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))
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)
def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) layout = QVBoxLayout() self.editor = TextEdit() # Setup the QTextEdit editor configuration self.editor.setAutoFormatting(QTextEdit.AutoAll) self.editor.selectionChanged.connect(self.update_format) # Initialize default font size. font = QFont('Times', 12) self.editor.setFont(font) # We need to repeat the size to init the current format. self.editor.setFontPointSize(12) # self.path holds the path of the currently open file. # If none, we haven't got a file open yet (or creating new). self.path = None layout.addWidget(self.editor) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) self.status = QStatusBar() self.setStatusBar(self.status) # Uncomment to disable native menubar on Mac # self.menuBar().setNativeMenuBar(False) file_toolbar = QToolBar("File") file_toolbar.setIconSize(QSize(14, 14)) self.addToolBar(file_toolbar) file_menu = self.menuBar().addMenu("&File") open_file_action = QAction( QIcon(os.path.join('images', 'blue-folder-open-document.png')), "Open file...", self) open_file_action.setStatusTip("Open file") open_file_action.triggered.connect(self.file_open) file_menu.addAction(open_file_action) file_toolbar.addAction(open_file_action) save_file_action = QAction(QIcon(os.path.join('images', 'disk.png')), "Save", self) save_file_action.setStatusTip("Save current page") save_file_action.triggered.connect(self.file_save) file_menu.addAction(save_file_action) file_toolbar.addAction(save_file_action) saveas_file_action = QAction( QIcon(os.path.join('images', 'disk--pencil.png')), "Save As...", self) saveas_file_action.setStatusTip("Save current page to specified file") saveas_file_action.triggered.connect(self.file_saveas) file_menu.addAction(saveas_file_action) file_toolbar.addAction(saveas_file_action) print_action = QAction(QIcon(os.path.join('images', 'printer.png')), "Print...", self) print_action.setStatusTip("Print current page") print_action.triggered.connect(self.file_print) file_menu.addAction(print_action) file_toolbar.addAction(print_action) edit_toolbar = QToolBar("Edit") edit_toolbar.setIconSize(QSize(16, 16)) self.addToolBar(edit_toolbar) edit_menu = self.menuBar().addMenu("&Edit") undo_action = QAction( QIcon(os.path.join('images', 'arrow-curve-180-left.png')), "Undo", self) undo_action.setStatusTip("Undo last change") undo_action.triggered.connect(self.editor.undo) edit_menu.addAction(undo_action) redo_action = QAction(QIcon(os.path.join('images', 'arrow-curve.png')), "Redo", self) redo_action.setStatusTip("Redo last change") redo_action.triggered.connect(self.editor.redo) edit_toolbar.addAction(redo_action) edit_menu.addAction(redo_action) edit_menu.addSeparator() cut_action = QAction(QIcon(os.path.join('images', 'scissors.png')), "Cut", self) cut_action.setStatusTip("Cut selected text") cut_action.setShortcut(QKeySequence.Cut) cut_action.triggered.connect(self.editor.cut) edit_toolbar.addAction(cut_action) edit_menu.addAction(cut_action) copy_action = QAction( QIcon(os.path.join('images', 'document-copy.png')), "Copy", self) copy_action.setStatusTip("Copy selected text") cut_action.setShortcut(QKeySequence.Copy) copy_action.triggered.connect(self.editor.copy) edit_toolbar.addAction(copy_action) edit_menu.addAction(copy_action) paste_action = QAction( QIcon(os.path.join('images', 'clipboard-paste-document-text.png')), "Paste", self) paste_action.setStatusTip("Paste from clipboard") cut_action.setShortcut(QKeySequence.Paste) paste_action.triggered.connect(self.editor.paste) edit_toolbar.addAction(paste_action) edit_menu.addAction(paste_action) select_action = QAction( QIcon(os.path.join('images', 'selection-input.png')), "Select all", self) select_action.setStatusTip("Select all text") cut_action.setShortcut(QKeySequence.SelectAll) select_action.triggered.connect(self.editor.selectAll) edit_menu.addAction(select_action) edit_menu.addSeparator() wrap_action = QAction( QIcon(os.path.join('images', 'arrow-continue.png')), "Wrap text to window", self) wrap_action.setStatusTip("Toggle wrap text to window") wrap_action.setCheckable(True) wrap_action.setChecked(True) wrap_action.triggered.connect(self.edit_toggle_wrap) edit_menu.addAction(wrap_action) format_toolbar = QToolBar("Format") format_toolbar.setIconSize(QSize(16, 16)) self.addToolBar(format_toolbar) format_menu = self.menuBar().addMenu("&Format") # We need references to these actions/settings to update as selection changes, so attach to self. self.fonts = QFontComboBox() self.fonts.currentFontChanged.connect(self.editor.setCurrentFont) format_toolbar.addWidget(self.fonts) self.fontsize = QComboBox() self.fontsize.addItems([str(s) for s in FONT_SIZES]) # Connect to the signal producing the text of the current selection. Convert the string to float # and set as the pointsize. We could also use the index + retrieve from FONT_SIZES. self.fontsize.currentIndexChanged[str].connect( lambda s: self.editor.setFontPointSize(float(s))) format_toolbar.addWidget(self.fontsize) 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.bold_action.toggled.connect(lambda x: self.editor.setFontWeight( QFont.Bold if x else QFont.Normal)) format_toolbar.addAction(self.bold_action) format_menu.addAction(self.bold_action) 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.italic_action.toggled.connect(self.editor.setFontItalic) format_toolbar.addAction(self.italic_action) format_menu.addAction(self.italic_action) 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.underline_action.toggled.connect(self.editor.setFontUnderline) format_toolbar.addAction(self.underline_action) format_menu.addAction(self.underline_action) format_menu.addSeparator() self.alignl_action = QAction( QIcon(os.path.join('images', 'edit-alignment.png')), "Align left", self) self.alignl_action.setStatusTip("Align text left") self.alignl_action.setCheckable(True) self.alignl_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignLeft)) format_toolbar.addAction(self.alignl_action) format_menu.addAction(self.alignl_action) self.alignc_action = QAction( QIcon(os.path.join('images', 'edit-alignment-center.png')), "Align center", self) self.alignc_action.setStatusTip("Align text center") self.alignc_action.setCheckable(True) self.alignc_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignCenter)) format_toolbar.addAction(self.alignc_action) format_menu.addAction(self.alignc_action) self.alignr_action = QAction( QIcon(os.path.join('images', 'edit-alignment-right.png')), "Align right", self) self.alignr_action.setStatusTip("Align text right") self.alignr_action.setCheckable(True) self.alignr_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignRight)) format_toolbar.addAction(self.alignr_action) format_menu.addAction(self.alignr_action) self.alignj_action = QAction( QIcon(os.path.join('images', 'edit-alignment-justify.png')), "Justify", self) self.alignj_action.setStatusTip("Justify text") self.alignj_action.setCheckable(True) self.alignj_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignJustify)) format_toolbar.addAction(self.alignj_action) format_menu.addAction(self.alignj_action) format_group = QActionGroup(self) format_group.setExclusive(True) format_group.addAction(self.alignl_action) format_group.addAction(self.alignc_action) format_group.addAction(self.alignr_action) format_group.addAction(self.alignj_action) format_menu.addSeparator() # A list of all format-related widgets/actions, so we can disable/enable signals when updating. self._format_actions = [ self.fonts, self.fontsize, self.bold_action, self.italic_action, self.underline_action, # We don't need to disable signals for alignment, as they are paragraph-wide. ] # Initialize. self.update_format() self.update_title() self.show()
class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) layout = QVBoxLayout() self.editor = TextEdit() # Setup the QTextEdit editor configuration self.editor.setAutoFormatting(QTextEdit.AutoAll) self.editor.selectionChanged.connect(self.update_format) # Initialize default font size. font = QFont('Times', 12) self.editor.setFont(font) # We need to repeat the size to init the current format. self.editor.setFontPointSize(12) # self.path holds the path of the currently open file. # If none, we haven't got a file open yet (or creating new). self.path = None layout.addWidget(self.editor) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) self.status = QStatusBar() self.setStatusBar(self.status) # Uncomment to disable native menubar on Mac # self.menuBar().setNativeMenuBar(False) file_toolbar = QToolBar("File") file_toolbar.setIconSize(QSize(14, 14)) self.addToolBar(file_toolbar) file_menu = self.menuBar().addMenu("&File") open_file_action = QAction( QIcon(os.path.join('images', 'blue-folder-open-document.png')), "Open file...", self) open_file_action.setStatusTip("Open file") open_file_action.triggered.connect(self.file_open) file_menu.addAction(open_file_action) file_toolbar.addAction(open_file_action) save_file_action = QAction(QIcon(os.path.join('images', 'disk.png')), "Save", self) save_file_action.setStatusTip("Save current page") save_file_action.triggered.connect(self.file_save) file_menu.addAction(save_file_action) file_toolbar.addAction(save_file_action) saveas_file_action = QAction( QIcon(os.path.join('images', 'disk--pencil.png')), "Save As...", self) saveas_file_action.setStatusTip("Save current page to specified file") saveas_file_action.triggered.connect(self.file_saveas) file_menu.addAction(saveas_file_action) file_toolbar.addAction(saveas_file_action) print_action = QAction(QIcon(os.path.join('images', 'printer.png')), "Print...", self) print_action.setStatusTip("Print current page") print_action.triggered.connect(self.file_print) file_menu.addAction(print_action) file_toolbar.addAction(print_action) edit_toolbar = QToolBar("Edit") edit_toolbar.setIconSize(QSize(16, 16)) self.addToolBar(edit_toolbar) edit_menu = self.menuBar().addMenu("&Edit") undo_action = QAction( QIcon(os.path.join('images', 'arrow-curve-180-left.png')), "Undo", self) undo_action.setStatusTip("Undo last change") undo_action.triggered.connect(self.editor.undo) edit_menu.addAction(undo_action) redo_action = QAction(QIcon(os.path.join('images', 'arrow-curve.png')), "Redo", self) redo_action.setStatusTip("Redo last change") redo_action.triggered.connect(self.editor.redo) edit_toolbar.addAction(redo_action) edit_menu.addAction(redo_action) edit_menu.addSeparator() cut_action = QAction(QIcon(os.path.join('images', 'scissors.png')), "Cut", self) cut_action.setStatusTip("Cut selected text") cut_action.setShortcut(QKeySequence.Cut) cut_action.triggered.connect(self.editor.cut) edit_toolbar.addAction(cut_action) edit_menu.addAction(cut_action) copy_action = QAction( QIcon(os.path.join('images', 'document-copy.png')), "Copy", self) copy_action.setStatusTip("Copy selected text") cut_action.setShortcut(QKeySequence.Copy) copy_action.triggered.connect(self.editor.copy) edit_toolbar.addAction(copy_action) edit_menu.addAction(copy_action) paste_action = QAction( QIcon(os.path.join('images', 'clipboard-paste-document-text.png')), "Paste", self) paste_action.setStatusTip("Paste from clipboard") cut_action.setShortcut(QKeySequence.Paste) paste_action.triggered.connect(self.editor.paste) edit_toolbar.addAction(paste_action) edit_menu.addAction(paste_action) select_action = QAction( QIcon(os.path.join('images', 'selection-input.png')), "Select all", self) select_action.setStatusTip("Select all text") cut_action.setShortcut(QKeySequence.SelectAll) select_action.triggered.connect(self.editor.selectAll) edit_menu.addAction(select_action) edit_menu.addSeparator() wrap_action = QAction( QIcon(os.path.join('images', 'arrow-continue.png')), "Wrap text to window", self) wrap_action.setStatusTip("Toggle wrap text to window") wrap_action.setCheckable(True) wrap_action.setChecked(True) wrap_action.triggered.connect(self.edit_toggle_wrap) edit_menu.addAction(wrap_action) format_toolbar = QToolBar("Format") format_toolbar.setIconSize(QSize(16, 16)) self.addToolBar(format_toolbar) format_menu = self.menuBar().addMenu("&Format") # We need references to these actions/settings to update as selection changes, so attach to self. self.fonts = QFontComboBox() self.fonts.currentFontChanged.connect(self.editor.setCurrentFont) format_toolbar.addWidget(self.fonts) self.fontsize = QComboBox() self.fontsize.addItems([str(s) for s in FONT_SIZES]) # Connect to the signal producing the text of the current selection. Convert the string to float # and set as the pointsize. We could also use the index + retrieve from FONT_SIZES. self.fontsize.currentIndexChanged[str].connect( lambda s: self.editor.setFontPointSize(float(s))) format_toolbar.addWidget(self.fontsize) 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.bold_action.toggled.connect(lambda x: self.editor.setFontWeight( QFont.Bold if x else QFont.Normal)) format_toolbar.addAction(self.bold_action) format_menu.addAction(self.bold_action) 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.italic_action.toggled.connect(self.editor.setFontItalic) format_toolbar.addAction(self.italic_action) format_menu.addAction(self.italic_action) 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.underline_action.toggled.connect(self.editor.setFontUnderline) format_toolbar.addAction(self.underline_action) format_menu.addAction(self.underline_action) format_menu.addSeparator() self.alignl_action = QAction( QIcon(os.path.join('images', 'edit-alignment.png')), "Align left", self) self.alignl_action.setStatusTip("Align text left") self.alignl_action.setCheckable(True) self.alignl_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignLeft)) format_toolbar.addAction(self.alignl_action) format_menu.addAction(self.alignl_action) self.alignc_action = QAction( QIcon(os.path.join('images', 'edit-alignment-center.png')), "Align center", self) self.alignc_action.setStatusTip("Align text center") self.alignc_action.setCheckable(True) self.alignc_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignCenter)) format_toolbar.addAction(self.alignc_action) format_menu.addAction(self.alignc_action) self.alignr_action = QAction( QIcon(os.path.join('images', 'edit-alignment-right.png')), "Align right", self) self.alignr_action.setStatusTip("Align text right") self.alignr_action.setCheckable(True) self.alignr_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignRight)) format_toolbar.addAction(self.alignr_action) format_menu.addAction(self.alignr_action) self.alignj_action = QAction( QIcon(os.path.join('images', 'edit-alignment-justify.png')), "Justify", self) self.alignj_action.setStatusTip("Justify text") self.alignj_action.setCheckable(True) self.alignj_action.triggered.connect( lambda: self.editor.setAlignment(Qt.AlignJustify)) format_toolbar.addAction(self.alignj_action) format_menu.addAction(self.alignj_action) format_group = QActionGroup(self) format_group.setExclusive(True) format_group.addAction(self.alignl_action) format_group.addAction(self.alignc_action) format_group.addAction(self.alignr_action) format_group.addAction(self.alignj_action) format_menu.addSeparator() # A list of all format-related widgets/actions, so we can disable/enable signals when updating. self._format_actions = [ self.fonts, self.fontsize, self.bold_action, self.italic_action, self.underline_action, # We don't need to disable signals for alignment, as they are paragraph-wide. ] # Initialize. self.update_format() self.update_title() self.show() 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.fonts.setCurrentFont(self.editor.currentFont()) # Nasty, but we get the font-size as a float but want it was an int self.fontsize.setCurrentText(str(int(self.editor.fontPointSize()))) self.italic_action.setChecked(self.editor.fontItalic()) self.underline_action.setChecked(self.editor.fontUnderline()) self.bold_action.setChecked(self.editor.fontWeight() == QFont.Bold) self.alignl_action.setChecked(self.editor.alignment() == Qt.AlignLeft) self.alignc_action.setChecked( self.editor.alignment() == Qt.AlignCenter) self.alignr_action.setChecked(self.editor.alignment() == Qt.AlignRight) self.alignj_action.setChecked( self.editor.alignment() == Qt.AlignJustify) self.block_signals(self._format_actions, False) def dialog_critical(self, s): dlg = QMessageBox(self) dlg.setText(s) dlg.setIcon(QMessageBox.Critical) dlg.show() def file_open(self): path, _ = QFileDialog.getOpenFileName( self, "Open file", "", "HTML documents (*.html);Text documents (*.txt);All files (*.*)") try: with open(path, 'rU') as f: text = f.read() except Exception as e: self.dialog_critical(str(e)) else: self.path = path # Qt will automatically try and guess the format as txt/html self.editor.setText(text) self.update_title() def file_save(self): if self.path is None: # If we do not have a path, we need to use Save As. return self.file_saveas() text = self.editor.toHtml() if splitext( self.path) in HTML_EXTENSIONS else self.editor.toPlainText() try: with open(self.path, 'w') as f: f.write(text) except Exception as e: self.dialog_critical(str(e)) def file_saveas(self): path, _ = QFileDialog.getSaveFileName( self, "Save file", "", "HTML documents (*.html);Text documents (*.txt);All files (*.*)") if not path: # If dialog is cancelled, will return '' return text = self.editor.toHtml() if splitext( path) in HTML_EXTENSIONS else self.editor.toPlainText() try: with open(path, 'w') as f: f.write(text) except Exception as e: self.dialog_critical(str(e)) else: self.path = path self.update_title() def file_print(self): dlg = QPrintDialog() if dlg.exec_(): self.editor.print_(dlg.printer()) def update_title(self): self.setWindowTitle( "%s - Megasolid Idiom" % (os.path.basename(self.path) if self.path else "Untitled")) def edit_toggle_wrap(self): self.editor.setLineWrapMode(1 if self.editor.lineWrapMode() == 0 else 0)
class subTitleEdit(QWidget): def __init__(self): super(subTitleEdit, self).__init__() self.rx = QRegExp("(^(?:(:?[0-1][0-9]|[0-2][0-3]):)(?:[0-5][0-9]:){2}(?:[0-2][0-9])$)") self.language_choice = 1 # 1 English, 2 Tamil, 3 Hindi self.initUI() def initUI(self): # Master Layout mainlayout = QVBoxLayout() mainlayout.setContentsMargins(0, 0, 0, 0) mainlayout.setMargin(3) mainlayout.setSpacing(0) mainlayout.setStretch(0, 0) # Top Layout (In Out Duration) subLayout = QHBoxLayout() subLayout.setContentsMargins(0, 0, 0, 0) subLayout.setMargin(2) subLayout.setSpacing(5) # Top Layout Controls self.no = QSpinBox() self.no.setMinimum(1) self.no.setMaximum(9999) # < Memory Concerns Lower self.tcIn = QLineEdit() self.tcOut = QLineEdit() self.tcDur = QLineEdit() index_lbl = QLabel("No:") intc_lbl = QLabel("In:") outtc_lbl = QLabel("Out:") durtc_lbl = QLabel("Duration:") # Add to Layout subLayout.addWidget(index_lbl) subLayout.addWidget(self.no) subLayout.addWidget(intc_lbl) subLayout.addWidget(self.tcIn) subLayout.addWidget(outtc_lbl) subLayout.addWidget(self.tcOut) subLayout.addWidget(durtc_lbl) subLayout.addWidget(self.tcDur) # Add to Main mainlayout.addLayout(subLayout) # Add Next Line Controls second_line_layout = QHBoxLayout() font = QFont("Helvetica", 13) self.font_family = QFontComboBox() self.font_family.setMaximumWidth(180) self.font_family.setCurrentFont(font) second_line_layout.addWidget(self.font_family) self.font_family.currentTextChanged.connect(self.set_font) self.font_size = QComboBox() self.font_size.addItems(["9", "10", "11", "12", "13", "14", "18", "24", "36", "48", "64"]) self.font_size.setCurrentText("13") self.font_size.setMaximumWidth(80) self.font_size.currentIndexChanged.connect(self.set_font) second_line_layout.addWidget(self.font_size) bold = QPushButton("B") bold.clicked.connect(self.bold) italic = QPushButton("I") italic.clicked.connect(self.italic) underline = QPushButton("U") underline.clicked.connect(self.underline) second_line_layout.addWidget(bold) second_line_layout.addWidget(italic) second_line_layout.addWidget(underline) # ------------------------------------------ language = QComboBox() language.addItems(["English", "Tamil", "Hindi"]) language.currentIndexChanged.connect(self.setup_language) language.setDisabled(True) # Disabled spellCheck = QPushButton("Spell Check") spellCheck.setDisabled(True) # Disabled autocomplete = QCheckBox("AutoCmp") autocomplete.toggled.connect(self.setup_autocomplete) second_line_layout.addWidget(language) second_line_layout.addWidget(spellCheck) second_line_layout.addWidget(autocomplete) second_line_layout.addStretch(1) mainlayout.addLayout(second_line_layout) # txtSubTitle = QTextEdit() self.subtitle = CompletionTextEdit() # print(self.subtitle.font()) # completer = DictionaryCompleter() # self.subtitle.setCompleter(completer) # self.subtitle.setMaximumHeight(100) mainlayout.addWidget(self.subtitle) # Setup TimeCode LineEdit Controls self.setup_linedt_tc(self.tcIn) self.setup_linedt_tc(self.tcOut) self.setup_linedt_tc(self.tcDur) self.tcOut.editingFinished.connect(self.calculate_duration) # Layout Operations self.setLayout(mainlayout) self.setTabOrder(self.tcDur, self.subtitle) self.show() def setup_linedt_tc(self, ctrl): ctrl.setDragEnabled(True) ctrl.setInputMask("99:99:99:99") ctrl.setText("00000000") ctrl.setCursorPosition(0) ctrl.setValidator(QRegExpValidator(self.rx)) def calculate_duration(self): tcIn = TimeCode(self.tcIn.text()) tcOut = TimeCode(self.tcOut.text()) if tcOut.frames > tcIn.frames: Dur = tcOut - tcIn self.tcDur.setText(Dur.timecode) @Slot() def set_font(self): self.subtitle.setCurrentFont(QFont(self.font_family.currentText(), int(self.font_size.currentText()))) @Slot() def bold(self): if self.subtitle.fontWeight() == QFont.Bold: self.subtitle.setFontWeight(QFont.Normal) else: self.subtitle.setFontWeight(QFont.Bold) @Slot() def italic(self): state = self.subtitle.fontItalic() self.subtitle.setFontItalic(not state) @Slot() def underline(self): state = self.subtitle.fontUnderline() self.subtitle.setFontUnderline(not state) def clearStyles(self): bold = self.subtitle.fontWeight() == QFont.Bold italic = self.subtitle.fontItalic() underline = self.subtitle.fontUnderline() if bold: self.subtitle.setFontWeight(QFont.Normal) if italic: self.subtitle.setFontItalic(False) if underline: self.subtitle.setFontUnderline(False) @Slot() def setup_language(self, event): event += 1 self.language_choice = event if self.language_choice == 1: # Tamil self.subtitle.setText("You have chosen English") elif self.language_choice == 2: # Tamil self.subtitle.setText("You have chosen Tamil") elif self.language_choice == 3: # Hindi self.subtitle.setText("You have chosen Hindi") @Slot() def setup_autocomplete(self, event): if self.language_choice == 1 and event == True: completer = en_autocomplete() self.subtitle.setCompleter(completer) elif self.language_choice == 1 and event == False: self.subtitle.removeCompleter()
def initUI(self): # Master Layout mainlayout = QVBoxLayout() mainlayout.setContentsMargins(0, 0, 0, 0) mainlayout.setMargin(3) mainlayout.setSpacing(0) mainlayout.setStretch(0, 0) # Top Layout (In Out Duration) subLayout = QHBoxLayout() subLayout.setContentsMargins(0, 0, 0, 0) subLayout.setMargin(2) subLayout.setSpacing(5) # Top Layout Controls self.no = QSpinBox() self.no.setMinimum(1) self.no.setMaximum(9999) # < Memory Concerns Lower self.tcIn = QLineEdit() self.tcOut = QLineEdit() self.tcDur = QLineEdit() index_lbl = QLabel("No:") intc_lbl = QLabel("In:") outtc_lbl = QLabel("Out:") durtc_lbl = QLabel("Duration:") # Add to Layout subLayout.addWidget(index_lbl) subLayout.addWidget(self.no) subLayout.addWidget(intc_lbl) subLayout.addWidget(self.tcIn) subLayout.addWidget(outtc_lbl) subLayout.addWidget(self.tcOut) subLayout.addWidget(durtc_lbl) subLayout.addWidget(self.tcDur) # Add to Main mainlayout.addLayout(subLayout) # Add Next Line Controls second_line_layout = QHBoxLayout() font = QFont("Helvetica", 13) self.font_family = QFontComboBox() self.font_family.setMaximumWidth(180) self.font_family.setCurrentFont(font) second_line_layout.addWidget(self.font_family) self.font_family.currentTextChanged.connect(self.set_font) self.font_size = QComboBox() self.font_size.addItems(["9", "10", "11", "12", "13", "14", "18", "24", "36", "48", "64"]) self.font_size.setCurrentText("13") self.font_size.setMaximumWidth(80) self.font_size.currentIndexChanged.connect(self.set_font) second_line_layout.addWidget(self.font_size) bold = QPushButton("B") bold.clicked.connect(self.bold) italic = QPushButton("I") italic.clicked.connect(self.italic) underline = QPushButton("U") underline.clicked.connect(self.underline) second_line_layout.addWidget(bold) second_line_layout.addWidget(italic) second_line_layout.addWidget(underline) # ------------------------------------------ language = QComboBox() language.addItems(["English", "Tamil", "Hindi"]) language.currentIndexChanged.connect(self.setup_language) language.setDisabled(True) # Disabled spellCheck = QPushButton("Spell Check") spellCheck.setDisabled(True) # Disabled autocomplete = QCheckBox("AutoCmp") autocomplete.toggled.connect(self.setup_autocomplete) second_line_layout.addWidget(language) second_line_layout.addWidget(spellCheck) second_line_layout.addWidget(autocomplete) second_line_layout.addStretch(1) mainlayout.addLayout(second_line_layout) # txtSubTitle = QTextEdit() self.subtitle = CompletionTextEdit() # print(self.subtitle.font()) # completer = DictionaryCompleter() # self.subtitle.setCompleter(completer) # self.subtitle.setMaximumHeight(100) mainlayout.addWidget(self.subtitle) # Setup TimeCode LineEdit Controls self.setup_linedt_tc(self.tcIn) self.setup_linedt_tc(self.tcOut) self.setup_linedt_tc(self.tcDur) self.tcOut.editingFinished.connect(self.calculate_duration) # Layout Operations self.setLayout(mainlayout) self.setTabOrder(self.tcDur, self.subtitle) self.show()
def setFontComboBox(self): vbox = QVBoxLayout() self.fontComboBox = QFontComboBox() vbox.addWidget(self.fontComboBox) self.setLayout(vbox)
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)
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