def _aboutToShowTabsMenu(self): self._menuTabs.clear() for idx in range(self.count()): tab = self._weTab(idx) if not tab or tab.isPinned(): continue action = QAction(self) action.setIcon(tab.icon()) if idx == self.currentIndex(): f = action.font() f.setBold(True) action.setFont(f) title = tab.title() title.replace('&', '&&') action.setText(gVar.appTools.truncatedText(title, 40)) # TODO: QVariant::fromValue(qobject_cast<QWidget*>(tab) action.setData(tab) action.triggered.connect(self._actionChangeIndex) self._menuTabs.addAction(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()