def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setupUi() self.findForm = Find(self) self.treeWidget.setStyleSheet(''' .QTreeWidget::item::selected { background-color: rgb(15,95,95); font: bold 24px; } ''') self.treeWidget.customContextMenuRequested.connect(self.contextMenuEvent) self.treeWidget.currentItemChanged.connect(self.showItemContent) self.textEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.textEdit.customContextMenuRequested.connect(self.textMenu) self.textEdit.setMarginWidth(1, 20) self.textEdit.setMarginsBackgroundColor(QtGui.QColor("#333333")) self.textEdit.setMarginsForegroundColor(QtGui.QColor("#CCCCCC")) self.textEdit.markerDefine(QsciScintilla.RightTriangle) self.textEdit.setMarkerBackgroundColor(QtGui.QColor("#ACF39D")) self.actionNew.triggered.connect(self.newDb) self.actionOpen.triggered.connect(self.openFile) self.treeAddNodeAction.triggered.connect(self.addParentItem) self.treeAddSubNodeAction.triggered.connect(self.addSubItem) self.textEdit.setFocusPolicy(QtCore.Qt.StrongFocus) self.textEdit.installEventFilter(self) self.editUndoAction.triggered.connect(self.txtUndo) self.editRedoAction.triggered.connect(self.txtRedo) self.searchAction.triggered.connect(self.findInNode) self.aboutAction.triggered.connect(self.about) self.wordWrapModeAction.triggered.connect(self.wordWrap) self.showLineNumbersAction.triggered.connect(self.showLineNumbers) self.fontBytes = c_char_p() self.fontBytes.value = b'Courier New' self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETSIZE, 0, 12) self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETFONT, self.fontBytes.value) self.textEdit.SendScintilla(QsciScintilla.SCI_SETCODEPAGE, 65001)
def findInNode(self): self.findForm = Find(self) self.findForm.show()
class MyForm(QtGui.QMainWindow, TakeNote): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setupUi() self.findForm = Find(self) self.treeWidget.setStyleSheet(''' .QTreeWidget::item::selected { background-color: rgb(15,95,95); font: bold 24px; } ''') self.treeWidget.customContextMenuRequested.connect(self.contextMenuEvent) self.treeWidget.currentItemChanged.connect(self.showItemContent) self.textEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.textEdit.customContextMenuRequested.connect(self.textMenu) self.textEdit.setMarginWidth(1, 20) self.textEdit.setMarginsBackgroundColor(QtGui.QColor("#333333")) self.textEdit.setMarginsForegroundColor(QtGui.QColor("#CCCCCC")) self.textEdit.markerDefine(QsciScintilla.RightTriangle) self.textEdit.setMarkerBackgroundColor(QtGui.QColor("#ACF39D")) self.actionNew.triggered.connect(self.newDb) self.actionOpen.triggered.connect(self.openFile) self.treeAddNodeAction.triggered.connect(self.addParentItem) self.treeAddSubNodeAction.triggered.connect(self.addSubItem) self.textEdit.setFocusPolicy(QtCore.Qt.StrongFocus) self.textEdit.installEventFilter(self) self.editUndoAction.triggered.connect(self.txtUndo) self.editRedoAction.triggered.connect(self.txtRedo) self.searchAction.triggered.connect(self.findInNode) self.aboutAction.triggered.connect(self.about) self.wordWrapModeAction.triggered.connect(self.wordWrap) self.showLineNumbersAction.triggered.connect(self.showLineNumbers) self.fontBytes = c_char_p() self.fontBytes.value = b'Courier New' self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETSIZE, 0, 12) self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETFONT, self.fontBytes.value) self.textEdit.SendScintilla(QsciScintilla.SCI_SETCODEPAGE, 65001) ### SEARCH START ### def findInNode(self): self.findForm = Find(self) self.findForm.show() def searchScintilla(self, pcharSearchValue, searchStart, searchEnd): ###Clear styling start ### self.textEdit.SendScintilla(QsciScintilla.SCI_STARTSTYLING, 0, 0) self.textEdit.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(self.textEdit.text()), 0) ###Clear styling end ### self.textEdit.SendScintilla(QsciScintilla.SCI_MARKERDELETEALL) cursorPosition = self.textEdit.getCursorPosition() res = 0 while res != -1: self.textEdit.SendScintilla(QsciScintilla.SCI_SETTARGETSTART, res) self.textEdit.SendScintilla(QsciScintilla.SCI_SETTARGETEND, len(self.textEdit.text())) res = self.textEdit.SendScintilla(QsciScintilla.SCI_SEARCHINTARGET, len(pcharSearchValue.value), pcharSearchValue.value) if res != -1: self.textEdit.SendScintilla(QsciScintilla.SCI_SETSELECTIONSTART, res) self.textEdit.SendScintilla(QsciScintilla.SCI_SETSELECTIONEND, res+len(pcharSearchValue.value)) self.textEdit.SendScintilla(QsciScintilla.SCI_MARKERADD, self.textEdit.lineIndexFromPosition(res)[0]) res += len(pcharSearchValue.value) self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETSIZE, 12, 12) self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 12, self.fontBytes.value) self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETBACK, 12, 2042550) self.textEdit.SendScintilla(QsciScintilla.SCI_STYLESETFORE, 12, 000) self.textEdit.SendScintilla(QsciScintilla.SCI_STARTSTYLING, res-len(pcharSearchValue.value), 12) self.textEdit.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(pcharSearchValue.value), 12) self.textEdit.setCursorPosition(cursorPosition[0], cursorPosition[1]) self.textEdit.SendScintilla(QsciScintilla.SCI_SETTARGETSTART, searchStart) self.textEdit.SendScintilla(QsciScintilla.SCI_SETTARGETEND, searchEnd) res = self.textEdit.SendScintilla(QsciScintilla.SCI_SEARCHINTARGET, len(pcharSearchValue.value), pcharSearchValue.value) if res != -1: self.textEdit.SendScintilla(QsciScintilla.SCI_SETSELECTIONSTART, res) self.textEdit.SendScintilla(QsciScintilla.SCI_SETSELECTIONEND, res+len(pcharSearchValue.value)) else: print('no more') ### SEARCH STOP ### ### EXTENDING THE STANDART CONTEXT MENU START ### def textMenu(self, position): editMenu = self.textEdit.createStandardContextMenu() editMenu.addSeparator() upperAction = editMenu.addAction(self.tr("To UPPER Case")) lowerAction = editMenu.addAction(self.tr("To lower case")) textAction = editMenu.exec_(self.textEdit.viewport().mapToGlobal(position)) if textAction == upperAction: self.upperCase() if textAction == lowerAction: self.lowerCase() ### EXTENDING THE STANDART CONTEXT MENU STOP ### ### UPPER/LOWER CASE START ### def upperCase(self): self.textEdit.SendScintilla(QsciScintillaBase.SCI_UPPERCASE) def lowerCase(self): self.textEdit.SendScintilla(QsciScintillaBase.SCI_LOWERCASE) ### UPPER/LOWER CASE STOP ### ### SET WORD WRAP START ### def wordWrap(self): if self.wordWrapModeAction.isChecked(): self.textEdit.SendScintilla(QsciScintillaBase.SCI_SETWRAPMODE, 1) else: self.textEdit.SendScintilla(QsciScintillaBase.SCI_SETWRAPMODE, 0) ### SET WORD WRAP STOP ### ### SET SHOW LINE NUMBERS START ### def showLineNumbers(self): if self.showLineNumbersAction.isChecked(): ## Margins colors # line numbers margin self.textEdit.setMarginWidth(1, 35) self.textEdit.setMarginLineNumbers(1, True) else: self.textEdit.setMarginWidth(1, 20) self.textEdit.setMarginLineNumbers(1, False) ### SET SHOW LINE NUMBERS STOP ### ### CREATING TREE WIDGET CONTEXT MENU START ### def contextMenuEvent(self, eventmenu): menu = QMenu(self) addNodeAction = menu.addAction("Add Node") addSubNodeAction = menu.addAction("Add Subnode") renameAction = menu.addAction("Rename") deleteAction = menu.addAction("Delete") action = menu.exec_(self.mapToGlobal(eventmenu.pos())) if action == addNodeAction: self.addParentItem() if action == addSubNodeAction: self.addSubItem() if action == renameAction: self.renameItem() if action == deleteAction: self.deleteItem() ### CREATING TREE WIDGET CONTEXT MENU STOP ### ### DB VARIABLES START ### def tables(self, sqliteUrl): db = create_engine(sqliteUrl, echo=False) self.metadata = MetaData(db) self.nodes = Table('node', self.metadata, Column('node_id', Integer, unique=True), Column('name', UnicodeText), Column('txt', UnicodeText), ) self.children = Table('children', self.metadata, Column('node_id', Integer, unique=True), Column('name', UnicodeText), Column('txt', UnicodeText), Column('father_id', Integer), ) ### DB VARIABLES STOP ### ### CREATING NEW DATABASE START ### def newDb(self): files_types = "DB (*.db)" self.filename, filter = QtGui.QFileDialog.getSaveFileNameAndFilter(self, 'Save file', '.db', files_types) sqliteUrl = ('sqlite:///%s' % self.filename) self.tables(sqliteUrl) self.metadata.create_all() self.redraw() self.setWindowTitle('%s - TakeNote' % self.filename) parserVal = MainCfg() parserVal.writeCfg(sectionValue=self.filename) ### CREATING NEW DATABASE STOP ### ### OPEN DB START ### def openFile(self): files_types = "DB (*.db)" self.filename, filter = QtGui.QFileDialog.getOpenFileNameAndFilter(self, 'Save file', '.db', files_types) sqliteUrl = ('sqlite:///%s' % self.filename) self.tables(sqliteUrl) self.redraw() self.setWindowTitle('%s - TakeNote' % self.filename) parseVal = MainCfg() parseVal.writeCfgUpdateValue('MAIN', 'default_db', self.filename) self.treeWidget.setCurrentItem(self.treeWidget.topLevelItem(0)) self.textEdit.hasFocus() ### OPEN DB STOP ### ### REDRAW TREE START ### def redraw(self): self.treeWidget.clear() s = select([self.nodes]) result = s.execute() for row in result: self.parentItem = QTreeWidgetItem() self.treeWidget.addTopLevelItem(self.parentItem) self.parentItem.setText(0, str(row[1])) self.parentItem.setStatusTip(1, str(row[0])) self.parentItem.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled) s1 = select([self.children], self.children.c.father_id == row[0]) result1 = s1.execute() for k in result1: self.childItem = QTreeWidgetItem() self.childItem = QTreeWidgetItem(self.parentItem) self.childItem.setText(0, k[1]) self.childItem.setStatusTip(1, str(k[0])) self.childItem.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled) self.parentItem.setExpanded(0) self.indexOfChildItem = self.parentItem.indexOfChild(self.childItem) ### REDRAW TREE START ### ### SHOW ITEM CONTENT START ### def showItemContent(self): try: self.treeWidget.currentItem().parent().indexOfChild(self.treeWidget.currentItem()) except: parentUniqueID = self.treeWidget.currentItem().statusTip(1) s1 = select([self.nodes.c.txt], self.nodes.c.node_id == parentUniqueID) currentParentItemContent = s1.execute() self.textEdit.setText(str(currentParentItemContent.fetchone()[0])) ### CURRENT LINE POS START ### for x in currentLinePos.keys(): if self.treeWidget.currentItem().text(0) == x: self.textEdit.setCursorPosition(currentLinePos[x], 0) ### CURRENT LINE POS END ### else: parentUniqueID2 = self.treeWidget.currentItem().parent().statusTip(1) childIndex = self.treeWidget.currentItem().statusTip(1) s = select([self.children.c.txt], self.children.c.node_id == childIndex and self.children.c.father_id == parentUniqueID2) currentChildItemContent = s.execute() self.textEdit.setText(str(currentChildItemContent.fetchone()[0])) ### CURRENT LINE POS START ### for x in currentLinePos.keys(): if self.treeWidget.currentItem().text(0) == x: self.textEdit.setCursorPosition(currentLinePos[x], 0) ### CURRENT LINE POS END ### ### SHOW ITEM CONTENT STOP ### ### EVENT FILTER START ### def eventFilter(self, obj, event): if event.type() == QtCore.QEvent.FocusOut: ### CURRENT LINE POS COLLECT START ### cursorPos = self.textEdit.getCursorPosition() currentLinePos[self.treeWidget.currentItem().text(0)] = cursorPos[0] ### CURRENT LINE POS COLLECT END ### try: self.treeWidget.currentItem().parent().indexOfChild(self.treeWidget.currentItem()) except: parentUniqueID = self.treeWidget.currentItem().statusTip(1) upd = update(self.nodes).where(self.nodes.c.node_id == parentUniqueID).values(txt=self.textEdit.text()) upd.execute() else: parentUniqueID2 = self.treeWidget.currentItem().parent().statusTip(1) childIndex = self.treeWidget.currentItem().statusTip(1) upd1 = update(self.children).where(self.children.c.node_id == childIndex and self.children.c.father_id == parentUniqueID2).values(txt=self.textEdit.text()) upd1.execute() return True return False ### EVENT FILTER STOP ### ### DEFINING NODE/SUBNODE NAME START ### def nodeName(self, textToShow): text, ok = QtGui.QInputDialog.getText(self, 'Name ', 'Node Name: ', QtGui.QLineEdit.Normal, textToShow) if ok and text != '': self.inputNodeName = text return self.inputNodeName ### DEFINING NODE/SUBNODE NAME STOP ### ### ADD PARENT ITEM START ### def addParentItem(self): self.nodeName(textToShow='') self.label = self.inputNodeName maxIdTmp = select([func.max(self.nodes.c.node_id)]) run = maxIdTmp.execute() fetchTmp = run.fetchone()[0] if type(fetchTmp) is int: maxId = fetchTmp+1 else: maxId = 0 self.nodes.insert().execute(name=self.label, node_id=maxId, txt="") self.redraw() self.treeWidget.setCurrentItem(self.parentItem) self.textEdit.clear() self.textEdit.setFocus() del self.inputNodeName ### ADD PARENT ITEM STOP ### ### ADD SUB ITEM START ### def addSubItem(self): if self.treeWidget.currentItem().parent() is None: parentId = self.treeWidget.currentItem().statusTip(1) indexOfSelectedParent = self.treeWidget.indexFromItem(self.treeWidget.currentItem()).row() else: parentId = self.treeWidget.currentItem().parent().statusTip(1) indexOfSelectedParent = self.treeWidget.indexFromItem(self.treeWidget.currentItem().parent()).row() self.nodeName(textToShow='') self.label = self.inputNodeName maxIdTmp = select([func.max(self.children.c.node_id)]) run = maxIdTmp.execute() fetchTmp = run.fetchone()[0] if type(fetchTmp) is int: maxId = fetchTmp+1 else: maxId = 0 self.children.insert().execute(name=self.label, node_id=maxId, txt="", father_id=parentId) model = self.treeWidget.model() self.redraw() indexItem = model.index(indexOfSelectedParent, 0) childNumber = self.treeWidget.itemFromIndex(indexItem).childCount() self.treeWidget.setCurrentItem(self.treeWidget.itemFromIndex(indexItem).child(childNumber-1)) self.textEdit.setFocus() self.textEdit.clear() del self.inputNodeName ### ADD SUB ITEM STOP ### ### RENAME ITEM START ### def renameItem(self): self.nodeName(textToShow=self.treeWidget.currentItem().text(0)) try: self.treeWidget.currentItem().parent().indexOfChild(self.treeWidget.currentItem()) except: parentUniqueID = self.treeWidget.currentItem().statusTip(1) renNode = update(self.nodes).where(self.nodes.c.node_id == parentUniqueID).values(name=self.inputNodeName) renNode.execute() self.redraw() else: parentUniqueID = self.treeWidget.currentItem().parent().statusTip(0) childIndex = self.treeWidget.currentItem().statusTip(1) renSubNode = update(self.children).where(self.children.c.node_id == childIndex and self.children.c.father_id == parentUniqueID).values(name=self.inputNodeName) renSubNode.execute() self.redraw() del self.inputNodeName ### RENAME ITEM STOP ### ### DELETE ITEM START ### def deleteItem(self): try: self.treeWidget.currentItem().parent().indexOfChild(self.treeWidget.currentItem()) parentIndexOfDeletedSubitem = self.treeWidget.indexFromItem(self.treeWidget.currentItem().parent()).row() deletedSubitemModel = self.treeWidget.model() except: parentUniqueID = self.treeWidget.currentItem().statusTip(1) delNode1 = self.nodes.delete().where(self.nodes.c.node_id == parentUniqueID) delNode2 = self.children.delete().where(self.children.c.father_id == parentUniqueID) delNode1.execute() delNode2.execute() self.textEdit.clear() self.redraw() self.treeWidget.setCurrentItem(self.treeWidget.topLevelItem(0)) else: parentUniqueID = self.treeWidget.currentItem().parent().statusTip(1) childIndex = self.treeWidget.currentItem().statusTip(1) delSub = self.children.delete().where(self.children.c.node_id == childIndex and self.children.c.father_id == parentUniqueID) delSub.execute() self.textEdit.clear() self.redraw() indexDeletedSubitem = deletedSubitemModel.index(parentIndexOfDeletedSubitem, 0) self.treeWidget.setCurrentItem(self.treeWidget.itemFromIndex(indexDeletedSubitem)) self.treeWidget.expand(indexDeletedSubitem) ### DELETE ITEM STOP ### ### UNDO REDO START ### def txtUndo(self): self.textEdit.undo() def txtRedo(self): self.textEdit.redo() ### UNDO REDO STOP ### ### ABOUT START ### def about(self): QMessageBox.about(self, self.tr("About TakeNote"), self.tr("TakeNote\n\n" "%s\n" "%s\n" "%s" % (__author__, __version__, __date__))) ### ABOUT END ### def closeEvent(self, event): parseVal = MainCfg() sizeHeight = self.size().height() sizeWidth = self.size().width() posX = self.pos().x() posY = self.pos().y() parseVal.writeCfgUpdateValue('MAIN', 'geometry', '%s, %s, %s, %s' % (posX, posY, sizeWidth, sizeHeight))