def set_enable_vertical_align(self, value): if value: for i in range(1, 4): for j in range(1, 5): k = (i - 1) * 4 + j - 1 self._clrbtn[k].setIcon(QIcon(img(f"editor/a{i}{j}.png"))) self._clrbtn[k].setVisible(True) else: for i in range(4): self._clrbtn[i].setIcon(QIcon(img(f"editor/a0{i + 1}.png"))) for i in range(4, 12): self._clrbtn[i].setVisible(False)
def __init__(self, text=None, color_caption="#F0F0FF", icon="", config=None): """ text - text in caption and tooltip icon - filename for icon of button """ QToolButton.__init__(self, None) self.setAutoRaise(True) self._cfg = config self._text = self.tr("Table") if text is None else text self._color_caption = color_caption # ------------------------------------------------------ self._colors_widget = InsertTableWidget(color_caption=color_caption) self._colors_widget.select_size.connect(self.select_size_) self._colors_widget.hide_form.connect(self.hide_form) self._colors_widget.other_size.connect(self.other_size) self._action = QAction(self._text, None) self._action.setIcon(QIcon(img(icon))) self._action.triggered.connect(self.show_form) self.setDefaultAction(self._action)
def __init__(self, color_caption): TestableWidget.__init__(self) self.setWindowFlags(Qt.Popup) self._text = self.tr("Alignment text") main_layout = QVBoxLayout() main_layout.setContentsMargins(0, 0, 0, 0) b = "border: 1px solid #9b9b9b;" self._styles = { "border": b, "border_blue": "border: 0px solid blue;", "frame": 'QFrame[frameShape="4"]{color: #9b9b9b;}' } self.setStyleSheet(self._styles["frame"]) # -- CAPTION ---------------------------------- caption = QFrame(self, flags=Qt.WindowFlags()) caption_layout = QHBoxLayout() self._lbl_caption = QLabel(self._text) self._lbl_caption.setStyleSheet(self._styles["border_blue"]) caption_layout.addWidget(self._lbl_caption, alignment=Qt.Alignment()) caption.setLayout(caption_layout) caption_layout.setContentsMargins(9, 5, 9, 5) caption.setStyleSheet( f"background-color: {color_caption}; {self._styles['border']}") # -- CELLS GRID ------------------------------- cellsbox = QFrame(self, flags=Qt.WindowFlags()) cells = QGridLayout() cellsbox.setLayout(cells) cells.setContentsMargins(9, 0, 9, 9) self._clrbtn = [] for i in range(1, 4): for j in range(1, 5): self._clrbtn.append(QToolButton()) self._clrbtn[-1].clicked.connect( lambda z, y=i, x=j: self.select_align_(x, y)) self._clrbtn[-1].setAutoRaise(True) sz = 48 self._clrbtn[-1].setFixedSize(sz, sz) self._clrbtn[-1].setIconSize(QSize(sz, sz)) self._clrbtn[-1].setIcon(QIcon(img(f"editor/a{i}{j}.png"))) # noinspection PyArgumentList cells.addWidget(self._clrbtn[-1], i - 1, j - 1) # --------------------------------------------- main_layout.addWidget(caption, alignment=Qt.Alignment()) main_layout.addWidget(cellsbox, alignment=Qt.Alignment()) self.setLayout(main_layout)
def __init__(self, test_mode=False): super(MainWindow, self).__init__(None) self.setWindowIcon(QIcon(img('editor/editor.png'))) self.cfg = Config() self._tabs = QTabWidget() self._actions: Dict[str, QAction] = {} self._menus: Dict[str, QMenu] = {} self._make_actions() self._spell = SpellChecker(enabled=not test_mode) self._init_ui()
def __init__(self, text=None, color_caption="#F0F0FF", icon="", config=None): """ text - text in caption and tooltip icon - filename for icon of button list_colors - list colors for select """ QToolButton.__init__(self, None) self.setAutoRaise(True) self._text = self.tr("Color") if text is None else text self._color_caption = color_caption # --- list of colors ----------------------------------- lst = [] if config: lst = [ c for c in config.get("TextEditor/Colors", "").split(";") if c ] if not lst: lst = [ "#99cc00", "#e2fbfe", "#fee5e2", "#fa8072", "#f5f7a8", "#fef65b", "#ff9a00", "#ff00f4", "#f6f900", "#914285", "#c0d6e4", "#f5f5dc", "#3d40a2", "#acd2cd", "#ff9966", "#a4c73c", "#ff7373", "#50d4ee", "#8d5959", "#104022", "#000000", "#160042", "#6e57d2", "#4c9828", "#444193", "#0000ff", "#ff0000", "white", "#AA0000", "#00AA00", "#0040C2", "#550000", "#004100", "#BF4040" ] if config: config["TextEditor/Colors"] = ";".join(lst) lst.append(color_caption) # the list must contain a background color self._colors = lst # ------------------------------------------------------ self._colors_widget = ColorsWidget(text=self._text, colors=self._colors, color_caption=color_caption) self._colors_widget.select_color.connect(self.select_color_) self._colors_widget.hide_form.connect(self.hide_color_form) self._colors_widget.other_color.connect(self.other_color) self._action = QAction(self._text, None) self._action.setIcon(QIcon(img(icon))) self._action.triggered.connect(self.show_form_colors) self.setDefaultAction(self._action)
def _make_actions(self): self._actions: Dict[str, QAction] = {} for name, title, triggered in self._actions_data(): self._actions[name] = QAction(title, None) self._actions[name].setIcon(QIcon(img("editor/" + name))) if triggered: self._actions[name].triggered.connect(triggered) self._actions["bold"].setCheckable(True) self._actions["invisible-symbol"].setCheckable(True) self._actions["save"].setEnabled(False) self._retranslate_ui()
def test_img(self): path = os.sep.join(os.path.dirname(__file__).split(os.sep)[:-1]) resource_dir = os.path.join(path, "resources") image_dir = os.path.join(resource_dir, "images") if not os.path.exists(image_dir): os.makedirs(image_dir) img_path = os.path.join(image_dir, "tmp.png") img_path = img_path.replace('\\', '/') if not os.path.exists(img_path): with open(img_path, "w") as f: f.write(" ") self.assertEqual(img_path, img("tmp")) self.assertEqual("", img("")) self.assertEqual("", img("tmp11.png")) self.assertEqual("", img("tmp11.png")) get_app_dir.cache_clear() img.cache_clear() rmtree(resource_dir)
def __init__(self, text=None, color_caption="#F0F0FF", icon=""): """ text - text in caption and tooltip icon - filename for icon of button """ QToolButton.__init__(self, None) self.setAutoRaise(True) self._text = self.tr("Alignment") if text is None else text self._color_caption = color_caption # ------------------------------------------------------ self._colors_widget = AlignTextWidget(color_caption=color_caption) self._colors_widget.select_align.connect(self.select_align_) self._colors_widget.hide_form.connect(self.hide_form) self._action = QAction(self._text, None) self._action.setIcon(QIcon(img(icon))) self._action.triggered.connect(self.show_form) self.setDefaultAction(self._action)
def _context_menu(self, pos): text = self._view.text if not text.textCursor().hasSelection(): # move to mouse position text.setTextCursor(text.cursorForPosition(pos)) image, width, height, fmt = None, -1, -1, "" if self._doc.in_image(): # select image text.setTextCursor(text.cursorForPosition(pos)) cursor = text.textCursor() p = cursor.position() cursor.setPosition(p) cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) text.setTextCursor(cursor) if "<img" not in cursor.selection().toHtml(): cursor.setPosition(p) cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor) text.setTextCursor(cursor) image, width, height, fmt = ( self._doc.get_image(cursor.selection().toHtml())) menu = text.createStandardContextMenu() # --------------------------------------------------------------------- # menu for change size of image if image: menu.insertSeparator(menu.actions()[0]) param = (image, width, height, fmt) change_size = QAction(self.tr("Change size of image"), None) change_size.triggered.connect( lambda y, x=param: self._change_image_size(x)) menu.insertAction(menu.actions()[0], change_size) # --------------------------------------------------------------------- # menu for spell checker if self._spell.enabled() and text.toPlainText(): old_cur = text.textCursor() cursor = text.cursorForPosition(pos) curchar = text.document().characterAt(cursor.position()) isalpha = curchar.isalpha() cursor.select(QTextCursor.WordUnderCursor) word = "" if isalpha: text.setTextCursor(cursor) word = cursor.selectedText() text.setTextCursor(old_cur) if word and not self._spell.check_word(word): to_dict_action = QAction( self.tr("Add to the dictionary"), None) to_dict_action.triggered.connect( lambda y, x=word: self._add_word_to_spell(x)) font = to_dict_action.font() repl_actions = [] to_repl = self._spell.candidates(word) if to_repl: self._cursor_to_change_word = cursor font.setBold(True) enabled = not self._view.text.isReadOnly() for wrd in to_repl: repl_actions.append(QAction(wrd, None)) repl_actions[-1].setFont(font) repl_actions[-1].setEnabled(enabled) repl_actions[-1].triggered.connect( lambda y, x=wrd: self._fix_word(x)) else: repl_actions.append(QWidgetAction(self)) label = QLabel(self.tr("There are no words to replace")) label.setContentsMargins(28, 6, 6, 6) label.setStyleSheet("color: red") repl_actions[0].setDefaultWidget(label) repl_actions[0].setEnabled(False) menu.insertSeparator(menu.actions()[0]) menu.insertAction(menu.actions()[0], to_dict_action) menu.insertSeparator(menu.actions()[0]) for a in reversed(repl_actions): menu.insertAction(menu.actions()[0], a) # --------------------------------------------------------------------- # solving the problem of missing icons in Windows name_actions = { self.tr("Undo"): "Undo", self.tr("Redo"): "Redo", self.tr("Cut"): "Cut", self.tr("Copy"): "Copy", self.tr("Paste"): "Paste", self.tr("Delete"): "Delete", } for a in menu.actions(): txt = a.text().replace("&", "") if txt in name_actions: a.setIcon(QIcon(img(f"edit-{name_actions[txt].lower()}"))) # --------------------------------------------------------------------- # menu for table if self._doc.in_table() and not self._view.text.isReadOnly(): menu.addSeparator() t_menu = menu.addMenu(self.tr("Table")) t_menu.addAction(self._actions["add-row"]) t_menu.addAction(self._actions["add-col"]) t_menu.addSeparator() t_menu.addAction(self._actions["del-row"]) t_menu.addAction(self._actions["del-col"]) t_menu.addSeparator() t_menu.addAction(self._actions["ins-row"]) t_menu.addAction(self._actions["ins-col"]) t_menu.addSeparator() t_menu.addAction(self._actions["merge-cells"]) t_menu.addAction(self._actions["split-cells"]) cursor = text.textCursor() self._actions["merge-cells"].setEnabled(cursor.hasSelection()) cell = cursor.currentTable().cellAt(cursor) self._actions["split-cells"].setEnabled( cell.rowSpan() > 1 or cell.columnSpan() > 1) menu.addSeparator() menu.addAction(self._actions["toPDF"]) menu.addAction(self._actions["print"]) menu.exec_(QCursor().pos()) self._restore_cursor()
def __init__(self, text, colors, color_caption): TestableWidget.__init__(self) self.setWindowFlags(Qt.Popup) main_layout = QVBoxLayout() main_layout.setContentsMargins(0, 0, 0, 0) border = "border: 1px solid #9b9b9b;" self.setStyleSheet('QFrame[frameShape="4"]{ color: #9b9b9b;}') # -- CAPTION ---------------------------------- caption = QFrame(self, flags=Qt.WindowFlags()) caption_layout = QHBoxLayout() self._lbl_caption = QLabel(text) self._lbl_caption.setStyleSheet("border: 0px solid blue;") caption_layout.addWidget(self._lbl_caption, alignment=Qt.Alignment()) caption.setLayout(caption_layout) caption_layout.setContentsMargins(9, 5, 9, 5) caption.setStyleSheet(f"background-color: {color_caption}; {border}") # -- COLORS GRID ------------------------------ colorbox = QFrame(self, flags=Qt.WindowFlags()) color_layout = QGridLayout() colorbox.setLayout(color_layout) color_layout.setContentsMargins(9, 0, 9, 0) nn = 7 self.clrbtn = [] for i, color in enumerate(colors): self.clrbtn.append(QToolButton()) css = (f"QToolButton {{{border}background-color:{color};}}" f"QToolButton:hover {{border: 1px solid red; }}") self.clrbtn[-1].setStyleSheet(css) self.clrbtn[-1].clicked.connect( lambda x, c=color: self.select_color_(c)) # noinspection PyArgumentList color_layout.addWidget(self.clrbtn[-1], i // nn, i % nn) # -- SPLITTER --------------------------------- h_frame = QFrame(None, flags=Qt.WindowFlags()) h_frame.setFrameShape(QFrame.HLine) h_frame.setContentsMargins(0, 0, 0, 0) # -- BOTTOM (other color) --------------------- btns = QFrame(self, flags=Qt.WindowFlags()) btn_layout = QHBoxLayout() other = QToolButton() other.clicked.connect(self.other_color_) other.setAutoRaise(True) other.setIcon(QIcon(img("editor/colorwheel"))) btn_layout.addWidget(other, alignment=Qt.Alignment()) self._lbl_other = QLabel(self.tr("other colors")) btn_layout.addWidget(self._lbl_other, alignment=Qt.Alignment()) btns.setLayout(btn_layout) btn_layout.setContentsMargins(9, 0, 9, 9) self.clrbtn.append(other) # --------------------------------------------- main_layout.addWidget(caption, alignment=Qt.Alignment()) main_layout.addWidget(colorbox, alignment=Qt.Alignment()) main_layout.addWidget(h_frame, alignment=Qt.Alignment()) main_layout.addWidget(btns, alignment=Qt.Alignment()) self.setLayout(main_layout)
def __init__(self, color_caption): TestableWidget.__init__(self) self.setWindowFlags(Qt.Popup) self._text = self.tr("Table") main_layout = QVBoxLayout() main_layout.setContentsMargins(0, 0, 0, 0) b = "border: 1px solid #9b9b9b;" self._styles = { "border": b, "border_blue": "border: 0px solid blue;", "selection": f"QToolButton {{{b}background-color:#fee5e2;}}", "white": f"QToolButton {{{b}background-color:white;}}", "frame": 'QFrame[frameShape="4"]{color: #9b9b9b;}'} self.setStyleSheet(self._styles["frame"]) # -- CAPTION ---------------------------------- caption = QFrame(self, flags=Qt.WindowFlags()) caption_layout = QHBoxLayout() self._lbl_caption = QLabel(self._text) self._lbl_caption.setStyleSheet(self._styles["border_blue"]) caption_layout.addWidget(self._lbl_caption, alignment=Qt.Alignment()) caption.setLayout(caption_layout) caption_layout.setContentsMargins(9, 5, 9, 5) caption.setStyleSheet( f"background-color: {color_caption}; {self._styles['border']}") # -- CELLS GRID ------------------------------- cellsbox = QFrame(self, flags=Qt.WindowFlags()) cells = QGridLayout() cells.setSpacing(1) cellsbox.setLayout(cells) cells.setContentsMargins(9, 0, 9, 0) self._nn = 10 self._clrbtn = [] colors = ["white" for _ in range(self._nn ** 2)] cellsbox.leaveEvent = lambda x: self._leave_cell() for i, color in enumerate(colors): self._clrbtn.append(QToolButton()) # noinspection PyPep8Naming self._clrbtn[-1].enterEvent = lambda x, n=i: self._enter_cell(n) self._clrbtn[-1].setStyleSheet(self._styles["white"]) self._clrbtn[-1].clicked.connect( lambda x, n=i: self.select_size_(n)) # noinspection PyArgumentList cells.addWidget(self._clrbtn[-1], i // self._nn, i % self._nn) # -- SPLITTER --------------------------------- h_frame = QFrame(None, flags=Qt.WindowFlags()) h_frame.setFrameShape(QFrame.HLine) h_frame.setContentsMargins(0, 0, 0, 0) # -- BOTTOM (other color) --------------------- btns = QFrame(self, flags=Qt.WindowFlags()) btn_layout = QHBoxLayout() other = QToolButton() other.clicked.connect(self.other_size_) other.setAutoRaise(True) other.setIcon(QIcon(img("editor/table_gray"))) btn_layout.addWidget(other, alignment=Qt.Alignment()) self._lbl_other = QLabel(self.tr("insert table")) btn_layout.addWidget(self._lbl_other, alignment=Qt.Alignment()) btns.setLayout(btn_layout) btn_layout.setContentsMargins(9, 0, 9, 9) self._clrbtn.append(other) # --------------------------------------------- main_layout.addWidget(caption, alignment=Qt.Alignment()) main_layout.addWidget(cellsbox, alignment=Qt.Alignment()) main_layout.addWidget(h_frame, alignment=Qt.Alignment()) main_layout.addWidget(btns, alignment=Qt.Alignment()) self.setLayout(main_layout)
def _make_actions(self): for name, title, triggered in self._actions_data(): self._actions[name] = QAction(title, None) self._actions[name].setIcon(QIcon(img("editor/" + name))) if triggered: self._actions[name].triggered.connect(triggered)