def __init__(self, db): super(KnowledgeModel, self).__init__(None, db) self.setTable("knowledge") self.select() logging.debug("%d rows in KnowledgeModel" % self.rowCount()) self.knowledgeTagsModel = KnowledgeTagsModel(db) self.tagModel = TagModel(db)
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.setWindowTitle("IKnow") # DB-Connection self.dbConnection = DatabaseConnection() if self.dbConnection.connect(): logging.info("Connected to the Database") else: logging.error("Failed to connect to the database") # Create Models self.tagModel = TagModel(self.dbConnection.db) self.knowledgeModel = KnowledgeModel(self.dbConnection.db) # Setup views self.ui.knowledgeTableView.setModel(self.knowledgeModel) self.ui.knowledgeTableView.hideColumn(0) self.ui.knowledgeTableView.setColumnWidth(1, 600) self.ui.knowledgeTableView.setColumnWidth(2, 700) self.ui.tagTreeWidget.setColumnWidth(0, 300) self.ui.tagTreeWidget.setColumnWidth(1, 30) # Load tags self.updateTagWidget() self.currentTag = None self.filterKnowledgeText = "" # Create menus act = QtGui.QAction("Remove selected Tag", self, statusTip="Remove the selected Tag. The child Tags are not touched.", triggered=self.removeSelectedTag) self.ui.tagTreeWidget.addAction(act) self.ui.tagTreeWidget.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) # Connect signals and slots self.ui.tagTreeWidget.currentItemChanged.connect(self.tagChanged) self.ui.newTagButton.clicked.connect(self.showNewTagButtonDialog) self.ui.updateTagsButton.clicked.connect(self.updateTagWidget) self.ui.newKnowledgeButton.clicked.connect(self.showNewKnowledgeDialog) self.ui.filterTagsEdit.textChanged.connect(self.updateTagWidget) self.ui.filterKnowledgeEdit.textChanged.connect(self.filterKnowledgeByText) self.ui.knowledgeTableView.doubleClicked.connect(self.showEditKnowledgeDialog)
class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.setWindowTitle("IKnow") # DB-Connection self.dbConnection = DatabaseConnection() if self.dbConnection.connect(): logging.info("Connected to the Database") else: logging.error("Failed to connect to the database") # Create Models self.tagModel = TagModel(self.dbConnection.db) self.knowledgeModel = KnowledgeModel(self.dbConnection.db) # Setup views self.ui.knowledgeTableView.setModel(self.knowledgeModel) self.ui.knowledgeTableView.hideColumn(0) self.ui.knowledgeTableView.setColumnWidth(1, 600) self.ui.knowledgeTableView.setColumnWidth(2, 700) self.ui.tagTreeWidget.setColumnWidth(0, 300) self.ui.tagTreeWidget.setColumnWidth(1, 30) # Load tags self.updateTagWidget() self.currentTag = None self.filterKnowledgeText = "" # Create menus act = QtGui.QAction("Remove selected Tag", self, statusTip="Remove the selected Tag. The child Tags are not touched.", triggered=self.removeSelectedTag) self.ui.tagTreeWidget.addAction(act) self.ui.tagTreeWidget.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) # Connect signals and slots self.ui.tagTreeWidget.currentItemChanged.connect(self.tagChanged) self.ui.newTagButton.clicked.connect(self.showNewTagButtonDialog) self.ui.updateTagsButton.clicked.connect(self.updateTagWidget) self.ui.newKnowledgeButton.clicked.connect(self.showNewKnowledgeDialog) self.ui.filterTagsEdit.textChanged.connect(self.updateTagWidget) self.ui.filterKnowledgeEdit.textChanged.connect(self.filterKnowledgeByText) self.ui.knowledgeTableView.doubleClicked.connect(self.showEditKnowledgeDialog) def removeSelectedTag(self): selectedItem = self.ui.tagTreeWidget.selectedItems()[0] IDtoRemove = int(selectedItem.text(1)) logging.debug("Remove selected Tag (ID=%d)." % IDtoRemove) self.tagModel.removeTag(IDtoRemove) self.updateTagWidget() def updateTagWidget(self): self.tagModel.updateTree() logging.debug("filter-Edit=%s" % self.ui.filterTagsEdit.text()) filter = self.ui.filterTagsEdit.text() if filter == "": filterIDs = None else: foundIDs = self.tagModel.getIDsFilteredByName(filter) logging.debug("foundIDs=" + str(foundIDs)) filterIDs = [] filterIDs.extend(foundIDs) for ID in foundIDs: logging.debug("foundIDs=%s" % str(foundIDs)) filterIDs.extend(self.tagModel.getParentIDsDownToRoot(ID)) filterIDs.extend(self.tagModel.getAllChildIDs(ID)) filterIDs = set(filterIDs) logging.debug("filterIDs=" + str(filterIDs)) # Clear tree self.ui.tagTreeWidget.clear() # No filter applied => Show all tags if filter == "": self.tagModel.fillTreeWidgetWithTags(self.ui.tagTreeWidget) # Found tags with applied filter if filter != "" and len(foundIDs) > 0: self.tagModel.fillTreeWidgetWithTags(self.ui.tagTreeWidget, filterIDs=filterIDs) self.ui.tagTreeWidget.expandAll() def showNewTagButtonDialog(self): logging.debug("Show NewTagDialog") newTagDlg = NewTagDialog(self, self.tagModel, self.tagModel.tagParentsModel, parentID=self.currentTag) newTagDlg.exec_() self.updateTagWidget() def showNewKnowledgeDialog(self): logging.debug("Show NewKnowledgeDialog") newKnowledgeDlg = NewKnowledgeDialog(self, self.tagModel, self.tagModel.tagParentsModel, self.knowledgeModel, parentID=self.currentTag) newKnowledgeDlg.exec_() self.reloadKnowledge() def showEditKnowledgeDialog(self, modelIndex): logging.debug("Show EditKnowledgeDialog") logging.debug("Row=%d" % modelIndex.row()) newKnowledgeDlg = NewKnowledgeDialog(self, self.tagModel, self.tagModel.tagParentsModel, self.knowledgeModel, editRow=modelIndex.row()) newKnowledgeDlg.exec_() self.knowledgeModel.setFilterByTagID(self.currentTag) def tagChanged(self, current, previous): self.currentTag = int(current.text(1)) logging.debug("currentTag = %d", self.currentTag) self.reloadKnowledge() def filterKnowledgeByText(self, filterText): self.filterKnowledgeText = filterText logging.debug("filterKnowledgeByText: self.filterKnowledgeText=%s" % self.filterKnowledgeText) self.reloadKnowledge() def reloadKnowledge(self): self.knowledgeModel.reload(self.currentTag, self.filterKnowledgeText)
class KnowledgeModel(QtSql.QSqlTableModel): def __init__(self, db): super(KnowledgeModel, self).__init__(None, db) self.setTable("knowledge") self.select() logging.debug("%d rows in KnowledgeModel" % self.rowCount()) self.knowledgeTagsModel = KnowledgeTagsModel(db) self.tagModel = TagModel(db) def setFilterByTagIDsAndText(self, tagIDs, filterText): if len(tagIDs) == 0: filterByTags = "1" else: knowledgeIDs = self.knowledgeTagsModel.getKnowledgeIDsFromTagIDs(tagIDs) logging.debug("getKnowledgeIDsFromTagIDs(%s)=%s" % (str(tagIDs), str(knowledgeIDs))) if len(knowledgeIDs) == 0: filterByTags = "0" else: filterByTags = getFilterFromIDs(knowledgeIDs, "ID") if filterText is not None and filterText != "": filterByText = '(title LIKE "%' + filterText + '%") OR (description LIKE "%' + filterText + '%")' else: filterByText = '' logging.debug('filterByTags="%s"' % filterByTags) logging.debug('filterByText="%s"' % filterByText) if filterByText != "": self.setFilter("(%s) AND (%s)" % (filterByTags, filterByText)) else: self.setFilter(filterByTags) logging.debug('knowledgeModel.filter = "%s"' % self.filter()) self.select() def addNewKnowledge(self, title, description): self.setFilter("") self.setSort(0, QtCore.Qt.SortOrder.AscendingOrder) self.select() record = QtSql.QSqlRecord() record.append(QtSql.QSqlField("ID")) record.append(QtSql.QSqlField("title")) record.append(QtSql.QSqlField("description")) record.append(QtSql.QSqlField("author")) record.setValue(1, title) record.setValue(2, description) record.setValue(3, "jan") if not self.insertRecord(self.rowCount() - 1, record): raise "Knowledge could not be inserted." return int(self.record(self.rowCount() - 1).value(0)) def updateKnowledge(self, row, title, description, newTagIDs): knowledgeID = self.record(row).value("ID") self.setData(self.index(row, self.fieldIndex("title")), title) self.setData(self.index(row, self.fieldIndex("description")), description) self.submitAll() logging.debug("updateKnowledge: knowledgeID=%d" % knowledgeID) oldTagIds = set(self.getTagIDsFromKnowledgeID(knowledgeID)) newTagIDs = set(newTagIDs) tagIDsToAdd = newTagIDs - oldTagIds tagIDsToRemove = oldTagIds - newTagIDs logging.debug("OldTagIDs: %s" % str(oldTagIds)) logging.debug("NewTagIDs: %s" % str(newTagIDs)) logging.debug("TagIDsToAdd: %s" % str(tagIDsToAdd)) logging.debug("TagIDsToRemove: %s" % str(tagIDsToRemove)) for tagID in tagIDsToAdd: self.addTagForKnowledge(knowledgeID, tagID) for tagID in tagIDsToRemove: self.removeTagFromKnowledge(knowledgeID, tagID) def removeTagFromKnowledge(self, knowledgeID, tagID): self.knowledgeTagsModel.removeTagFromKnowledge(knowledgeID, tagID) def addTagForKnowledge(self, knowledgeID, tagID): self.knowledgeTagsModel.addTagForKnowledge(knowledgeID, tagID) def getTagIDsFromKnowledgeID(self, knowledgeID): return self.knowledgeTagsModel.getTagIDsFromKnowledgeID(knowledgeID) def reload(self, currentTag=None, filterText=None): if currentTag is not None: tagIDs = [currentTag] tagIDs.extend(self.tagModel.getAllChildIDs(currentTag)) else: tagIDs = [] logging.debug("reload(): tagIDs = %s" % str(tagIDs)) self.setFilterByTagIDsAndText(tagIDs, filterText) self.select()