class Giswater(QObject): def __init__(self, iface): ''' Constructor :param iface: An interface instance that will be passed to this class which provides the hook by which you can manipulate the QGIS application at run time. :type iface: QgsInterface ''' super(Giswater, self).__init__() # Save reference to the QGIS interface self.iface = iface self.legend = iface.legendInterface() # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) self.plugin_name = os.path.basename(self.plugin_dir) # initialize locale locale = QSettings().value('locale/userLocale') locale_path = os.path.join(self.plugin_dir, 'i18n', self.plugin_name+'_{}.qm'.format(locale)) if os.path.exists(locale_path): self.translator = QTranslator() self.translator.load(locale_path) if qVersion() > '4.3.3': QCoreApplication.installTranslator(self.translator) # Load local settings of the plugin setting_file = os.path.join(self.plugin_dir, 'config', self.plugin_name+'.config') self.settings = QSettings(setting_file, QSettings.IniFormat) self.settings.setIniCodec(sys.getfilesystemencoding()) # Set controller to handle settings and database self.controller = DaoController(self.settings, self.plugin_name) self.controller.set_database_connection() self.dao = self.controller.getDao() self.schema_name = self.controller.getSchemaName() # Declare instance attributes self.icon_folder = self.plugin_dir+'/icons/' self.actions = {} self.search_plus = None # {function_name, map_tool} self.map_tools = {} # Define signals self.set_signals() def set_signals(self): ''' Define widget and event signals ''' self.iface.projectRead.connect(self.project_read) self.legend.currentLayerChanged.connect(self.current_layer_changed) def tr(self, message): if self.controller: return self.controller.tr(message) def showInfo(self, text, duration = 5): self.iface.messageBar().pushMessage("", text, QgsMessageBar.INFO, duration) def showWarning(self, text, duration = 5): self.iface.messageBar().pushMessage("", text, QgsMessageBar.WARNING, duration) def create_action(self, index_action=None, text='', toolbar=None, menu=None, is_checkable=True, function_name=None, parent=None): if parent is None: parent = self.iface.mainWindow() icon = None if index_action is not None: icon_path = self.icon_folder+index_action+'.png' if os.path.exists(icon_path): icon = QIcon(icon_path) if icon is None: action = QAction(text, parent) else: action = QAction(icon, text, parent) if toolbar is not None: toolbar.addAction(action) if menu is not None: self.iface.addPluginToMenu(menu, action) if index_action is not None: self.actions[index_action] = action else: self.actions[text] = action if function_name is not None: try: action.setCheckable(is_checkable) if int(index_action) in (17, 20, 26, 32): callback_function = getattr(self, function_name) action.triggered.connect(callback_function) else: water_soft = function_name[:2] callback_function = getattr(self, water_soft+'_generic') action.triggered.connect(partial(callback_function, function_name)) except AttributeError: print index_action+". Callback function not found: "+function_name action.setEnabled(False) else: action.setEnabled(False) return action def add_action(self, index_action, toolbar, parent): ''' Add new action into specified toolbar This action has to be defined in the configuration file ''' action = None text_action = self.tr(index_action+'_text') function_name = self.settings.value('actions/'+str(index_action)+'_function') if function_name: map_tool = None action = self.create_action(index_action, text_action, toolbar, None, True, function_name, parent) if int(index_action) == 13: map_tool = LineMapTool(self.iface, self.settings, action, index_action, self.controller) elif int(index_action) == 16: map_tool = MoveNode(self.iface, self.settings, action, index_action, self.controller) elif int(index_action) in (10, 11, 12, 14, 15, 8, 29): map_tool = PointMapTool(self.iface, self.settings, action, index_action, self.controller) else: pass if map_tool: self.map_tools[function_name] = map_tool return action def initGui(self): ''' Create the menu entries and toolbar icons inside the QGIS GUI ''' parent = self.iface.mainWindow() if self.controller is None: return # Create plugin main menu self.menu_name = self.tr('menu_name') # Get table or view related with 'arc' and 'node' self.table_arc = self.settings.value('db/table_arc', 'v_edit_arc') self.table_node = self.settings.value('db/table_node', 'v_edit_node') # Get SRID self.srid = self.settings.value('status/srid') # Create UD, WS, MANAGEMENT and EDIT toolbars or not? self.toolbar_ud_enabled = bool(int(self.settings.value('status/toolbar_ud_enabled', 1))) self.toolbar_ws_enabled = bool(int(self.settings.value('status/toolbar_ws_enabled', 1))) self.toolbar_mg_enabled = bool(int(self.settings.value('status/toolbar_mg_enabled', 1))) self.toolbar_ed_enabled = bool(int(self.settings.value('status/toolbar_ed_enabled', 1))) if self.toolbar_ud_enabled: self.toolbar_ud_name = self.tr('toolbar_ud_name') self.toolbar_ud = self.iface.addToolBar(self.toolbar_ud_name) self.toolbar_ud.setObjectName(self.toolbar_ud_name) if self.toolbar_ws_enabled: self.toolbar_ws_name = self.tr('toolbar_ws_name') self.toolbar_ws = self.iface.addToolBar(self.toolbar_ws_name) self.toolbar_ws.setObjectName(self.toolbar_ws_name) if self.toolbar_mg_enabled: self.toolbar_mg_name = self.tr('toolbar_mg_name') self.toolbar_mg = self.iface.addToolBar(self.toolbar_mg_name) self.toolbar_mg.setObjectName(self.toolbar_mg_name) if self.toolbar_ed_enabled: self.toolbar_ed_name = self.tr('toolbar_ed_name') self.toolbar_ed = self.iface.addToolBar(self.toolbar_ed_name) self.toolbar_ed.setObjectName(self.toolbar_ed_name) # UD toolbar if self.toolbar_ud_enabled: self.ag_ud = QActionGroup(parent); self.add_action('01', self.toolbar_ud, self.ag_ud) self.add_action('02', self.toolbar_ud, self.ag_ud) self.add_action('04', self.toolbar_ud, self.ag_ud) self.add_action('05', self.toolbar_ud, self.ag_ud) self.add_action('03', self.toolbar_ud, self.ag_ud) # WS toolbar if self.toolbar_ws_enabled: self.ag_ws = QActionGroup(parent); self.add_action('10', self.toolbar_ws, self.ag_ws) self.add_action('11', self.toolbar_ws, self.ag_ws) self.add_action('12', self.toolbar_ws, self.ag_ws) self.add_action('14', self.toolbar_ws, self.ag_ws) self.add_action('15', self.toolbar_ws, self.ag_ws) self.add_action('08', self.toolbar_ws, self.ag_ws) self.add_action('29', self.toolbar_ws, self.ag_ws) self.add_action('13', self.toolbar_ws, self.ag_ws) # MANAGEMENT toolbar if self.toolbar_mg_enabled: self.ag_mg = QActionGroup(parent); self.add_action('16', self.toolbar_mg, self.ag_mg) self.add_action('28', self.toolbar_mg, self.ag_mg) for i in range(17,28): self.add_action(str(i), self.toolbar_mg, self.ag_mg) # EDIT toolbar if self.toolbar_ed_enabled: self.ag_ed = QActionGroup(parent); for i in range(30,36): self.add_action(str(i), self.toolbar_ed, self.ag_ed) # Project initialization self.project_read() def unload(self): ''' Removes the plugin menu item and icon from QGIS GUI ''' for action_index, action in self.actions.iteritems(): self.iface.removePluginMenu(self.menu_name, action) self.iface.removeToolBarIcon(action) if self.toolbar_ud_enabled: del self.toolbar_ud if self.toolbar_ws_enabled: del self.toolbar_ws if self.toolbar_mg_enabled: del self.toolbar_mg if self.toolbar_ed_enabled: del self.toolbar_ed if self.search_plus is not None: self.search_plus.unload() ''' Slots ''' def disable_actions(self): ''' Utility to disable all actions ''' for i in range(1,30): key = str(i).zfill(2) if key in self.actions: action = self.actions[key] action.setEnabled(False) def project_read(self): ''' Function executed when a user opens a QGIS project (*.qgs) ''' # Check if we have any layer loaded layers = self.iface.legendInterface().layers() if len(layers) == 0: return # Initialize variables self.layer_arc = None self.layer_node = None table_arc = '"'+self.schema_name+'"."'+self.table_arc+'"' table_node = '"'+self.schema_name+'"."'+self.table_node+'"' # Iterate over all layers to get 'arc' and 'node' layer ''' for cur_layer in layers: uri = cur_layer.dataProvider().dataSourceUri().lower() pos_ini = uri.find('table=') pos_fi = uri.find('" ') uri_table = uri if pos_ini <> -1 and pos_fi <> -1: uri_table = uri[pos_ini+6:pos_fi+1] if uri_table == table_arc: self.layer_arc = cur_layer if uri_table == table_node: self.layer_node = cur_layer # Disable toolbar actions and manage current layer selected self.disable_actions() self.current_layer_changed(self.iface.activeLayer()) # Create SearchPlus object try: if self.search_plus is None: self.search_plus = SearchPlus(self.iface, self.srid) self.search_plus.removeMemoryLayers() status = self.search_plus.populateGui() self.actions['32'].setEnabled(status) self.actions['32'].setCheckable(False) if not status: self.search_plus.dlg.setVisible(False) except: pass def current_layer_changed(self, layer): ''' Manage new layer selected ''' self.disable_actions() if layer is None: layer = self.iface.activeLayer() self.current_layer = layer try: list_index_action = self.settings.value('layers/'+self.current_layer.name(), None) if list_index_action: if type(list_index_action) is list: for index_action in list_index_action: if index_action != '-1' and str(index_action) in self.actions: self.actions[index_action].setEnabled(True) elif type(list_index_action) is unicode: index_action = str(list_index_action) if index_action != '-1' and str(index_action) in self.actions: self.actions[index_action].setEnabled(True) except AttributeError, e: print "current_layer_changed: "+str(e) except KeyError, e: print "current_layer_changed: "+str(e)
class Giswater(QObject): def __init__(self, iface): ''' Constructor :param iface: An interface instance that will be passed to this class which provides the hook by which you can manipulate the QGIS application at run time. :type iface: QgsInterface ''' super(Giswater, self).__init__() # Save reference to the QGIS interface self.iface = iface self.legend = iface.legendInterface() # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) self.plugin_name = os.path.basename(self.plugin_dir) # initialize locale locale = QSettings().value('locale/userLocale') locale_path = os.path.join(self.plugin_dir, 'i18n', self.plugin_name + '_{}.qm'.format(locale)) if os.path.exists(locale_path): self.translator = QTranslator() self.translator.load(locale_path) if qVersion() > '4.3.3': QCoreApplication.installTranslator(self.translator) # Load local settings of the plugin setting_file = os.path.join(self.plugin_dir, 'config', self.plugin_name + '.config') self.settings = QSettings(setting_file, QSettings.IniFormat) self.settings.setIniCodec(sys.getfilesystemencoding()) # Set controller to handle settings and database self.controller = DaoController(self.settings, self.plugin_name) self.controller.set_database_connection() self.dao = self.controller.getDao() self.schema_name = self.controller.getSchemaName() # Declare instance attributes self.icon_folder = self.plugin_dir + '/icons/' self.actions = {} self.search_plus = None # {function_name, map_tool} self.map_tools = {} # Define signals self.set_signals() def set_signals(self): ''' Define widget and event signals ''' self.iface.projectRead.connect(self.project_read) self.legend.currentLayerChanged.connect(self.current_layer_changed) def tr(self, message): if self.controller: return self.controller.tr(message) def showInfo(self, text, duration=5): self.iface.messageBar().pushMessage("", text, QgsMessageBar.INFO, duration) def showWarning(self, text, duration=5): self.iface.messageBar().pushMessage("", text, QgsMessageBar.WARNING, duration) def create_action(self, index_action=None, text='', toolbar=None, menu=None, is_checkable=True, function_name=None, parent=None): if parent is None: parent = self.iface.mainWindow() icon = None if index_action is not None: icon_path = self.icon_folder + index_action + '.png' if os.path.exists(icon_path): icon = QIcon(icon_path) if icon is None: action = QAction(text, parent) else: action = QAction(icon, text, parent) if toolbar is not None: toolbar.addAction(action) if menu is not None: self.iface.addPluginToMenu(menu, action) if index_action is not None: self.actions[index_action] = action else: self.actions[text] = action if function_name is not None: try: action.setCheckable(is_checkable) if int(index_action) in (17, 20, 26, 32): callback_function = getattr(self, function_name) action.triggered.connect(callback_function) else: water_soft = function_name[:2] callback_function = getattr(self, water_soft + '_generic') action.triggered.connect( partial(callback_function, function_name)) except AttributeError: print index_action + ". Callback function not found: " + function_name action.setEnabled(False) else: action.setEnabled(False) return action def add_action(self, index_action, toolbar, parent): ''' Add new action into specified toolbar This action has to be defined in the configuration file ''' action = None text_action = self.tr(index_action + '_text') function_name = self.settings.value('actions/' + str(index_action) + '_function') if function_name: map_tool = None action = self.create_action(index_action, text_action, toolbar, None, True, function_name, parent) if int(index_action) == 13: map_tool = LineMapTool(self.iface, self.settings, action, index_action, self.controller) elif int(index_action) == 16: map_tool = MoveNode(self.iface, self.settings, action, index_action, self.controller) elif int(index_action) in (10, 11, 12, 14, 15, 8, 29): map_tool = PointMapTool(self.iface, self.settings, action, index_action, self.controller) else: pass if map_tool: self.map_tools[function_name] = map_tool return action def initGui(self): ''' Create the menu entries and toolbar icons inside the QGIS GUI ''' parent = self.iface.mainWindow() if self.controller is None: return # Create plugin main menu self.menu_name = self.tr('menu_name') # Get table or view related with 'arc' and 'node' self.table_arc = self.settings.value('db/table_arc', 'v_edit_arc') self.table_node = self.settings.value('db/table_node', 'v_edit_node') # Get SRID self.srid = self.settings.value('status/srid') # Create UD, WS, MANAGEMENT and EDIT toolbars or not? self.toolbar_ud_enabled = bool( int(self.settings.value('status/toolbar_ud_enabled', 1))) self.toolbar_ws_enabled = bool( int(self.settings.value('status/toolbar_ws_enabled', 1))) self.toolbar_mg_enabled = bool( int(self.settings.value('status/toolbar_mg_enabled', 1))) self.toolbar_ed_enabled = bool( int(self.settings.value('status/toolbar_ed_enabled', 1))) if self.toolbar_ud_enabled: self.toolbar_ud_name = self.tr('toolbar_ud_name') self.toolbar_ud = self.iface.addToolBar(self.toolbar_ud_name) self.toolbar_ud.setObjectName(self.toolbar_ud_name) if self.toolbar_ws_enabled: self.toolbar_ws_name = self.tr('toolbar_ws_name') self.toolbar_ws = self.iface.addToolBar(self.toolbar_ws_name) self.toolbar_ws.setObjectName(self.toolbar_ws_name) if self.toolbar_mg_enabled: self.toolbar_mg_name = self.tr('toolbar_mg_name') self.toolbar_mg = self.iface.addToolBar(self.toolbar_mg_name) self.toolbar_mg.setObjectName(self.toolbar_mg_name) if self.toolbar_ed_enabled: self.toolbar_ed_name = self.tr('toolbar_ed_name') self.toolbar_ed = self.iface.addToolBar(self.toolbar_ed_name) self.toolbar_ed.setObjectName(self.toolbar_ed_name) # UD toolbar if self.toolbar_ud_enabled: self.ag_ud = QActionGroup(parent) self.add_action('01', self.toolbar_ud, self.ag_ud) self.add_action('02', self.toolbar_ud, self.ag_ud) self.add_action('04', self.toolbar_ud, self.ag_ud) self.add_action('05', self.toolbar_ud, self.ag_ud) self.add_action('03', self.toolbar_ud, self.ag_ud) # WS toolbar if self.toolbar_ws_enabled: self.ag_ws = QActionGroup(parent) self.add_action('10', self.toolbar_ws, self.ag_ws) self.add_action('11', self.toolbar_ws, self.ag_ws) self.add_action('12', self.toolbar_ws, self.ag_ws) self.add_action('14', self.toolbar_ws, self.ag_ws) self.add_action('15', self.toolbar_ws, self.ag_ws) self.add_action('08', self.toolbar_ws, self.ag_ws) self.add_action('29', self.toolbar_ws, self.ag_ws) self.add_action('13', self.toolbar_ws, self.ag_ws) # MANAGEMENT toolbar if self.toolbar_mg_enabled: self.ag_mg = QActionGroup(parent) self.add_action('16', self.toolbar_mg, self.ag_mg) self.add_action('28', self.toolbar_mg, self.ag_mg) for i in range(17, 28): self.add_action(str(i), self.toolbar_mg, self.ag_mg) # EDIT toolbar if self.toolbar_ed_enabled: self.ag_ed = QActionGroup(parent) for i in range(30, 36): self.add_action(str(i), self.toolbar_ed, self.ag_ed) # Project initialization self.project_read() def unload(self): ''' Removes the plugin menu item and icon from QGIS GUI ''' for action_index, action in self.actions.iteritems(): self.iface.removePluginMenu(self.menu_name, action) self.iface.removeToolBarIcon(action) if self.toolbar_ud_enabled: del self.toolbar_ud if self.toolbar_ws_enabled: del self.toolbar_ws if self.toolbar_mg_enabled: del self.toolbar_mg if self.toolbar_ed_enabled: del self.toolbar_ed if self.search_plus is not None: self.search_plus.unload() ''' Slots ''' def disable_actions(self): ''' Utility to disable all actions ''' for i in range(1, 30): key = str(i).zfill(2) if key in self.actions: action = self.actions[key] action.setEnabled(False) def project_read(self): ''' Function executed when a user opens a QGIS project (*.qgs) ''' # Check if we have any layer loaded layers = self.iface.legendInterface().layers() if len(layers) == 0: return # Initialize variables self.layer_arc = None self.layer_node = None table_arc = '"' + self.schema_name + '"."' + self.table_arc + '"' table_node = '"' + self.schema_name + '"."' + self.table_node + '"' # Iterate over all layers to get 'arc' and 'node' layer ''' for cur_layer in layers: uri = cur_layer.dataProvider().dataSourceUri().lower() pos_ini = uri.find('table=') pos_fi = uri.find('" ') uri_table = uri if pos_ini <> -1 and pos_fi <> -1: uri_table = uri[pos_ini + 6:pos_fi + 1] if uri_table == table_arc: self.layer_arc = cur_layer if uri_table == table_node: self.layer_node = cur_layer # Disable toolbar actions and manage current layer selected self.disable_actions() self.current_layer_changed(self.iface.activeLayer()) # Create SearchPlus object try: if self.search_plus is None: self.search_plus = SearchPlus(self.iface, self.srid) self.search_plus.removeMemoryLayers() status = self.search_plus.populateGui() self.actions['32'].setEnabled(status) self.actions['32'].setCheckable(False) if not status: self.search_plus.dlg.setVisible(False) except: pass def current_layer_changed(self, layer): ''' Manage new layer selected ''' self.disable_actions() if layer is None: layer = self.iface.activeLayer() self.current_layer = layer try: list_index_action = self.settings.value( 'layers/' + self.current_layer.name(), None) if list_index_action: if type(list_index_action) is list: for index_action in list_index_action: if index_action != '-1' and str( index_action) in self.actions: self.actions[index_action].setEnabled(True) elif type(list_index_action) is unicode: index_action = str(list_index_action) if index_action != '-1' and str( index_action) in self.actions: self.actions[index_action].setEnabled(True) except AttributeError, e: print "current_layer_changed: " + str(e) except KeyError, e: print "current_layer_changed: " + str(e)
class ParentDialog(object): def __init__(self, iface, dialog, layer, feature): ''' Constructor class ''' self.iface = iface self.dialog = dialog self.layer = layer self.feature = feature self.init_config() def init_config(self): # initialize plugin directory user_folder = os.path.expanduser("~") self.plugin_name = 'giswater' self.plugin_dir = os.path.join( user_folder, '.qgis2/python/plugins/' + self.plugin_name) # Get config file setting_file = os.path.join(self.plugin_dir, 'config', self.plugin_name + '.config') if not os.path.isfile(setting_file): message = "Config file not found at: " + setting_file self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) self.close() return self.settings = QSettings(setting_file, QSettings.IniFormat) self.settings.setIniCodec(sys.getfilesystemencoding()) # Set controller to handle settings and database connection # TODO: Try to make only one connection self.controller = DaoController(self.settings, self.plugin_name) status = self.controller.set_database_connection() if not status: message = self.controller.getLastError() self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) return self.schema_name = self.controller.getSchemaName() self.dao = self.controller.getDao() def translate_form(self, context_name): ''' Translate widgets of the form to current language ''' # Get objects of type: QLabel widget_list = self.dialog.findChildren(QLabel) for widget in widget_list: self.translate_widget(context_name, widget) def translate_widget(self, context_name, widget): ''' Translate widget text ''' if widget: widget_name = widget.objectName() text = utils_giswater.tr(context_name, widget_name) if text != widget_name: widget.setText(text) def load_tab_add_info(self): ''' Load data from tab 'Add. info' ''' pass def load_tab_analysis(self): ''' Load data from tab 'Analysis' ''' pass def load_tab_document(self): ''' TODO: Load data from tab 'Document' ''' pass def load_tab_picture(self): ''' TODO: Load data from tab 'Document' ''' pass def load_tab_event(self): ''' TODO: Load data from tab 'Event' ''' pass def load_tab_log(self): ''' TODO: Load data from tab 'Log' ''' pass def load_tab_rtc(self): ''' TODO: Load data from tab 'RTC' ''' pass def load_data(self): ''' Load data from related tables ''' self.load_tab_add_info() self.load_tab_analysis() self.load_tab_document() self.load_tab_picture() self.load_tab_event() self.load_tab_log() self.load_tab_rtc() def save_tab_add_info(self): ''' Save tab from tab 'Add. info' ''' pass def save_tab_analysis(self): ''' Save tab from tab 'Analysis' ''' pass def save_tab_document(self): ''' TODO: Save tab from tab 'Document' ''' pass def save_tab_picture(self): ''' TODO: Save tab from tab 'Document' ''' pass def save_tab_event(self): ''' TODO: Save tab from tab 'Event' ''' pass def save_tab_log(self): ''' TODO: Save tab from tab 'Log' ''' pass def save_tab_rtc(self): ''' TODO: Save tab from tab 'RTC' ''' pass def save_data(self): ''' Save data from related tables ''' self.save_tab_add_info() self.save_tab_analysis() self.save_tab_document() self.save_tab_picture() self.save_tab_event() self.save_tab_log() self.save_tab_rtc() ''' Slot functions ''' def save(self): ''' Save feature ''' self.save_data() self.dialog.accept() self.layer.commitChanges() self.close() def close(self): ''' Close form without saving ''' self.layer.rollBack() self.dialog.parent().setVisible(False)
class NodeDialog(): def __init__(self, iface, dialog, layer, feature): self.iface = iface self.dialog = dialog self.layer = layer self.feature = feature self.initConfig() def initConfig(self): self.node_id = utils_giswater.getStringValue2("node_id") self.epa_type = utils_giswater.getSelectedItem("epa_type") # initialize plugin directory user_folder = os.path.expanduser("~") self.plugin_name = 'giswater' self.plugin_dir = os.path.join( user_folder, '.qgis2/python/plugins/' + self.plugin_name) # Get config file setting_file = os.path.join(self.plugin_dir, 'config', self.plugin_name + '.config') if not os.path.isfile(setting_file): message = "Config file not found at: " + setting_file self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) self.close() return self.settings = QSettings(setting_file, QSettings.IniFormat) self.settings.setIniCodec(sys.getfilesystemencoding()) # Get widget controls self.cbo_cat_nodetype_id = self.dialog.findChild( QComboBox, "cat_nodetype_iddd") self.cbo_nodecat_id = self.dialog.findChild(QComboBox, "nodecat_id") self.tab_analysis = self.dialog.findChild(QTabWidget, "tab_analysis") self.tab_event = self.dialog.findChild(QTabWidget, "tab_event") # Set controller to handle settings and database connection # TODO: Try to make only one connection self.controller = DaoController(self.settings, self.plugin_name) status = self.controller.set_database_connection() if not status: message = self.controller.getLastError() self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) return self.schema_name = self.controller.getSchemaName() self.dao = self.controller.getDao() # Manage tab visibility self.setTabsVisibility() # Manage i18n self.translateForm() # Fill combo 'node type' from 'epa_type' self.fillNodeType() # Load data from related tables self.loadData() # Set layer in editing mode self.layer.startEditing() def translateForm(self): # Get objects of type: QLabel context_name = 'ws_node' widget_list = self.dialog.findChildren(QLabel) for widget in widget_list: self.translateWidget(context_name, widget) def translateWidget(self, context_name, widget): if widget: widget_name = widget.objectName() text = utils_giswater.tr(context_name, widget_name) if text != widget_name: widget.setText(text) def loadData(self): # Tab 'Add. info' if self.epa_type == 'TANK': sql = "SELECT vmax, area FROM " + self.schema_name + ".man_tank WHERE node_id = " + self.node_id row = self.dao.get_row(sql) if row: utils_giswater.setText("man_tank_vmax", str(row[0])) utils_giswater.setText("man_tank_area", str(row[1])) ''' Slots ''' def fillNodeType(self): """ Define and execute query to populate combo 'cat_nodetype_id' """ cat_nodetype_id = utils_giswater.getSelectedItem("cat_nodetype_id") sql = "SELECT id, man_table, epa_table FROM " + self.schema_name + ".arc_type WHERE epa_default = '" + self.epa_type + "' UNION " sql += "SELECT id, man_table, epa_table FROM " + self.schema_name + ".node_type WHERE epa_default = '" + self.epa_type + "' ORDER BY id" rows = self.dao.get_rows(sql) self.cbo_cat_nodetype_id = self.dialog.findChild( QComboBox, "cat_nodetype_id") utils_giswater.fillComboBox(self.cbo_cat_nodetype_id, rows) utils_giswater.setSelectedItem('cat_nodetype_id', cat_nodetype_id) def changeNodeType(self, index): """ Define and execute query to populate combo 'nodecat_id_dummy' """ cat_nodetype_id = utils_giswater.getSelectedItem("cat_nodetype_id") sql = "SELECT id FROM " + self.schema_name + ".cat_arc WHERE arctype_id = '" + cat_nodetype_id + "' UNION " sql += "SELECT id FROM " + self.schema_name + ".cat_node WHERE nodetype_id = '" + cat_nodetype_id + "' ORDER BY id" rows = self.dao.get_rows(sql) self.cbo_nodecat_id = self.dialog.findChild(QComboBox, "nodecat_id_dummy") utils_giswater.fillComboBox(self.cbo_nodecat_id, rows, False) self.changeNodeCat(0) def changeNodeCat(self, index): """ Just select item to 'real' combo 'nodecat_id' (that is hidden) """ dummy = utils_giswater.getSelectedItem("nodecat_id_dummy") utils_giswater.setSelectedItem("nodecat_id", dummy) def changeEpaType(self, index): """ Just select item to 'real' combo 'nodecat_id' (that is hidden) """ epa_type = utils_giswater.getSelectedItem("epa_type") self.save() self.iface.openFeatureForm(self.layer, self.feature) def setTabsVisibility(self): man_visible = False index_tab = 0 if self.epa_type == 'JUNCTION': index_tab = 0 if self.epa_type == 'RESERVOIR' or self.epa_type == 'HYDRANT': index_tab = 1 if self.epa_type == 'TANK': index_tab = 2 man_visible = True if self.epa_type == 'PUMP': index_tab = 3 if self.epa_type == 'VALVE': index_tab = 4 if self.epa_type == 'SHORTPIPE' or self.epa_type == 'FILTER': index_tab = 5 if self.epa_type == 'MEASURE INSTRUMENT': index_tab = 6 # Tab 'Add. info': Manage visibility of these widgets utils_giswater.setWidgetVisible("label_man_tank_vmax", man_visible) utils_giswater.setWidgetVisible("label_man_tank_area", man_visible) utils_giswater.setWidgetVisible("man_tank_vmax", man_visible) utils_giswater.setWidgetVisible("man_tank_area", man_visible) # Move 'visible' tab to last position and remove previous ones self.tab_analysis.tabBar().moveTab(index_tab, 5) for i in range(0, self.tab_analysis.count() - 1): self.tab_analysis.removeTab(0) self.tab_event.tabBar().moveTab(index_tab, 6) for i in range(0, self.tab_event.count() - 1): self.tab_event.removeTab(0) def save(self): if self.epa_type == 'TANK': vmax = utils_giswater.getStringValue2("man_tank_vmax") area = utils_giswater.getStringValue2("man_tank_area") sql = "UPDATE " + self.schema_name + ".man_tank SET vmax = " + str( vmax) + ", area = " + str(area) sql += " WHERE node_id = " + str(self.node_id) print sql self.dao.execute_sql(sql) self.dialog.accept() self.layer.commitChanges() self.close() def close(self): self.layer.rollBack() self.dialog.parent().setVisible(False)
class ParentDialog(object): def __init__(self, iface, dialog, layer, feature): ''' Constructor class ''' self.iface = iface self.dialog = dialog self.layer = layer self.feature = feature self.init_config() def init_config(self): # initialize plugin directory user_folder = os.path.expanduser("~") self.plugin_name = 'giswater' self.plugin_dir = os.path.join(user_folder, '.qgis2/python/plugins/'+self.plugin_name) # Get config file setting_file = os.path.join(self.plugin_dir, 'config', self.plugin_name+'.config') if not os.path.isfile(setting_file): message = "Config file not found at: "+setting_file self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) self.close() return self.settings = QSettings(setting_file, QSettings.IniFormat) self.settings.setIniCodec(sys.getfilesystemencoding()) # Set controller to handle settings and database connection # TODO: Try to make only one connection self.controller = DaoController(self.settings, self.plugin_name) status = self.controller.set_database_connection() if not status: message = self.controller.getLastError() self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) return self.schema_name = self.controller.getSchemaName() self.dao = self.controller.getDao() def translate_form(self, context_name): ''' Translate widgets of the form to current language ''' # Get objects of type: QLabel widget_list = self.dialog.findChildren(QLabel) for widget in widget_list: self.translate_widget(context_name, widget) def translate_widget(self, context_name, widget): ''' Translate widget text ''' if widget: widget_name = widget.objectName() text = utils_giswater.tr(context_name, widget_name) if text != widget_name: widget.setText(text) def load_tab_add_info(self): ''' Load data from tab 'Add. info' ''' pass def load_tab_analysis(self): ''' Load data from tab 'Analysis' ''' pass def load_tab_document(self): ''' TODO: Load data from tab 'Document' ''' pass def load_tab_picture(self): ''' TODO: Load data from tab 'Document' ''' pass def load_tab_event(self): ''' TODO: Load data from tab 'Event' ''' pass def load_tab_log(self): ''' TODO: Load data from tab 'Log' ''' pass def load_tab_rtc(self): ''' TODO: Load data from tab 'RTC' ''' pass def load_data(self): ''' Load data from related tables ''' self.load_tab_add_info() self.load_tab_analysis() self.load_tab_document() self.load_tab_picture() self.load_tab_event() self.load_tab_log() self.load_tab_rtc() def save_tab_add_info(self): ''' Save tab from tab 'Add. info' ''' pass def save_tab_analysis(self): ''' Save tab from tab 'Analysis' ''' pass def save_tab_document(self): ''' TODO: Save tab from tab 'Document' ''' pass def save_tab_picture(self): ''' TODO: Save tab from tab 'Document' ''' pass def save_tab_event(self): ''' TODO: Save tab from tab 'Event' ''' pass def save_tab_log(self): ''' TODO: Save tab from tab 'Log' ''' pass def save_tab_rtc(self): ''' TODO: Save tab from tab 'RTC' ''' pass def save_data(self): ''' Save data from related tables ''' self.save_tab_add_info() self.save_tab_analysis() self.save_tab_document() self.save_tab_picture() self.save_tab_event() self.save_tab_log() self.save_tab_rtc() ''' Slot functions ''' def save(self): ''' Save feature ''' self.save_data() self.dialog.accept() self.layer.commitChanges() self.close() def close(self): ''' Close form without saving ''' self.layer.rollBack() self.dialog.parent().setVisible(False)
class NodeDialog(): def __init__(self, iface, dialog, layer, feature): self.iface = iface self.dialog = dialog self.layer = layer self.feature = feature self.initConfig() def initConfig(self): self.node_id = utils_giswater.getStringValue2("node_id") self.epa_type = utils_giswater.getSelectedItem("epa_type") # initialize plugin directory user_folder = os.path.expanduser("~") self.plugin_name = 'giswater' self.plugin_dir = os.path.join(user_folder, '.qgis2/python/plugins/'+self.plugin_name) # Get config file setting_file = os.path.join(self.plugin_dir, 'config', self.plugin_name+'.config') if not os.path.isfile(setting_file): message = "Config file not found at: "+setting_file self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) self.close() return self.settings = QSettings(setting_file, QSettings.IniFormat) self.settings.setIniCodec(sys.getfilesystemencoding()) # Get widget controls self.cbo_cat_nodetype_id = self.dialog.findChild(QComboBox, "cat_nodetype_iddd") self.cbo_nodecat_id = self.dialog.findChild(QComboBox, "nodecat_id") self.tab_analysis = self.dialog.findChild(QTabWidget, "tab_analysis") self.tab_event = self.dialog.findChild(QTabWidget, "tab_event") # Set controller to handle settings and database connection # TODO: Try to make only one connection self.controller = DaoController(self.settings, self.plugin_name) status = self.controller.set_database_connection() if not status: message = self.controller.getLastError() self.iface.messageBar().pushMessage(message, QgsMessageBar.WARNING, 5) return self.schema_name = self.controller.getSchemaName() self.dao = self.controller.getDao() # Manage tab visibility self.setTabsVisibility() # Manage i18n self.translateForm() # Fill combo 'node type' from 'epa_type' self.fillNodeType() # Load data from related tables self.loadData() # Set layer in editing mode self.layer.startEditing() def translateForm(self): # Get objects of type: QLabel context_name = 'ws_node' widget_list = self.dialog.findChildren(QLabel) for widget in widget_list: self.translateWidget(context_name, widget) def translateWidget(self, context_name, widget): if widget: widget_name = widget.objectName() text = utils_giswater.tr(context_name, widget_name) if text != widget_name: widget.setText(text) def loadData(self): # Tab 'Add. info' if self.epa_type == 'TANK': sql = "SELECT vmax, area FROM "+self.schema_name+".man_tank WHERE node_id = "+self.node_id row = self.dao.get_row(sql) if row: utils_giswater.setText("man_tank_vmax", str(row[0])) utils_giswater.setText("man_tank_area", str(row[1])) ''' Slots ''' def fillNodeType(self): """ Define and execute query to populate combo 'cat_nodetype_id' """ cat_nodetype_id = utils_giswater.getSelectedItem("cat_nodetype_id") sql = "SELECT id, man_table, epa_table FROM "+self.schema_name+".arc_type WHERE epa_default = '"+self.epa_type+"' UNION " sql+= "SELECT id, man_table, epa_table FROM "+self.schema_name+".node_type WHERE epa_default = '"+self.epa_type+"' ORDER BY id" rows = self.dao.get_rows(sql) self.cbo_cat_nodetype_id = self.dialog.findChild(QComboBox, "cat_nodetype_id") utils_giswater.fillComboBox(self.cbo_cat_nodetype_id, rows) utils_giswater.setSelectedItem('cat_nodetype_id', cat_nodetype_id) def changeNodeType(self, index): """ Define and execute query to populate combo 'nodecat_id_dummy' """ cat_nodetype_id = utils_giswater.getSelectedItem("cat_nodetype_id") sql = "SELECT id FROM "+self.schema_name+".cat_arc WHERE arctype_id = '"+cat_nodetype_id+"' UNION " sql+= "SELECT id FROM "+self.schema_name+".cat_node WHERE nodetype_id = '"+cat_nodetype_id+"' ORDER BY id" rows = self.dao.get_rows(sql) self.cbo_nodecat_id = self.dialog.findChild(QComboBox, "nodecat_id_dummy") utils_giswater.fillComboBox(self.cbo_nodecat_id, rows, False) self.changeNodeCat(0) def changeNodeCat(self, index): """ Just select item to 'real' combo 'nodecat_id' (that is hidden) """ dummy = utils_giswater.getSelectedItem("nodecat_id_dummy") utils_giswater.setSelectedItem("nodecat_id", dummy) def changeEpaType(self, index): """ Just select item to 'real' combo 'nodecat_id' (that is hidden) """ epa_type = utils_giswater.getSelectedItem("epa_type") self.save() self.iface.openFeatureForm(self.layer, self.feature) def setTabsVisibility(self): man_visible = False index_tab = 0 if self.epa_type == 'JUNCTION': index_tab = 0 if self.epa_type == 'RESERVOIR' or self.epa_type == 'HYDRANT': index_tab = 1 if self.epa_type == 'TANK': index_tab = 2 man_visible = True if self.epa_type == 'PUMP': index_tab = 3 if self.epa_type == 'VALVE': index_tab = 4 if self.epa_type == 'SHORTPIPE' or self.epa_type == 'FILTER': index_tab = 5 if self.epa_type == 'MEASURE INSTRUMENT': index_tab = 6 # Tab 'Add. info': Manage visibility of these widgets utils_giswater.setWidgetVisible("label_man_tank_vmax", man_visible) utils_giswater.setWidgetVisible("label_man_tank_area", man_visible) utils_giswater.setWidgetVisible("man_tank_vmax", man_visible) utils_giswater.setWidgetVisible("man_tank_area", man_visible) # Move 'visible' tab to last position and remove previous ones self.tab_analysis.tabBar().moveTab(index_tab, 5); for i in range(0, self.tab_analysis.count() - 1): self.tab_analysis.removeTab(0) self.tab_event.tabBar().moveTab(index_tab, 6); for i in range(0, self.tab_event.count() - 1): self.tab_event.removeTab(0) def save(self): if self.epa_type == 'TANK': vmax = utils_giswater.getStringValue2("man_tank_vmax") area = utils_giswater.getStringValue2("man_tank_area") sql = "UPDATE "+self.schema_name+".man_tank SET vmax = "+str(vmax)+ ", area = "+str(area) sql+= " WHERE node_id = "+str(self.node_id) print sql self.dao.execute_sql(sql) self.dialog.accept() self.layer.commitChanges() self.close() def close(self): self.layer.rollBack() self.dialog.parent().setVisible(False)