def __init__(self, parent=None, State=VIEW): QWidget.__init__(self,parent) self.setupUi(self) self._defaultEditTriggers = self.tvAdminUnits.editTriggers() self._state = State self._onStateChange() self._notifBar = NotificationBar(self.vlNotification) #Configure validating line edit controls # invalidMsg = "{} already exists." # self.txtUnitCode.setModelAttr(AdminSpatialUnitSet,"Code") # self.txtUnitCode.setInvalidMessage(invalidMsg) # self.txtUnitCode.setNotificationBar(self._notifBar) ''' Initialize formatter for the rendering the admin unit nodes and insert the root node into the tree view model. ''' self._adminUnitNodeFormatter = AdminUnitFormatter( self.tvAdminUnits, self ) self._rtNode = self._adminUnitNodeFormatter.rootNode self._adminUnitTreeModel = STRTreeViewModel( self._adminUnitNodeFormatter.root(), view=self.tvAdminUnits ) self.tvAdminUnits.setModel(self._adminUnitTreeModel) self.tvAdminUnits.hideColumn(2) self.tvAdminUnits.setColumnWidth(0,220) #Connects slots self.connect(self.btnAdd, SIGNAL("clicked()"), self.onCreateAdminUnit) self.connect(self.btnClear, SIGNAL("clicked()"), self.onClearSelection) self.connect(self.btnRemove, SIGNAL("clicked()"), self.onDeleteSelection) self.connect(self._adminUnitTreeModel, SIGNAL("dataChanged(const QModelIndex&,const QModelIndex&)"), self.onModelDataChanged)
def _load_root_node(self, root): """ Load the search results (formatted into an object of type 'stdm.navigaion.STR') into the tree view. """ strTreeViewModel = STRTreeViewModel(root, view=self.tvSTRResults) self.tvSTRResults.setModel(strTreeViewModel) # Resize tree columns to fit contents self._resize_columns() #Capture selection changes signals when # results are returned in the tree view resultsSelModel = self.tvSTRResults.selectionModel() resultsSelModel.currentChanged.connect(self.on_select_results)
def __init__(self, parent=None, State=VIEW): QWidget.__init__(self,parent) self.setupUi(self) self._defaultEditTriggers = self.tvAdminUnits.editTriggers() self._state = State self._onStateChange() self._notifBar = NotificationBar(self.vlNotification) #Configure validating line edit controls # invalidMsg = "{} already exists." # self.txtUnitCode.setModelAttr(AdminSpatialUnitSet,"Code") # self.txtUnitCode.setInvalidMessage(invalidMsg) # self.txtUnitCode.setNotificationBar(self._notifBar) ''' Initialize formatter for the rendering the admin unit nodes and insert the root node into the tree view model. ''' self._adminUnitNodeFormatter = AdminUnitFormatter( self.tvAdminUnits, self ) self._rtNode = self._adminUnitNodeFormatter.rootNode self._adminUnitTreeModel = STRTreeViewModel( self._adminUnitNodeFormatter.root(), view=self.tvAdminUnits ) self.tvAdminUnits.setModel(self._adminUnitTreeModel) self.tvAdminUnits.hideColumn(2) self.tvAdminUnits.setColumnWidth(0,220) self.tvAdminUnits.expandAll() #Connects slots self.connect(self.btnAdd, SIGNAL("clicked()"), self.onCreateAdminUnit) self.connect(self.btnClear, SIGNAL("clicked()"), self.onClearSelection) self.connect(self.btnRemove, SIGNAL("clicked()"), self.onDeleteSelection) self.connect(self._adminUnitTreeModel, SIGNAL("dataChanged(const QModelIndex&,const QModelIndex&)"), self.onModelDataChanged)
class AdminUnitManager(QWidget, Ui_frmAdminUnitManager): ''' Administrative Unit Manager Widget ''' #Signal raised when the state (view/manage) of the widet changes. stateChanged = pyqtSignal('bool') def __init__(self, parent=None, State=VIEW): QWidget.__init__(self,parent) self.setupUi(self) self._defaultEditTriggers = self.tvAdminUnits.editTriggers() self._state = State self._onStateChange() self._notifBar = NotificationBar(self.vlNotification) #Configure validating line edit controls # invalidMsg = "{} already exists." # self.txtUnitCode.setModelAttr(AdminSpatialUnitSet,"Code") # self.txtUnitCode.setInvalidMessage(invalidMsg) # self.txtUnitCode.setNotificationBar(self._notifBar) ''' Initialize formatter for the rendering the admin unit nodes and insert the root node into the tree view model. ''' self._adminUnitNodeFormatter = AdminUnitFormatter( self.tvAdminUnits, self ) self._rtNode = self._adminUnitNodeFormatter.rootNode self._adminUnitTreeModel = STRTreeViewModel( self._adminUnitNodeFormatter.root(), view=self.tvAdminUnits ) self.tvAdminUnits.setModel(self._adminUnitTreeModel) self.tvAdminUnits.hideColumn(2) self.tvAdminUnits.setColumnWidth(0,220) self.tvAdminUnits.expandAll() #Connects slots self.connect(self.btnAdd, SIGNAL("clicked()"), self.onCreateAdminUnit) self.connect(self.btnClear, SIGNAL("clicked()"), self.onClearSelection) self.connect(self.btnRemove, SIGNAL("clicked()"), self.onDeleteSelection) self.connect(self._adminUnitTreeModel, SIGNAL("dataChanged(const QModelIndex&,const QModelIndex&)"), self.onModelDataChanged) def model(self): """ :return: Returns the model associated with the administrative unit view. :rtype: STRTreeViewModel """ return self._adminUnitTreeModel def selection_model(self): """ :return: Returns the selection model associated with the administrative unit tree view. :rtype: QItemSelectionModel """ return self.tvAdminUnits.selectionModel() def state(self): ''' Returns the current state that the widget has been configured in. ''' return self._state def setState(self,state): ''' Set the state of the widget. ''' self._state = state self._onStateChange() def notificationBar(self): ''' Returns the application notification widget. ''' return self._notifBar def _onStateChange(self): ''' Configure controls upon changing the state of the widget. ''' manageControls = False if self._state == VIEW else True self.btnRemove.setVisible(manageControls) self.btnClear.setVisible(manageControls) self.gbManage.setVisible(manageControls) if manageControls: self.tvAdminUnits.setEditTriggers(self._defaultEditTriggers) else: self.tvAdminUnits.setEditTriggers(QAbstractItemView.NoEditTriggers) self.stateChanged.emit(manageControls) def onCreateAdminUnit(self): ''' Slot raised on clicking to add a new administrative unit. ''' self._notifBar.clear() if self.txtUnitName.text() == "": msg = QApplication.translate("AdminUnitManager","Name of the administrative unit cannot be empty.") self._notifBar.insertErrorNotification(msg) self.txtUnitName.setFocus() return if not self.txtUnitName.validate(): return if self.txtUnitCode.text() == "": msg = QApplication.translate("AdminUnitManager","Code of the administrative unit cannot be empty.") self._notifBar.insertErrorNotification(msg) self.txtUnitCode.setFocus() return # if not self.txtUnitCode.validate(): # return #Get current row selection selIndexes = self.tvAdminUnits.selectionModel().selectedRows(0) if len(selIndexes) == 0: #Get the number of items in the tree view rootIndex = self.tvAdminUnits.rootIndex() rowCount = self._adminUnitTreeModel.rowCount(rootIndex) if rowCount > 0: msg = QApplication.translate("AdminUnitManager", "You have not selected any parent node for the new administrative unit. Do " \ "you want to add it as one of the topmost administrative units?\nClick Yes to " \ "proceed or No to cancel.") selOption = QMessageBox.warning(self,QApplication.translate("AdminUnitManager","No Parent Node Selected"),msg, QMessageBox.Yes|QMessageBox.No) if selOption == QMessageBox.Yes: parentNode = self._rtNode #We are interested in the model index of the root node parentModelIndex = rootIndex parentModel = None else: return #Do not prompt user and immediately add the administrative unit to the root node. else: parentNode = self._rtNode parentModelIndex = rootIndex parentModel = None else: #Get model index for the first column as this is where the new node will be added as the child parentModelIndex = selIndexes[0] parentNode = self._adminUnitTreeModel._getNode(parentModelIndex) parentID = parentNode.data(2) ausModel = AdminSpatialUnitSet() parentModel = ausModel.queryObject().filter(AdminSpatialUnitSet.id == parentID).first() adminUnitModel = AdminSpatialUnitSet(self.txtUnitName.text(), self.txtUnitCode.text(), parentModel) #Commit transaction to the database adminUnitModel.save() #Extract properties from the model ausProps = self._adminUnitNodeFormatter._extractAdminUnitSetInfo(adminUnitModel) childNode = BaseSTRNode(ausProps, parentNode) #Insert row into the view self._adminUnitTreeModel.insertRows(parentNode.childCount(), 1, parentModelIndex) self.clearInputs() def onClearSelection(self): ''' Slot that removes any existing selections in the tree view. ''' self.tvAdminUnits.selectionModel().clearSelection() def onModelDataChanged(self,oldindex,newindex): ''' Slot raised when the model data is changed. ''' #Get model index containing ID property refNode = self._adminUnitTreeModel._getNode(newindex) ausID = refNode.data(2) ausHandler = AdminSpatialUnitSet() ausObj = ausHandler.queryObject().filter(AdminSpatialUnitSet.id == ausID).first() if ausObj != None: attrColumn = newindex.column() if attrColumn == 0: ausObj.Name = refNode.data(0) elif attrColumn == 1: ausObj.Code = refNode.data(1) ausObj.update() def onDeleteSelection(self): ''' Slot raised to delete current selection of administrative unit. ''' self._notifBar.clear() #Get current row selection selIndexes = self.tvAdminUnits.selectionModel().selectedRows(2) if len(selIndexes) == 0: msg = QApplication.translate("AdminUnitManager", "Please select the administrative unit to delete.") self._notifBar.insertWarningNotification(msg) else: delmsg = QApplication.translate("AdminUnitManager", "This action will delete the selected administrative unit plus any " \ "existing children under it. It cannot be undone.\nClick Yes to " \ "delete or No to cancel.") selOption = QMessageBox.warning(self,QApplication.translate("AdminUnitManager","Confirm deletion"),delmsg, QMessageBox.Yes|QMessageBox.No) if selOption == QMessageBox.Yes: #Get the node in the current selection delIndex = selIndexes[0] ausNode = self._adminUnitTreeModel._getNode(delIndex) ausId = ausNode.data(2) ausHandler = AdminSpatialUnitSet() ausObj = ausHandler.queryObject().filter(AdminSpatialUnitSet.id == ausId).first() if not ausObj is None: ausObj.delete() #Remove item in tree view self._adminUnitTreeModel.removeRows(delIndex.row(), 1, delIndex.parent()) #Notify user self._notifBar.clear() successmsg = QApplication.translate("AdminUnitManager", "Administrative unit successfully deleted.") self._notifBar.insertSuccessNotification(successmsg) def selectedAdministrativeUnit(self): ''' Returns the selected administrative unit object otherwise, if there is no selection then it returns None. ''' selIndexes = self.tvAdminUnits.selectionModel().selectedRows(2) if len(selIndexes) == 0: selAdminUnit = None else: selIndex = selIndexes[0] ausNode = self._adminUnitTreeModel._getNode(selIndex) ausId = ausNode.data(2) ausHandler = AdminSpatialUnitSet() selAdminUnit = ausHandler.queryObject().filter(AdminSpatialUnitSet.id == ausId).first() return selAdminUnit def clearInputs(self): ''' Clears the input controls. ''' self.txtUnitCode.clear() self.txtUnitName.clear()
class AdminUnitManager(WIDGET, BASE): ''' Administrative Unit Manager Widget ''' # Signal raised when the state (view/manage) of the widet changes. stateChanged = pyqtSignal('bool') def __init__(self, parent=None, State=VIEW): QWidget.__init__(self, parent) self.setupUi(self) self.btnRemove.setIcon(GuiUtils.get_icon('remove.png')) self.btnClear.setIcon(GuiUtils.get_icon('reset.png')) self.btnAdd.setIcon(GuiUtils.get_icon('add.png')) self._defaultEditTriggers = self.tvAdminUnits.editTriggers() self._state = State self._onStateChange() self._notifBar = NotificationBar(self.vlNotification) # Configure validating line edit controls # invalidMsg = "{} already exists." # self.txtUnitCode.setModelAttr(AdminSpatialUnitSet,"Code") # self.txtUnitCode.setInvalidMessage(invalidMsg) # self.txtUnitCode.setNotificationBar(self._notifBar) ''' Initialize formatter for the rendering the admin unit nodes and insert the root node into the tree view model. ''' self._adminUnitNodeFormatter = AdminUnitFormatter( self.tvAdminUnits, self) self._rtNode = self._adminUnitNodeFormatter.rootNode self._adminUnitTreeModel = STRTreeViewModel( self._adminUnitNodeFormatter.root(), view=self.tvAdminUnits) self.tvAdminUnits.setModel(self._adminUnitTreeModel) self.tvAdminUnits.hideColumn(2) self.tvAdminUnits.setColumnWidth(0, 220) self.tvAdminUnits.expandAll() # Connects slots self.btnAdd.clicked.connect(self.onCreateAdminUnit) self.btnClear.clicked.connect(self.onClearSelection) self.btnRemove.clicked.connect(self.onDeleteSelection) self._adminUnitTreeModel.dataChanged.connect(self.onModelDataChanged) def model(self): """ :return: Returns the model associated with the administrative unit view. :rtype: STRTreeViewModel """ return self._adminUnitTreeModel def selection_model(self): """ :return: Returns the selection model associated with the administrative unit tree view. :rtype: QItemSelectionModel """ return self.tvAdminUnits.selectionModel() def state(self): ''' Returns the current state that the widget has been configured in. ''' return self._state def setState(self, state): ''' Set the state of the widget. ''' self._state = state self._onStateChange() def notificationBar(self): ''' Returns the application notification widget. ''' return self._notifBar def _onStateChange(self): ''' Configure controls upon changing the state of the widget. ''' manageControls = False if self._state == VIEW else True self.btnRemove.setVisible(manageControls) self.btnClear.setVisible(manageControls) self.gbManage.setVisible(manageControls) if manageControls: self.tvAdminUnits.setEditTriggers(self._defaultEditTriggers) else: self.tvAdminUnits.setEditTriggers(QAbstractItemView.NoEditTriggers) self.stateChanged.emit(manageControls) def onCreateAdminUnit(self): ''' Slot raised on clicking to add a new administrative unit. ''' self._notifBar.clear() if self.txtUnitName.text() == "": msg = QApplication.translate( "AdminUnitManager", "Name of the administrative unit cannot be empty.") self._notifBar.insertErrorNotification(msg) self.txtUnitName.setFocus() return if not self.txtUnitName.validate(): return if self.txtUnitCode.text() == "": msg = QApplication.translate( "AdminUnitManager", "Code of the administrative unit cannot be empty.") self._notifBar.insertErrorNotification(msg) self.txtUnitCode.setFocus() return # if not self.txtUnitCode.validate(): # return # Get current row selection selIndexes = self.tvAdminUnits.selectionModel().selectedRows(0) if len(selIndexes) == 0: # Get the number of items in the tree view rootIndex = self.tvAdminUnits.rootIndex() rowCount = self._adminUnitTreeModel.rowCount(rootIndex) if rowCount > 0: msg = QApplication.translate("AdminUnitManager", "You have not selected any parent node for the new administrative unit. Do " \ "you want to add it as one of the topmost administrative units?\nClick Yes to " \ "proceed or No to cancel.") selOption = QMessageBox.warning( self, QApplication.translate("AdminUnitManager", "No Parent Node Selected"), msg, QMessageBox.Yes | QMessageBox.No) if selOption == QMessageBox.Yes: parentNode = self._rtNode # We are interested in the model index of the root node parentModelIndex = rootIndex parentModel = None else: return # Do not prompt user and immediately add the administrative unit to the root node. else: parentNode = self._rtNode parentModelIndex = rootIndex parentModel = None else: # Get model index for the first column as this is where the new node will be added as the child parentModelIndex = selIndexes[0] parentNode = self._adminUnitTreeModel._getNode(parentModelIndex) parentID = parentNode.data(2) ausModel = AdminSpatialUnitSet() parentModel = ausModel.queryObject().filter( AdminSpatialUnitSet.id == parentID).first() adminUnitModel = AdminSpatialUnitSet(self.txtUnitName.text(), self.txtUnitCode.text(), parentModel) # Commit transaction to the database adminUnitModel.save() # Extract properties from the model ausProps = self._adminUnitNodeFormatter._extractAdminUnitSetInfo( adminUnitModel) childNode = BaseSTRNode(ausProps, parentNode) # Insert row into the view self._adminUnitTreeModel.insertRows(parentNode.childCount(), 1, parentModelIndex) self.clearInputs() def onClearSelection(self): ''' Slot that removes any existing selections in the tree view. ''' self.tvAdminUnits.selectionModel().clearSelection() def onModelDataChanged(self, oldindex, newindex): ''' Slot raised when the model data is changed. ''' # Get model index containing ID property refNode = self._adminUnitTreeModel._getNode(newindex) ausID = refNode.data(2) ausHandler = AdminSpatialUnitSet() ausObj = ausHandler.queryObject().filter( AdminSpatialUnitSet.id == ausID).first() if ausObj != None: attrColumn = newindex.column() if attrColumn == 0: ausObj.Name = refNode.data(0) elif attrColumn == 1: ausObj.Code = refNode.data(1) ausObj.update() def onDeleteSelection(self): ''' Slot raised to delete current selection of administrative unit. ''' self._notifBar.clear() # Get current row selection selIndexes = self.tvAdminUnits.selectionModel().selectedRows(2) if len(selIndexes) == 0: msg = QApplication.translate( "AdminUnitManager", "Please select the administrative unit to delete.") self._notifBar.insertWarningNotification(msg) else: delmsg = QApplication.translate("AdminUnitManager", "This action will delete the selected administrative unit plus any " \ "existing children under it. It cannot be undone.\nClick Yes to " \ "delete or No to cancel.") selOption = QMessageBox.warning( self, QApplication.translate("AdminUnitManager", "Confirm deletion"), delmsg, QMessageBox.Yes | QMessageBox.No) if selOption == QMessageBox.Yes: # Get the node in the current selection delIndex = selIndexes[0] ausNode = self._adminUnitTreeModel._getNode(delIndex) ausId = ausNode.data(2) ausHandler = AdminSpatialUnitSet() ausObj = ausHandler.queryObject().filter( AdminSpatialUnitSet.id == ausId).first() if not ausObj is None: ausObj.delete() # Remove item in tree view self._adminUnitTreeModel.removeRows( delIndex.row(), 1, delIndex.parent()) # Notify user self._notifBar.clear() successmsg = QApplication.translate( "AdminUnitManager", "Administrative unit successfully deleted.") self._notifBar.insertSuccessNotification(successmsg) def selectedAdministrativeUnit(self): ''' Returns the selected administrative unit object otherwise, if there is no selection then it returns None. ''' selIndexes = self.tvAdminUnits.selectionModel().selectedRows(2) if len(selIndexes) == 0: selAdminUnit = None else: selIndex = selIndexes[0] ausNode = self._adminUnitTreeModel._getNode(selIndex) ausId = ausNode.data(2) ausHandler = AdminSpatialUnitSet() selAdminUnit = ausHandler.queryObject().filter( AdminSpatialUnitSet.id == ausId).first() return selAdminUnit def clearInputs(self): ''' Clears the input controls. ''' self.txtUnitCode.clear() self.txtUnitName.clear()