def openFile(self): if settings.get("file_dialog_dir"): self.curDir = '~/' else: self.curDir = settings.get("file_dialog_dir") fn = QFileDialog.getOpenFileName(self, self.tr("Open File..."), self.curDir, self.tr("HTML-Files (*.htm *.html);;All Files (*)")) QApplication.setOverrideCursor(Qt.WaitCursor) if fn: self.lastFolder = os.path.dirname(fn) if os.path.exists(fn): if os.path.isfile(fn): f = QFile(fn) if not f.open(QIODevice.ReadOnly | QIODevice.Text): QtGui.QMessageBox.information(self.parent(), self.tr("Error - Lector"), self.tr("Can't open '%s.'" % fn)) else: stream = QTextStream(f) text = stream.readAll() self.setText(text) else: QMessageBox.information(self.parent(), self.tr("Error - Lector"), self.tr("'%s' is not a file." % fn)) QApplication.restoreOverrideCursor()
def initSpellchecker(self): # TODO: disable spellchecker icon in case of not working enchant try: import enchant spellDictDir = settings.get('spellchecker:directory') if spellDictDir: if enchant.__ver_major__ >= 1 and enchant.__ver_minor__ >= 6: enchant.set_param("enchant.myspell.dictionary.path", spellDictDir) else: print("Your pyenchant version is to old. Please " \ "upgrade to version 1.6.0 or higher, if you want " \ "to use spellchecker.") return None spellLang = settings.get('spellchecker:lang') if spellLang in enchant.list_languages(): # enchant.dict_exists(spellLang) do now work for me on linux... self.dict = enchant.Dict(spellLang) else: # try dictionary based on the current locale try: self.dict = enchant.Dict() settings.set('spellchecker:lang', self.dict.tag) except: # we don not have working dictionary... return None if self.dict: self.usePWL(self.dict) except: print("can not start spellchecker!!!") import traceback traceback.print_exc() return None
def saveAs(self): if settings.get("file_dialog_dir"): self.curDir = '~/' else: self.curDir = settings.get("file_dialog_dir") filename = QFileDialog.getSaveFileName(self, self.tr("Save document"), self.curDir, self.tr("ODT document (*.odt);;Text file (*.txt);;" "HTML file (*.html);;PDF file(*.pdf)") ) if not filename: return self.curDir = os.path.dirname(filename) settings.set("file_dialog_dir", self.curDir) dw = QTextDocumentWriter() dw.setFormat('ODF') # Default format # Check for alternative output format if filename.rsplit('.', 1)[1] == "txt": dw.setFormat('plaintext') if filename.rsplit('.', 1)[1] in ("html", "htm"): dw.setFormat('HTML') if filename.rsplit('.', 1)[1] in ("PDF", "pdf"): self.filePrintPdf(filename) return dw.setFileName(filename) dw.write(self.document())
def get_tesseract_languages(): """ get list of lang """ tess_exec = settings.get('tesseract-ocr:executable') if not tess_exec: tess_exec = 'tesseract' try: poTess = Popen([tess_exec, '--list-langs'], shell=False, stdout=PIPE, stderr=PIPE) stdout_message, lTess = poTess.communicate() # we need to remove not needed information e.g. OpenCL performamnce out_split = lTess.split('\n') langlist = list() add_lang = False for row in out_split: if row.startswith('List of'): add_lang = True if add_lang: langlist.append(row.strip()) if langlist: return langlist else: return get_tesseract_languages_old() except OSError, ex: print "ex", ex return None
def __init__(self, parent = None): QTextEdit.__init__(self) self.setupEditor() state = settings.get('editor:spell') if state == "": # no settings state = True self.toggleSpell(state) onOff = settings.get('editor:whiteSpace') if onOff == "": # no settings onOff = True self.togglewhiteSpace(onOff) self.currentCharFormatChanged.connect( self.CharFormatChanged)
def usePWL(self, dictionary): """ Restart spellchecker with personal private list """ import enchant pwlDict = settings.get('spellchecker:pwlDict') pwlLang = settings.get('spellchecker:pwlLang') if pwlLang: try: (name, extension) = pwlDict.rsplit('.', 1) pwlDict = name + '_' + dictionary.tag + "." + extension except: pwlDict = name + '_' + dictionary.tag self.dict = enchant.DictWithPWL(dictionary.tag, pwlDict) self.highlighter = Highlighter(self.document()) self.highlighter.setDict(self.dict)
def get_tesseract_languages_old(): """ make a list of installed language data files """ tess_exec = settings.get('tesseract-ocr:executable') if not tess_exec: tess_exec = 'tesseract' try: poTess = Popen([tess_exec, 'a', 'a', '-l', 'invalid'], shell=False, stdout=PIPE, stderr=PIPE) stdout_message, lTess = poTess.communicate() tess_data_prefix, langdata_ext = \ extract_tesseract_languages_path(lTess) except OSError, ex: print "ex", ex return None
def createActions(self): self.settingsAction = QAction(self.tr("Settings"), self) self.settingsAction.setIcon(QIcon(":/icons/icons/configure.png")) self.settingsAction.triggered.connect(self.settings) self.addAction(self.settingsAction) self.saveDocAsAction = QAction(self.tr("Save As"), self) self.saveDocAsAction.triggered.connect(self.SaveDocumentAs) self.saveDocAsAction.setIcon(QIcon(":/icons/icons/filesave.png")) self.addAction(self.saveDocAsAction) self.spellAction = QAction(self.tr("Spellchecking"), self) self.spellAction.setIcon( QIcon(":/icons/icons/tools-check-spelling.png")) self.spellAction.setCheckable(True) self.spellAction.setChecked(settings.get('editor:spell')) self.spellAction.toggled.connect(self.spell) self.insertSeparator(self.spellAction) self.addAction(self.spellAction) self.whiteSpaceAction = QAction(self.tr("Show whitespace"), self) self.whiteSpaceAction.setIcon( QIcon(":/icons/icons/whiteSpace.png")) self.whiteSpaceAction.setCheckable(True) self.whiteSpaceAction.setChecked(settings.get('editor:whiteSpace')) self.whiteSpaceAction.toggled.connect(self.whiteSpace) self.addAction(self.whiteSpaceAction) self.BoldAction = QAction( QIcon(":/icons/icons/format-text-bold.png"), self.tr("&Bold"), self, shortcut=Qt.CTRL + Qt.Key_B, triggered=self.bold, checkable=True) self.addAction(self.BoldAction) self.ItalicAction = QAction(self.tr("Italic"), self) self.ItalicAction.setIcon( QIcon(":/icons/icons/format-text-italic.png")) self.ItalicAction.setShortcut(Qt.CTRL + Qt.Key_I) self.ItalicAction.setCheckable(True) self.ItalicAction.triggered.connect(self.italic) self.addAction(self.ItalicAction) self.UnderlineAction = QAction(self.tr("Underline"), self) self.UnderlineAction.setIcon( QIcon(":/icons/icons/format-text-underline.png")) self.UnderlineAction.setCheckable(True) self.UnderlineAction.setShortcut(Qt.CTRL + Qt.Key_U) self.UnderlineAction.triggered.connect(self.underline) self.addAction(self.UnderlineAction) self.StrikethroughAction = QAction(self.tr("Strikethrough"), self) self.StrikethroughAction.setIcon( QIcon(":/icons/icons/format-text-strikethrough.png")) self.StrikethroughAction.setCheckable(True) self.StrikethroughAction.triggered.connect(self.strikethrough) self.addAction(self.StrikethroughAction) self.SubscriptAction = QAction(self.tr("Subscript"), self) self.SubscriptAction.setIcon( QIcon(":/icons/icons/format-text-subscript.png")) self.SubscriptAction.setCheckable(True) self.SubscriptAction.triggered.connect(self.subscript) self.addAction(self.SubscriptAction) self.SuperscriptAction = QAction(self.tr("Superscript"), self) self.SuperscriptAction.setIcon( QIcon(":/icons/icons/format-text-superscript.png")) self.SuperscriptAction.setCheckable(True) self.SuperscriptAction.triggered.connect(self.superscript) self.addAction(self.SuperscriptAction)
def contextMenuEvent(self, event): """ Creates two context menus: 1. no modifier -> spellchecker & clear emnu 2. ctrl modifier -> Text change & Insert symbol """ contextMenu = self.createStandardContextMenu() spellMenu = True if (QApplication.keyboardModifiers() & Qt.ControlModifier): spellMenu = False self.clearAction = QAction(self.tr("Clear"), contextMenu) contextMenu.addSeparator() contextMenu.addAction(self.clearAction) if not len(self.toPlainText()): self.clearAction.setEnabled(False) self.clearAction.triggered.connect(self.clear) if not spellMenu: textOpsMenu = QMenu(self.tr("Text change...")) removeEOLAction = QAction(self.tr("Join lines"), textOpsMenu, ) textOpsMenu.addAction(removeEOLAction) removeEOLAction.triggered.connect(self.removeEOL) textOpsMenu.addSeparator() toUppercaseAction = QAction(self.tr("to UPPERCASE"), textOpsMenu) textOpsMenu.addAction(toUppercaseAction) toUppercaseAction.triggered.connect(self.toUppercase) toLowercaseAction = QAction(self.tr("to lowercase"), textOpsMenu) textOpsMenu.addAction(toLowercaseAction) toLowercaseAction.triggered.connect(self.toLowercase) toTitleAction = QAction(self.tr("to Title"), textOpsMenu) textOpsMenu.addAction(toTitleAction) toTitleAction.triggered.connect(self.toTitlecase) toCapsAction = QAction(self.tr("to Capitalize"), textOpsMenu) textOpsMenu.addAction(toCapsAction) toCapsAction.triggered.connect(self.toCaps) contextMenu.insertSeparator(contextMenu.actions()[0]) contextMenu.insertMenu(contextMenu.actions()[0], textOpsMenu) insertSymbolMenu = QMenu(self.tr("Insert symbol...")) settings_symbols = settings.get('editor:symbols') if settings_symbols: self.symbols = settings_symbols.split('\n') for symbol in self.symbols: action = SpellAction(symbol, insertSymbolMenu) action.correct.connect( self.insertSymbol) insertSymbolMenu.addAction(action) contextMenu.insertMenu(contextMenu.actions()[0], insertSymbolMenu) if not self.textCursor().hasSelection() and spellMenu: # Select the word under the cursor for spellchecker cursor = self.textCursor() cursor.select(QTextCursor.WordUnderCursor) self.setTextCursor(cursor) text = self.textCursor().selectedText() #TODO: put to configuration list of ignored starting/ending chars # remove u"„" from selection if text.startswith(u"„") or text.startswith(u"“"): text = text[1:] selectionEnd = cursor.selectionEnd() cursor.setPosition(cursor.position() - len(text)) cursor.setPosition(selectionEnd, QTextCursor.KeepAnchor) self.setTextCursor(cursor) # remove u"”" from selection if text.endswith(u"”") or text.startswith(u"“"): selectionEnd = cursor.selectionEnd() cursor.setPosition(cursor.position() - len(text)) cursor.setPosition(selectionEnd - 1, QTextCursor.KeepAnchor) text = text[:-1] self.setTextCursor(cursor) # Check if the selected word is misspelled and offer spelling # suggestions if it is. if self.textCursor().hasSelection(): if not self.dict.check(text): spell_menu = QMenu(self.tr("Spelling Suggestions")) addWordAcction = QAction(self.tr('Add word...'), spell_menu) addWordAcction.triggered.connect(self.addWord) spell_menu.addAction(addWordAcction) for word in self.dict.suggest(text): action = SpellAction(word, spell_menu) action.correct.connect(self.changeText) spell_menu.addAction(action) contextMenu.insertSeparator(contextMenu.actions()[1]) contextMenu.insertMenu(contextMenu.actions()[0], spell_menu) # Only add the spelling suggests to the menu if there are # suggestions. if len(spell_menu.actions()) != 1: spell_menu.insertSeparator(spell_menu.actions()[1]) contextMenu.exec_(event.globalPos()) event.accept()
def setEditorFont(self): self.setFont(QFont(settings.get('editor:font')))
tess_exec = settings.get('tesseract-ocr:executable') if not tess_exec: tess_exec = 'tesseract' try: poTess = Popen([tess_exec, 'a', 'a', '-l', 'invalid'], shell=False, stdout=PIPE, stderr=PIPE) stdout_message, lTess = poTess.communicate() tess_data_prefix, langdata_ext = \ extract_tesseract_languages_path(lTess) except OSError, ex: print "ex", ex return None # env. setting can help to handle path with spaces tess_data_prefix = settings.get('tesseract-ocr:TESSDATA_PREFIX:') if not tess_data_prefix: tess_data_prefix = os.getenv('TESSDATA_PREFIX') tessdata_path = os.path.join(tess_data_prefix, "tessdata") if not os.path.exists(tessdata_path): print "Tesseract data path ('%s') do not exist!" % tessdata_path return None langdata = glob(tess_data_prefix + os.path.sep + '*' + langdata_ext) return [os.path.splitext(os.path.split(uc)[1])[0] for uc in langdata] def get_spellchecker_languages(directory = None): """ Check if spellchecker is installed and provide list of languages """ try:
if not tess_exec: tess_exec = 'tesseract' try: poTess = Popen([tess_exec, 'a', 'a', '-l', 'invalid'], shell=False, stdout=PIPE, stderr=PIPE) stdout_message, lTess = poTess.communicate() tess_data_prefix, langdata_ext = \ extract_tesseract_languages_path(lTess) except OSError, ex: print "ex", ex return None # env. setting can help to handle path with spaces tess_data_prefix = settings.get('tesseract-ocr:TESSDATA_PREFIX:') if not tess_data_prefix: tess_data_prefix = os.getenv('TESSDATA_PREFIX') tessdata_path = os.path.join(tess_data_prefix, "tessdata") if not os.path.exists(tessdata_path): print "Tesseract data path ('%s') do not exist!" % tessdata_path return None langdata = glob(tess_data_prefix + os.path.sep + '*' + langdata_ext) return [os.path.splitext(os.path.split(uc)[1])[0] for uc in langdata] def get_spellchecker_languages(directory=None): """ Check if spellchecker is installed and provide list of languages """