def testSettingsPath(self): """ test that settings path is respected """ QSettings().clear() s1 = QgsShortcutsManager(None, '/path1/') s2 = QgsShortcutsManager(None, '/path2/') action1 = QAction('action', None) s1.registerAction(action1) s1.setKeySequence(action1, 'B') action2 = QAction('action', None) s2.registerAction(action2) s2.setKeySequence(action2, 'C') # test retrieving r1 = QgsShortcutsManager(None, '/path1/') r2 = QgsShortcutsManager(None, '/path2/') raction1 = QAction('action', None) r1.registerAction(raction1) raction2 = QAction('action', None) r2.registerAction(raction2) self.assertEqual(raction1.shortcut().toString(), 'B') self.assertEqual(raction2.shortcut().toString(), 'C')
def initGui(self): """ initialisation GUI""" self.action = QAction(QIcon(self.icon_path), QApplication.translate(self.name_plug, self.name_plug), self.iface.mainWindow()) self.action.setObjectName(self.name_plug) # QObject.connect(self.action, SIGNAL('triggered()'), self.run) self.action.triggered.connect(self.run) # Add toolbar button and menu item self.iface.addToolBarIcon(self.action) self.iface.addPluginToMenu(QApplication.translate(self.name_plug, self.name_plug), self.action)
class MascPlug: """QGIS Plugin Implementation.""" def __init__(self, iface): self.iface = iface self.dlg = None self.icon_path = ":/plugins/Mascaret/icones/icon_base.png" self.name_plug = 'Mascaret' def initGui(self): """ initialisation GUI""" self.action = QAction( QIcon(self.icon_path), QApplication.translate(self.name_plug, self.name_plug), self.iface.mainWindow()) self.action.setObjectName(self.name_plug) # QObject.connect(self.action, SIGNAL('triggered()'), self.run) self.action.triggered.connect(self.run) # Add toolbar button and menu item self.iface.addToolBarIcon(self.action) self.iface.addPluginToMenu( QApplication.translate(self.name_plug, self.name_plug), self.action) def unload(self): # Remove the plugin menu item and icon self.iface.removeToolBarIcon(self.action) self.iface.removePluginMenu( QApplication.translate(self.name_plug, self.name_plug), self.action) if self.dlg is not None: self.dlg.close() def run(self): # keep opened only one instance if self.dlg is None: self.dlg = MascPlugDialog(self.iface) self.dlg.destroyed.connect(self.on_destroyed) # QObject.connect(self.dlg, SIGNAL('destroyed(QObject *)'), self.on_destroyed) self.dlg.show() self.dlg.raise_() self.dlg.setWindowState(self.dlg.windowState() & ~Qt.WindowMinimized) self.dlg.activateWindow() def on_destroyed(self, obj): self.dlg = None
def __init__(self, parent, canvas, add_dock_widget): QDockWidget.__init__(self, parent) self.setupUi(self) self.selectCurrentPathAction = QAction(self.tr('Select current path'), self.selectButton) self.selectCurrentPathAction.triggered.connect( self.onSelectCurrentPathAction) self.selectButton.setDefaultAction(self.selectCurrentPathAction) self.configureSelectionAction = QAction(self.tr('Configure Select'), self.selectButton) self.configureSelectionAction.triggered.connect( self.onConfigureSelectAction) self.selectButton.addAction(self.configureSelectionAction) self.setAttribute(Qt.WA_DeleteOnClose) self.canvas = canvas self.addDockWidget = add_dock_widget
def initGui(self): self.plugin_menu = self.iface.pluginMenu() # Main window icon = QIcon(resource('icon-32.png')) self.main_action = QAction(icon, 'GeoHealth', self.iface.mainWindow()) self.plugin_menu.addAction(self.main_action) # noinspection PyUnresolvedReferences self.main_action.triggered.connect(self.open_main_window)
def test_gui_building_aggregate(self): mock_action = QAction(IFACE.mainWindow()) self.viewer_dock = ViewerDock(IFACE, mock_action) IFACE.setActiveLayer(self.dmg_by_asset_layer) output_type = 'Recovery Curves' idx = self.viewer_dock.output_type_cbx.findText(output_type) self.assertNotEqual(idx, -1, 'Output type %s not found' % output_type) self.viewer_dock.output_type_cbx.setCurrentIndex(idx) approach = 'Aggregate' idx = self.viewer_dock.approach_cbx.findText(approach) self.assertNotEqual(idx, -1, 'Approach %s not found' % approach) self.viewer_dock.approach_cbx.setCurrentIndex(idx) IFACE.activeLayer().select([1])
def testUnregister(self): """ test unregistering from manager """ QSettings().clear() s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setKey('x') shortcut1.setObjectName('shortcut1') shortcut2 = QShortcut(None) shortcut2.setKey('y') shortcut2.setObjectName('shortcut2') action1 = QAction('action1', None) action1.setShortcut('x') action2 = QAction('action2', None) action2.setShortcut('y') # try unregistering objects not registered in manager self.assertFalse(s.unregisterShortcut(shortcut1)) self.assertFalse(s.unregisterAction(action1)) # try unregistering objects from manager s.registerShortcut(shortcut1) s.registerShortcut(shortcut2) s.registerAction(action1) s.registerAction(action2) self.assertEqual(set(s.listActions()), set([action1, action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut1, shortcut2])) self.assertTrue(s.unregisterAction(action1)) self.assertTrue(s.unregisterShortcut(shortcut1)) self.assertEqual(set(s.listActions()), set([action2])) self.assertEqual(set(s.listShortcuts()), set([shortcut2])) self.assertTrue(s.unregisterAction(action2)) self.assertTrue(s.unregisterShortcut(shortcut2))
def reset_gui(self): mock_action = QAction(IFACE.mainWindow()) self.viewer_dock = ViewerDock(IFACE, mock_action) IFACE.newProject()
def testRegisterAction(self): """ test registering actions """ QSettings().clear() s = QgsShortcutsManager(None) action1 = QAction('action1', None) action1.setShortcut('x') self.assertTrue(s.registerAction(action1, 'A')) action2 = QAction('action2', None) action2.setShortcut('y') self.assertTrue(s.registerAction(action2, 'B')) # actions should have been set to default sequences self.assertEqual(action1.shortcut().toString(), 'A') self.assertEqual(action2.shortcut().toString(), 'B') # test that adding an action should set its shortcut automatically s.setKeySequence('action1', 'C') s.setKeySequence('action2', 'D') s = QgsShortcutsManager(None) self.assertTrue(s.registerAction(action1, 'A')) self.assertTrue(s.registerAction(action2, 'B')) # actions should have been set to previous shortcuts self.assertEqual(action1.shortcut().toString(), 'C') self.assertEqual(action2.shortcut().toString(), 'D') # test registering an action containing '&' in name s = QgsShortcutsManager(None) action = QAction('&action1', None) self.assertTrue(s.registerAction(action)) self.assertEqual(action1.shortcut().toString(), 'C')
def testSetSequence(self): """ test setting key sequences """ QSettings().clear() s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setObjectName('shortcut1') shortcut2 = QShortcut(None) shortcut2.setObjectName('shortcut2') action1 = QAction('action1', None) action2 = QAction('action2', None) s.registerShortcut(shortcut1, 'A') s.registerShortcut(shortcut2, 'B') s.registerAction(action1, 'C') s.registerAction(action2, 'D') # test setting by action/shortcut self.assertTrue(s.setKeySequence(shortcut1, 'E')) self.assertTrue(s.setKeySequence(shortcut2, 'F')) self.assertTrue(s.setKeySequence(action1, 'G')) self.assertTrue(s.setKeySequence(action2, 'H')) # test that action/shortcuts have been updated self.assertEqual(shortcut1.key().toString(), 'E') self.assertEqual(shortcut2.key().toString(), 'F') self.assertEqual(action1.shortcut().toString(), 'G') self.assertEqual(action2.shortcut().toString(), 'H') # new manager s = QgsShortcutsManager(None) # new shortcuts shortcut1 = QShortcut(None) shortcut1.setObjectName('shortcut1') shortcut2 = QShortcut(None) shortcut2.setObjectName('shortcut2') action1 = QAction('action1', None) action2 = QAction('action2', None) # register them s.registerShortcut(shortcut1, 'A') s.registerShortcut(shortcut2, 'B') s.registerAction(action1, 'C') s.registerAction(action2, 'D') # check that previously set sequence has been restored self.assertEqual(shortcut1.key().toString(), 'E') self.assertEqual(shortcut2.key().toString(), 'F') self.assertEqual(action1.shortcut().toString(), 'G') self.assertEqual(action2.shortcut().toString(), 'H') # same test, using setObjectKeySequence QSettings().clear() s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setObjectName('shortcut1') action1 = QAction('action1', None) s.registerShortcut(shortcut1, 'A') s.registerAction(action1, 'C') self.assertTrue(s.setObjectKeySequence(shortcut1, 'E')) self.assertTrue(s.setObjectKeySequence(action1, 'G')) self.assertEqual(shortcut1.key().toString(), 'E') self.assertEqual(action1.shortcut().toString(), 'G') s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setObjectName('shortcut1') action1 = QAction('action1', None) s.registerShortcut(shortcut1, 'A') s.registerAction(action1, 'C') self.assertEqual(shortcut1.key().toString(), 'E') self.assertEqual(action1.shortcut().toString(), 'G') # same test, using setKeySequence by name QSettings().clear() s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setObjectName('shortcut1') action1 = QAction('action1', None) s.registerShortcut(shortcut1, 'A') s.registerAction(action1, 'C') self.assertFalse(s.setKeySequence('invalid_name', 'E')) self.assertTrue(s.setKeySequence('shortcut1', 'E')) self.assertTrue(s.setKeySequence('action1', 'G')) self.assertEqual(shortcut1.key().toString(), 'E') self.assertEqual(action1.shortcut().toString(), 'G') s = QgsShortcutsManager(None) shortcut1 = QShortcut(None) shortcut1.setObjectName('shortcut1') action1 = QAction('action1', None) s.registerShortcut(shortcut1, 'A') s.registerAction(action1, 'C') self.assertEqual(shortcut1.key().toString(), 'E') self.assertEqual(action1.shortcut().toString(), 'G')
def setUp(self): IFACE.newProject() curr_dir_name = os.path.dirname(__file__) self.data_dir_name = os.path.join(curr_dir_name, os.pardir, 'data') mock_action = QAction(IFACE.mainWindow()) self.viewer_dock = ViewerDock(IFACE, mock_action)
def add_action( self, icon_path, text, callback, enabled_flag=True, add_to_menu=True, add_to_toolbar=True, status_tip=None, whats_this=None, parent=None): """Add a toolbar icon to the toolbar. :param icon_path: Path to the icon for this action. Can be a resource path (e.g. ':/plugins/foo/bar.png') or a normal file system path. :type icon_path: str :param text: Text that should be shown in menu items for this action. :type text: str :param callback: Function to be called when the action is triggered. :type callback: function :param enabled_flag: A flag indicating if the action should be enabled by default. Defaults to True. :type enabled_flag: bool :param add_to_menu: Flag indicating whether the action should also be added to the menu. Defaults to True. :type add_to_menu: bool :param add_to_toolbar: Flag indicating whether the action should also be added to the toolbar. Defaults to True. :type add_to_toolbar: bool :param status_tip: Optional text to show in a popup when mouse pointer hovers over the action. :type status_tip: str :param parent: Parent widget for the new action. Defaults None. :type parent: QWidget :param whats_this: Optional text to show in the status bar when the mouse pointer hovers over the action. :returns: The action that was created. Note that the action is also added to self.actions list. :rtype: QAction """ icon = QIcon(icon_path) action = QAction(icon, text, parent) action.triggered.connect(callback) action.setEnabled(enabled_flag) if status_tip is not None: action.setStatusTip(status_tip) if whats_this is not None: action.setWhatsThis(whats_this) if add_to_toolbar: self.toolbar.addAction(action) if add_to_menu: if hasattr(self.iface, 'addPluginToWebMenu'): self.iface.addPluginToWebMenu('', action) # We'll also keep it in the Plugin menu for the time being... # else: # self.iface.addPluginToMenu(self.menuName, action) self.iface.addPluginToMenu(self.menuName, action) self.actions.append(action) return action
class QgepProfileDockWidget(QDockWidget, Ui_QgepDockWidget): # Signal emitted when the widget is closed closed = pyqtSignal() canvas = None addDockWidget = None # Lookup table for vertical exaggeration values veLUT = { 1: 1, 2: 2, 3: 3, 4: 5, 5: 10, 6: 20, 7: 30, 8: 50, 9: 100, 10: 500 } def __init__(self, parent, canvas, add_dock_widget): QDockWidget.__init__(self, parent) self.setupUi(self) self.selectCurrentPathAction = QAction(self.tr('Select current path'), self.selectButton) self.selectCurrentPathAction.triggered.connect( self.onSelectCurrentPathAction) self.selectButton.setDefaultAction(self.selectCurrentPathAction) self.configureSelectionAction = QAction(self.tr('Configure Select'), self.selectButton) self.configureSelectionAction.triggered.connect( self.onConfigureSelectAction) self.selectButton.addAction(self.configureSelectionAction) self.setAttribute(Qt.WA_DeleteOnClose) self.canvas = canvas self.addDockWidget = add_dock_widget def showIt(self): # self.setLocation( Qt.BottomDockWidgetArea ) self.location = Qt.BottomDockWidgetArea minsize = self.minimumSize() maxsize = self.maximumSize() self.setMinimumSize(minsize) self.setMaximumSize(maxsize) self.canvas.setRenderFlag(False) self.addDockWidget(self.location, self) self.canvas.setRenderFlag(True) self.printButton.clicked.connect(self.onPrintButtonClicked) self.mSliderVerticalExaggeration.valueChanged.connect( self.onVerticalExaggerationChanged) def closeEvent(self, event): self.closed.emit() return QDockWidget.closeEvent(self, event) def addPlotWidget(self, plot_widget): self.plotWidget = plot_widget self.verticalLayoutForPlot.addWidget(self.plotWidget) ve_val = self.veLUT[self.mSliderVerticalExaggeration.value()] self.plotWidget.changeVerticalExaggeration(ve_val) @pyqtSlot(int) def onVerticalExaggerationChanged(self, value): ve_val = self.veLUT[value] self.mLblVerticalExaggeration.setText(unicode(ve_val) + 'x') self.plotWidget.changeVerticalExaggeration(ve_val) @pyqtSlot() def onPrintButtonClicked(self): self.plotWidget.printProfile() @pyqtSlot() def onConfigureSelectAction(self): dlg = QDialog() dlg.setWindowTitle(self.tr('Selection Options')) dlg.setLayout(QGridLayout()) ww_current_checkbox = QCheckBox(self.tr('Wastewater current')) status, _ = QgsProject.instance().readBoolEntry( 'Qgep', 'FollowWastewaterCurrent', True) ww_current_checkbox.setChecked(status) ww_planned_checkbox = QCheckBox(self.tr('Wastewater planned')) status, _ = QgsProject.instance().readBoolEntry( 'Qgep', 'FollowWastewaterPlanned', True) ww_planned_checkbox.setChecked(status) rw_current_checkbox = QCheckBox(self.tr('Rainwater current')) status, _ = QgsProject.instance().readBoolEntry( 'Qgep', 'FollowRainwaterCurrent', True) rw_current_checkbox.setChecked(status) rw_planned_checkbox = QCheckBox(self.tr('Rainwater planned')) status, _ = QgsProject.instance().readBoolEntry( 'Qgep', 'FollowRainwaterPlanned', True) rw_planned_checkbox.setChecked(status) btn_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) btn_box.accepted.connect(dlg.accept) btn_box.rejected.connect(dlg.reject) dlg.layout().addWidget(ww_current_checkbox) dlg.layout().addWidget(ww_planned_checkbox) dlg.layout().addWidget(rw_current_checkbox) dlg.layout().addWidget(rw_planned_checkbox) dlg.layout().addWidget(btn_box) if dlg.exec_(): QgsProject.instance().writeEntry('Qgep', 'FollowWastewaterCurrent', ww_current_checkbox.isChecked()) QgsProject.instance().writeEntry('Qgep', 'FollowWastewaterPlanned', ww_planned_checkbox.isChecked()) QgsProject.instance().writeEntry('Qgep', 'FollowRainwaterCurrent', rw_current_checkbox.isChecked()) QgsProject.instance().writeEntry('Qgep', 'FollowRainwaterPlanned', rw_planned_checkbox.isChecked()) @pyqtSlot() def onSelectCurrentPathAction(self): reaches = list() wastewater_nodes = list() wastewater_structures = list() for item in self.edges: item_information = item[2] if item_information['objType'] == 'reach': reaches.append(item_information['baseFeature']) for item in self.nodes: if item['objType'] == 'wastewater_node': wastewater_nodes.append(item['objId']) qgep_wastewater_structures_layer = QgepLayerManager.layer( 'vw_qgep_wastewater_structure') wastewater_nodes_layer = QgepLayerManager.layer('vw_wastewater_node') qgep_reach_layer = QgepLayerManager.layer('vw_qgep_reach') catchment_areas_layer = QgepLayerManager.layer('od_catchment_area') wastewater_node_list = ','.join( ("'" + id + "'" for id in wastewater_nodes)) reach_list = ','.join(("'" + id + "'" for id in reaches)) if catchment_areas_layer: request = QgsFeatureRequest() filters = list() if QgsProject.instance().readBoolEntry('Qgep', 'FollowWastewaterCurrent', True)[0]: filters.append( 'fk_wastewater_networkelement_ww_current IN ({})'.format( wastewater_node_list)) if QgsProject.instance().readBoolEntry('Qgep', 'FollowWastewaterPlanned', True)[0]: filters.append( 'fk_wastewater_networkelement_ww_planned IN ({})'.format( wastewater_node_list)) if QgsProject.instance().readBoolEntry('Qgep', 'FollowRainwaterCurrent', True)[0]: filters.append( 'fk_wastewater_networkelement_rw_current IN ({})'.format( wastewater_node_list)) if QgsProject.instance().readBoolEntry('Qgep', 'FollowRainwaterPlanned', True)[0]: filters.append( 'fk_wastewater_networkelement_rw_planned IN ({})'.format( wastewater_node_list)) if filters: request.setFilterExpression(' OR '.join(filters)) features = catchment_areas_layer.getFeatures(request) catchment_areas_layer.setSelectedFeatures( [f.id() for f in features]) if qgep_reach_layer: request = QgsFeatureRequest() request.setFilterExpression('obj_id IN ({})'.format(reach_list)) features = qgep_reach_layer.getFeatures(request) qgep_reach_layer.setSelectedFeatures([f.id() for f in features]) if wastewater_nodes_layer: request = QgsFeatureRequest() request.setFilterExpression( 'obj_id IN ({})'.format(wastewater_node_list)) features = wastewater_nodes_layer.getFeatures(request) ids = list() for feature in features: ids.append(feature.id()) wastewater_structures.append( feature['fk_wastewater_structure']) wastewater_nodes_layer.setSelectedFeatures(ids) wastewater_structure_list = ','.join( ("'" + id + "'" for id in wastewater_structures)) if qgep_wastewater_structures_layer: request = QgsFeatureRequest() request.setFilterExpression( 'obj_id IN ({})'.format(wastewater_structure_list)) features = qgep_wastewater_structures_layer.getFeatures(request) qgep_wastewater_structures_layer.setSelectedFeatures( [f.id() for f in features]) def setTree(self, nodes, edges): self.nodes = nodes self.edges = edges self.selectCurrentPathAction.setEnabled(self.nodes is not None)
def add_action( self, icon_path, text, callback, enabled_flag=True, add_to_menu=True, add_to_toolbar=True, status_tip=None, whats_this=None, parent=None): """Add a toolbar icon to the toolbar. :param icon_path: Path to the icon for this action. Can be a resource path (e.g. ':/plugins/foo/bar.png') or a normal file system path. :type icon_path: str :param text: Text that should be shown in menu items for this action. :type text: str :param callback: Function to be called when the action is triggered. :type callback: function :param enabled_flag: A flag indicating if the action should be enabled by default. Defaults to True. :type enabled_flag: bool :param add_to_menu: Flag indicating whether the action should also be added to the menu. Defaults to True. :type add_to_menu: bool :param add_to_toolbar: Flag indicating whether the action should also be added to the toolbar. Defaults to True. :type add_to_toolbar: bool :param status_tip: Optional text to show in a popup when mouse pointer hovers over the action. :type status_tip: str :param parent: Parent widget for the new action. Defaults None. :type parent: QWidget :param whats_this: Optional text to show in the status bar when the mouse pointer hovers over the action. :returns: The action that was created. Note that the action is also added to self.actions list. :rtype: QAction """ icon = QIcon(icon_path) action = QAction(icon, text, parent) action.triggered.connect(callback) action.setEnabled(enabled_flag) if status_tip is not None: action.setStatusTip(status_tip) if whats_this is not None: action.setWhatsThis(whats_this) if add_to_toolbar: self.toolbar.addAction(action) if add_to_menu: self.iface.addPluginToMenu( self.menu, action) self.actions.append(action) return action