def show_keywords_editor(self): """Show the keywords editor.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.keywords_dialog import KeywordsDialog # Next block is a fix for #776 if self.iface.activeLayer() is None: return try: keyword_io = KeywordIO() keyword_io.read_keywords(self.iface.activeLayer()) except UnsupportedProviderError: # noinspection PyUnresolvedReferences,PyCallByClass QMessageBox.warning( None, self.tr('Unsupported layer type'), self.tr( 'The layer you have selected cannot be used for ' 'analysis because its data type is unsupported.')) return # End of fix for #776 # Fix for #793 except NoKeywordsFoundError: # we will create them from scratch in the dialog pass # End of fix for #793 dialog = KeywordsDialog( self.iface.mainWindow(), self.iface, self.dock_widget) dialog.exec_() # modal
def test_on_rad_postprocessing_toggled(self): """Test hazard radio button toggle behaviour works""" layer = make_polygon_layer() defaults = get_defaults() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) button = dialog.radPostprocessing button.setChecked(False) button.click() message = ( 'Toggling the postprocessing radio did not add a ' 'category to the keywords list.') self.assertEqual(dialog.get_value_for_key( 'category'), 'postprocessing', message) message = ( 'Toggling the postprocessing radio did not add an ' 'aggregation attribute to the keywords list.') self.assertEqual(dialog.get_value_for_key( defaults['AGGR_ATTR_KEY']), 'KAB_NAME', message) message = ( 'Toggling the postprocessing radio did not add a ' 'female ratio attribute to the keywords list.') self.assertEqual(dialog.get_value_for_key( defaults['FEMALE_RATIO_ATTR_KEY']), dialog.global_default_string, message) message = ( 'Toggling the postprocessing radio did not add a ' 'female ratio default value to the keywords list.') self.assertEqual(float(dialog.get_value_for_key( defaults['FEMALE_RATIO_KEY'])), defaults['FEMALE_RATIO'], message)
def showKeywordsEditor(self): """Show the keywords editor. This slot is called when the user clicks the keyword editor toolbar icon or menu item associated with this plugin .. see also:: :func:`Plugin.initGui`. Args: None. Returns: None. Raises: no exceptions explicitly raised. """ # import here only so that it is AFTER i18n set up from safe_qgis.tools.keywords_dialog import KeywordsDialog if self.iface.activeLayer() is None: return myDialog = KeywordsDialog( self.iface.mainWindow(), self.iface, self.dockWidget) myDialog.setModal(True) myDialog.show()
def test_get_value_for_key(self): """Test get value for key works.""" make_padang_layer() dialog = KeywordsDialog(PARENT, IFACE) expected_value = 'hazard' value = dialog.get_value_for_key('category') self.assertEqual(value, expected_value)
def test_on_dsb_female_ratio_default_value_changed(self): """Test hazard radio button toggle behaviour works""" layer = make_polygon_layer() defaults = get_defaults() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) button = dialog.radPostprocessing button.setChecked(False) button.click() female_ratio_box = dialog.cboFemaleRatioAttribute # set to Don't use index = female_ratio_box.findText(dialog.do_not_use_string) message = (dialog.do_not_use_string + ' not found') self.assertNotEqual(index, -1, message) female_ratio_box.setCurrentIndex(index) message = ( 'Toggling the female ratio attribute combo to' ' "Don\'t use" did not add it to the keywords list.') self.assertEqual(dialog.get_value_for_key( defaults['FEMALE_RATIO_ATTR_KEY']), dialog.do_not_use_string, message) message = ( 'Toggling the female ratio attribute combo to' ' "Don\'t use" did not disable dsbFemaleRatioDefault.') is_enabled = dialog.dsbFemaleRatioDefault.isEnabled() assert not is_enabled, message message = ( 'Toggling the female ratio attribute combo to' ' "Don\'t use" did not remove the keyword.') assert (dialog.get_value_for_key(defaults['FEMALE_RATIO']) is None), \ message # set to TEST_REAL index = female_ratio_box.findText('TEST_REAL') message = 'TEST_REAL not found' assert (index != -1), message female_ratio_box.setCurrentIndex(index) message = ( 'Toggling the female ratio attribute combo to "TEST_REAL"' ' did not add it to the keywords list.') assert dialog.get_value_for_key( defaults['FEMALE_RATIO_ATTR_KEY']) == 'TEST_REAL', message message = ( 'Toggling the female ratio attribute combo to "TEST_REAL"' ' did not disable dsbFemaleRatioDefault.') is_enabled = dialog.dsbFemaleRatioDefault.isEnabled() assert not is_enabled, message message = ( 'Toggling the female ratio attribute combo to "TEST_REAL"' ' did not remove the keyword.') assert (dialog.get_value_for_key(defaults['FEMALE_RATIO']) is None), message
def test_getValueForKey(self): """Test get value for key works""" makePadangLayer() myDialog = KeywordsDialog(PARENT, IFACE) myExpectedValue = 'hazard' myValue = myDialog.get_value_for_key('category') myMessage = ('\nExpected key value of %s\nGot %s' % (myExpectedValue, myValue)) assert myValue == myExpectedValue, myMessage
def test_on_radHazard_toggled(self): """Test hazard radio button toggle behaviour works""" myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radHazard myButton.setChecked(False) myButton.click() myMessage = ('Toggling the hazard radio did not add a category ' 'to the keywords list.') assert myDialog.get_value_for_key('category') == 'hazard', myMessage
def test_reset(self): """Test form reset works.""" dialog = KeywordsDialog(PARENT, IFACE) dialog.leTitle.setText('Foo') dialog.reset(False) expected_result = '' result = dialog.leTitle.text() message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_on_radHazard_toggled(self): """Test hazard radio button toggle behaviour works""" myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radHazard myButton.setChecked(False) # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myMessage = ('Toggling the hazard radio did not add a category ' 'to the keywords list.') assert myDialog.get_value_for_key('category') == 'hazard', myMessage
def test_reset(self): """Test form reset works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.leTitle.setText('Foo') myDialog.reset(False) myExpectedResult = '' myResult = myDialog.leTitle.text() myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage
def Xtest_on_radExposure_toggled(self): """Test exposure radio button toggle behaviour works""" # Cannot get this test to work, but it works fine in the safe_qgis myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radExposure myButton.setChecked(False) myButton.click() myMessage = ('Toggling the exposure radio did not add a category ' 'to the keywords list.') assert myDialog.get_value_for_key('category') == 'exposure', myMessage
def test_on_rad_hazard_toggled(self): """Test hazard radio button toggle behaviour works""" dialog = KeywordsDialog(PARENT, IFACE) button = dialog.radHazard button.setChecked(False) button.click() message = ( 'Toggling the hazard radio did not add a category ' 'to the keywords list.') self.assertEqual( dialog.get_value_for_key('category'), 'hazard', message)
def show_keywords_editor(self): """Show the keywords editor.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.keywords_dialog import KeywordsDialog if self.iface.activeLayer() is None: return myDialog = KeywordsDialog( self.iface.mainWindow(), self.iface, self.dockWidget) myDialog.exec_() # modal
def test_remove_item_by_value(self): """Test remove item by its value works.""" make_padang_layer() dialog = KeywordsDialog(PARENT, IFACE) dialog.remove_item_by_value('hazard') keywords = dialog.get_keywords() expected_keywords = { 'source': 'USGS', 'title': 'An earthquake in Padang like in 2009', 'subcategory': 'earthquake', 'unit': 'MMI'} self.assertEqual(keywords, expected_keywords)
def test_add_warnings_for_colons(self): """Test add entry to list works.""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.add_list_entry('bar', 'fo:o') result = dialog.get_value_for_key('bar') expected_result = 'fo.o' message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message) # # Check the user gets a message if they put colons in the value # expected_result = 'Colons are not allowed, replaced with "."' result = str(dialog.lblMessage.text()) message = ( 'lblMessage error \nGot: %s\nExpected: %s\n' % (result, expected_result)) self.assertEqual(result, expected_result, message) # # Check the user gets a message if they put colons in the key # dialog.add_list_entry('ba:r', 'foo') expected_result = 'Colons are not allowed, replaced with "."' result = str(dialog.lblMessage.text()) message = ( 'lblMessage error \nGot: %s\nExpected: %s\n' % (result, expected_result)) self.assertEqual(result, expected_result, message)
def test_addWarningsForColons(self): """Test add entry to list works""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myDialog.add_list_entry('bar', 'fo:o') myResult = myDialog.get_value_for_key('bar') myExpectedResult = 'fo.o' myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage # # Check the user gets a message if they put colons in the value # myExpectedResult = 'Colons are not allowed, replaced with "."' myResult = str(myDialog.lblMessage.text()) myMessage = ('lblMessage error \nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage # # Check the user gets a message if they put colons in the key # myDialog.add_list_entry('ba:r', 'foo') myExpectedResult = 'Colons are not allowed, replaced with "."' myResult = str(myDialog.lblMessage.text()) myMessage = ('lblMessage error \nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage
def test_load_state_from_keywords(self): """Test load state from keywords works.""" layer = make_padang_layer() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords() keywords = dialog.get_keywords() expected_keywords = { 'title': 'An earthquake in Padang like in 2009', 'category': 'hazard', 'source': 'USGS', 'subcategory': 'earthquake', 'unit': 'MMI'} self.assertEqual(keywords, expected_keywords)
def test_layer_without_keywords(self): """Test load state from keywords works""" myDialog = KeywordsDialog(PARENT, IFACE) myLayer = make_keywordless_layer() myDialog.layer = myLayer myDialog.load_state_from_keywords() myKeywords = myDialog.get_keywords() #check that a default title is given (see #https://github.com/AIFDR/inasafe/issues/111) myExpectedKeywords = { 'category': 'exposure', 'title': 'Keywordless Layer'} myMessage = ('\nGot: %s\nExpected: %s\n' % (myKeywords, myExpectedKeywords)) assert myKeywords == myExpectedKeywords, myMessage
def test_removeItemByValue(self): """Test remove item by its value works""" makePadangLayer() myDialog = KeywordsDialog(PARENT, IFACE) myDialog.remove_item_by_value('hazard') myKeywords = myDialog.get_keywords() myExpectedKeywords = {'source': 'USGS', 'title': 'An earthquake in Padang like in 2009', 'subcategory': 'earthquake', 'unit': 'MMI'} myMessage = ('\nGot: %s\nExpected: %s\n' % (myKeywords, myExpectedKeywords)) assert myKeywords == myExpectedKeywords, myMessage
def test_on_pbn_remove_clicked(self): """Test pressing remove works on key list""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) result = dialog.lstKeywords.count() expected_result = 0 message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message) dialog.add_list_entry('bar', 'foo') result = dialog.lstKeywords.count() expected_result = 1 message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_on_subcategory_currentindexchanged(self): """Test subcategory combo change event works""" dialog = KeywordsDialog(PARENT, IFACE) button = dialog.radHazard button.setChecked(True) button = dialog.radExposure button.click() combo = dialog.cboSubcategory combo.setCurrentIndex(1) # change from 'Not set' to 'structure' message = ( 'Changing the subcategory did not add %s to the keywords list' % combo.currentText()) key = dialog.get_value_for_key('subcategory') self.assertTrue(key is not None, message) assert key in str(combo.currentText()), message
def test_on_cboSubcategory_currentIndexChanged(self): """Test subcategory combo change event works""" myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radHazard myButton.setChecked(True) myButton = myDialog.radExposure myButton.click() myCombo = myDialog.cboSubcategory myCombo.setCurrentIndex(1) # change from 'Not set' to 'structure' myMessage = ('Changing the subcategory did not add ' 'to the keywords list for %s' % myCombo.currentText()) myKey = myDialog.get_value_for_key('subcategory') assert myKey is not None, myMessage assert myKey in str(myCombo.currentText()), myMessage
def test_set_subcategory_list(self): """Test set subcategory list works""" dialog = KeywordsDialog(PARENT, IFACE) subcategory_list = OrderedDict([ ('population [density]', 'population [density]'), ('population [count]', 'population [count]'), ('building', 'building'), ('building [osm]', 'building [osm]'), ('building [sigab]', 'building [sigab]'), ('road', 'road')]) selected_item = 'building' dialog.set_subcategory_list(subcategory_list, selected_item) result = str(dialog.cboSubcategory.currentText()) message = ('\nGot: %s\nExpected: %s\n' % (result, selected_item)) self.assertTrue(result == selected_item, message)
def test_loadStateFromKeywords(self): """Test load state from keywords works""" myDialog = KeywordsDialog(PARENT, IFACE) myLayer = makePadangLayer() myDialog.layer = myLayer myDialog.load_state_from_keywords() myKeywords = myDialog.get_keywords() myExpectedKeywords = {'title': 'An earthquake in Padang like in 2009', 'category': 'hazard', 'source': 'USGS', 'subcategory': 'earthquake', 'unit': 'MMI'} myMessage = ('\nGot:\n%s\nExpected:\n%s\n' % (myKeywords, myExpectedKeywords)) assert myKeywords == myExpectedKeywords, myMessage
def test_add_keyword_when_press_ok_button(self): """Test add keyword when ok button is pressed.""" # _, path = makePadangLayerClone() clone_padang_layer() dialog = KeywordsDialog(PARENT, IFACE) dialog.radUserDefined.setChecked(True) dialog.leKey.setText('foo') dialog.leValue.setText('bar') ok_button = dialog.buttonBox.button(QtGui.QDialogButtonBox.Ok) ok_button.click() # delete temp file # removeTempFile(path) expected_result = 'bar' result = dialog.get_value_for_key('foo') self.assertEqual(result, expected_result)
def test_on_dsbFemaleRatioDefault_valueChanged(self): """Test hazard radio button toggle behaviour works""" myLayer = makePolygonLayer() myDefaults = defaults() myDialog = KeywordsDialog(PARENT, IFACE, layer=myLayer) myButton = myDialog.radPostprocessing myButton.setChecked(False) # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myFemaleRatioAttrBox = myDialog.cboFemaleRatioAttribute #set to Don't use myIndex = myFemaleRatioAttrBox.findText( myDialog.tr('Don\'t use')) myMessage = (myDialog.tr('Don\'t use') + ' not found') assert (myIndex != -1), myMessage myFemaleRatioAttrBox.setCurrentIndex(myIndex) myMessage = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not add it to the keywords list.') assert myDialog.get_value_for_key( myDefaults['FEM_RATIO_ATTR_KEY']) == myDialog.tr('Don\'t use'), \ myMessage myMessage = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not disable dsbFemaleRatioDefault.') myIsEnabled = myDialog.dsbFemaleRatioDefault.isEnabled() assert not myIsEnabled, myMessage myMessage = ('Toggling the female ratio attribute combo to' ' "Don\'t use" did not remove the keyword.') assert (myDialog.get_value_for_key(myDefaults['FEM_RATIO']) is None), \ myMessage #set to TEST_REAL myIndex = myFemaleRatioAttrBox.findText('TEST_REAL') myMessage = 'TEST_REAL not found' assert (myIndex != -1), myMessage myFemaleRatioAttrBox.setCurrentIndex(myIndex) myMessage = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not add it to the keywords list.') assert myDialog.get_value_for_key( myDefaults['FEM_RATIO_ATTR_KEY']) == 'TEST_REAL', myMessage myMessage = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not disable dsbFemaleRatioDefault.') myIsEnabled = myDialog.dsbFemaleRatioDefault.isEnabled() assert not myIsEnabled, myMessage myMessage = ('Toggling the female ratio attribute combo to "TEST_REAL"' ' did not remove the keyword.') assert (myDialog.get_value_for_key(myDefaults['FEM_RATIO']) is None), myMessage
def test_on_pbnRemove_clicked(self): """Test pressing remove works on key list""" myDialog = KeywordsDialog(PARENT, IFACE) myDialog.reset(False) myResult = myDialog.lstKeywords.count() myExpectedResult = 0 myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage myDialog.add_list_entry('bar', 'foo') myResult = myDialog.lstKeywords.count() myExpectedResult = 1 myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) #print 'Dict', myDialog.getKeywords() assert myResult == myExpectedResult, myMessage
def test_addKeywordWhenPressOkButton(self): """Test add keyword when ok button is pressed.""" #_, myFile = makePadangLayerClone() makePadangLayerClone() myDialog = KeywordsDialog(PARENT, IFACE) myDialog.radUserDefined.setChecked(True) myDialog.leKey.setText('foo') myDialog.leValue.setText('bar') okButton = myDialog.buttonBox.button(QtGui.QDialogButtonBox.Ok) okButton.click() # delete temp file # removeTempFile(myFile) myExpectedResult = 'bar' myResult = myDialog.get_value_for_key('foo') myMessage = ('\nGot: %s\nExpected: %s\n' % (myResult, myExpectedResult)) assert myExpectedResult == myResult, myMessage
def test_on_cboSubcategory_currentIndexChanged(self): """Test subcategory combo change event works""" myDialog = KeywordsDialog(PARENT, IFACE) myButton = myDialog.radHazard myButton.setChecked(True) myButton = myDialog.radExposure # noinspection PyArgumentList QTest.mouseClick(myButton, QtCore.Qt.LeftButton) myCombo = myDialog.cboSubcategory # noinspection PyArgumentList QTest.mouseClick(myCombo, QtCore.Qt.LeftButton) QTest.keyClick(myCombo, QtCore.Qt.Key_Up) QTest.keyClick(myCombo, QtCore.Qt.Key_Enter) myMessage = ('Changing the subcategory did not add ' 'to the keywords list for %s' % myCombo.currentText()) myKey = myDialog.get_value_for_key('subcategory') assert myKey is not None, myMessage assert myKey in str(myCombo.currentText()), myMessage
def test_remove_iItem_by_key(self): """Test remove item by its key works.""" dialog = KeywordsDialog(PARENT, IFACE) dialog.reset(False) dialog.add_list_entry('bar', 'foo') dialog.remove_item_by_key('bar') result = dialog.lstKeywords.count() expected_result = 0 message = '\nGot: %s\nExpected: %s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_layer_without_keywords(self): """Test load state from keywords works.""" layer = make_keywordless_layer() dialog = KeywordsDialog(PARENT, IFACE, layer=layer) dialog.load_state_from_keywords()
def test_layer_without_keywords(self): """Test load state from keywords works""" myDialog = KeywordsDialog(PARENT, IFACE) myLayer = make_keywordless_layer() myDialog.layer = myLayer myDialog.load_state_from_keywords()
class Plugin: """The QGIS interface implementation for the InaSAFE plugin. This class acts as the 'glue' between QGIS and our custom logic. It creates a toolbar and menu bar entry and launches the InaSAFE user interface if these are activated. """ def __init__(self, iface): """Class constructor. On instantiation, the plugin instance will be assigned a copy of the QGIS iface object which will allow this plugin to access and manipulate the running QGIS instance that spawned it. :param iface:Quantum GIS iface instance. This instance is automatically passed to the plugin by QGIS when it loads the plugin. :type iface: QGisAppInterface """ # Save reference to the QGIS interface self.iface = iface self.dock_widget = None self.action_import_dialog = None self.action_save_scenario = None self.action_batch_runner = None self.action_shake_converter = None self.action_minimum_needs = None self.action_global_minimum_needs = None self.action_impact_merge_dlg = None self.key_action = None self.action_function_browser = None self.action_options = None self.action_keywords_dialog = None self.action_keywords_wizard = None self.action_extent_selector = None self.translator = None self.toolbar = None self.actions = [] # list of all QActions we create for InaSAFE self.action_dock = None self.action_toggle_rubberbands = None self.message_bar_item = None # print self.tr('InaSAFE') # For enable/disable the keyword editor icon self.iface.currentLayerChanged.connect(self.layer_changed) # noinspection PyArgumentList def change_i18n(self, new_locale): """Change internationalisation for the plugin. Override the system locale and then see if we can get a valid translation file for whatever locale is effectively being used. :param new_locale: the new locale i.e. 'id', 'af', etc. :type new_locale: str :raises: TranslationLoadException """ os.environ['LANG'] = str(new_locale) LOGGER.debug('%s %s %s' % (new_locale, QLocale.system().name(), os.environ['LANG'])) root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) translation_path = os.path.join(root, 'safe_qgis', 'i18n', 'inasafe_' + str(new_locale) + '.qm') if os.path.exists(translation_path): self.translator = QTranslator() result = self.translator.load(translation_path) if not result: message = 'Failed to load translation for %s' % new_locale raise TranslationLoadError(message) # noinspection PyTypeChecker,PyCallByClass QCoreApplication.installTranslator(self.translator) LOGGER.debug('%s %s' % (translation_path, os.path.exists(translation_path))) # noinspection PyMethodMayBeStatic def tr(self, message): """Get the translation for a string using Qt translation API. We implement this ourselves since we do not inherit QObject. :param message: String for translation. :type message: str, QString :returns: Translated version of message. :rtype: QString """ # noinspection PyTypeChecker,PyArgumentList,PyCallByClass return QCoreApplication.translate('Plugin', message) def add_action(self, action, add_to_toolbar=True): """Add a toolbar icon to the InaSAFE toolbar. :param action: The action that should be added to the toolbar. :type action: QAction :param add_to_toolbar: Flag indicating whether the action should also be added to the InaSAFE toolbar. Defaults to True. :type add_to_toolbar: bool """ # store in the class list of actions for easy plugin unloading self.actions.append(action) self.iface.addPluginToMenu(self.tr('InaSAFE'), action) if add_to_toolbar: self.toolbar.addAction(action) # noinspection PyPep8Naming def initGui(self): """Gui initialisation procedure (for QGIS plugin api). .. note:: Don't change the name of this method from initGui! This method is called by QGIS and should be used to set up any graphical user interface elements that should appear in QGIS by default (i.e. before the user performs any explicit action with the plugin). """ self.toolbar = self.iface.addToolBar('InaSAFE') self.toolbar.setObjectName('InaSAFEToolBar') # Import dock here as it needs to be imported AFTER i18n is set up from safe_qgis.widgets.dock import Dock self.dock_widget = None # -------------------------------------- # Create action for plugin dockable window (show/hide) # -------------------------------------- # pylint: disable=W0201 self.action_dock = QAction(QIcon(':/plugins/inasafe/icon.svg'), self.tr('Toggle InaSAFE Dock'), self.iface.mainWindow()) self.action_dock.setObjectName('InaSAFEDockToggle') self.action_dock.setStatusTip(self.tr('Show/hide InaSAFE dock widget')) self.action_dock.setWhatsThis(self.tr('Show/hide InaSAFE dock widget')) self.action_dock.setCheckable(True) self.action_dock.setChecked(True) self.action_dock.triggered.connect(self.toggle_dock_visibility) self.add_action(self.action_dock) # -------------------------------------- # Create action for keywords editor # -------------------------------------- self.action_keywords_dialog = QAction( QIcon(':/plugins/inasafe/show-keyword-editor.svg'), self.tr('InaSAFE Keyword Editor'), self.iface.mainWindow()) self.action_keywords_dialog.setStatusTip( self.tr('Open InaSAFE keywords editor')) self.action_keywords_dialog.setWhatsThis( self.tr('Open InaSAFE keywords editor')) self.action_keywords_dialog.setEnabled(False) self.action_keywords_dialog.triggered.connect( self.show_keywords_editor) self.add_action(self.action_keywords_dialog) # -------------------------------------- # Create action for keywords creation wizard # -------------------------------------- self.action_keywords_wizard = QAction( QIcon(':/plugins/inasafe/show-keyword-wizard.svg'), self.tr('InaSAFE Keywords Creation Wizard'), self.iface.mainWindow()) self.action_keywords_wizard.setStatusTip( self.tr('Open InaSAFE keywords creation wizard')) self.action_keywords_wizard.setWhatsThis( self.tr('Open InaSAFE keywords creation wizard')) self.action_keywords_wizard.setEnabled(False) self.action_keywords_wizard.triggered.connect( self.show_keywords_wizard) self.add_action(self.action_keywords_wizard) # -------------------------------------- # Create action for options dialog # -------------------------------------- self.action_options = QAction( QIcon(':/plugins/inasafe/configure-inasafe.svg'), self.tr('InaSAFE Options'), self.iface.mainWindow()) self.action_options.setStatusTip( self.tr('Open InaSAFE options dialog')) self.action_options.setWhatsThis( self.tr('Open InaSAFE options dialog')) self.action_options.triggered.connect(self.show_options) self.add_action(self.action_options) # -------------------------------------- # Create action for impact functions doc dialog # -------------------------------------- self.action_function_browser = QAction( QIcon(':/plugins/inasafe/show-impact-functions.svg'), self.tr('InaSAFE Impact Functions Browser'), self.iface.mainWindow()) self.action_function_browser.setStatusTip( self.tr('Open InaSAFE Impact Functions Browser')) self.action_function_browser.setWhatsThis( self.tr('Open InaSAFE Impact Functions Browser')) self.action_function_browser.triggered.connect( self.show_function_browser) self.add_action(self.action_function_browser) # Short cut for Open Impact Functions Doc self.key_action = QAction("Test Plugin", self.iface.mainWindow()) self.iface.registerMainWindowAction(self.key_action, "F7") self.key_action.triggered.connect(self.shortcut_f7) # --------------------------------------- # Create action for minimum needs dialog # --------------------------------------- self.action_minimum_needs = QAction( QIcon(':/plugins/inasafe/show-minimum-needs.svg'), self.tr('InaSAFE Minimum Needs Tool'), self.iface.mainWindow()) self.action_minimum_needs.setStatusTip( self.tr('Open InaSAFE minimum needs tool')) self.action_minimum_needs.setWhatsThis( self.tr('Open InaSAFE minimum needs tool')) self.action_minimum_needs.triggered.connect(self.show_minimum_needs) self.add_action(self.action_minimum_needs) # ---------------------------------------------- # Create action for global minimum needs dialog # ---------------------------------------------- self.action_global_minimum_needs = QAction( QIcon(':/plugins/inasafe/show-global-minimum-needs.svg'), self.tr('InaSAFE Global Minimum Needs Configuration'), self.iface.mainWindow()) self.action_global_minimum_needs.setStatusTip( self.tr('Open InaSAFE global minimum needs configuration')) self.action_global_minimum_needs.setWhatsThis( self.tr('Open InaSAFE global minimum needs configuration')) self.action_global_minimum_needs.triggered.connect( self.show_global_minimum_needs_configuration) self.add_action(self.action_global_minimum_needs) # --------------------------------------- # Create action for converter dialog # --------------------------------------- self.action_shake_converter = QAction( QIcon(':/plugins/inasafe/show-converter-tool.svg'), self.tr('InaSAFE Converter'), self.iface.mainWindow()) self.action_shake_converter.setStatusTip( self.tr('Open InaSAFE Converter')) self.action_shake_converter.setWhatsThis( self.tr('Open InaSAFE Converter')) self.action_shake_converter.triggered.connect( self.show_shakemap_importer) self.add_action(self.action_shake_converter) # --------------------------------------- # Create action for batch runner dialog # --------------------------------------- self.action_batch_runner = QAction( QIcon(':/plugins/inasafe/show-batch-runner.svg'), self.tr('InaSAFE Batch Runner'), self.iface.mainWindow()) self.action_batch_runner.setStatusTip( self.tr('Open InaSAFE Batch Runner')) self.action_batch_runner.setWhatsThis( self.tr('Open InaSAFE Batch Runner')) self.action_batch_runner.triggered.connect(self.show_batch_runner) self.add_action(self.action_batch_runner) # --------------------------------------- # Create action for batch runner dialog # --------------------------------------- self.action_save_scenario = QAction( QIcon(':/plugins/inasafe/save-as-scenario.svg'), self.tr('Save current scenario'), self.iface.mainWindow()) message = self.tr('Save current scenario to text file') self.action_save_scenario.setStatusTip(message) self.action_save_scenario.setWhatsThis(message) # noinspection PyUnresolvedReferences self.action_save_scenario.triggered.connect(self.save_scenario) self.add_action(self.action_save_scenario) # -------------------------------------- # Create action for import OSM Dialog # -------------------------------------- self.action_import_dialog = QAction( QIcon(':/plugins/inasafe/show-osm-download.svg'), self.tr('InaSAFE OpenStreetMap Downloader'), self.iface.mainWindow()) self.action_import_dialog.setStatusTip( self.tr('InaSAFE OpenStreetMap Downloader')) self.action_import_dialog.setWhatsThis( self.tr('InaSAFE OpenStreetMap Downloader')) self.action_import_dialog.triggered.connect(self.show_osm_downloader) self.add_action(self.action_import_dialog) # -------------------------------------- # Create action for impact layer merge Dialog # -------------------------------------- self.action_impact_merge_dlg = QAction( QIcon(':/plugins/inasafe/show-impact-merge.svg'), self.tr('InaSAFE Impact Layer Merge'), self.iface.mainWindow()) self.action_impact_merge_dlg.setStatusTip( self.tr('InaSAFE Impact Layer Merge')) self.action_impact_merge_dlg.setWhatsThis( self.tr('InaSAFE Impact Layer Merge')) self.action_impact_merge_dlg.triggered.connect(self.show_impact_merge) self.add_action(self.action_impact_merge_dlg) # -------------------------------------- # create dockwidget and tabify it with the legend # -------------------------------------- self.dock_widget = Dock(self.iface) self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock_widget) myLegendTab = self.iface.mainWindow().findChild(QApplication, 'Legend') if myLegendTab: self.iface.mainWindow().tabifyDockWidget(myLegendTab, self.dock_widget) self.dock_widget.raise_() # # Hook up a slot for when the dock is hidden using its close button # or view-panels # self.dock_widget.visibilityChanged.connect(self.toggle_inasafe_action) # Also deal with the fact that on start of QGIS dock may already be # hidden. self.action_dock.setChecked(self.dock_widget.isVisible()) # pylint: disable=W0201 # --------------------------------------- # Create action for toggling rubber bands # --------------------------------------- self.action_toggle_rubberbands = QAction( QIcon(':/plugins/inasafe/toggle-rubber-bands.svg'), self.tr('Toggle scenario outlines'), self.iface.mainWindow()) message = self.tr('Toggle rubber bands showing scenarion extents.') self.action_toggle_rubberbands.setStatusTip(message) self.action_toggle_rubberbands.setWhatsThis(message) # Set initial state self.action_toggle_rubberbands.setCheckable(True) settings = QSettings() flag = bool(settings.value('inasafe/showRubberBands', False, type=bool)) self.action_toggle_rubberbands.setChecked(flag) # noinspection PyUnresolvedReferences self.action_toggle_rubberbands.triggered.connect( self.dock_widget.toggle_rubber_bands) self.add_action(self.action_toggle_rubberbands) # --------------------------------------- # Create action for analysis extent dialog # --------------------------------------- self.action_extent_selector = QAction( QIcon(':/plugins/inasafe/set-extents-tool.svg'), self.tr('Set the analysis area for InaSAFE'), self.iface.mainWindow()) self.action_extent_selector.setStatusTip( self.tr('Set the analysis area for InaSAFE')) self.action_extent_selector.setWhatsThis( self.tr('Set the analysis area for InaSAFE')) self.action_extent_selector.triggered.connect( self.show_extent_selector) self.add_action(self.action_extent_selector) # noinspection PyMethodMayBeStatic def clear_modules(self): """Unload inasafe functions and try to return QGIS to before InaSAFE. """ from safe.impact_functions import core core.unload_plugins() # next lets force remove any inasafe related modules modules = [] for myModule in sys.modules: if 'inasafe' in myModule: # Check if it is really one of our modules i.e. exists in the # plugin directory tokens = myModule.split('.') path = '' for myToken in tokens: path += os.path.sep + myToken parent = os.path.abspath( os.path.join(__file__, os.path.pardir, os.path.pardir)) full_path = os.path.join(parent, path + '.py') if os.path.exists(os.path.abspath(full_path)): LOGGER.debug('Removing: %s' % myModule) modules.append(myModule) for myModule in modules: del (sys.modules[myModule]) for myModule in sys.modules: if 'inasafe' in myModule: print myModule # Lets also clean up all the path additions that were made package_path = os.path.abspath( os.path.join(os.path.dirname(__file__), os.path.pardir)) LOGGER.debug('Path to remove: %s' % package_path) # We use a list comprehension to ensure duplicate entries are removed LOGGER.debug(sys.path) sys.path = [y for y in sys.path if package_path not in y] LOGGER.debug(sys.path) def unload(self): """GUI breakdown procedure (for QGIS plugin api). .. note:: Don't change the name of this method from unload! This method is called by QGIS and should be used to *remove* any graphical user interface elements that should appear in QGIS. """ # Remove the plugin menu item and icon for myAction in self.actions: self.iface.removePluginMenu(self.tr('InaSAFE'), myAction) self.iface.removeToolBarIcon(myAction) self.iface.mainWindow().removeDockWidget(self.dock_widget) self.iface.mainWindow().removeToolBar(self.toolbar) self.dock_widget.setVisible(False) self.dock_widget.destroy() self.iface.currentLayerChanged.disconnect(self.layer_changed) self.clear_modules() def toggle_inasafe_action(self, checked): """Check or un-check the toggle inaSAFE toolbar button. This slot is called when the user hides the inaSAFE panel using its close button or using view->panels. :param checked: True if the dock should be shown, otherwise False. :type checked: bool """ self.action_dock.setChecked(checked) # Run method that performs all the real work def toggle_dock_visibility(self): """Show or hide the dock widget.""" if self.dock_widget.isVisible(): self.dock_widget.setVisible(False) else: self.dock_widget.setVisible(True) self.dock_widget.raise_() def show_extent_selector(self): """Show the extent selector widget for defining analysis extents.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.extent_selector_dialog import ExtentSelectorDialog widget = ExtentSelectorDialog(self.iface, self.iface.mainWindow(), extent=self.dock_widget.user_extent, crs=self.dock_widget.user_extent_crs) widget.clear_extent.connect( self.dock_widget.clear_user_analysis_extent) widget.extent_defined.connect( self.dock_widget.define_user_analysis_extent) # Needs to be non modal to support hide -> interact with map -> show widget.show() # non modal def show_minimum_needs(self): """Show the minimum needs dialog.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.minimum_needs.needs_calculator_dialog import ( NeedsCalculatorDialog) dialog = NeedsCalculatorDialog(self.iface.mainWindow()) dialog.show() # non modal def show_global_minimum_needs_configuration(self): """Show the minimum needs dialog.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.minimum_needs.needs_manager_dialog import ( NeedsManagerDialog) dialog = NeedsManagerDialog(self.iface.mainWindow()) dialog.exec_() # modal def show_impact_merge(self): """Show the impact layer merge dialog.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.impact_merge_dialog import ImpactMergeDialog dialog = ImpactMergeDialog(self.iface.mainWindow()) dialog.exec_() # modal def show_options(self): """Show the options dialog.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.options_dialog import OptionsDialog dialog = OptionsDialog(self.iface, self.dock_widget, self.iface.mainWindow()) dialog.exec_() # modal def show_keywords_editor(self): """Show the keywords editor.""" # import here only so that it is AFTER i18n set up from safe_qgis.tools.keywords_dialog import KeywordsDialog # Next block is a fix for #776 if self.iface.activeLayer() is None: return try: keyword_io = KeywordIO() keyword_io.read_keywords(self.iface.activeLayer()) except UnsupportedProviderError: # noinspection PyUnresolvedReferences,PyCallByClass # noinspection PyTypeChecker,PyArgumentList QMessageBox.warning( None, self.tr('Unsupported layer type'), self.tr('The layer you have selected cannot be used for ' 'analysis because its data type is unsupported.')) return # End of fix for #776 # Fix for #793 except NoKeywordsFoundError: # we will create them from scratch in the dialog pass # End of fix for #793 # Fix for filtered-layer except InvalidParameterError, e: # noinspection PyTypeChecker,PyTypeChecker,PyArgumentList QMessageBox.warning(None, self.tr('Invalid Layer'), e.message) return dialog = KeywordsDialog(self.iface.mainWindow(), self.iface, self.dock_widget) dialog.exec_() # modal
def test_check_aggregation(self): """Test for keywords dialog's behavior for aggregation layer.""" layer = clone_shp_layer(name='kabupaten_jakarta', include_keywords=True, source_directory=BOUNDDATA) dialog = KeywordsDialog(PARENT, IFACE, layer=layer) # Load existing keywords keywords = dialog.get_keywords() expected_keywords = { 'category': 'postprocessing', 'aggregation attribute': 'KAB_NAME', 'title': 'kabupaten jakarta', 'elderly ratio attribute': 'Global default', 'youth ratio default': '0.263', 'source': 'OpenStreetMap', 'elderly ratio default': '0.078', 'adult ratio attribute': 'Global default', 'female ratio attribute': 'Global default', 'youth ratio attribute': 'Global default', 'female ratio default': '0.5', 'adult ratio default': '0.659' } message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (True, good_sum_ratio) self.assertEqual(True, good_sum_ratio, message) # Change youth ratio attribute to Don't Use dialog.cboYouthRatioAttribute.setCurrentIndex(1) keywords = dialog.get_keywords() expected_keywords = { 'category': 'postprocessing', 'aggregation attribute': 'KAB_NAME', 'title': 'kabupaten jakarta', 'elderly ratio attribute': 'Global default', 'source': 'OpenStreetMap', 'elderly ratio default': '0.078', 'adult ratio attribute': 'Global default', 'female ratio attribute': 'Global default', 'youth ratio attribute': 'Don\'t use', 'female ratio default': '0.5', 'adult ratio default': '0.659' } message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (True, good_sum_ratio) self.assertEqual(True, good_sum_ratio, message) # Change youth ratio attribute to Global Default # Change youth ratio default to 0.99 dialog.cboYouthRatioAttribute.setCurrentIndex(0) dialog.dsbYouthRatioDefault.setValue(0.99) keywords = dialog.get_keywords() expected_keywords = { 'category': 'postprocessing', 'aggregation attribute': 'KAB_NAME', 'title': 'kabupaten jakarta', 'elderly ratio attribute': 'Global default', 'youth ratio default': '0.99', 'source': 'OpenStreetMap', 'elderly ratio default': '0.078', 'adult ratio attribute': 'Global default', 'female ratio attribute': 'Global default', 'youth ratio attribute': 'Global default', 'female ratio default': '0.5', 'adult ratio default': '0.659' } message = 'Expected %s but I got %s' % (expected_keywords, keywords) self.assertDictEqual(expected_keywords, keywords, message) good_sum_ratio, _ = dialog.age_ratios_are_valid(keywords) message = 'Expected %s but I got %s' % (False, good_sum_ratio) self.assertEqual(False, good_sum_ratio, message) # We need to delete reference to layer on Windows before removing # the files del layer del dialog.layer # Using clone_shp_layer the files are saved in testing dir under # InaSAFE temp dir shutil.rmtree(temp_dir(sub_dir='testing'))