def _get_name(widget: QTextEdit, *, ispath: bool = False) -> str: """Return the file name of widget.""" text = widget.toPlainText() place_text = widget.placeholderText() if ispath: return text if isdir(text) else place_text return ''.join(x for x in text if x.isalnum() or x in "._- ") or place_text
class ReportBugDialog(EasyDialog): NAME = _("Report bug") HELP_BODY = _("Please copy-n-paste the error message in the " "Process log (if any). Please explain briefly what you were " "doing when the bug occurred. Please add anything that can " "help me reproduce the error. Thanks for your time and " "contribution to improving ezcad.") sig_start = Signal(str, str, str, str, str) def __init__(self, parent=None): EasyDialog.__init__(self, parent) self.setup_page() def setup_page(self): text = _("Email server") default = 'localhost' self.server = self.create_lineedit(text, default=default) self.layout.addWidget(self.server) text = _("Sender") default = 'email address e.g. [email protected]' self.sender = self.create_lineedit(text, default=default) self.layout.addWidget(self.sender) text = _("Receivers") default = 'email addresses separated by comma' self.receivers = self.create_lineedit(text, default=default) self.layout.addWidget(self.receivers) text = _("Subject") self.subject = self.create_lineedit(text) self.layout.addWidget(self.subject) self.body = QTextEdit(self) self.body.setFontFamily("monospace") self.body.setText(self.helpMessage) self.layout.addWidget(self.body) action = self.create_action() self.layout.addWidget(action) def apply(self): server = self.server.edit.text() sender = self.sender.edit.text() receivers = self.receivers.edit.text() subject = self.subject.edit.text() body = self.body.toPlainText() receivers = [x.strip() for x in receivers.split(',')] self.sig_start.emit(sender, receivers, subject, body, server)
class TextEditor(BaseDialog): """Array Editor Dialog""" def __init__(self, text, title='', font=None, parent=None, readonly=False): QDialog.__init__(self, parent) # Destroying the C++ object right after closing the dialog box, # otherwise it may be garbage-collected in another QThread # (e.g. the editor's analysis thread in Spyder), thus leading to # a segmentation fault on UNIX or an application crash on Windows self.setAttribute(Qt.WA_DeleteOnClose) self.text = None self.btn_save_and_close = None # Display text as unicode if it comes as bytes, so users see # its right representation if is_binary_string(text): self.is_binary = True text = to_text_string(text, 'utf8') else: self.is_binary = False self.layout = QVBoxLayout() self.setLayout(self.layout) # Text edit self.edit = QTextEdit(parent) self.edit.setReadOnly(readonly) self.edit.textChanged.connect(self.text_changed) self.edit.setPlainText(text) if font is None: font = get_font() self.edit.setFont(font) self.layout.addWidget(self.edit) # Buttons configuration btn_layout = QHBoxLayout() btn_layout.addStretch() if not readonly: self.btn_save_and_close = QPushButton(_('Save and Close')) self.btn_save_and_close.setDisabled(True) self.btn_save_and_close.clicked.connect(self.accept) btn_layout.addWidget(self.btn_save_and_close) self.btn_close = QPushButton(_('Close')) self.btn_close.setAutoDefault(True) self.btn_close.setDefault(True) self.btn_close.clicked.connect(self.reject) btn_layout.addWidget(self.btn_close) self.layout.addLayout(btn_layout) # Make the dialog act as a window self.setWindowFlags(Qt.Window) self.setWindowIcon(ima.icon('edit')) if title: try: unicode_title = to_text_string(title) except UnicodeEncodeError: unicode_title = u'' else: unicode_title = u'' self.setWindowTitle(_("Text editor") + \ u"%s" % (u" - " + unicode_title if unicode_title else u"")) @Slot() def text_changed(self): """Text has changed""" # Save text as bytes, if it was initially bytes if self.is_binary: self.text = to_binary_string(self.edit.toPlainText(), 'utf8') else: self.text = to_text_string(self.edit.toPlainText()) if self.btn_save_and_close: self.btn_save_and_close.setEnabled(True) self.btn_save_and_close.setAutoDefault(True) self.btn_save_and_close.setDefault(True) def get_value(self): """Return modified text""" # It is import to avoid accessing Qt C++ object as it has probably # already been destroyed, due to the Qt.WA_DeleteOnClose attribute return self.text def setup_and_check(self, value): """Verify if TextEditor is able to display strings passed to it.""" try: to_text_string(value, 'utf8') return True except: return False
class ZhuNoteForm(QWidget): def __init__(self, path=None): QWidget.__init__(self) self.initUI(path) def initUI(self, path): pathLabel = QLabel('Path') filenameLabel = QLabel('Filename') timeLabel = QLabel('Time') titleLabel = QLabel('Title') keywordLabel = QLabel('Keyword') figureLabel = QLabel('Figure') htmlLabel = QLabel('HTML') bodyLabel = QLabel('Body') self.pathEdit = QLineEdit(path) self.pathEdit.setReadOnly(True) self.filenameEdit = QLineEdit() self.timeEdit = QLineEdit() self.titleEdit = QLineEdit() self.keywordEdit = QLineEdit() self.figureEdit = QLineEdit() self.htmlEdit = QLineEdit() self.bodyEdit = QTextEdit() # If more than one keyword, delimit with comma. # Same for figure and html filenames. #btnSave = QPushButton('Save') #btnSave.setToolTip('Save script to file') #btnSave.clicked.connect(self.saveFile) # Replace save button with keyboard shortcut # Save move hand from keyboard to mouse. grid = QGridLayout() grid.setSpacing(5) row = 0 grid.addWidget(pathLabel, row, 0) grid.addWidget(self.pathEdit, row, 1) row += 1 grid.addWidget(filenameLabel, row, 0) grid.addWidget(self.filenameEdit, row, 1) row += 1 grid.addWidget(figureLabel, row, 0) grid.addWidget(self.figureEdit, row, 1) row += 1 grid.addWidget(htmlLabel, row, 0) grid.addWidget(self.htmlEdit, row, 1) row += 1 grid.addWidget(timeLabel, row, 0) grid.addWidget(self.timeEdit, row, 1) row += 1 grid.addWidget(titleLabel, row, 0) grid.addWidget(self.titleEdit, row, 1) row += 1 grid.addWidget(keywordLabel, row, 0) grid.addWidget(self.keywordEdit, row, 1) row += 1 grid.addWidget(bodyLabel, row, 0) grid.addWidget(self.bodyEdit, row, 1, 6, 1) #grid.addWidget(btnSave, 11, 1) self.actOpen = QAction('Open', self) self.actOpen.setShortcut('Ctrl+O') self.actOpen.triggered.connect(self.openFile) self.filenameEdit.addAction(self.actOpen) self.actSave = QAction('Save', self) self.actSave.setShortcut('Ctrl+S') self.actSave.triggered.connect(self.saveFile) self.bodyEdit.addAction(self.actSave) self.setLayout(grid) #self.setGeometry(300, 300, 600, 400) self.setWindowTitle('Form - ZhuNote') #self.show() def setFont(self, font): #font = self.bodyEdit.font() # current font #font = QFont() # default font self.pathEdit.setFont(font) self.filenameEdit.setFont(font) self.timeEdit.setFont(font) self.titleEdit.setFont(font) self.keywordEdit.setFont(font) self.figureEdit.setFont(font) self.htmlEdit.setFont(font) self.bodyEdit.setFont(font) def clear(self): self.filenameEdit.clear() self.timeEdit.clear() self.titleEdit.clear() self.keywordEdit.clear() self.figureEdit.clear() self.htmlEdit.clear() self.bodyEdit.clear() def viewDict(self, dictNote): self.filenameEdit.setText(dictNote['Filename']) self.timeEdit.setText(dictNote['Time']) self.titleEdit.setText(dictNote['Title']) self.keywordEdit.setText(dictNote['Keyword']) self.figureEdit.setText(dictNote['Figure']) self.htmlEdit.setText(dictNote['HTML']) self.bodyEdit.setText(dictNote['Body']) def openFile(self): path = self.pathEdit.text() fn = self.filenameEdit.text() ffn = os.path.join(path, fn) with open(ffn, 'rb') as f: dictNote = pickle.load(f) self.viewDict(dictNote) def saveFile(self): #fn = timeStr + '.txt' # Use title as filename to overwrite existing note file. base = self.titleEdit.text().replace(' ', '_') txtfn = base + '.txt' pklfn = base + '.pkl' path = self.pathEdit.text() timeStr = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') self.filenameEdit.setText(pklfn) self.timeEdit.setText(timeStr) textSum = '' text = 'Filename: ' + txtfn + '\n' textSum += text text = 'Time: ' + timeStr + '\n' textSum += text text = 'Title: ' + self.titleEdit.text() + '\n' textSum += text text = 'Keyword: ' + self.keywordEdit.text() + '\n' textSum += text text = 'Figure: ' + self.figureEdit.text() + '\n' textSum += text text = 'HTML: ' + self.htmlEdit.text() + '\n' textSum += text text = 'Body: ' + self.bodyEdit.toPlainText() + '\n' textSum += text dictNote = {} dictNote['Filename'] = pklfn dictNote['Time'] = timeStr dictNote['Title'] = self.titleEdit.text() dictNote['Keyword'] = self.keywordEdit.text() dictNote['Figure'] = self.figureEdit.text() dictNote['HTML'] = self.htmlEdit.text() dictNote['Body'] = self.bodyEdit.toPlainText() txtffn = os.path.join(path, txtfn) pklffn = os.path.join(path, pklfn) # Check if file exist if os.path.isfile(txtffn): choice = QMessageBox.question( self, 'Warning', "File exists. Do you want overwrite?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if choice == QMessageBox.Yes: self.writeFile(textSum, txtffn, dictNote, pklffn) else: print("Change title and re-save.") return 1 else: self.writeFile(textSum, txtffn, dictNote, pklffn) return 0 @staticmethod def writeFile(textSum, txtfn, dictNote, pklfn): """ input are full filename (with absolute path) """ with open(txtfn, 'w', encoding='utf-8') as f: f.write(textSum) with open(pklfn, 'wb') as f: pickle.dump(dictNote, f, -1)
class TextEditor(QDialog): """Array Editor Dialog""" def __init__(self, text, title='', font=None, parent=None, readonly=False, size=(400, 300)): QDialog.__init__(self, parent) # Destroying the C++ object right after closing the dialog box, # otherwise it may be garbage-collected in another QThread # (e.g. the editor's analysis thread in Spyder), thus leading to # a segmentation fault on UNIX or an application crash on Windows self.setAttribute(Qt.WA_DeleteOnClose) self.text = None # Display text as unicode if it comes as bytes, so users see # its right representation if is_binary_string(text): self.is_binary = True text = to_text_string(text, 'utf8') else: self.is_binary = False self.layout = QVBoxLayout() self.setLayout(self.layout) # Text edit self.edit = QTextEdit(parent) self.edit.textChanged.connect(self.text_changed) self.edit.setReadOnly(readonly) self.edit.setPlainText(text) if font is None: font = get_font() self.edit.setFont(font) self.layout.addWidget(self.edit) # Buttons configuration buttons = QDialogButtonBox.Ok if not readonly: buttons = buttons | QDialogButtonBox.Cancel bbox = QDialogButtonBox(buttons) bbox.accepted.connect(self.accept) bbox.rejected.connect(self.reject) self.layout.addWidget(bbox) # Make the dialog act as a window self.setWindowFlags(Qt.Window) self.setWindowIcon(ima.icon('edit')) self.setWindowTitle(_("Text editor") + \ "%s" % (" - "+str(title) if str(title) else "")) self.resize(size[0], size[1]) def text_changed(self): """Text has changed""" # Save text as bytes, if it was initially bytes if self.is_binary: self.text = to_binary_string(self.edit.toPlainText(), 'utf8') else: self.text = to_text_string(self.edit.toPlainText()) def get_value(self): """Return modified text""" # It is import to avoid accessing Qt C++ object as it has probably # already been destroyed, due to the Qt.WA_DeleteOnClose attribute return self.text def setup_and_check(self, value): """Verify if TextEditor is able to display strings passed to it.""" try: to_text_string(value, 'utf8') return True except: return False
class TextEditor(QDialog): """Array Editor Dialog""" def __init__(self, text, title='', font=None, parent=None, readonly=False, size=(400, 300)): QDialog.__init__(self, parent) # Destroying the C++ object right after closing the dialog box, # otherwise it may be garbage-collected in another QThread # (e.g. the editor's analysis thread in Spyder), thus leading to # a segmentation fault on UNIX or an application crash on Windows self.setAttribute(Qt.WA_DeleteOnClose) self.text = None self.btn_save_and_close = None # Display text as unicode if it comes as bytes, so users see # its right representation if is_binary_string(text): self.is_binary = True text = to_text_string(text, 'utf8') else: self.is_binary = False self.layout = QVBoxLayout() self.setLayout(self.layout) # Text edit self.edit = QTextEdit(parent) self.edit.setReadOnly(readonly) self.edit.textChanged.connect(self.text_changed) self.edit.setPlainText(text) if font is None: font = get_font() self.edit.setFont(font) self.layout.addWidget(self.edit) # Buttons configuration btn_layout = QHBoxLayout() btn_layout.addStretch() if not readonly: self.btn_save_and_close = QPushButton(_('Save and Close')) self.btn_save_and_close.setDisabled(True) self.btn_save_and_close.clicked.connect(self.accept) btn_layout.addWidget(self.btn_save_and_close) self.btn_close = QPushButton(_('Close')) self.btn_close.setAutoDefault(True) self.btn_close.setDefault(True) self.btn_close.clicked.connect(self.reject) btn_layout.addWidget(self.btn_close) self.layout.addLayout(btn_layout) # Make the dialog act as a window self.setWindowFlags(Qt.Window) self.setWindowIcon(ima.icon('edit')) self.setWindowTitle(_("Text editor") + \ "%s" % (" - "+str(title) if str(title) else "")) self.resize(size[0], size[1]) @Slot() def text_changed(self): """Text has changed""" # Save text as bytes, if it was initially bytes if self.is_binary: self.text = to_binary_string(self.edit.toPlainText(), 'utf8') else: self.text = to_text_string(self.edit.toPlainText()) if self.btn_save_and_close: self.btn_save_and_close.setEnabled(True) self.btn_save_and_close.setAutoDefault(True) self.btn_save_and_close.setDefault(True) def get_value(self): """Return modified text""" # It is import to avoid accessing Qt C++ object as it has probably # already been destroyed, due to the Qt.WA_DeleteOnClose attribute return self.text def setup_and_check(self, value): """Verify if TextEditor is able to display strings passed to it.""" try: to_text_string(value, 'utf8') return True except: return False
class NoteEditor(QMainWindow): def __init__(self, parent, noteType, noteFileName="", b=None, c=None, v=None): super().__init__() self.parent, self.noteType = parent, noteType self.noteFileName = noteFileName if not self.noteType == "file": if v: self.b, self.c, self.v = b, c, v else: self.b, self.c, self.v = config.studyB, config.studyC, config.studyV # default - "Rich" mode for editing self.html = True # default - text is not modified; no need for saving new content self.parent.noteSaved = True config.noteOpened = True config.lastOpenedNote = (noteType, b, c, v) # specify window size self.resizeWindow(2/3, 2/3) # setup interface self.setupMenuBar() self.addToolBarBreak() self.setupToolBar() if config.hideNoteEditorStyleToolbar: self.toolBar.hide() self.addToolBarBreak() self.setupTextUtility() if config.hideNoteEditorTextUtility: self.ttsToolbar.hide() self.translateToolbar.hide() self.setupLayout() # display content when first launched self.displayInitialContent() self.editor.setFocus() # specify window title self.updateWindowTitle() # re-implementing close event, when users close this widget def closeEvent(self, event): if self.parent.noteSaved: config.noteOpened = False event.accept() if config.lastOpenedNote and config.openBibleNoteAfterEditorClosed: #if config.lastOpenedNote[0] == "file": # self.parent.externalFileButtonClicked() if config.lastOpenedNote[0] == "book": self.parent.openStudyBookNote() elif config.lastOpenedNote[0] == "chapter": self.parent.openStudyChapterNote() elif config.lastOpenedNote[0] == "verse": self.parent.openStudyVerseNote() else: if self.parent.warningNotSaved(): self.parent.noteSaved = True config.noteOpened = False event.accept() else: self.parent.bringToForeground(self) event.ignore() # re-implement keyPressEvent, control+S for saving file def keyPressEvent(self, event): keys = { Qt.Key_O: self.openFileDialog, Qt.Key_S: self.saveNote, Qt.Key_B: self.format_bold, Qt.Key_I: self.format_italic, Qt.Key_U: self.format_underline, Qt.Key_M: self.format_custom, Qt.Key_D: self.format_clear, Qt.Key_F: self.focusSearchField, } key = event.key() if event.modifiers() == Qt.ControlModifier and key in keys: keys[key]() # window appearance def resizeWindow(self, widthFactor, heightFactor): availableGeometry = QGuiApplication.instance().desktop().availableGeometry() self.resize(availableGeometry.width() * widthFactor, availableGeometry.height() * heightFactor) def updateWindowTitle(self): if self.noteType == "file": if self.noteFileName: *_, title = os.path.split(self.noteFileName) else: title = "NEW" else: title = self.parent.bcvToVerseReference(self.b, self.c, self.v) if self.noteType == "book": title, *_ = title.split(" ") elif self.noteType == "chapter": title, *_ = title.split(":") mode = {True: "rich", False: "plain"} notModified = {True: "", False: " [modified]"} self.setWindowTitle("Note Editor ({1} mode) - {0}{2}".format(title, mode[self.html], notModified[self.parent.noteSaved])) # switching between "rich" & "plain" mode def switchMode(self): if self.html: note = self.editor.toHtml() note = re.sub("<body style={0}[ ]*?font-family:[ ]*?'[^']*?';[ ]*?font-size:[ ]*?[0-9]+?pt;".format('"'), "<body style={0}font-family:'{1}'; font-size:{2}pt;".format('"', config.font, config.fontSize), note) self.editor.setPlainText(note) self.html = False self.updateWindowTitle() else: note = self.editor.toPlainText() self.editor.setHtml(note) self.html = True self.updateWindowTitle() # without this hide / show command below, QTextEdit does not update the text in some devices self.hide() self.show() def setupMenuBar(self): if config.toolBarIconFullSize: self.setupMenuBarFullIconSize() else: self.setupMenuBarStandardIconSize() def setupMenuBarStandardIconSize(self): self.menuBar = QToolBar() self.menuBar.setWindowTitle(config.thisTranslation["note_title"]) self.menuBar.setContextMenuPolicy(Qt.PreventContextMenu) # In QWidget, self.menuBar is treated as the menubar without the following line # In QMainWindow, the following line adds the configured QToolBar as part of the toolbar of the main window self.addToolBar(self.menuBar) newButton = QPushButton() newButton.setToolTip("{0}\n[Ctrl/Cmd + N]".format(config.thisTranslation["menu7_create"])) newButtonFile = os.path.join("htmlResources", "newfile.png") newButton.setIcon(QIcon(newButtonFile)) newButton.clicked.connect(self.newNoteFile) self.menuBar.addWidget(newButton) openButton = QPushButton() openButton.setToolTip("{0}\n[Ctrl/Cmd + O]".format(config.thisTranslation["menu7_open"])) openButtonFile = os.path.join("htmlResources", "open.png") openButton.setIcon(QIcon(openButtonFile)) openButton.clicked.connect(self.openFileDialog) self.menuBar.addWidget(openButton) self.menuBar.addSeparator() saveButton = QPushButton() saveButton.setToolTip("{0}\n[Ctrl/Cmd + S]".format(config.thisTranslation["note_save"])) saveButtonFile = os.path.join("htmlResources", "save.png") saveButton.setIcon(QIcon(saveButtonFile)) saveButton.clicked.connect(self.saveNote) self.menuBar.addWidget(saveButton) saveAsButton = QPushButton() saveAsButton.setToolTip(config.thisTranslation["note_saveAs"]) saveAsButtonFile = os.path.join("htmlResources", "saveas.png") saveAsButton.setIcon(QIcon(saveAsButtonFile)) saveAsButton.clicked.connect(self.openSaveAsDialog) self.menuBar.addWidget(saveAsButton) self.menuBar.addSeparator() toolBarButton = QPushButton() toolBarButton.setToolTip(config.thisTranslation["note_print"]) toolBarButtonFile = os.path.join("htmlResources", "print.png") toolBarButton.setIcon(QIcon(toolBarButtonFile)) toolBarButton.clicked.connect(self.printNote) self.menuBar.addWidget(toolBarButton) self.menuBar.addSeparator() switchButton = QPushButton() switchButton.setToolTip(config.thisTranslation["note_mode"]) switchButtonFile = os.path.join("htmlResources", "switch.png") switchButton.setIcon(QIcon(switchButtonFile)) switchButton.clicked.connect(self.switchMode) self.menuBar.addWidget(switchButton) self.menuBar.addSeparator() # decreaseFontSizeButton = QPushButton() # decreaseFontSizeButton.setToolTip(config.thisTranslation["menu2_smaller"]) # decreaseFontSizeButtonFile = os.path.join("htmlResources", "fontMinus.png") # decreaseFontSizeButton.setIcon(QIcon(decreaseFontSizeButtonFile)) # decreaseFontSizeButton.clicked.connect(self.decreaseNoteEditorFontSize) # self.menuBar.addWidget(decreaseFontSizeButton) # # increaseFontSizeButton = QPushButton() # increaseFontSizeButton.setToolTip(config.thisTranslation["menu2_larger"]) # increaseFontSizeButtonFile = os.path.join("htmlResources", "fontPlus.png") # increaseFontSizeButton.setIcon(QIcon(increaseFontSizeButtonFile)) # increaseFontSizeButton.clicked.connect(self.increaseNoteEditorFontSize) # self.menuBar.addWidget(increaseFontSizeButton) # self.menuBar.addSeparator() self.searchLineEdit = QLineEdit() self.searchLineEdit.setClearButtonEnabled(True) self.searchLineEdit.setToolTip(config.thisTranslation["menu5_search"]) self.searchLineEdit.setMaximumWidth(400) self.searchLineEdit.returnPressed.connect(self.searchLineEntered) self.menuBar.addWidget(self.searchLineEdit) self.menuBar.addSeparator() toolBarButton = QPushButton() toolBarButton.setToolTip(config.thisTranslation["note_toolbar"]) toolBarButtonFile = os.path.join("htmlResources", "toolbar.png") toolBarButton.setIcon(QIcon(toolBarButtonFile)) toolBarButton.clicked.connect(self.toggleToolbar) self.menuBar.addWidget(toolBarButton) toolBarButton = QPushButton() toolBarButton.setToolTip(config.thisTranslation["note_textUtility"]) toolBarButtonFile = os.path.join("htmlResources", "textUtility.png") toolBarButton.setIcon(QIcon(toolBarButtonFile)) toolBarButton.clicked.connect(self.toggleTextUtility) self.menuBar.addWidget(toolBarButton) self.menuBar.addSeparator() def setupMenuBarFullIconSize(self): self.menuBar = QToolBar() self.menuBar.setWindowTitle(config.thisTranslation["note_title"]) self.menuBar.setContextMenuPolicy(Qt.PreventContextMenu) # In QWidget, self.menuBar is treated as the menubar without the following line # In QMainWindow, the following line adds the configured QToolBar as part of the toolbar of the main window self.addToolBar(self.menuBar) iconFile = os.path.join("htmlResources", "newfile.png") self.menuBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + N]".format(config.thisTranslation["menu7_create"]), self.newNoteFile) iconFile = os.path.join("htmlResources", "open.png") self.menuBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + O]".format(config.thisTranslation["menu7_open"]), self.openFileDialog) self.menuBar.addSeparator() iconFile = os.path.join("htmlResources", "save.png") self.menuBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + S]".format(config.thisTranslation["note_save"]), self.saveNote) iconFile = os.path.join("htmlResources", "saveas.png") self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["note_saveAs"], self.openSaveAsDialog) self.menuBar.addSeparator() iconFile = os.path.join("htmlResources", "print.png") self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["note_print"], self.printNote) self.menuBar.addSeparator() iconFile = os.path.join("htmlResources", "switch.png") self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["note_mode"], self.switchMode) self.menuBar.addSeparator() # iconFile = os.path.join("htmlResources", "fontMinus.png") # self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["menu2_smaller"], self.decreaseNoteEditorFontSize) # # iconFile = os.path.join("htmlResources", "fontPlus.png") # self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["menu2_larger"], self.increaseNoteEditorFontSize) # self.menuBar.addSeparator() self.searchLineEdit = QLineEdit() self.searchLineEdit.setToolTip("{0}\n[Ctrl/Cmd + F]".format(config.thisTranslation["menu5_search"])) self.searchLineEdit.setMaximumWidth(400) self.searchLineEdit.returnPressed.connect(self.searchLineEntered) self.menuBar.addWidget(self.searchLineEdit) self.menuBar.addSeparator() iconFile = os.path.join("htmlResources", "toolbar.png") self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["note_toolbar"], self.toggleToolbar) iconFile = os.path.join("htmlResources", "textUtility.png") self.menuBar.addAction(QIcon(iconFile), config.thisTranslation["note_textUtility"], self.toggleTextUtility) self.menuBar.addSeparator() def toggleToolbar(self): if config.hideNoteEditorStyleToolbar: self.toolBar.show() config.hideNoteEditorStyleToolbar = False else: self.toolBar.hide() config.hideNoteEditorStyleToolbar = True def toggleTextUtility(self): if config.hideNoteEditorTextUtility: self.ttsToolbar.show() self.translateToolbar.show() config.hideNoteEditorTextUtility = False else: self.ttsToolbar.hide() self.translateToolbar.hide() config.hideNoteEditorTextUtility = True def printNote(self): #document = QTextDocument("Sample Page") document = self.editor.document() printer = QPrinter() myPrintDialog = QPrintDialog(printer, self) if myPrintDialog.exec_() == QDialog.Accepted: return document.print_(printer) def setupToolBar(self): if config.toolBarIconFullSize: self.setupToolBarFullIconSize() else: self.setupToolBarStandardIconSize() def setupToolBarStandardIconSize(self): self.toolBar = QToolBar() self.toolBar.setWindowTitle(config.thisTranslation["noteTool_title"]) self.toolBar.setContextMenuPolicy(Qt.PreventContextMenu) # self.toolBar can be treated as an individual widget and positioned with a specified layout # In QMainWindow, the following line adds the configured QToolBar as part of the toolbar of the main window self.addToolBar(self.toolBar) items = ( ("noteTool_textFont", "font.png", self.format_font), ("noteTool_textColor", "textColor.png", self.format_textColor), ("noteTool_textBackgroundColor", "textBgColor.png", self.format_textBackgroundColor), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar) self.toolBar.addSeparator() items = ( ("noteTool_header1", "header1.png", self.format_header1), ("noteTool_header2", "header2.png", self.format_header2), ("noteTool_header3", "header3.png", self.format_header3), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar) self.toolBar.addSeparator() items = ( ("{0}\n[Ctrl/Cmd + B]".format(config.thisTranslation["noteTool_bold"]), "bold.png", self.format_bold), ("{0}\n[Ctrl/Cmd + I]".format(config.thisTranslation["noteTool_italic"]), "italic.png", self.format_italic), ("{0}\n[Ctrl/Cmd + U]".format(config.thisTranslation["noteTool_underline"]), "underline.png", self.format_underline), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar, translation=False) self.toolBar.addSeparator() items = ( ("noteTool_superscript", "superscript.png", self.format_superscript), ("noteTool_subscript", "subscript.png", self.format_subscript), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar) self.toolBar.addSeparator() self.parent.addStandardIconButton("{0}\n[Ctrl/Cmd + M]\n\n{1}\n* {4}\n* {5}\n* {6}\n\n{2}\n*1 {4}\n*2 {5}\n*3 {6}\n\n{3}\n{10}{4}|{5}|{6}{11}\n{10}{7}|{8}|{9}{11}".format(config.thisTranslation["noteTool_trans0"], config.thisTranslation["noteTool_trans1"], config.thisTranslation["noteTool_trans2"], config.thisTranslation["noteTool_trans3"], config.thisTranslation["noteTool_no1"], config.thisTranslation["noteTool_no2"], config.thisTranslation["noteTool_no3"], config.thisTranslation["noteTool_no4"], config.thisTranslation["noteTool_no5"], config.thisTranslation["noteTool_no6"], "{", "}"), "custom.png", self.format_custom, self.toolBar, translation=False) self.toolBar.addSeparator() items = ( ("noteTool_left", "align_left.png", self.format_left), ("noteTool_centre", "align_center.png", self.format_center), ("noteTool_right", "align_right.png", self.format_right), ("noteTool_justify", "align_justify.png", self.format_justify), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar) self.toolBar.addSeparator() self.parent.addStandardIconButton("{0}\n[Ctrl/Cmd + D]".format(config.thisTranslation["noteTool_delete"]), "clearFormat.png", self.format_clear, self.toolBar, translation=False) self.toolBar.addSeparator() items = ( ("noteTool_hyperlink", "hyperlink.png", self.openHyperlinkDialog), ("noteTool_externalImage", "gallery.png", self.openImageDialog), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar) self.toolBar.addSeparator() items = ( ("noteTool_image", "addImage.png", self.addInternalImage), ("noteTool_exportImage", "export.png", self.exportNoteImages), ) for item in items: toolTip, icon, action = item self.parent.addStandardIconButton(toolTip, icon, action, self.toolBar) self.toolBar.addSeparator() def setupToolBarFullIconSize(self): self.toolBar = QToolBar() self.toolBar.setWindowTitle(config.thisTranslation["noteTool_title"]) self.toolBar.setContextMenuPolicy(Qt.PreventContextMenu) # self.toolBar can be treated as an individual widget and positioned with a specified layout # In QMainWindow, the following line adds the configured QToolBar as part of the toolbar of the main window self.addToolBar(self.toolBar) iconFile = os.path.join("htmlResources", "font.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_textFont"], self.format_font) iconFile = os.path.join("htmlResources", "textColor.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_textColor"], self.format_textColor) iconFile = os.path.join("htmlResources", "textBgColor.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_textBackgroundColor"], self.format_textBackgroundColor) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "header1.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_header1"], self.format_header1) iconFile = os.path.join("htmlResources", "header2.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_header2"], self.format_header2) iconFile = os.path.join("htmlResources", "header3.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_header3"], self.format_header3) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "bold.png") self.toolBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + B]".format(config.thisTranslation["noteTool_bold"]), self.format_bold) iconFile = os.path.join("htmlResources", "italic.png") self.toolBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + I]".format(config.thisTranslation["noteTool_italic"]), self.format_italic) iconFile = os.path.join("htmlResources", "underline.png") self.toolBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + U]".format(config.thisTranslation["noteTool_underline"]), self.format_underline) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "custom.png") self.toolBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + M]\n\n{1}\n* {4}\n* {5}\n* {6}\n\n{2}\n*1 {4}\n*2 {5}\n*3 {6}\n\n{3}\n{10}{4}|{5}|{6}{11}\n{10}{7}|{8}|{9}{11}".format(config.thisTranslation["noteTool_trans0"], config.thisTranslation["noteTool_trans1"], config.thisTranslation["noteTool_trans2"], config.thisTranslation["noteTool_trans3"], config.thisTranslation["noteTool_no1"], config.thisTranslation["noteTool_no2"], config.thisTranslation["noteTool_no3"], config.thisTranslation["noteTool_no4"], config.thisTranslation["noteTool_no5"], config.thisTranslation["noteTool_no6"], "{", "}"), self.format_custom) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "align_left.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_left"], self.format_left) iconFile = os.path.join("htmlResources", "align_center.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_centre"], self.format_center) iconFile = os.path.join("htmlResources", "align_right.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_right"], self.format_right) iconFile = os.path.join("htmlResources", "align_justify.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_justify"], self.format_justify) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "clearFormat.png") self.toolBar.addAction(QIcon(iconFile), "{0}\n[Ctrl/Cmd + D]".format(config.thisTranslation["noteTool_delete"]), self.format_clear) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "hyperlink.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_hyperlink"], self.openHyperlinkDialog) iconFile = os.path.join("htmlResources", "gallery.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_externalImage"], self.openImageDialog) self.toolBar.addSeparator() iconFile = os.path.join("htmlResources", "addImage.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_image"], self.addInternalImage) iconFile = os.path.join("htmlResources", "export.png") self.toolBar.addAction(QIcon(iconFile), config.thisTranslation["noteTool_exportImage"], self.exportNoteImages) self.toolBar.addSeparator() def setupLayout(self): self.editor = QTextEdit() self.editor.setStyleSheet("font-family:'{0}'; font-size:{1}pt;".format(config.font, config.fontSize)); self.editor.textChanged.connect(self.textChanged) self.setCentralWidget(self.editor) #self.layout = QGridLayout() #self.layout.setMenuBar(self.menuBar) #self.layout.addWidget(self.toolBar, 0, 0) #self.layout.addWidget(self.editor, 1, 0) #self.setLayout(self.layout) # adjustment of note editor font size def increaseNoteEditorFontSize(self): if self.html: self.editor.selectAll() config.noteEditorFontSize += 1 self.editor.setFontPointSize(config.noteEditorFontSize) self.hide() self.show() def decreaseNoteEditorFontSize(self): if self.html and not config.noteEditorFontSize == 0: self.editor.selectAll() config.noteEditorFontSize -= 1 self.editor.setFontPointSize(config.noteEditorFontSize) self.hide() self.show() # search field entered def searchLineEntered(self): searchString = self.searchLineEdit.text() if searchString: cursor = self.editor.document().find(searchString, self.editor.textCursor()) if cursor: self.editor.setTextCursor(cursor) self.hide() self.show() def focusSearchField(self): self.searchLineEdit.setFocus() # track if the text being modified def textChanged(self): if self.parent.noteSaved: self.parent.noteSaved = False self.updateWindowTitle() # display content when first launched def displayInitialContent(self): if self.noteType == "file": if self.noteFileName: self.openNoteFile(self.noteFileName) else: self.newNoteFile() else: self.openBibleNote() self.editor.selectAll() self.editor.setFontPointSize(config.noteEditorFontSize) self.editor.moveCursor(QTextCursor.Start, QTextCursor.MoveAnchor) self.parent.noteSaved = True def getEmptyPage(self): strict = '' if config.includeStrictDocTypeInNote: strict = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">' return """{4}<html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li {0} white-space: pre-wrap; {1} </style></head><body style="font-family:'{2}'; font-size:{3}pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>"""\ .format("{", "}", config.font, config.fontSize, strict) # load chapter / verse notes from sqlite database def openBibleNote(self): if self.noteType == "book": note = NoteService.getBookNote(self.b) elif self.noteType == "chapter": note = NoteService.getChapterNote(self.b, self.c) elif self.noteType == "verse": note = NoteService.getVerseNote(self.b, self.c, self.v) if note == config.thisTranslation["empty"]: note = self.getEmptyPage() else: note = self.fixNoteFont(note) if self.html: self.editor.setHtml(note) else: self.editor.setPlainText(note) # File I / O def newNoteFile(self): if self.parent.noteSaved: self.newNoteFileAction() elif self.parent.warningNotSaved(): self.newNoteFileAction() def newNoteFileAction(self): self.noteType = "file" self.noteFileName = "" #self.editor.clear() defaultText = self.getEmptyPage() if self.html: self.editor.setHtml(defaultText) else: self.editor.setPlainText(defaultText) self.parent.noteSaved = True self.updateWindowTitle() self.hide() self.show() def openFileDialog(self): if self.parent.noteSaved: self.openFileDialogAction() elif self.parent.warningNotSaved(): self.openFileDialogAction() def openFileDialogAction(self): options = QFileDialog.Options() fileName, filtr = QFileDialog.getOpenFileName(self, config.thisTranslation["menu7_open"], "notes", "UniqueBible.app Note Files (*.uba);;HTML Files (*.html);;HTM Files (*.htm);;All Files (*)", "", options) if fileName: self.openNoteFile(fileName) def openNoteFile(self, fileName): try: f = open(fileName, "r", encoding="utf-8") except: print("Failed to open '{0}'".format(fileName)) note = f.read() f.close() self.noteType = "file" self.noteFileName = fileName note = self.fixNoteFont(note) if self.html: self.editor.setHtml(note) else: self.editor.setPlainText(note) self.parent.noteSaved = True self.updateWindowTitle() self.hide() self.show() def saveNote(self): if self.html: note = self.editor.toHtml() else: note = self.editor.toPlainText() note = self.fixNoteFont(note) if self.noteType == "book": NoteService.saveBookNote(self.b, note) if config.openBibleNoteAfterSave: self.parent.openBookNote(self.b,) self.parent.noteSaved = True self.updateWindowTitle() elif self.noteType == "chapter": NoteService.saveChapterNote(self.b, self.c, note) if config.openBibleNoteAfterSave: self.parent.openChapterNote(self.b, self.c) self.parent.noteSaved = True self.updateWindowTitle() elif self.noteType == "verse": NoteService.saveVerseNote(self.b, self.c, self.v, note) if config.openBibleNoteAfterSave: self.parent.openVerseNote(self.b, self.c, self.v) self.parent.noteSaved = True self.updateWindowTitle() elif self.noteType == "file": if self.noteFileName == "": self.openSaveAsDialog() else: self.saveAsNote(self.noteFileName) def openSaveAsDialog(self): if self.noteFileName: *_, defaultName = os.path.split(self.noteFileName) else: defaultName = "new.uba" options = QFileDialog.Options() fileName, filtr = QFileDialog.getSaveFileName(self, config.thisTranslation["note_saveAs"], os.path.join("notes", defaultName), "UniqueBible.app Note Files (*.uba);;HTML Files (*.html);;HTM Files (*.htm);;All Files (*)", "", options) if fileName: if not "." in os.path.basename(fileName): fileName = fileName + ".uba" self.saveAsNote(fileName) def saveAsNote(self, fileName): if self.html: note = self.editor.toHtml() else: note = self.editor.toPlainText() note = self.fixNoteFont(note) f = open(fileName, "w", encoding="utf-8") f.write(note) f.close() self.noteFileName = fileName self.parent.addExternalFileHistory(fileName) self.parent.setExternalFileButton() self.parent.noteSaved = True self.updateWindowTitle() def fixNoteFont(self, note): note = re.sub("<body style={0}[ ]*?font-family:[ ]*?'[^']*?';[ ]*?font-size:[ ]*?[0-9]+?pt;".format('"'), "<body style={0}font-family:'{1}'; font-size:{2}pt;".format('"', config.font, config.fontSize), note) if not config.includeStrictDocTypeInNote: note = re.sub("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\n""", "", note) return note # formatting styles def format_clear(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: selectedText = """<span style="font-family:'{0}'; font-size:{1}pt;">{2}</span>""".format(config.font, config.fontSize, selectedText) self.editor.insertHtml(selectedText) else: selectedText = re.sub("<[^\n<>]*?>", "", selectedText) self.editor.insertPlainText(selectedText) else: self.selectTextFirst() def format_header1(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.insertHtml("<h1>{0}</h1>".format(selectedText)) else: self.editor.insertPlainText("<h1>{0}</h1>".format(selectedText)) else: self.selectTextFirst() def format_header2(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.insertHtml("<h2>{0}</h2>".format(selectedText)) else: self.editor.insertPlainText("<h2>{0}</h2>".format(selectedText)) else: self.selectTextFirst() def format_header3(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.insertHtml("<h3>{0}</h3>".format(selectedText)) else: self.editor.insertPlainText("<h3>{0}</h3>".format(selectedText)) else: self.selectTextFirst() def format_font(self): selectedText = self.editor.textCursor().selectedText() if selectedText: ok, font = QFontDialog.getFont(QFont(config.font, config.fontSize), self) if ok: if self.html: self.editor.setCurrentFont(font) else: fontFamily, fontSize, i1, i2, fontWeight, italic, underline, strikeout, *_ = font.key().split(",") spanTag = """<span style="font-family:'{0}'; font-size:{1}pt;""".format(fontFamily, fontSize) # add font weight if fontWeight == "25": spanTag += " font-weight:200;" elif fontWeight == "75": spanTag += " font-weight:600;" # add italic style if italic == "1": spanTag += " font-style:italic;" # add both underline and strikeout style if underline == "1" and strikeout == "1": spanTag += " text-decoration: underline line-through;" # add underline style elif underline == "1": spanTag += " text-decoration: underline;" # add strikeout style elif strikeout == "1": spanTag += " text-decoration: line-through;" # close tag spanTag += '">' self.editor.insertPlainText("{0}{1}</span>".format(spanTag, selectedText)) else: self.selectTextFirst() def format_textColor(self): selectedText = self.editor.textCursor().selectedText() if selectedText: color = QColorDialog.getColor(Qt.darkRed, self) if color.isValid(): if self.html: self.editor.setTextColor(color) else: self.editor.insertPlainText('<span style="color:{0};">{1}</span>'.format(color.name(), self.editor.textCursor().selectedText())) else: self.selectTextFirst() def format_textBackgroundColor(self): selectedText = self.editor.textCursor().selectedText() if selectedText: color = QColorDialog.getColor(Qt.yellow, self) if color.isValid(): if self.html: self.editor.setTextBackgroundColor(color) else: self.editor.insertPlainText('<span style="background-color:{0};">{1}</span>'.format(color.name(), selectedText)) else: self.selectTextFirst() def format_bold(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: # Reference: https://doc.qt.io/qt-5/qfont.html#Weight-enum # Bold = 75 self.editor.setFontWeight(75) else: self.editor.insertPlainText("<b>{0}</b>".format(selectedText)) else: self.selectTextFirst() def format_italic(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.setFontItalic(True) else: self.editor.insertPlainText("<i>{0}</i>".format(selectedText)) else: self.selectTextFirst() def format_underline(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.setFontUnderline(True) else: self.editor.insertPlainText("<u>{0}</u>".format(selectedText)) else: self.selectTextFirst() def format_superscript(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.insertHtml("<sup>{0}</sup>".format(selectedText)) else: self.editor.insertPlainText("<sup>{0}</sup>".format(selectedText)) else: self.selectTextFirst() def format_subscript(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.insertHtml("<sub>{0}</sub>".format(selectedText)) else: self.editor.insertPlainText("<sub>{0}</sub>".format(selectedText)) else: self.selectTextFirst() def format_center(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.setAlignment(Qt.AlignCenter) else: self.editor.insertPlainText("<div style='text-align:center;'>{0}</div>".format(selectedText)) else: self.selectTextFirst() def format_justify(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.setAlignment(Qt.AlignJustify) else: self.editor.insertPlainText("<div style='text-align:justify;'>{0}</div>".format(selectedText)) else: self.selectTextFirst() def format_left(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.setAlignment(Qt.AlignLeft) else: self.editor.insertPlainText("<div style='text-align:left;'>{0}</div>".format(selectedText)) else: self.selectTextFirst() def format_right(self): selectedText = self.editor.textCursor().selectedText() if selectedText: if self.html: self.editor.setAlignment(Qt.AlignRight) else: self.editor.insertPlainText("<div style='text-align:right;'>{0}</div>".format(selectedText)) else: self.selectTextFirst() def format_custom(self): selectedText = self.editor.textCursor().selectedText() if selectedText: selectedText = self.customFormat(selectedText) if self.html: self.editor.insertHtml(selectedText) else: self.editor.insertPlainText(selectedText) else: self.selectTextFirst() def customFormat(self, text): # QTextEdit's line break character by pressing ENTER in plain & html mode " " # please note that " " is not an empty string text = text.replace(" ", "\n") text = re.sub("^\*[0-9]+? (.*?)$", r"<ol><li>\1</li></ol>", text, flags=re.M) text = text.replace("</ol>\n<ol>", "\n") text = re.sub("^\* (.*?)$", r"<ul><li>\1</li></ul>", text, flags=re.M) text = text.replace("</ul>\n<ul>", "\n") text = re.sub("^{.*?}$", self.formatHTMLTable, text, flags=re.M) text = text.replace("</table>\n<table>", "\n") # add style to table here # please note that QTextEdit supports HTML 4, rather than HTML 5 # take this old reference: https://www.w3schools.com/tags/tag_table.asp text = text.replace('<table>', '<table border="1" cellpadding="5">') # convert back to QTextEdit linebreak text = text.replace("\n", " ") # wrap with default font and font-size text = """<span style="font-family:'{0}'; font-size:{1}pt;">{2}</span>""".format(config.font, config.fontSize, text) return text def formatHTMLTable(self, match): row = match.group()[1:-1] row = "".join(["<td>{0}</td>".format(cell) for cell in row.split("|")]) return "<table><tr>{0}</tr></table>".format(row) def addInternalImage(self): self.openImageDialog(external=False) def openImageDialog(self, external=True): options = QFileDialog.Options() fileName, filtr = QFileDialog.getOpenFileName(self, config.thisTranslation["html_open"], self.parent.openFileNameLabel.text(), "JPG Files (*.jpg);;JPEG Files (*.jpeg);;PNG Files (*.png);;GIF Files (*.gif);;BMP Files (*.bmp);;All Files (*)", "", options) if fileName: if external: self.linkExternalImage(fileName) else: self.embedImage(fileName) def embedImage(self, fileName): name, extension = os.path.splitext(os.path.basename(fileName)) with open(fileName, "rb") as fileObject: binaryData = fileObject.read() encodedData = base64.b64encode(binaryData) asciiString = encodedData.decode('ascii') imageTag = '<img src="data:image/{2};base64,{0}" alt="{1}">'.format(asciiString, name, extension[1:]) if self.html: self.editor.insertHtml(imageTag) else: self.editor.insertPlainText(imageTag) def exportNoteImages(self): options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly directory = QFileDialog.getExistingDirectory(self, config.thisTranslation["select_a_folder"], self.parent.directoryLabel.text(), options) if directory: if self.html: htmlText = self.editor.toHtml() else: htmlText = self.editor.toPlainText() searchPattern = r'src=(["{0}])data:image/([^<>]+?);[ ]*?base64,[ ]*?([^ <>]+?)\1'.format("'") for counter, value in enumerate(re.findall(searchPattern, htmlText)): *_, ext, asciiString = value binaryString = asciiString.encode("ascii") binaryData = base64.b64decode(binaryString) imageFilePath = os.path.join(directory, "image{0}.{1}".format(counter + 1, ext)) with open(imageFilePath, "wb") as fileObject: fileObject.write(binaryData) def linkExternalImage(self, fileName): imageTag = '<img src="{0}" alt="UniqueBible.app">'.format(fileName) if self.html: self.editor.insertHtml(imageTag) else: self.editor.insertPlainText(imageTag) def openHyperlinkDialog(self): selectedText = self.editor.textCursor().selectedText() if selectedText: text, ok = QInputDialog.getText(self, "UniqueBible.app", config.thisTranslation["noteTool_hyperlink"], QLineEdit.Normal, selectedText) if ok and text != '': hyperlink = '<a href="{0}">{1}</a>'.format(text, selectedText) hyperlink = """<span style="font-family:'{0}'; font-size:{1}pt;">{2}</span>""".format(config.font, config.fontSize, hyperlink) if self.html: self.editor.insertHtml(hyperlink) else: self.editor.insertPlainText(hyperlink) else: self.selectTextFirst() def setupTextUtility(self): self.ttsToolbar = QToolBar() self.ttsToolbar.setWindowTitle(config.thisTranslation["noteTool_title"]) self.ttsToolbar.setContextMenuPolicy(Qt.PreventContextMenu) # self.toolBar can be treated as an individual widget and positioned with a specified layout # In QMainWindow, the following line adds the configured QToolBar as part of the toolbar of the main window self.addToolBar(self.ttsToolbar) self.languageCombo = QComboBox() self.ttsToolbar.addWidget(self.languageCombo) if config.espeak: languages = TtsLanguages().isoLang2epeakLang else: languages = TtsLanguages().isoLang2qlocaleLang self.languageCodes = list(languages.keys()) for code in self.languageCodes: self.languageCombo.addItem(languages[code][1]) # Check if selected tts engine has the language user specify. if not (config.ttsDefaultLangauge in self.languageCodes): config.ttsDefaultLangauge = "en" # Set initial item initialIndex = self.languageCodes.index(config.ttsDefaultLangauge) self.languageCombo.setCurrentIndex(initialIndex) button = QPushButton(config.thisTranslation["speak"]) button.setToolTip(config.thisTranslation["speak"]) button.clicked.connect(self.speakText) self.ttsToolbar.addWidget(button) button = QPushButton(config.thisTranslation["stop"]) button.setToolTip(config.thisTranslation["stop"]) button.clicked.connect(self.parent.textCommandParser.stopTtsAudio) self.ttsToolbar.addWidget(button) self.translateToolbar = QToolBar() self.translateToolbar.setWindowTitle(config.thisTranslation["noteTool_title"]) self.translateToolbar.setContextMenuPolicy(Qt.PreventContextMenu) # self.toolBar can be treated as an individual widget and positioned with a specified layout # In QMainWindow, the following line adds the configured QToolBar as part of the toolbar of the main window self.addToolBar(self.translateToolbar) self.fromLanguageCombo = QComboBox() self.translateToolbar.addWidget(self.fromLanguageCombo) self.fromLanguageCombo.addItems(["[Auto]"] +Translator.fromLanguageNames) initialIndex = 0 self.fromLanguageCombo.setCurrentIndex(initialIndex) button = QPushButton(config.thisTranslation["context1_translate"]) button.setToolTip(config.thisTranslation["context1_translate"]) button.clicked.connect(self.translateText) self.translateToolbar.addWidget(button) self.toLanguageCombo = QComboBox() self.translateToolbar.addWidget(self.toLanguageCombo) self.toLanguageCombo.addItems(Translator.toLanguageNames) initialIndex = Translator.toLanguageNames.index(config.userLanguage) self.toLanguageCombo.setCurrentIndex(initialIndex) def speakText(self): text = self.editor.textCursor().selectedText() if text: if config.isTtsInstalled: if ":::" in text: text = text.split(":::")[-1] command = "SPEAK:::{0}:::{1}".format(self.languageCodes[self.languageCombo.currentIndex()], text) self.parent.runTextCommand(command) else: self.displayMessage(config.thisTranslation["message_noSupport"]) else: self.selectTextFirst() def translateText(self): text = self.editor.textCursor().selectedText() if text: translator = Translator() if translator.language_translator is not None: fromLanguage = Translator.fromLanguageCodes[self.fromLanguageCombo.currentIndex() - 1] if self.fromLanguageCombo.currentIndex() != 0 else translator.identify(text) toLanguage = Translator.toLanguageCodes[self.toLanguageCombo.currentIndex()] result = translator.translate(text, fromLanguage, toLanguage) self.editor.insertPlainText(result) else: self.displayMessage(config.thisTranslation["ibmWatsonNotEnalbed"]) webbrowser.open("https://github.com/eliranwong/UniqueBible/wiki/IBM-Watson-Language-Translator") else: self.selectTextFirst() def selectTextFirst(self): self.displayMessage(config.thisTranslation["selectTextFirst"]) def displayMessage(self, message="", title="UniqueBible"): reply = QMessageBox.information(self, title, message)
class MappingWidget(CustomWidget): def __init__(self, *args, **kwargs): super(MappingWidget, self).__init__(*args, **kwargs) self.move(WINDOWDIMS_MAPPING[0], WINDOWDIMS_MAPPING[1]) self.resize(WINDOWDIMS_MAPPING[2], WINDOWDIMS_MAPPING[3]) # configure lsl stream outlet_info = pylsl.StreamInfo(name='sensorimotor_mapping', type='map', channel_count=1, nominal_srate=pylsl.IRREGULAR_RATE, channel_format=pylsl.cf_string, source_id='mapping1214') self.map_stream = pylsl.StreamOutlet(outlet_info) def create_control_panel(self): pass def create_plots(self, theme='dark', **kwargs): bold_font = QFont() bold_font.setPixelSize(12) bold_font.setBold(True) self.layout().addStretch() self.layout().addWidget(QLabel("Channels: ", font=bold_font)) self.layout().addSpacing(10) # Add check boxes for channels self.channels = {} for chan_ix in range(len(self.group_info)): chan_lbl = self.group_info[chan_ix]['label'] chk_box = QCheckBox(chan_lbl) chk_box.stateChanged.connect(self.check_channel_and_stim) self.channels[chan_lbl] = chk_box self.layout().addWidget(chk_box) self.layout().addSpacing(10) bt_clear = QPushButton("Uncheck all") bt_clear.clicked.connect(lambda: self.uncheck_all(self.channels)) self.layout().addWidget(bt_clear) self.layout().addSpacing(20) # Stimuli types self.layout().addWidget(QLabel("Stimuli: ", font=bold_font)) self.layout().addSpacing(10) self.mapping_stimuli = {} for stim in MAPPINGSTIMULI: self.mapping_stimuli[stim] = QCheckBox(stim) self.mapping_stimuli[stim].stateChanged.connect( self.check_channel_and_stim) self.layout().addWidget(self.mapping_stimuli[stim]) self.mapping_stimuli['Custom'] = QCheckBox() l = QHBoxLayout() self.custom_stimulus = QLineEdit("Custom") l.addWidget(self.mapping_stimuli['Custom']) l.addSpacing(3) l.addWidget(self.custom_stimulus) self.layout().addLayout(l) self.layout().addSpacing(10) bt_clear = QPushButton("Uncheck all") bt_clear.clicked.connect( lambda: self.uncheck_all(self.mapping_stimuli)) self.layout().addWidget(bt_clear) self.layout().addSpacing(20) # side self.layout().addWidget(QLabel("Body Side: ", font=bold_font)) self.layout().addSpacing(10) self.sides = {'Left': QCheckBox("Left"), 'Right': QCheckBox("Right")} l = QHBoxLayout() l.addWidget(self.sides['Left']) l.addWidget(self.sides['Right']) self.layout().addLayout(l) self.layout().addSpacing(20) # Body part self.layout().addWidget(QLabel("Limb: ", font=bold_font)) body_widget = QWidget(self) body_widget.setLayout(QGridLayout()) body_widget.layout().setContentsMargins(0, 0, 0, 0) lbl = QLabel() lbl.setPixmap( QPixmap( os.path.join(os.path.dirname(__file__), 'icons', 'HalfBody.png'))) body_widget.layout().addWidget(lbl, 0, 0, 20, 10) self.body_parts = {} cb = QCheckBox('') self.body_parts['Head'] = cb body_widget.layout().addWidget(cb, 1, 0, 1, 1) cb = QCheckBox('') self.body_parts['Arm'] = cb body_widget.layout().addWidget(cb, 8, 4, 1, 1) cb = QCheckBox('') self.body_parts['Hand'] = cb body_widget.layout().addWidget(cb, 10, 5, 1, 1) cb = QCheckBox('') self.body_parts['Leg'] = cb body_widget.layout().addWidget(cb, 14, 1, 1, 1) cb = QCheckBox('') self.body_parts['Foot'] = cb body_widget.layout().addWidget(cb, 18, 1, 1, 1) bt_clear = QPushButton("Uncheck all") bt_clear.clicked.connect(lambda: self.uncheck_all(self.body_parts)) body_widget.layout().addWidget(bt_clear, 20, 0, 1, 10) self.layout().addWidget(body_widget) self.layout().addSpacing(20) self.bt_map = QPushButton("Submit Response") self.bt_map.setEnabled(False) self.bt_map.setMinimumHeight(40) self.bt_map.clicked.connect(self.submit_map) self.layout().addWidget(self.bt_map) self.layout().addSpacing(10) bt_clear = QPushButton("Clear Channel") bt_clear.setMinimumHeight(20) bt_clear.clicked.connect(self.clear_data) self.layout().addWidget(bt_clear) self.layout().addStretch() # manual notes self.layout().addWidget(QLabel("Note: ", font=bold_font)) self.note_field = QTextEdit() self.note_field.setMaximumHeight(80) self.note_field.textChanged.connect(self.check_note) self.layout().addWidget(self.note_field) self.layout().addSpacing(10) self.bt_note = QPushButton("Submit Note") self.bt_note.setEnabled(False) self.bt_note.setMinimumHeight(20) self.bt_note.clicked.connect(self.submit_note) self.layout().addWidget(self.bt_note) self.layout().addStretch() def uncheck_all(self, d): for k, v in d.items(): v.setChecked(False) def check_channel_and_stim(self): if any([x.isChecked() for x in self.channels.values()]) and \ any([x.isChecked() for x in self.mapping_stimuli.values()]): self.bt_map.setEnabled(True) else: self.bt_map.setEnabled(False) def check_note(self): tmp = self.note_field.toPlainText() if len(tmp) > 0: self.bt_note.setEnabled(True) else: self.bt_note.setEnabled(False) def submit_map(self): out_dict = {} for chan in [s for s, c in self.channels.items() if c.isChecked()]: side = [s for s, c in self.sides.items() if c.isChecked()] if not side: side = ['Unspecified'] limb = [l for l, c in self.body_parts.items() if c.isChecked()] if not limb: limb = ['Unspecified'] out_dict[chan] = { 'Stimuli': [s for s, c in self.mapping_stimuli.items() if c.isChecked()], 'Sides': side, 'Limbs': limb, } out_string = json.dumps(out_dict) self.map_stream.push_sample([out_string]) # stims = '' # for stim, cb in self.mapping_stimuli.items(): # if cb.isChecked(): # if stim == "Custom": # stims += self.custom_stimulus.text() + ',' # else: # stims += stim + ',' # stims = stims.rstrip(',') # # limbs = '' # for limb, cb in self.body_parts.items(): # if cb.isChecked(): # limbs += limb + ',' # limbs = limbs.rstrip(',') # # for lbl, chan in self.channels.items(): # if chan.isChecked(): # if stims != '' and limbs != '': # self.map_stream.push_sample([lbl, stims + '__' + limbs + ';']) # else: # self.map_stream.push_sample([lbl, 'Clear']) def submit_note(self): out_string = self.note_field.toPlainText() self.map_stream.push_sample([out_string]) self.note_field.setPlainText("") def clear_data(self): out_dict = {} for chan in [s for s, c in self.channels.items() if c.isChecked()]: out_dict[chan] = 'Clear' out_string = json.dumps(out_dict) self.map_stream.push_sample([out_string]) def refresh_axes(self): pass def clear(self): pass
class SubjectWidget(QWidget): subject_change = Signal(int) def __init__(self, subject_settings): super(SubjectWidget, self).__init__() self.subject_enums = DBWrapper().return_enums('subject') subject_layout = QGridLayout(self) subject_layout.setColumnMinimumWidth(2, 60) subject_layout.addWidget(QLabel("Id: "), 0, 0, 1, 1) self.id_combo = QComboBox() self.id_combo.setEditable(True) self.id_combo.addItem('') self.id_combo.addItems(DBWrapper().list_all_subjects()) self.id_combo.currentIndexChanged.connect(self.load_subject) self.id_combo.lineEdit().editingFinished.connect(self.check_subject) subject_layout.addWidget(self.id_combo, 0, 1, 1, 4) subject_layout.addWidget(QLabel("Name: "), 1, 0, 1, 1) self.name_edit = QLineEdit() self.name_edit.setMaxLength(135) subject_layout.addWidget(self.name_edit, 1, 1, 1, 4) subject_layout.addWidget(QLabel("Sex: "), 2, 0, 1, 1) self.sex_combo = QComboBox() self.sex_combo.addItems(self.subject_enums['sex'] if 'sex' in self.subject_enums.keys() else []) self.sex_combo.setCurrentIndex(0) subject_layout.addWidget(self.sex_combo, 2, 1, 1, 1) subject_layout.addWidget((QLabel("Date of birth: ")), 3, 0, 1, 1) self.dob_calendar = QCalendarWidget() subject_layout.addWidget(self.dob_calendar, 3, 1, 1, 3) subject_layout.addWidget(QLabel("NSP file comment: "), 4, 0, 1, 1) self.file_comment = QTextEdit("") self.file_comment.setMaximumHeight(150) subject_layout.addWidget(self.file_comment, 4, 1, 1, 4) # Subject Settings self.subject_settings = subject_settings if not self.subject_settings: self.update_settings_from_db(-1) self.update_subject() def update_subject(self): self.name_edit.setText( self.read_dict_value(self.subject_settings, 'name')) self.id_combo.setCurrentText( self.read_dict_value(self.subject_settings, 'id')) self.sex_combo.setCurrentText( self.read_dict_value(self.subject_settings, 'sex')) dob = self.read_dict_value(self.subject_settings, 'birthday') if dob not in [None, '']: q_dob = QDate.fromString(dob, 'yyyy-MM-d') self.dob_calendar.setSelectedDate(q_dob) else: self.dob_calendar.setSelectedDate(QDate.currentDate()) def update_settings_from_db(self, idx): for key, value in DBWrapper().load_subject_details(idx).items(): self.subject_settings[key] = value def load_subject(self): # id is a unique and mandatory field self.check_subject() self.update_subject() def check_subject(self): # when changing the id in the combobox, can be modifying or entering an existing subject id. Check to load data # if so. curr_id = self.id_combo.currentText() if curr_id != '': self.update_settings_from_db(curr_id) self.subject_change.emit(self.subject_settings['subject_id']) else: self.update_settings_from_db(-1) self.subject_change.emit(-1) @staticmethod def read_dict_value(dictionary, value): return str(dictionary[value]) if value in dictionary.keys() else '' def to_dict(self): self.subject_settings['id'] = self.id_combo.currentText() self.subject_settings['name'] = self.name_edit.text() self.subject_settings['sex'] = self.sex_combo.currentText() self.subject_settings['birthday'] = self.dob_calendar.selectedDate( ).toPyDate() self.subject_settings['NSP_comment'] = self.file_comment.toPlainText()
class ModelInfo(QWidget): """A widget that shows infos about the model""" def __init__(self, appdata: CnaData): QWidget.__init__(self) self.appdata = appdata self.layout = QVBoxLayout() label = QLabel("Description") self.layout.addWidget(label) self.description = QTextEdit() self.description.setPlaceholderText("Enter a project description") self.layout.addWidget(self.description) h1 = QHBoxLayout() label = QLabel("Optimization direction") h1.addWidget(label) self.opt_direction = QComboBox() self.opt_direction.insertItem(1, "minimize") self.opt_direction.insertItem(2, "maximize") h1.addWidget(self.opt_direction) self.layout.addItem(h1) self.setLayout(self.layout) self.description.textChanged.connect(self.description_changed) self.opt_direction.currentTextChanged.connect( self.opt_direction_changed) self.update() def update(self): if "description" in self.appdata.project.meta_data: description = self.appdata.project.meta_data["description"] else: description = "" self.description.textChanged.disconnect(self.description_changed) self.description.setText(description) self.description.textChanged.connect(self.description_changed) x = self.appdata.project.cobra_py_model.objective_direction self.opt_direction.currentTextChanged.disconnect( self.opt_direction_changed) if x == "max": self.opt_direction.setCurrentIndex(1) elif x == "min": self.opt_direction.setCurrentIndex(0) self.opt_direction.currentTextChanged.connect( self.opt_direction_changed) def description_changed(self): self.appdata.project.meta_data[ "description"] = self.description.toPlainText() self.appdata.window.unsaved_changes() def opt_direction_changed(self): if self.opt_direction.currentIndex() == 0: self.appdata.project.cobra_py_model.objective_direction = "min" self.optimizationDirectionChanged.emit("min") if self.opt_direction.currentIndex() == 1: self.appdata.project.cobra_py_model.objective_direction = "max" self.optimizationDirectionChanged.emit("max") optimizationDirectionChanged = Signal(str)
class ErrorDialog(QDialog): """ Dialog to present user the exception information. User can send error report (possible to add custom information) """ def __init__(self, exception: Exception, description: str, additional_notes: str = "", additional_info=None): super().__init__() self.exception = exception self.additional_notes = additional_notes self.send_report_btn = QPushButton("Send information") self.send_report_btn.setDisabled(not state_store.report_errors) self.cancel_btn = QPushButton("Cancel") self.error_description = QTextEdit() self.traceback_summary = additional_info if additional_info is None: self.error_description.setText("".join( traceback.format_exception(type(exception), exception, exception.__traceback__))) elif isinstance(additional_info, traceback.StackSummary): self.error_description.setText("".join(additional_info.format())) elif isinstance(additional_info[1], traceback.StackSummary): self.error_description.setText("".join( additional_info[1].format())) self.error_description.append(str(exception)) self.error_description.setReadOnly(True) self.additional_info = QTextEdit() self.contact_info = QLineEdit() self.user_name = QLineEdit() self.cancel_btn.clicked.connect(self.reject) self.send_report_btn.clicked.connect(self.send_information) layout = QVBoxLayout() self.desc = QLabel(description) self.desc.setWordWrap(True) info_text = QLabel( "If you see these dialog it not means that you do something wrong. " "In such case you should see some message box not error report dialog." ) info_text.setWordWrap(True) layout.addWidget(info_text) layout.addWidget(self.desc) layout.addWidget(self.error_description) layout.addWidget(QLabel("Contact information")) layout.addWidget(self.contact_info) layout.addWidget(QLabel("User name")) layout.addWidget(self.user_name) layout.addWidget(QLabel("Additional information from user:"******"Sending reports was disabled by runtime flag. " "You can report it manually by creating report on " "https://github.com/4DNucleome/PartSeg/issues")) btn_layout = QHBoxLayout() btn_layout.addWidget(self.cancel_btn) btn_layout.addWidget(self.send_report_btn) layout.addLayout(btn_layout) self.setLayout(layout) if isinstance(additional_info, tuple): self.exception_tuple = additional_info[0], None else: exec_info = exc_info_from_error(exception) self.exception_tuple = event_from_exception(exec_info) def exec(self): self.exec_() def exec_(self): """ Check if dialog should be shown base on :py:data:`state_store.show_error_dialog`. If yes then show dialog. Otherwise print exception traceback on stderr. """ # TODO check if this check is needed if not state_store.show_error_dialog: sys.__excepthook__(type(self.exception), self.exception, self.exception.__traceback__) return False super().exec_() def send_information(self): """ Function with construct final error message and send it using sentry. """ with sentry_sdk.push_scope() as scope: text = self.desc.text() + "\n\nVersion: " + __version__ + "\n" if len(self.additional_notes) > 0: scope.set_extra("additional_notes", self.additional_notes) if len(self.additional_info.toPlainText()) > 0: scope.set_extra("user_information", self.additional_info.toPlainText()) if len(self.contact_info.text()) > 0: scope.set_extra("contact", self.contact_info.text()) event, hint = self.exception_tuple event["message"] = text if self.traceback_summary is not None: scope.set_extra("traceback", self.error_description.toPlainText()) event_id = sentry_sdk.capture_event(event, hint=hint) if event_id is None: event_id = sentry_sdk.hub.Hub.current.last_event_id() if len(self.additional_info.toPlainText()) > 0: contact_text = self.contact_info.text() user_name = self.user_name.text() data = { "comments": self.additional_info.toPlainText(), "event_id": event_id, "email": contact_text if _email_regexp.match(contact_text) else "*****@*****.**", "name": user_name or getpass.getuser(), } r = requests.post( url=_feedback_url, data=data, headers={ "Authorization": "DSN https://[email protected]/1309302" }, ) if r.status_code != 200: data["email"] = "*****@*****.**" data["name"] = getpass.getuser() requests.post( url=_feedback_url, data=data, headers={ "Authorization": "DSN https://[email protected]/1309302" }, ) # sentry_sdk.capture_event({"message": text, "level": "error", "exception": self.exception}) self.accept()