def test_translation(self):
        """Test for metadata translation."""
        from safe.test.utilities import (
            clone_shp_layer, remove_vector_temp_file)
        from safe.test.utilities import BOUNDDATA

        from safe.test.utilities import get_qgis_app
        # Get QGis app handle
        # noinspection PyPep8Naming
        _, _, IFACE, PARENT = get_qgis_app()

        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        layer = clone_shp_layer(
            name='kabupaten_jakarta',
            include_keywords=True,
            source_directory=BOUNDDATA)
        # noinspection PyTypeChecker
        dialog = WizardDialog(PARENT, IFACE)
        dialog.set_keywords_creation_mode(layer)
        expected_categories = ['ancaman']
        # noinspection PyTypeChecker
        self.check_list(expected_categories,
                        dialog.step_kw_purpose.lstCategories)

        self.check_current_text(
            'ancaman', dialog.step_kw_purpose.lstCategories)

        dialog.pbnNext.click()

        remove_vector_temp_file(layer.source())
    def launch_keyword_wizard(self):
        """Launch keyword creation wizard."""
        # make sure selected layer is the output layer
        if self.iface.activeLayer() != self.output_layer:
            return

        # launch wizard dialog
        keyword_wizard = WizardDialog(
            self.iface.mainWindow(), self.iface, self.dock_widget)
        keyword_wizard.set_keywords_creation_mode(self.output_layer)
        keyword_wizard.exec_()  # modal
Example #3
0
    def show_function_centric_wizard(self):
        """Show the function centric wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since it is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_function_centric_mode()
        # non-modal in order to hide for selecting user extent
        self.wizard.show()
Example #4
0
    def show_keywords_wizard(self):
        """Show the keywords creation wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        if self.iface.activeLayer() is None:
            return

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since the IFCW is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_keywords_creation_mode()
        self.wizard.exec_()  # modal
class MultiBufferDialog(QtGui.QDialog, FORM_CLASS):
    """Dialog implementation class for the InaSAFE multi buffer tool."""

    def __init__(self, parent=None, iface=None, dock_widget=None):
        """Constructor for the multi buffer dialog.

        :param parent: Parent widget of this dialog.
        :type parent: QWidget
        """
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)
        self.setWindowTitle(self.tr('InaSAFE Multi Buffer Tool'))
        self.parent = parent
        self.iface = iface
        self.dock_widget = dock_widget
        self.keyword_wizard = None

        # output file properties initialisation
        self.data_store = None
        self.output_directory = None
        self.output_filename = None
        self.output_extension = None
        self.output_layer = None
        self.classification = []

        # set icon
        self.add_class_button.setIcon(
            QIcon(resources_path('img', 'icons', 'add.svg')))
        self.remove_class_button.setIcon(
            QIcon(resources_path('img', 'icons', 'remove.svg')))

        # prepare dialog initialisation
        self.layer.setFilters(QgsMapLayerProxyModel.VectorLayer)
        self.directory_button_status()
        self.add_class_button_status()
        self.ok_button_status()
        self.output_form.setPlaceholderText(
            self.tr('[Create a temporary layer]'))
        self.keyword_wizard_checkbox.setChecked(True)

        # set signal
        self.layer.layerChanged.connect(self.directory_button_status)
        self.layer.layerChanged.connect(self.ok_button_status)
        self.output_form.textChanged.connect(self.ok_button_status)
        self.directory_button.clicked.connect(
            self.on_directory_button_tool_clicked)
        self.radius_form.valueChanged.connect(self.add_class_button_status)
        self.class_form.textChanged.connect(self.add_class_button_status)
        self.add_class_button.clicked.connect(
            self.populate_hazard_classification)
        self.add_class_button.clicked.connect(self.ok_button_status)
        self.remove_class_button.clicked.connect(
            self.remove_selected_classification)
        self.remove_class_button.clicked.connect(self.ok_button_status)

        # Set up things for context help
        self.help_button = self.button_box.button(QtGui.QDialogButtonBox.Help)
        # Allow toggling the help button
        self.help_button.setCheckable(True)
        self.help_button.toggled.connect(self.help_toggled)
        self.main_stacked_widget.setCurrentIndex(1)

        # Fix for issue 1699 - cancel button does nothing
        cancel_button = self.button_box.button(QtGui.QDialogButtonBox.Cancel)
        cancel_button.clicked.connect(self.reject)
        # Fix ends
        ok_button = self.button_box.button(QtGui.QDialogButtonBox.Ok)
        ok_button.clicked.connect(self.accept)

    def accept(self):
        """Process the layer for multi buffering and generate a new layer.

        .. note:: This is called on OK click.
        """
        # set parameter from dialog
        input_layer = self.layer.currentLayer()
        output_path = self.output_form.text()
        radius = self.get_classification()
        # monkey patch keywords so layer works on multi buffering function
        input_layer.keywords = {'inasafe_fields': {}}

        # run multi buffering
        self.output_layer = multi_buffering(input_layer, radius)

        # save output layer to data store and check whether user
        # provide the output path.
        if output_path:
            self.output_directory, self.output_filename = (
                os.path.split(output_path))
            self.output_filename, self.output_extension = (
                os.path.splitext(self.output_filename))

        # if user do not provide the output path, create a temporary file.
        else:
            self.output_directory = temp_dir(sub_dir='work')
            self.output_filename = (
                unique_filename(
                    prefix='hazard_layer',
                    suffix='.geojson',
                    dir=self.output_directory))
            self.output_filename = os.path.split(self.output_filename)[1]
            self.output_filename, self.output_extension = (
                os.path.splitext(self.output_filename))

        self.data_store = Folder(self.output_directory)
        if self.output_extension == '.shp':
            self.data_store.default_vector_format = 'shp'
        elif self.output_extension == '.geojson':
            self.data_store.default_vector_format = 'geojson'
        self.data_store.add_layer(self.output_layer, self.output_filename)

        # add output layer to map canvas
        self.output_layer = self.data_store.layer(self.output_filename)

        QgsMapLayerRegistry.instance().addMapLayers(
            [self.output_layer])
        self.iface.setActiveLayer(self.output_layer)
        self.iface.zoomToActiveLayer()
        self.done(QtGui.QDialog.Accepted)

        if self.keyword_wizard_checkbox.isChecked():
            self.launch_keyword_wizard()

    @pyqtSignature('')  # prevents actions being handled twice
    def on_directory_button_tool_clicked(self):
        """Autoconnect slot activated when directory button is clicked."""
        # noinspection PyCallByClass,PyTypeChecker
        # set up parameter from dialog
        input_path = self.layer.currentLayer().source()
        input_directory, self.output_filename = os.path.split(input_path)
        file_extension = os.path.splitext(self.output_filename)[1]
        self.output_filename = os.path.splitext(self.output_filename)[0]
        # show Qt file directory dialog
        output_path = QFileDialog.getSaveFileName(
            self,
            self.tr('Output file'),
            '%s_multi_buffer%s' % (
                os.path.join(input_directory, self.output_filename),
                file_extension),
            'GeoJSON (*.geojson);;Shapefile (*.shp)')
        # set selected path to the dialog
        self.output_form.setText(output_path)

    def get_output_from_input(self):
        """Populate output form with default output path based on input layer.
        """
        input_path = self.layer.currentLayer().source()
        output_path = (
            os.path.splitext(input_path)[0] + '_multi_buffer' +
            os.path.splitext(input_path)[1])
        self.output_form.setText(output_path)

    def populate_hazard_classification(self):
        """Populate hazard classification on hazard class form."""
        new_class = {
            'value': self.radius_form.value(),
            'name': self.class_form.text()}
        self.classification.append(new_class)
        self.classification = sorted(
            self.classification, key=itemgetter('value'))

        self.hazard_class_form.clear()
        for item in self.classification:
            new_item = '{value} - {name}'.format(
                value=item['value'], name=item['name'])
            self.hazard_class_form.addItem(new_item)

        self.radius_form.setValue(0)
        self.class_form.clear()
        self.ok_button_status()

    def remove_selected_classification(self):
        """Remove selected item on hazard class form."""
        removed_classes = self.hazard_class_form.selectedItems()
        current_item = self.hazard_class_form.currentItem()
        removed_index = self.hazard_class_form.indexFromItem(current_item)
        del self.classification[removed_index.row()]
        for item in removed_classes:
            self.hazard_class_form.takeItem(
                self.hazard_class_form.row(item))

    def get_classification(self):
        """Get all hazard class created by user.

        :return: Hazard class definition created by user.
        :rtype: OrderedDict
        """
        classification_dictionary = {}
        for item in self.classification:
            classification_dictionary[item['value']] = item['name']

        classification_dictionary = OrderedDict(
            sorted(classification_dictionary.items()))

        return classification_dictionary

    def directory_button_status(self):
        """Function to enable or disable directory button."""
        if self.layer.currentLayer():
            self.directory_button.setEnabled(True)
        else:
            self.directory_button.setEnabled(False)

    def add_class_button_status(self):
        """Function to enable or disable add class button."""
        if self.class_form.text() and self.radius_form >= 0:
            self.add_class_button.setEnabled(True)
        else:
            self.add_class_button.setEnabled(False)

    def ok_button_status(self):
        """Function to enable or disable OK button."""
        if not self.layer.currentLayer():
            self.button_box.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
        elif (self.hazard_class_form.count() > 0 and
                self.layer.currentLayer().name() and
                    len(self.output_form.text()) >= 0):
            self.button_box.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
        else:
            self.button_box.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)

    @pyqtSlot()
    @pyqtSignature('bool')  # prevents actions being handled twice
    def help_toggled(self, flag):
        """Show or hide the help tab in the stacked widget.

        :param flag: Flag indicating whether help should be shown or hidden.
        :type flag: bool
        """
        if flag:
            self.help_button.setText(self.tr('Hide Help'))
            self.show_help()
        else:
            self.help_button.setText(self.tr('Show Help'))
            self.hide_help()

    def hide_help(self):
        """Hide the usage info from the user."""
        self.main_stacked_widget.setCurrentIndex(1)

    def show_help(self):
        """Show usage info to the user."""
        # Read the header and footer html snippets
        self.main_stacked_widget.setCurrentIndex(0)
        header = html_header()
        footer = html_footer()

        string = header

        message = multi_buffer_help()

        string += message.to_html()
        string += footer

        self.help_web_view.setHtml(string)

    def launch_keyword_wizard(self):
        """Launch keyword creation wizard."""
        # make sure selected layer is the output layer
        if self.iface.activeLayer() != self.output_layer:
            return

        # launch wizard dialog
        self.keyword_wizard = WizardDialog(
            self.iface.mainWindow(),
            self.iface,
            self.dock_widget)
        self.keyword_wizard.set_keywords_creation_mode(self.output_layer)
        self.keyword_wizard.exec_()  # modal
    def test_analysis_wizard(self):
        """Test Analysis Wizard."""
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()
        number_of_column = len(hazard_all)

        volcano_layer = load_test_vector_layer(
            'hazard',
            'volcano_krb.shp',
            clone=True)

        structure_layer = load_test_vector_layer(
            'exposure',
            'buildings.shp',
            clone=True)

        test_layers = [volcano_layer, structure_layer]

        QgsMapLayerRegistry.instance().addMapLayers(test_layers)
        # Need to set the layers manually to map canvas. See:
        # https://gist.github.com/ismailsunni/dd2c30a38cef0147bd0dc8d6ba1aeac6
        qgs_map_canvas_layers = [QgsMapCanvasLayer(x) for x in test_layers]
        CANVAS.setLayerSet(qgs_map_canvas_layers)

        count = len(dialog.iface.mapCanvas().layers())
        self.assertEqual(count, len(test_layers))

        # step_fc_functions1: test function matrix dimensions
        col_count = dialog.step_fc_functions1.tblFunctions1.columnCount()
        self.assertEqual(col_count, number_of_column)
        row_count = dialog.step_fc_functions1.tblFunctions1.rowCount()
        self.assertEqual(row_count, len(exposure_all))

        # Select Volcano vs Structure
        volcano_index = hazard_all.index(hazard_volcano)
        structure_index = exposure_all.index(exposure_structure)

        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(
            structure_index, volcano_index)

        selected_hazard = dialog.step_fc_functions1.selected_value(
            layer_purpose_hazard['key'])
        selected_exposure = dialog.step_fc_functions1.selected_value(
            layer_purpose_exposure['key'])
        self.assertEqual(selected_hazard, hazard_volcano)
        self.assertEqual(selected_exposure, exposure_structure)

        # step_fc_functions1: press next
        dialog.pbnNext.click()

        # step_fc_functions2
        # Check in the correct step
        self.check_current_step(dialog.step_fc_functions2)
        hazard_polygon_index = get_allowed_geometries(
            layer_purpose_hazard['key']).index(layer_geometry_polygon)
        exposure_polygon_index = get_allowed_geometries(
            layer_purpose_exposure['key']).index(layer_geometry_polygon)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(
            exposure_polygon_index, hazard_polygon_index)

        selected_hazard_geometry = dialog.step_fc_functions2.selected_value(
            layer_purpose_hazard['key'])
        selected_exposure_geometry = dialog.step_fc_functions2.selected_value(
            layer_purpose_exposure['key'])
        self.assertEqual(selected_hazard_geometry, layer_geometry_polygon)
        self.assertEqual(selected_exposure_geometry, layer_geometry_polygon)

        # step_fc_functions2: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_hazlayer_origin)

        # step hazard origin: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)

        # Check the number of layer in the list
        self.assertEqual(
            dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.count(), 1)

        # step hazard from canvas: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_explayer_origin)

        # step exposure origin: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_explayer_from_canvas)

        # Check the number of layer in the list
        self.assertEqual(
            dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.count(), 1)

        # step exposure from canvas: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_agglayer_origin)

        # Check no aggregation
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.setChecked(True)

        # step aggregation origin: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_summary)

        # step extent: press next
        dialog.pbnNext.click()

        # Check in the correct step
        self.check_current_step(dialog.step_fc_analysis)
    def test_existing_complex_keywords(self):
        """Test for existing complex keywords in wizard in locale mode."""
        from safe.test.utilities import (
            clone_shp_layer, remove_vector_temp_file)
        layer = clone_shp_layer(
            name='tsunami_polygon', include_keywords=True, source_directory='')

        from safe.test.utilities import get_qgis_app
        # Get QGis app handle
        # noinspection PyPep8Naming
        _, _, IFACE, PARENT = get_qgis_app()

        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        # noinspection PyTypeChecker
        dialog = WizardDialog(PARENT, IFACE)
        dialog.set_keywords_creation_mode(layer)

        # select hazard
        self.select_from_list_widget('ancaman',
                                     dialog.step_kw_purpose.lstCategories)
        dialog.pbnNext.click()

        # select volcano
        self.select_from_list_widget('gunung berapi', dialog.
                                     step_kw_subcategory.lstSubcategories)
        dialog.pbnNext.click()

        # select volcano categorical unit
        self.select_from_list_widget('Kategori gunung berapi',
                                     dialog.step_kw_unit.lstUnits)
        dialog.pbnNext.click()

        # select GRIDCODE
        self.select_from_list_widget(
            'GRIDCODE', dialog.step_kw_field.lstFields)
        dialog.pbnNext.click()

        unit = dialog.step_kw_unit.selected_unit()
        default_classes = unit['classes']
        unassigned_values = []  # no need to check actually, not save in file
        assigned_values = {
            'low': ['5.0'],
            'medium': ['3.0', '4.0'],
            'high': ['2.0']
        }
        dialog.step_kw_classify.populate_classified_values(
            unassigned_values, assigned_values, default_classes)
        dialog.pbnNext.click()

        source = 'Source'
        source_scale = 'Source Scale'
        source_url = 'Source Url'
        source_date = QDateTime.fromString(
            '06-12-2015 12:30',
            'dd-MM-yyyy HH:mm')

        dialog.step_kw_source.leSource.setText(source)
        dialog.step_kw_source.leSource_scale.setText(source_scale)
        dialog.step_kw_source.leSource_url.setText(source_url)
        dialog.step_kw_source.leSource_date.seDateTime(source_date)
        dialog.pbnNext.click()  # next
        dialog.pbnNext.click()  # finish

        # noinspection PyTypeChecker
        dialog = WizardDialog(PARENT, IFACE)
        dialog.set_keywords_creation_mode(layer)

        # step 1 of 7 - select category
        self.check_current_text(
            'ancaman', dialog.step_kw_purpose.lstCategories)

        # Click Next
        dialog.pbnNext.click()

        # step 2 of 7 - select subcategory
        # noinspection PyTypeChecker
        self.check_current_text('gunung berapi',
                                dialog.step_kw_subcategory.lstSubcategories)

        # Click Next
        dialog.pbnNext.click()

        # step 3 of 7 - select volcano units
        self.check_current_text('Kategori gunung berapi',
                                dialog.step_kw_unit.lstUnits)

        # Click Next
        dialog.pbnNext.click()

        # step 4 of 7 - select field
        self.check_current_text('GRIDCODE', dialog.step_kw_field.lstFields)

        # Click Next
        dialog.pbnNext.click()

        for index in range(dialog.step_classify.lstUniqueValues.count()):
            message = ('%s Should be in unassigned values' %
                       dialog.step_classify.lstUniqueValues.item(index).text())
            self.assertIn(
                dialog.step_classify.lstUniqueValues.item(index).text(),
                unassigned_values,
                message)
        real_assigned_values = dialog.step_classify.selected_mapping()
        self.assertDictEqual(real_assigned_values, assigned_values)

        # Click Next
        dialog.pbnNext.click()

        # step 6 of 7 - enter source
        message = ('Invalid Next button state in step 6! Disabled while '
                   'source is optional')
        self.assertTrue(dialog.pbnNext.isEnabled(), message)

        message = 'Source should be %s' % source
        self.assertEqual(
            dialog.step_kw_source.leSource.text(), source, message)
        message = 'Source Url should be %s' % source_url
        self.assertEqual(dialog.step_kw_source.leSource_url.text(),
                         source_url, message)
        message = 'Source Date should be %s' % source_date.toString(
            'dd-MM-yyyy HH:mm')
        self.assertEqual(dialog.step_kw_source.leSource_date.dateTime(),
                         source_date, message)
        message = 'Source Scale should be %s' % source_scale
        self.assertEqual(dialog.step_kw_source.leSource_scale.text(),
                         source_scale, message)
        dialog.pbnNext.click()

        dialog.pbnCancel.click()

        remove_vector_temp_file(layer.source())
    def test_input_function_centric_wizard_test_3(self):
        """Test various usecases of the wizard:
           keywordless layers, disjoint layers, browsers,
           stepping back and forth ."""

        chosen_if1 = 'FloodRasterBuildingFunction'
        chosen_if2 = 'ClassifiedRasterHazardBuildingFunction'

        expected_hazard_layers_count = 2
        expected_exposure_layers_count = 2
        expected_aggregation_layers_count = 2

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        # Hazard layer without keywords
        layer = clone_raster_layer(
            name='keywordless_layer',
            extension='.tif',
            include_keywords=False,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Hazard layer - disjoint
        layer = clone_raster_layer(
            name='continuous_flood_unaligned_big_size',
            extension='.tif',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Hazard layer
        layer = clone_raster_layer(
            name='classified_flood_20_20',
            extension='.asc',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Exposure layer
        layer = clone_shp_layer(
            name='building-points',
            include_keywords=True,
            source_directory=standard_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Exposure layer without keywords
        layer = clone_shp_layer(
            name='building-points',
            include_keywords=False,
            source_directory=standard_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Aggregation layer
        layer = clone_shp_layer(
            name='district_osm_jakarta',
            include_keywords=True,
            source_directory=standard_data_path('boundaries'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Aggregation layer without keywords
        layer = clone_shp_layer(
            name='grid_jakarta',
            include_keywords=False,
            source_directory=standard_data_path('boundaries'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # step_fc_functions1: select functions for flood x structure
        self.check_current_step(dialog.step_fc_functions1)
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(3, 1)
        dialog.pbnNext.click()

        # step_fc_functions2: select functions for raster x point
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(1, 0)
        dialog.pbnNext.click()

        # step_fc_function: test if FloodRasterBuildingFunction is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(dialog.step_fc_function.lstFunctions.count())]
        self.assertTrue(chosen_if1 in flood_ifs)

        # step_fc_function: select FloodRasterBuildingFunction and
        # press ok
        self.check_current_step(dialog.step_fc_function)
        chosen_if_row = flood_ifs.index(chosen_if1)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_origin:
        self.check_current_step(dialog.step_fc_hazlayer_origin)

        # step_fc_hazlayer_from_canvas: test the lstCanvasHazLayers state
        # Note this step is tested prior to step_fc_hazlayer_origin
        # as the list is prepared prior to autoselecting the radiobuttons
        count = dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.count()
        self.assertEqual(count, expected_hazard_layers_count)

        # test if hazard browser works
        dialog.step_fc_hazlayer_origin.rbHazLayerFromBrowser.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_browser)
        # step back and continue with hazard from canvas
        dialog.pbnBack.click()
        dialog.step_fc_hazlayer_origin.rbHazLayerFromCanvas.click()
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        # Select the second (keywordless) layer in order to trigger preparing
        # the 'missing keywords' description.
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(1)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_purpose)
        dialog.pbnBack.click()
        # Now select the first (proper) layer and press ok
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(0)
        dialog.pbnNext.click()

        # step_fc_explayer_origin
        self.check_current_step(dialog.step_fc_explayer_origin)
        count = dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.count()
        self.assertEqual(count, expected_exposure_layers_count)

        # test if exposure browser works
        dialog.step_fc_explayer_origin.rbExpLayerFromBrowser.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_from_browser)
        # step back and continue with exposure from canvas
        dialog.pbnBack.click()
        dialog.step_fc_explayer_origin.rbExpLayerFromCanvas.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        # Select the second (keywordless) layer in order to trigger preparing
        # the 'missing keywords' description.
        dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.setCurrentRow(1)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_purpose)
        dialog.pbnBack.click()
        # Now select the first (proper) layer and press ok
        dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.setCurrentRow(0)
        dialog.pbnNext.click()

        # step_fc_disjoint_layers
        self.check_current_step(dialog.step_fc_disjoint_layers)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_function)

        # Select ClassifiedRasterHazardBuildingFunction
        chosen_if_row = flood_ifs.index(chosen_if2)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)

        # Select the first (proper) layer and press ok
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(0)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.setCurrentRow(0)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_agglayer_origin)

        # test if no aggregation works
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_extent)

        # step back and test if aggregation browser works
        dialog.pbnBack.click()
        dialog.step_fc_agglayer_origin.rbAggLayerFromBrowser.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_agglayer_from_browser)

        # step back and continue with aggregation from canvas
        dialog.pbnBack.click()
        dialog.step_fc_agglayer_origin.rbAggLayerFromCanvas.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_agglayer_from_canvas)
        count = dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.count()
        self.assertEqual(count, expected_aggregation_layers_count)
        # Select the second (keywordless) layer in order to trigger preparing
        # the 'missing keywords' description.
        dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.setCurrentRow(1)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_purpose)
        dialog.pbnBack.click()
        # Now select the first (proper) layer and press ok
        dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.setCurrentRow(0)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnNext.click()

        self.check_current_step(dialog.step_fc_params)
        # Step back and enter the params step again
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_summary)

        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_agglayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_agglayer_origin)
        # No need to test more backward steps (already tested in other test)
        dialog.pbnCancel.click()
    def test_input_function_centric_wizard_test_4(self):
        """Test keyword creation wizard called from the
           impact function centric one
        """

        chosen_if = 'FloodEvacuationRasterHazardFunction'

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        # Just the hazard layer in two copies
        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=False,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=False,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # step_fc_functions1: select functions for flood x population
        self.check_current_step(dialog.step_fc_functions1)
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(1, 1)
        dialog.pbnNext.click()

        # step_fc_functions2: select functions for raster x raster
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(0, 0)
        dialog.pbnNext.click()

        # step_fc_function: test if FloodEvacuationRasterHazardFunction
        # is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(dialog.step_fc_function.lstFunctions.count())]
        self.assertTrue(chosen_if in flood_ifs)

        # step_fc_function: select FloodEvacuationRasterHazardFunction and
        # press ok
        self.check_current_step(dialog.step_fc_function)
        chosen_if_row = flood_ifs.index(chosen_if)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_origin:
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas:
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        # Select the first layer and register it as unsuitable (Tsunmami)
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(0)
        dialog.pbnNext.click()

        # step_kw_purpose
        self.check_current_step(dialog.step_kw_purpose)
        self.select_from_list_widget(
            'Hazard', dialog.step_kw_purpose.lstCategories)
        dialog.pbnNext.click()

        # step_kw_subcategory
        self.check_current_step(dialog.step_kw_subcategory)
        self.select_from_list_widget(
            'Tsunami', dialog.step_kw_subcategory.lstSubcategories)
        dialog.pbnNext.click()

        # step_kw_hazard_category
        self.check_current_step(dialog.step_kw_hazard_category)
        self.select_from_list_widget(
            'Single event',
            dialog.step_kw_hazard_category.lstHazardCategories)
        dialog.pbnNext.click()

        # step_kw_layermode
        self.check_current_step(dialog.step_kw_layermode)
        self.select_from_list_widget(
            'Continuous', dialog.step_kw_layermode.lstLayerModes)
        dialog.pbnNext.click()

        # step_kw_unit
        self.check_current_step(dialog.step_kw_unit)
        self.select_from_list_widget(
            'Metres', dialog.step_kw_unit.lstUnits)
        dialog.pbnNext.click()

        # step_kw_source
        self.check_current_step(dialog.step_kw_source)
        dialog.pbnNext.click()

        # step_kw_title
        self.check_current_step(dialog.step_kw_title)
        dialog.pbnNext.click()

        # step_kw_summary
        self.check_current_step(dialog.step_kw_summary)
        # step back and forth
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_kw_title)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_summary)

        # Finish the keyword thread and register the unsuitable layer
        dialog.pbnNext.click()

        # Should be again in the step_fc_hazlayer_from_canvas
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        # Now there is only one layer listed - register it properly
        dialog.pbnNext.click()

        # step_kw_purpose
        self.check_current_step(dialog.step_kw_purpose)
        self.select_from_list_widget(
            'Hazard', dialog.step_kw_purpose.lstCategories)
        dialog.pbnNext.click()

        # step_kw_subcategory
        self.check_current_step(dialog.step_kw_subcategory)
        self.select_from_list_widget(
            'Flood', dialog.step_kw_subcategory.lstSubcategories)
        dialog.pbnNext.click()

        # step_kw_hazard_category
        self.check_current_step(dialog.step_kw_hazard_category)
        self.select_from_list_widget(
            'Single event',
            dialog.step_kw_hazard_category.lstHazardCategories)
        dialog.pbnNext.click()

        # step_kw_layermode
        self.check_current_step(dialog.step_kw_layermode)
        self.select_from_list_widget(
            'Continuous', dialog.step_kw_layermode.lstLayerModes)
        dialog.pbnNext.click()

        # step_kw_unit
        self.check_current_step(dialog.step_kw_unit)
        self.select_from_list_widget(
            'Metres', dialog.step_kw_unit.lstUnits)
        dialog.pbnNext.click()

        # step_kw_source
        self.check_current_step(dialog.step_kw_source)
        dialog.pbnNext.click()

        # step_kw_title
        self.check_current_step(dialog.step_kw_title)
        dialog.pbnNext.click()

        # step_kw_summary
        self.check_current_step(dialog.step_kw_summary)
        dialog.pbnNext.click()

        # Should be in the step_fc_explayer_origin
        self.check_current_step(dialog.step_fc_explayer_origin)

        dialog.pbnCancel.click()
Example #10
0
class Plugin(object):
    """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
        """
        # Register all the impact functions
        register_impact_functions()
        # 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_minimum_needs_config = None
        self.key_action = None
        self.action_options = None
        self.action_keywords_wizard = None
        self.action_function_centric_wizard = None
        self.action_extent_selector = None
        self.translator = None
        self.toolbar = None
        self.wizard = 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
        # Flag indicating if toolbar should show only common icons or not
        self.full_toolbar = False
        # 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['INASAFE_LANG'] = str(new_locale)

        LOGGER.debug('%s %s %s' % (
            new_locale, QLocale.system().name(), os.environ['INASAFE_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)

    def _create_dock_toggle_action(self):
        """Create action for plugin dockable window (show/hide)."""
        # pylint: disable=W0201
        icon = resources_path('img', 'icons', 'icon.svg')
        self.action_dock = QAction(
            QIcon(icon),
            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 creation wizard
        # -------------------------------------

    def _create_keywords_wizard_action(self):
        """Create action for keywords creation wizard."""
        icon = resources_path('img', 'icons', 'show-keyword-wizard.svg')
        self.action_keywords_wizard = QAction(
            QIcon(icon),
            self.tr('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)

    def _create_analysis_wizard_action(self):
        """Create action for IF-centric wizard."""
        icon = resources_path('img', 'icons', 'show-wizard.svg')
        self.action_function_centric_wizard = QAction(
            QIcon(icon),
            self.tr('Impact Function Centric Wizard'),
            self.iface.mainWindow())
        self.action_function_centric_wizard.setStatusTip(self.tr(
            'Open InaSAFE impact function centric wizard'))
        self.action_function_centric_wizard.setWhatsThis(self.tr(
            'Open InaSAFE impact function centric wizard'))
        self.action_function_centric_wizard.setEnabled(True)
        self.action_function_centric_wizard.triggered.connect(
            self.show_function_centric_wizard)
        self.add_action(self.action_function_centric_wizard)

    def _create_options_dialog_action(self):
        """Create action for options dialog."""
        icon = resources_path('img', 'icons', 'configure-inasafe.svg')
        self.action_options = QAction(
            QIcon(icon),
            self.tr('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, add_to_toolbar=self.full_toolbar)

    def _create_minimum_needs_action(self):
        """Create action for minimum needs dialog."""
        icon = resources_path('img', 'icons', 'show-minimum-needs.svg')
        self.action_minimum_needs = QAction(
            QIcon(icon),
            self.tr('Minimum Needs Calculator'), self.iface.mainWindow())
        self.action_minimum_needs.setStatusTip(self.tr(
            'Open InaSAFE minimum needs calculator'))
        self.action_minimum_needs.setWhatsThis(self.tr(
            'Open InaSAFE minimum needs calculator'))
        self.action_minimum_needs.triggered.connect(self.show_minimum_needs)
        self.add_action(
            self.action_minimum_needs, add_to_toolbar=self.full_toolbar)

    def _create_minimum_needs_options_action(self):
        """Create action for global minimum needs dialog."""
        icon = resources_path('img', 'icons', 'show-global-minimum-needs.svg')
        self.action_minimum_needs_config = QAction(
            QIcon(icon),
            self.tr('Minimum Needs Configuration'),
            self.iface.mainWindow())
        self.action_minimum_needs_config.setStatusTip(self.tr(
            'Open InaSAFE minimum needs configuration'))
        self.action_minimum_needs_config.setWhatsThis(self.tr(
            'Open InaSAFE minimum needs configuration'))
        self.action_minimum_needs_config.triggered.connect(
            self.show_minimum_needs_configuration)
        self.add_action(
            self.action_minimum_needs_config, add_to_toolbar=self.full_toolbar)

    def _create_shakemap_converter_action(self):
        """Create action for converter dialog."""
        icon = resources_path('img', 'icons', 'show-converter-tool.svg')
        self.action_shake_converter = QAction(
            QIcon(icon),
            self.tr('Shakemap 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, add_to_toolbar=self.full_toolbar)

    def _create_batch_runner_action(self):
        """Create action for batch runner dialog."""
        icon = resources_path('img', 'icons', 'show-batch-runner.svg')
        self.action_batch_runner = QAction(
            QIcon(icon),
            self.tr('Batch Runner'), self.iface.mainWindow())
        self.action_batch_runner.setStatusTip(self.tr(
            'Open Batch Runner'))
        self.action_batch_runner.setWhatsThis(self.tr(
            'Open Batch Runner'))
        self.action_batch_runner.triggered.connect(self.show_batch_runner)
        self.add_action(
            self.action_batch_runner, add_to_toolbar=self.full_toolbar)

    def _create_save_scenario_action(self):
        """Create action for save scenario dialog."""
        icon = resources_path('img', 'icons', 'save-as-scenario.svg')
        self.action_save_scenario = QAction(
            QIcon(icon),
            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, add_to_toolbar=self.full_toolbar)

    def _create_osm_downloader_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'show-osm-download.svg')
        self.action_import_dialog = QAction(
            QIcon(icon),
            self.tr('OpenStreetMap Downloader'),
            self.iface.mainWindow())
        self.action_import_dialog.setStatusTip(self.tr(
            'OpenStreetMap Downloader'))
        self.action_import_dialog.setWhatsThis(self.tr(
            'OpenStreetMap Downloader'))
        self.action_import_dialog.triggered.connect(self.show_osm_downloader)
        self.add_action(self.action_import_dialog)

    def _create_add_osm_layer_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'add-osm-tiles-layer.svg')
        self.action_add_osm_layer = QAction(
            QIcon(icon),
            self.tr('Add OpenStreetMap Tile Layer'),
            self.iface.mainWindow())
        self.action_add_osm_layer.setStatusTip(self.tr(
            'Add OpenStreetMap Tile Layer'))
        self.action_add_osm_layer.setWhatsThis(self.tr(
            'Use this to add an OSM layer to your map. '
            'It needs internet access to function.'))
        self.action_add_osm_layer.triggered.connect(self.add_osm_layer)
        self.add_action(self.action_add_osm_layer)

    def _create_add_petajakarta_layer_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'add-petajakarta-layer.svg')
        self.action_add_petajakarta_layer = QAction(
            QIcon(icon),
            self.tr('Add PetaJakarta Flood Layer'),
            self.iface.mainWindow())
        self.action_add_petajakarta_layer.setStatusTip(self.tr(
            'Add PetaJakarta Flood Layer'))
        self.action_add_petajakarta_layer.setWhatsThis(self.tr(
            'Use this to add a PetaJakarta layer to your map. '
            'It needs internet access to function.'))
        self.action_add_petajakarta_layer.triggered.connect(
            self.add_petajakarta_layer)
        self.add_action(
            self.action_add_petajakarta_layer,
            add_to_toolbar=False)

    def _create_raster_reclassify_layer_action(self):
        """Create action for Raster Reclassification to vector."""
        # Disabled for 3.5
        final_release = release_status() == 'final'
        settings = QSettings()
        self.developer_mode = settings.value(
            'inasafe/developer_mode', False, type=bool)
        if not final_release and self.developer_mode:
            icon = resources_path(
                'img', 'icons', 'raster-reclassify-layer.svg')
            self.action_raster_reclassify_layer = QAction(
                QIcon(icon),
                self.tr('Reclassify Raster to Vector Layer'),
                self.iface.mainWindow())
            self.action_raster_reclassify_layer.setStatusTip(self.tr(
                'Reclassify Raster to Vector Layer'))
            self.action_raster_reclassify_layer.setWhatsThis(self.tr(
                'Use this to reclassify Raster Layer into Vector Layer '
                'with defined thresholds as classifier.'))
            self.action_raster_reclassify_layer.triggered.connect(
                self.raster_reclassify)
            self.add_action(
                self.action_raster_reclassify_layer,
                add_to_toolbar=False)

    def _create_rubber_bands_action(self):
        """Create action for toggling rubber bands."""
        icon = resources_path('img', 'icons', 'toggle-rubber-bands.svg')
        self.action_toggle_rubberbands = QAction(
            QIcon(icon),
            self.tr('Toggle Scenario Outlines'), self.iface.mainWindow())
        message = self.tr('Toggle rubber bands showing scenario 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)

    def _create_analysis_extent_action(self):
        """Create action for analysis extent dialog."""
        icon = resources_path('img', 'icons', 'set-extents-tool.svg')
        self.action_extent_selector = QAction(
            QIcon(icon),
            self.tr('Set Analysis Area'),
            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)

    def _create_test_layers_action(self):
        """Create action for adding layers (developer mode, non final only)."""
        final_release = release_status() == 'final'
        settings = QSettings()
        self.developer_mode = settings.value(
            'inasafe/developer_mode', False, type=bool)
        if not final_release and self.developer_mode:
            icon = resources_path('img', 'icons', 'add-test-layers.svg')
            self.action_add_layers = QAction(
                QIcon(icon),
                self.tr('Add Test Layers'),
                self.iface.mainWindow())
            self.action_add_layers.setStatusTip(self.tr(
                'Add test layers'))
            self.action_add_layers.setWhatsThis(self.tr(
                'Add test layers'))
            self.action_add_layers.triggered.connect(
                self.add_test_layers)

            self.add_action(self.action_add_layers)

    def _create_run_test_action(self):
        """Create action for running tests (developer mode, non final only)."""
        final_release = release_status() == 'final'
        settings = QSettings()
        self.developer_mode = settings.value(
            'inasafe/developer_mode', False, type=bool)
        if not final_release and self.developer_mode:

            default_package = unicode(settings.value(
                'inasafe/testPackage', 'safe', type=str))
            msg = self.tr('Run tests in %s' % default_package)

            self.test_button = QToolButton()
            self.test_button.setMenu(QMenu())
            self.test_button.setPopupMode(QToolButton.MenuButtonPopup)

            icon = resources_path('img', 'icons', 'run-tests.svg')
            self.action_run_tests = QAction(
                QIcon(icon),
                msg,
                self.iface.mainWindow())

            self.action_run_tests.setStatusTip(msg)
            self.action_run_tests.setWhatsThis(msg)
            self.action_run_tests.triggered.connect(
                self.run_tests)

            self.test_button.menu().addAction(self.action_run_tests)
            self.test_button.setDefaultAction(self.action_run_tests)

            self.action_select_package = QAction(
                QIcon(icon),
                self.tr('Select package'),
                self.iface.mainWindow())

            self.action_select_package.setStatusTip(self.tr(
                'Select Test Package'))
            self.action_select_package.setWhatsThis(self.tr(
                'Select Test Package'))
            self.action_select_package.triggered.connect(
                self.select_test_package)
            self.test_button.menu().addAction(self.action_select_package)
            self.toolbar.addWidget(self.test_button)

            self.add_action(self.action_run_tests, add_to_toolbar=False)
            self.add_action(self.action_select_package, add_to_toolbar=False)

    def _create_dock(self):
        """Create dockwidget and tabify it with the legend."""
        # Import dock here as it needs to be imported AFTER i18n is set up
        from safe.gui.widgets.dock import Dock
        self.dock_widget = Dock(self.iface)
        self.dock_widget.setObjectName('InaSAFE-Dock')
        self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock_widget)
        legend_tab = self.iface.mainWindow().findChild(QApplication, 'Legend')
        if legend_tab:
            self.iface.mainWindow().tabifyDockWidget(
                legend_tab, self.dock_widget)
            self.dock_widget.raise_()

    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')
        self.dock_widget = None
        # Now create the actual dock
        self._create_dock()
        # And all the menu actions
        # Configuration Group
        self._create_dock_toggle_action()
        self._create_options_dialog_action()
        self._create_minimum_needs_options_action()
        self._create_analysis_extent_action()
        self._create_rubber_bands_action()
        self._add_spacer_to_menu()
        self._create_keywords_wizard_action()
        self._create_analysis_wizard_action()
        self._add_spacer_to_menu()
        self._create_osm_downloader_action()
        self._create_add_osm_layer_action()
        self._create_add_petajakarta_layer_action()
        self._create_raster_reclassify_layer_action()
        self._create_shakemap_converter_action()
        self._create_minimum_needs_action()
        self._create_test_layers_action()
        self._create_run_test_action()
        self._add_spacer_to_menu()
        self._create_batch_runner_action()
        self._create_save_scenario_action()

        # 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())

    def _add_spacer_to_menu(self):
        """Create a spacer to the menu to separate action groups."""
        separator = QAction(self.iface.mainWindow())
        separator.setSeparator(True)
        self.iface.addPluginToMenu(self.tr('InaSAFE'), separator)

    def clear_modules(self):
        """Unload inasafe functions and try to return QGIS to before InaSAFE.

        .. todo:: I think this function can be removed. TS.
        """
        # next lets force remove any inasafe related modules
        modules = []
        for module in sys.modules:
            if 'inasafe' in module:
                # Check if it is really one of our modules i.e. exists in the
                # plugin directory
                tokens = module.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' % module)
                    modules.append(module)
        for module in modules:
            del (sys.modules[module])
        for module in sys.modules:
            if 'inasafe' in module:
                print module

        # 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
        if self.wizard:
            self.wizard.deleteLater()
        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)

    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 add_test_layers(self):
        """Add standard test layers."""
        from safe.test.utilities import load_standard_layers
        load_standard_layers()
        rect = QgsRectangle(106.806, -6.195, 106.837, -6.167)
        self.iface.mapCanvas().setExtent(rect)

    def select_test_package(self):
        """Select the test package."""
        settings = QSettings()
        default_package = 'safe'
        user_package = unicode(settings.value(
            'inasafe/testPackage', default_package, type=str))

        test_package, _ = QInputDialog.getText(
            self.iface.mainWindow(),
            self.tr('Select the python test package'),
            self.tr('Select the python test package'),
            QLineEdit.Normal,
            user_package)

        if test_package == '':
            test_package = default_package

        settings.setValue('inasafe/testPackage', test_package)
        msg = self.tr('Run tests in %s' % test_package)
        self.action_run_tests.setWhatsThis(msg)
        self.action_run_tests.setText(msg)

    def run_tests(self):
        """Run unit tests in the python console."""
        from PyQt4.QtGui import QDockWidget
        main_window = self.iface.mainWindow()
        action = main_window.findChild(QAction, 'mActionShowPythonDialog')
        action.trigger()
        settings = QSettings()
        package = unicode(settings.value(
            'inasafe/testPackage', 'safe', type=str))
        for child in main_window.findChildren(QDockWidget, 'PythonConsole'):
            if child.objectName() == 'PythonConsole':
                child.show()
                for widget in child.children():
                    if 'PythonConsoleWidget' in str(widget.__class__):
                        # print "Console widget found"
                        shell = widget.shell
                        shell.runCommand(
                            'from inasafe.test_suite import test_package')
                        shell.runCommand('test_package(\'%s\')' % package)
                        break

    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.gui.tools.extent_selector_dialog import ExtentSelectorDialog

        widget = ExtentSelectorDialog(
            self.iface,
            self.iface.mainWindow(),
            extent=self.dock_widget.extent.user_extent,
            crs=self.dock_widget.extent.user_extent_crs)
        widget.clear_extent.connect(
            self.dock_widget.extent.clear_user_analysis_extent)
        widget.extent_defined.connect(
            self.dock_widget.define_user_analysis_extent)
        # This ensures that run button state is updated on dialog close
        widget.extent_selector_closed.connect(
            self.dock_widget.show_next_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.gui.tools.minimum_needs.needs_calculator_dialog import (
            NeedsCalculatorDialog
        )

        dialog = NeedsCalculatorDialog(self.iface.mainWindow())
        dialog.exec_()

    def show_minimum_needs_configuration(self):
        """Show the minimum needs dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.minimum_needs.needs_manager_dialog import (
            NeedsManagerDialog)

        dialog = NeedsManagerDialog(
            parent=self.iface.mainWindow(),
            dock=self.dock_widget)
        dialog.exec_()  # modal

    def show_options(self):
        """Show the options dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.options_dialog import OptionsDialog

        dialog = OptionsDialog(
            self.iface,
            self.dock_widget,
            self.iface.mainWindow())
        dialog.exec_()  # modal

    def show_keywords_wizard(self):
        """Show the keywords creation wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        if self.iface.activeLayer() is None:
            return

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since the IFCW is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_keywords_creation_mode()
        self.wizard.exec_()  # modal

    def show_function_centric_wizard(self):
        """Show the function centric wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since it is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_function_centric_mode()
        # non-modal in order to hide for selecting user extent
        self.wizard.show()

    def show_shakemap_importer(self):
        """Show the converter dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.shake_grid.shakemap_converter_dialog import (
            ShakemapConverterDialog)

        dialog = ShakemapConverterDialog(self.iface.mainWindow())
        dialog.exec_()  # modal

    def show_osm_downloader(self):
        """Show the OSM buildings downloader dialog."""
        from safe.gui.tools.osm_downloader_dialog import OsmDownloaderDialog

        dialog = OsmDownloaderDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def add_osm_layer(self):
        """Add OSM tile layer to the map.

        This uses a gdal wrapper around the OSM tile service - see the
        WorldOSM.gdal file for how it is constructed.
        """
        path = resources_path('osm', 'WorldOSM.gdal')
        layer = QgsRasterLayer(path, self.tr('OpenStreetMap'))
        registry = QgsMapLayerRegistry.instance()

        # For older versions we just add directly to the top of legend
        if QGis.QGIS_VERSION_INT < 20400:
            # True flag adds layer directly to legend
            registry.addMapLayer(layer, True)
            return
        # Otherwise try to add it as the last layer in the list
        # False flag prevents layer being added to legend
        registry.addMapLayer(layer, False)
        root = QgsProject.instance().layerTreeRoot()
        index = len(root.findLayers()) + 1
        # LOGGER.info('Inserting layer %s at position %s' % (
        #    layer.source(), index))
        root.insertLayer(index, layer)
        QgsMapLayerRegistry.instance().addMapLayer(layer)

    def add_petajakarta_layer(self):
        """Add petajakarta layer to the map.

        This uses the PetaJakarta API to fetch the latest floods in JK. See
        https://petajakarta.org/banjir/en/data/api/#aggregates
        """
        from safe.gui.tools.peta_jakarta_dialog import PetaJakartaDialog
        dialog = PetaJakartaDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def raster_reclassify(self):
        """Show dialog for Raster Reclassification.

        This will convert Raster Layer to Vector Layer
        """
        from safe.gui.tools.raster_reclassify_dialog import \
            RasterReclassifyDialog
        dialog = RasterReclassifyDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def show_batch_runner(self):
        """Show the batch runner dialog."""
        from safe.gui.tools.batch.batch_dialog import BatchDialog

        dialog = BatchDialog(
            parent=self.iface.mainWindow(),
            iface=self.iface,
            dock=self.dock_widget)
        dialog.exec_()  # modal

    def save_scenario(self):
        """Save current scenario to text file"""
        from safe.gui.tools.save_scenario import SaveScenarioDialog

        dialog = SaveScenarioDialog(
            iface=self.iface,
            dock=self.dock_widget)
        dialog.save_scenario()

    def _disable_keyword_tools(self):
        """Internal helper to disable the keyword and wizard actions."""
        self.action_keywords_wizard.setEnabled(False)

    def layer_changed(self, layer):
        """Enable or disable keywords editor icon when active layer changes.

        :param layer: The layer that is now active.
        :type layer: QgsMapLayer
        """
        if not layer:
            self._disable_keyword_tools()
            return
        if not hasattr(layer, 'providerType'):
            self._disable_keyword_tools()
            return
        if layer.providerType() == 'wms':
            self._disable_keyword_tools()
            return
        if is_raster_layer(layer) and layer.bandCount() > 1:
            self._disable_keyword_tools()
            return

        self.action_keywords_wizard.setEnabled(True)

    def shortcut_f7(self):
        """Executed when user press F7 - will show the shakemap importer."""
        self.show_shakemap_importer()
class ShakemapConverterDialog(QDialog, FORM_CLASS):
    """Importer for shakemap grid.xml files."""
    def __init__(self, parent=None, iface=None, dock_widget=None):
        """Constructor for the dialog.

        Show the grid converter dialog.

        :param parent: parent - widget to use as parent.
        :type parent: QWidget

        :param iface: QGIS QGisAppInterface instance.
        :type iface: QGisAppInterface

        :param dock_widget: Dock widget instance.
        :type dock_widget: Dock

        """
        QDialog.__init__(self, parent)
        self.parent = parent
        self.iface = iface
        self.dock_widget = dock_widget
        self.setupUi(self)
        self.setWindowTitle(
            self.tr('InaSAFE %s Shakemap Converter' % get_version()))
        self.warning_text = set()
        self.on_input_path_textChanged()
        self.on_output_path_textChanged()
        self.update_warning()
        self.output_layer = None

        # Event register
        # noinspection PyUnresolvedReferences
        self.use_output_default.toggled.connect(
            self.get_output_from_input)
        # noinspection PyUnresolvedReferences
        self.input_path.textChanged.connect(self.on_input_path_textChanged)
        # noinspection PyUnresolvedReferences
        self.output_path.textChanged.connect(self.on_output_path_textChanged)
        self.load_result.clicked.connect(self.load_result_toggled)

        # Set up things for context help
        self.help_button = self.button_box.button(QtGui.QDialogButtonBox.Help)
        # Allow toggling the help button
        self.help_button.setCheckable(True)
        self.help_button.toggled.connect(self.help_toggled)
        self.main_stacked_widget.setCurrentIndex(1)

    # noinspection PyPep8Naming
    def on_output_path_textChanged(self):
        """Action when output file name is changed.
        """
        output_path = self.output_path.text()
        output_not_xml_msg = self.tr('output file is not .tif')
        if not output_path.endswith('.tif'):
            self.warning_text.add(output_not_xml_msg)
        elif output_not_xml_msg in self.warning_text:
            self.warning_text.remove(output_not_xml_msg)
        self.update_warning()

    # noinspection PyPep8Naming
    def on_input_path_textChanged(self):
        """Action when input file name is changed.
        """
        input_path = self.input_path.text()
        input_not_grid_msg = self.tr('input file is not .xml')

        if not input_path.endswith('.xml'):
            self.warning_text.add(input_not_grid_msg)
        elif input_not_grid_msg in self.warning_text:
            self.warning_text.remove(input_not_grid_msg)

        if self.use_output_default.isChecked():
            self.get_output_from_input()
        self.update_warning()

    def update_warning(self):
        """Update warning message and enable/disable Ok button."""
        if len(self.warning_text) == 0:
            self.button_box.button(QDialogButtonBox.Ok).setEnabled(True)
            return

        header = html_header()
        footer = html_footer()
        string = header
        heading = m.Heading(self.tr('Shakemap Grid Importer'), **INFO_STYLE)
        tips = m.BulletedList()
        self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
        message = m.Message()
        message.add(heading)
        for warning in self.warning_text:
            tips.add(warning)

        message.add(tips)
        string += message.to_html()
        string += footer
        self.info_web_view.setHtml(string)

    def get_output_from_input(self):
        """Create default output location based on input location.
        """
        input_path = self.input_path.text()
        if input_path.endswith('.xml'):
            output_path = input_path[:-3] + 'tif'
        elif input_path == '':
            output_path = ''
        else:
            last_dot = input_path.rfind('.')
            if last_dot == -1:
                output_path = ''
            else:
                output_path = input_path[:last_dot + 1] + 'tif'
        self.output_path.setText(output_path)

    def accept(self):
        """Handler for when OK is clicked.
        """
        input_path = self.input_path.text()
        input_title = self.line_edit_title.text()
        input_source = self.line_edit_source.text()
        output_path = self.output_path.text()
        if not output_path.endswith('.tif'):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Output file name must be tif file')))
        if not os.path.exists(input_path):
            # noinspection PyArgumentList,PyCallByClass,PyTypeChecker
            QMessageBox.warning(
                self,
                self.tr('InaSAFE'),
                (self.tr('Input file does not exist')))
            return

        if self.nearest_mode.isChecked():
            algorithm = 'nearest'
        else:
            algorithm = 'invdist'

        QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))

        file_name = convert_mmi_data(
            input_path,
            input_title,
            input_source,
            output_path,
            algorithm=algorithm,
            algorithm_filename_flag=True)

        # reclassify raster
        file_info = QFileInfo(file_name)
        base_name = file_info.baseName()
        self.output_layer = QgsRasterLayer(file_name, base_name)
        self.output_layer.keywords = KeywordIO.read_keywords(self.output_layer)
        self.output_layer.keywords['classification'] = (
            earthquake_mmi_scale['key'])
        keywords = self.output_layer.keywords
        if self.output_layer.isValid():
            self.output_layer = reclassify(
                self.output_layer, overwrite_input=True)
            KeywordIO.write_keywords(self.output_layer, keywords)
        else:
            LOGGER.debug("Failed to load")

        QtGui.qApp.restoreOverrideCursor()

        if self.load_result.isChecked():
            # noinspection PyTypeChecker
            mmi_ramp_roman(self.output_layer)
            self.output_layer.saveDefaultStyle()
            if not self.output_layer.isValid():
                LOGGER.debug("Failed to load")
            else:
                # noinspection PyArgumentList
                QgsMapLayerRegistry.instance().addMapLayer(self.output_layer)
                iface.zoomToActiveLayer()

        if (self.keyword_wizard_checkbox.isChecked() and
                self.keyword_wizard_checkbox.isEnabled()):
            self.launch_keyword_wizard()

        self.done(self.Accepted)

    @pyqtSignature('')  # prevents actions being handled twice
    def on_open_input_tool_clicked(self):
        """Autoconnect slot activated when open input tool button is clicked.
        """
        # noinspection PyCallByClass,PyTypeChecker
        filename = QFileDialog.getOpenFileName(
            self, self.tr('Input file'), 'grid.xml',
            self.tr('Raw grid file (*.xml)'))
        self.input_path.setText(filename)

    @pyqtSignature('')  # prevents actions being handled twice
    def on_open_output_tool_clicked(self):
        """Autoconnect slot activated when open output tool button is clicked.
        """
        # noinspection PyCallByClass,PyTypeChecker
        filename = QFileDialog.getSaveFileName(
            self, self.tr('Output file'), 'grid.tif',
            self.tr('Raster file (*.tif)'))
        self.output_path.setText(filename)

    def load_result_toggled(self):
        """Function that perform action when load_result checkbox is clicked.
        """
        if self.load_result.isChecked():
            self.keyword_wizard_checkbox.setEnabled(True)
        else:
            self.keyword_wizard_checkbox.setEnabled(False)

    @pyqtSlot()
    @pyqtSignature('bool')  # prevents actions being handled twice
    def help_toggled(self, flag):
        """Show or hide the help tab in the stacked widget.

        .. versionadded: 3.2.1

        :param flag: Flag indicating whether help should be shown or hidden.
        :type flag: bool
        """
        if flag:
            self.help_button.setText(self.tr('Hide Help'))
            self.show_help()
        else:
            self.help_button.setText(self.tr('Show Help'))
            self.hide_help()

    def hide_help(self):
        """Hide the usage info from the user.

        .. versionadded: 3.2.1
        """
        self.main_stacked_widget.setCurrentIndex(1)

    def show_help(self):
        """Show usage info to the user."""
        # Read the header and footer html snippets
        self.main_stacked_widget.setCurrentIndex(0)
        header = html_header()
        footer = html_footer()

        string = header

        message = shakemap_converter_help()

        string += message.to_html()
        string += footer

        self.help_web_view.setHtml(string)

    def launch_keyword_wizard(self):
        """Launch keyword creation wizard."""
        # make sure selected layer is the output layer
        if self.iface.activeLayer() != self.output_layer:
            return

        # launch wizard dialog
        self.keyword_wizard = WizardDialog(
            self.iface.mainWindow(),
            self.iface,
            self.dock_widget)
        self.keyword_wizard.set_keywords_creation_mode(self.output_layer)
        self.keyword_wizard.exec_()  # modal
Example #12
0
class Plugin(object):

    """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

        # Actions
        self.action_add_layers = None
        self.action_add_osm_layer = None
        self.action_add_petabencana_layer = None
        self.action_batch_runner = None
        self.action_dock = None
        self.action_extent_selector = None
        self.action_field_mapping = None
        self.action_function_centric_wizard = None
        self.action_import_dialog = None
        self.action_keywords_wizard = None
        self.action_minimum_needs = None
        self.action_minimum_needs_config = None
        self.action_multi_buffer = None
        self.action_options = None
        self.action_run_tests = None
        self.action_save_scenario = None
        self.action_shake_converter = None
        self.action_show_definitions = None
        self.action_toggle_rubberbands = None

        self.translator = None
        self.toolbar = None
        self.wizard = None
        self.actions = []  # list of all QActions we create for InaSAFE

        self.message_bar_item = None
        # Flag indicating if toolbar should show only common icons or not
        self.full_toolbar = False
        # print self.tr('InaSAFE')
        # For enable/disable the keyword editor icon
        self.iface.currentLayerChanged.connect(self.layer_changed)

    # 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_to_legend=False):
        """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

        :param add_to_legend: Flag indicating whether the action should also
            be added to the layer legend menu. Default to False.
        :type add_to_legend: 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)
        if add_to_legend:
            # The id is the action name without spaces, tabs ...
            self.iface.legendInterface().addLegendLayerAction(
                action,
                self.tr('InaSAFE'),
                ''.join(action.text().split()),
                QgsMapLayer.VectorLayer,
                True)
            self.iface.legendInterface().addLegendLayerAction(
                action,
                self.tr('InaSAFE'),
                ''.join(action.text().split()),
                QgsMapLayer.RasterLayer,
                True)

    def _create_dock_toggle_action(self):
        """Create action for plugin dockable window (show/hide)."""
        # pylint: disable=W0201
        icon = resources_path('img', 'icons', 'icon.svg')
        self.action_dock = QAction(
            QIcon(icon),
            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 creation wizard
        # -------------------------------------

    def _create_keywords_wizard_action(self):
        """Create action for keywords creation wizard."""
        icon = resources_path('img', 'icons', 'show-keyword-wizard.svg')
        self.action_keywords_wizard = QAction(
            QIcon(icon),
            self.tr('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, add_to_legend=True)

    def _create_analysis_wizard_action(self):
        """Create action for IF-centric wizard."""
        icon = resources_path('img', 'icons', 'show-wizard.svg')
        self.action_function_centric_wizard = QAction(
            QIcon(icon),
            self.tr('Impact Function Centric Wizard'),
            self.iface.mainWindow())
        self.action_function_centric_wizard.setStatusTip(self.tr(
            'Open InaSAFE impact function centric wizard'))
        self.action_function_centric_wizard.setWhatsThis(self.tr(
            'Open InaSAFE impact function centric wizard'))
        self.action_function_centric_wizard.setEnabled(True)
        self.action_function_centric_wizard.triggered.connect(
            self.show_function_centric_wizard)
        self.add_action(self.action_function_centric_wizard)

    def _create_options_dialog_action(self):
        """Create action for options dialog."""
        icon = resources_path('img', 'icons', 'configure-inasafe.svg')
        self.action_options = QAction(
            QIcon(icon),
            self.tr('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, add_to_toolbar=self.full_toolbar)

    def _create_minimum_needs_action(self):
        """Create action for minimum needs dialog."""
        icon = resources_path('img', 'icons', 'show-minimum-needs.svg')
        self.action_minimum_needs = QAction(
            QIcon(icon),
            self.tr('Minimum Needs Calculator'), self.iface.mainWindow())
        self.action_minimum_needs.setStatusTip(self.tr(
            'Open InaSAFE minimum needs calculator'))
        self.action_minimum_needs.setWhatsThis(self.tr(
            'Open InaSAFE minimum needs calculator'))
        self.action_minimum_needs.triggered.connect(self.show_minimum_needs)
        self.add_action(
            self.action_minimum_needs, add_to_toolbar=self.full_toolbar)

    def _create_multi_buffer_action(self):
        """Create action for multi buffer dialog."""
        icon = resources_path('img', 'icons', 'show-multi-buffer.svg')
        self.action_multi_buffer = QAction(
            QIcon(icon),
            self.tr('Multi Buffer'), self.iface.mainWindow())
        self.action_multi_buffer.setStatusTip(self.tr(
            'Open InaSAFE multi buffer'))
        self.action_multi_buffer.setWhatsThis(self.tr(
            'Open InaSAFE multi buffer'))
        self.action_multi_buffer.triggered.connect(
            self.show_multi_buffer)
        self.add_action(self.action_multi_buffer,
            add_to_toolbar=self.full_toolbar)

    def _create_minimum_needs_options_action(self):
        """Create action for global minimum needs dialog."""
        icon = resources_path('img', 'icons', 'show-global-minimum-needs.svg')
        self.action_minimum_needs_config = QAction(
            QIcon(icon),
            self.tr('Minimum Needs Configuration'),
            self.iface.mainWindow())
        self.action_minimum_needs_config.setStatusTip(self.tr(
            'Open InaSAFE minimum needs configuration'))
        self.action_minimum_needs_config.setWhatsThis(self.tr(
            'Open InaSAFE minimum needs configuration'))
        self.action_minimum_needs_config.triggered.connect(
            self.show_minimum_needs_configuration)
        self.add_action(
            self.action_minimum_needs_config, add_to_toolbar=self.full_toolbar)

    def _create_shakemap_converter_action(self):
        """Create action for converter dialog."""
        icon = resources_path('img', 'icons', 'show-converter-tool.svg')
        self.action_shake_converter = QAction(
            QIcon(icon),
            self.tr('Shakemap 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, add_to_toolbar=self.full_toolbar)

    def _create_batch_runner_action(self):
        """Create action for batch runner dialog."""
        icon = resources_path('img', 'icons', 'show-batch-runner.svg')
        self.action_batch_runner = QAction(
            QIcon(icon),
            self.tr('Batch Runner'), self.iface.mainWindow())
        self.action_batch_runner.setStatusTip(self.tr(
            'Open Batch Runner'))
        self.action_batch_runner.setWhatsThis(self.tr(
            'Open Batch Runner'))
        self.action_batch_runner.triggered.connect(self.show_batch_runner)
        self.add_action(
            self.action_batch_runner, add_to_toolbar=self.full_toolbar)

    def _create_save_scenario_action(self):
        """Create action for save scenario dialog."""
        icon = resources_path('img', 'icons', 'save-as-scenario.svg')
        self.action_save_scenario = QAction(
            QIcon(icon),
            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, add_to_toolbar=self.full_toolbar)

    def _create_osm_downloader_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'show-osm-download.svg')
        self.action_import_dialog = QAction(
            QIcon(icon),
            self.tr('OpenStreetMap Downloader'),
            self.iface.mainWindow())
        self.action_import_dialog.setStatusTip(self.tr(
            'OpenStreetMap Downloader'))
        self.action_import_dialog.setWhatsThis(self.tr(
            'OpenStreetMap Downloader'))
        self.action_import_dialog.triggered.connect(self.show_osm_downloader)
        self.add_action(self.action_import_dialog)

    def _create_add_osm_layer_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'add-osm-tiles-layer.svg')
        self.action_add_osm_layer = QAction(
            QIcon(icon),
            self.tr('Add OpenStreetMap Tile Layer'),
            self.iface.mainWindow())
        self.action_add_osm_layer.setStatusTip(self.tr(
            'Add OpenStreetMap Tile Layer'))
        self.action_add_osm_layer.setWhatsThis(self.tr(
            'Use this to add an OSM layer to your map. '
            'It needs internet access to function.'))
        self.action_add_osm_layer.triggered.connect(self.add_osm_layer)
        self.add_action(self.action_add_osm_layer)

    def _create_show_definitions_action(self):
        """Create action for showing definitions / help."""
        icon = resources_path('img', 'icons', 'show-inasafe-help.svg')
        self.action_show_definitions = QAction(
            QIcon(icon),
            self.tr('InaSAFE Help'),
            self.iface.mainWindow())
        self.action_show_definitions.setStatusTip(self.tr(
            'Show InaSAFE Help'))
        self.action_show_definitions.setWhatsThis(self.tr(
            'Use this to show a document describing all InaSAFE concepts.'))
        self.action_show_definitions.triggered.connect(
            self.show_definitions)
        self.add_action(
            self.action_show_definitions,
            add_to_toolbar=True)

    def _create_field_mapping_action(self):
        """Create action for showing field mapping dialog."""
        icon = resources_path('img', 'icons', 'show-mapping-tool.svg')
        self.action_field_mapping = QAction(
            QIcon(icon),
            self.tr('InaSAFE Field Mapping Tool'),
            self.iface.mainWindow())
        self.action_field_mapping.setStatusTip(self.tr(
            'Assign field mapping to layer.'))
        self.action_field_mapping.setWhatsThis(self.tr(
            'Use this tool to assign field mapping in layer.'))
        self.action_field_mapping.setEnabled(False)
        self.action_field_mapping.triggered.connect(
            self.show_field_mapping)
        self.add_action(
            self.action_field_mapping,
            add_to_toolbar=True)

    def _create_add_petabencana_layer_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'add-petabencana-layer.svg')
        self.action_add_petabencana_layer = QAction(
            QIcon(icon),
            self.tr('Add PetaBencana Flood Layer'),
            self.iface.mainWindow())
        self.action_add_petabencana_layer.setStatusTip(self.tr(
            'Add PetaBencana Flood Layer'))
        self.action_add_petabencana_layer.setWhatsThis(self.tr(
            'Use this to add a PetaBencana layer to your map. '
            'It needs internet access to function.'))
        self.action_add_petabencana_layer.triggered.connect(
            self.add_petabencana_layer)
        self.add_action(
            self.action_add_petabencana_layer,
            add_to_toolbar=False)

    def _create_rubber_bands_action(self):
        """Create action for toggling rubber bands."""
        icon = resources_path('img', 'icons', 'toggle-rubber-bands.svg')
        self.action_toggle_rubberbands = QAction(
            QIcon(icon),
            self.tr('Toggle Scenario Outlines'), self.iface.mainWindow())
        message = self.tr('Toggle rubber bands showing scenario 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)

    def _create_analysis_extent_action(self):
        """Create action for analysis extent dialog."""
        icon = resources_path('img', 'icons', 'set-extents-tool.svg')
        self.action_extent_selector = QAction(
            QIcon(icon),
            self.tr('Set Analysis Area'),
            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)

    def _create_test_layers_action(self):
        """Create action for adding layers (developer mode, non final only)."""
        final_release = inasafe_release_status == 'final'
        settings = QSettings()
        self.developer_mode = settings.value(
            'inasafe/developer_mode', False, type=bool)
        if not final_release and self.developer_mode:
            icon = resources_path('img', 'icons', 'add-test-layers.svg')
            self.action_add_layers = QAction(
                QIcon(icon),
                self.tr('Add Test Layers'),
                self.iface.mainWindow())
            self.action_add_layers.setStatusTip(self.tr(
                'Add test layers'))
            self.action_add_layers.setWhatsThis(self.tr(
                'Add test layers'))
            self.action_add_layers.triggered.connect(
                self.add_test_layers)

            self.add_action(self.action_add_layers)

    def _create_run_test_action(self):
        """Create action for running tests (developer mode, non final only)."""
        final_release = inasafe_release_status == 'final'
        settings = QSettings()
        self.developer_mode = settings.value(
            'inasafe/developer_mode', False, type=bool)
        if not final_release and self.developer_mode:

            default_package = unicode(settings.value(
                'inasafe/testPackage', 'safe', type=str))
            msg = self.tr('Run tests in %s' % default_package)

            self.test_button = QToolButton()
            self.test_button.setMenu(QMenu())
            self.test_button.setPopupMode(QToolButton.MenuButtonPopup)

            icon = resources_path('img', 'icons', 'run-tests.svg')
            self.action_run_tests = QAction(
                QIcon(icon),
                msg,
                self.iface.mainWindow())

            self.action_run_tests.setStatusTip(msg)
            self.action_run_tests.setWhatsThis(msg)
            self.action_run_tests.triggered.connect(
                self.run_tests)

            self.test_button.menu().addAction(self.action_run_tests)
            self.test_button.setDefaultAction(self.action_run_tests)

            self.action_select_package = QAction(
                QIcon(icon),
                self.tr('Select package'),
                self.iface.mainWindow())

            self.action_select_package.setStatusTip(self.tr(
                'Select Test Package'))
            self.action_select_package.setWhatsThis(self.tr(
                'Select Test Package'))
            self.action_select_package.triggered.connect(
                self.select_test_package)
            self.test_button.menu().addAction(self.action_select_package)
            self.toolbar.addWidget(self.test_button)

            self.add_action(self.action_run_tests, add_to_toolbar=False)
            self.add_action(self.action_select_package, add_to_toolbar=False)

    def _create_dock(self):
        """Create dockwidget and tabify it with the legend."""
        # Import dock here as it needs to be imported AFTER i18n is set up
        from safe.gui.widgets.dock import Dock
        self.dock_widget = Dock(self.iface)
        self.dock_widget.setObjectName('InaSAFE-Dock')
        self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock_widget)
        legend_tab = self.iface.mainWindow().findChild(QApplication, 'Legend')
        if legend_tab:
            self.iface.mainWindow().tabifyDockWidget(
                legend_tab, self.dock_widget)
            self.dock_widget.raise_()

    # 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')
        self.dock_widget = None
        # Now create the actual dock
        self._create_dock()
        # And all the menu actions
        # Configuration Group
        self._create_dock_toggle_action()
        self._create_options_dialog_action()
        self._create_minimum_needs_options_action()
        self._create_analysis_extent_action()
        self._create_rubber_bands_action()
        self._add_spacer_to_menu()
        self._create_keywords_wizard_action()
        self._create_analysis_wizard_action()
        self._add_spacer_to_menu()
        self._create_field_mapping_action()
        self._create_osm_downloader_action()
        self._create_add_osm_layer_action()
        self._create_add_petabencana_layer_action()
        self._create_shakemap_converter_action()
        self._create_minimum_needs_action()
        self._create_multi_buffer_action()
        self._create_test_layers_action()
        self._create_run_test_action()
        self._add_spacer_to_menu()
        self._create_batch_runner_action()
        self._create_save_scenario_action()
        self._add_spacer_to_menu()
        self._create_show_definitions_action()

        # 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())

    def _add_spacer_to_menu(self):
        """Create a spacer to the menu to separate action groups."""
        separator = QAction(self.iface.mainWindow())
        separator.setSeparator(True)
        self.iface.addPluginToMenu(self.tr('InaSAFE'), separator)

    @staticmethod
    def clear_modules():
        """Unload inasafe functions and try to return QGIS to before InaSAFE.

        .. todo:: I think this function can be removed. TS.
        """
        # next lets force remove any inasafe related modules
        modules = []
        for module in sys.modules:
            if 'inasafe' in module:
                # Check if it is really one of our modules i.e. exists in the
                # plugin directory
                tokens = module.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' % module)
                    modules.append(module)
        for module in modules:
            del (sys.modules[module])
        for module in sys.modules:
            if 'inasafe' in module:
                print module

        # 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
        if self.wizard:
            self.wizard.deleteLater()
        for myAction in self.actions:
            self.iface.removePluginMenu(self.tr('InaSAFE'), myAction)
            self.iface.removeToolBarIcon(myAction)
            self.iface.legendInterface().removeLegendLayerAction(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)

        # Unload QGIS expressions loaded by the plugin.
        for qgis_expression in qgis_expressions().keys():
            QgsExpression.unregisterFunction(qgis_expression)

    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 add_test_layers(self):
        """Add standard test layers."""
        from safe.test.utilities import load_standard_layers
        load_standard_layers()
        rect = QgsRectangle(106.806, -6.195, 106.837, -6.167)
        self.iface.mapCanvas().setExtent(rect)

    def select_test_package(self):
        """Select the test package."""
        settings = QSettings()
        default_package = 'safe'
        user_package = unicode(settings.value(
            'inasafe/testPackage', default_package, type=str))

        test_package, _ = QInputDialog.getText(
            self.iface.mainWindow(),
            self.tr('Select the python test package'),
            self.tr('Select the python test package'),
            QLineEdit.Normal,
            user_package)

        if test_package == '':
            test_package = default_package

        settings.setValue('inasafe/testPackage', test_package)
        msg = self.tr('Run tests in %s' % test_package)
        self.action_run_tests.setWhatsThis(msg)
        self.action_run_tests.setText(msg)

    def run_tests(self):
        """Run unit tests in the python console."""
        from PyQt4.QtGui import QDockWidget
        main_window = self.iface.mainWindow()
        action = main_window.findChild(QAction, 'mActionShowPythonDialog')
        action.trigger()
        settings = QSettings()
        package = unicode(settings.value(
            'inasafe/testPackage', 'safe', type=str))
        for child in main_window.findChildren(QDockWidget, 'PythonConsole'):
            if child.objectName() == 'PythonConsole':
                child.show()
                for widget in child.children():
                    if 'PythonConsoleWidget' in str(widget.__class__):
                        # print "Console widget found"
                        shell = widget.shell
                        shell.runCommand(
                            'from inasafe.test_suite import test_package')
                        shell.runCommand('test_package(\'%s\')' % package)
                        break

    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.gui.tools.extent_selector_dialog import ExtentSelectorDialog
        widget = ExtentSelectorDialog(
            self.iface,
            self.iface.mainWindow(),
            extent=self.dock_widget.extent.user_extent,
            crs=self.dock_widget.extent.crs)
        widget.clear_extent.connect(
            self.dock_widget.extent.clear_user_analysis_extent)
        widget.extent_defined.connect(
            self.dock_widget.define_user_analysis_extent)
        # This ensures that run button state is updated on dialog close
        widget.extent_selector_closed.connect(
            self.dock_widget.validate_impact_function)
        # 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.gui.tools.minimum_needs.needs_calculator_dialog import (
            NeedsCalculatorDialog
        )

        dialog = NeedsCalculatorDialog(self.iface.mainWindow())
        dialog.exec_()

    def show_minimum_needs_configuration(self):
        """Show the minimum needs dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.minimum_needs.needs_manager_dialog import (
            NeedsManagerDialog)

        dialog = NeedsManagerDialog(
            parent=self.iface.mainWindow(),
            dock=self.dock_widget)
        dialog.exec_()  # modal

    def show_options(self):
        """Show the options dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.options_dialog import OptionsDialog

        dialog = OptionsDialog(
            iface=self.iface,
            parent=self.iface.mainWindow())
        if dialog.exec_():  # modal
            self.dock_widget.read_settings()

    def show_keywords_wizard(self):
        """Show the keywords creation wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        if self.iface.activeLayer() is None:
            return

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since the IFCW is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_keywords_creation_mode()
        self.wizard.exec_()  # modal

    def show_function_centric_wizard(self):
        """Show the function centric wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since it is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_function_centric_mode()
        # non-modal in order to hide for selecting user extent
        self.wizard.show()

    def show_shakemap_importer(self):
        """Show the converter dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.shake_grid.shakemap_converter_dialog import (
            ShakemapConverterDialog)

        dialog = ShakemapConverterDialog(
            self.iface.mainWindow(), self.iface, self.dock_widget)
        dialog.exec_()  # modal

    def show_multi_buffer(self):
        """Show the multi buffer tool."""
        from safe.gui.tools.multi_buffer_dialog import (
            MultiBufferDialog)

        dialog = MultiBufferDialog(
            self.iface.mainWindow(), self.iface, self.dock_widget)
        dialog.exec_()  # modal

    def show_osm_downloader(self):
        """Show the OSM buildings downloader dialog."""
        from safe.gui.tools.osm_downloader_dialog import OsmDownloaderDialog

        dialog = OsmDownloaderDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def add_osm_layer(self):
        """Add OSM tile layer to the map.

        This uses a gdal wrapper around the OSM tile service - see the
        WorldOSM.gdal file for how it is constructed.
        """
        path = resources_path('osm', 'WorldOSM.gdal')
        layer = QgsRasterLayer(path, self.tr('OpenStreetMap'))
        registry = QgsMapLayerRegistry.instance()

        # For older versions we just add directly to the top of legend
        if QGis.QGIS_VERSION_INT < 20400:
            # True flag adds layer directly to legend
            registry.addMapLayer(layer, True)
            return
        # Otherwise try to add it as the last layer in the list
        # False flag prevents layer being added to legend
        registry.addMapLayer(layer, False)
        root = QgsProject.instance().layerTreeRoot()
        index = len(root.findLayers()) + 1
        # LOGGER.info('Inserting layer %s at position %s' % (
        #    layer.source(), index))
        root.insertLayer(index, layer)
        QgsMapLayerRegistry.instance().addMapLayer(layer)

    def show_definitions(self):
        """Show InaSAFE Definitions (a report showing all key metadata)."""
        from safe.gui.tools.help_dialog import HelpDialog
        from safe.gui.tools.help import definitions_help
        dialog = HelpDialog(
            self.iface.mainWindow(),
            definitions_help.definitions_help())
        dialog.show()  # non modal

    def show_field_mapping(self):
        """Show InaSAFE Field Mapping."""
        from safe.gui.tools.field_mapping_dialog import FieldMappingDialog
        dialog = FieldMappingDialog(
            parent=self.iface.mainWindow(),
            iface=self.iface,)
        if dialog.exec_():  # modal
            LOGGER.debug('Show field mapping accepted')
            self.dock_widget.layer_changed(self.iface.activeLayer())
        else:
            LOGGER.debug('Show field mapping not accepted')

    def add_petabencana_layer(self):
        """Add petabencana layer to the map.

        This uses the PetaBencana API to fetch the latest floods in JK. See
        https://data.petabencana.id/floods
        """
        from safe.gui.tools.peta_bencana_dialog import PetaBencanaDialog
        dialog = PetaBencanaDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def show_batch_runner(self):
        """Show the batch runner dialog."""
        from safe.gui.tools.batch.batch_dialog import BatchDialog

        dialog = BatchDialog(
            parent=self.iface.mainWindow(),
            iface=self.iface,
            dock=self.dock_widget)
        dialog.exec_()  # modal

    def save_scenario(self):
        """Save current scenario to text file"""
        from safe.gui.tools.save_scenario import SaveScenarioDialog

        dialog = SaveScenarioDialog(
            iface=self.iface,
            dock=self.dock_widget)
        dialog.save_scenario()

    def layer_changed(self, layer):
        """Enable or disable keywords editor icon when active layer changes.

        :param layer: The layer that is now active.
        :type layer: QgsMapLayer
        """
        if not layer:
            enable_keyword_wizard = False
        elif not hasattr(layer, 'providerType'):
            enable_keyword_wizard = False
        elif layer.providerType() == 'wms':
            enable_keyword_wizard = False
        else:
            enable_keyword_wizard = True

        try:
            if layer:
                if is_raster_layer(layer):
                    enable_field_mapping_tool = False
                else:
                    keywords = KeywordIO().read_keywords(layer)
                    layer_purpose = keywords.get('layer_purpose')

                    if not layer_purpose:
                        enable_field_mapping_tool = False

                    if layer_purpose == layer_purpose_exposure['key']:
                        layer_subcategory = keywords.get('exposure')
                    elif layer_purpose == layer_purpose_hazard['key']:
                        layer_subcategory = keywords.get('hazard')
                    else:
                        layer_subcategory = None
                    field_groups = get_field_groups(
                        layer_purpose, layer_subcategory)
                    if len(field_groups) == 0:
                        # No field group, disable field mapping tool.
                        enable_field_mapping_tool = False
                    else:
                        enable_field_mapping_tool = True
            else:
                enable_field_mapping_tool = False
        except (KeywordNotFoundError, NoKeywordsFoundError, MetadataReadError):
            # No keywords, disable field mapping tool.
            enable_field_mapping_tool = False

        self.action_keywords_wizard.setEnabled(enable_keyword_wizard)
        self.action_field_mapping.setEnabled(enable_field_mapping_tool)

    def shortcut_f7(self):
        """Executed when user press F7 - will show the shakemap importer."""
        self.show_shakemap_importer()
    def test_input_function_centric_wizard(self):
        """Test the IFCW mode: FloodRasterBuildingFunction"""

        expected_test_layer_count = 2

        expected_hazards_count = 5
        # expected_exposures_count = 3
        expected_exposures_count = 4
        expected_flood_structure_functions_count = 4
        expected_raster_polygon_functions_count = 2
        expected_functions_count = 2
        chosen_if = 'FloodRasterBuildingFunction'

        expected_hazard_layers_count = 1
        expected_exposure_layers_count = 1
        expected_aggregation_layers_count = 0

        # expected_summary_key = 'minimum needs'
        # expected_summary_value_fragment = 'rice'

        # expected_report_size = 4055  # as saved on Ubuntu
        # TS : changed tolerance from 120 to 160 because above change
        # causes fail on fedora
        # AG: updated the tolerance from 160 to 190
        # MD: more tolerance please! 190 -> 200
        # tolerance = 200  # windows EOL etc

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        layer = clone_shp_layer(
            name='buildings',
            include_keywords=True,
            source_directory=standard_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Check the environment first
        self.assertIsNotNone(layer.dataProvider())

        count = len(dialog.iface.mapCanvas().layers())
        self.assertEqual(count, expected_test_layer_count)

        # step_fc_functions1: test function matrix dimensions
        col_count = dialog.step_fc_functions1.tblFunctions1.columnCount()
        self.assertEqual(col_count, expected_hazards_count)
        row_count = dialog.step_fc_functions1.tblFunctions1.rowCount()
        self.assertEqual(row_count, expected_exposures_count)

        # step_fc_functions1: test number of functions for flood x structure
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(3, 1)
        count = len(dialog.step_fc_functions1.selected_functions_1())
        self.assertEqual(count, expected_flood_structure_functions_count)

        # step_fc_functions1: press ok
        dialog.pbnNext.click()

        # step_fc_functions2: test number of functions for raster flood
        # and polygon structure
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(3, 0)

        count = len(dialog.step_fc_functions2.selected_functions_2())
        self.assertEqual(count, expected_raster_polygon_functions_count)

        # step_fc_functions2: press ok
        dialog.pbnNext.click()

        # step_fc_function: test number of available functions
        self.check_current_step(dialog.step_fc_function)
        count = dialog.step_fc_function.lstFunctions.count()
        self.assertEqual(count, expected_functions_count)

        # step_fc_function: test if chosen_if is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(count)]
        self.assertTrue(chosen_if in flood_ifs)

        # step_fc_function: select FloodRasterBuildingImpactFunction and
        # press ok
        chosen_if_row = flood_ifs.index(chosen_if)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas: test the lstCanvasHazLayers state
        # Note this step is tested prior to step_fc_hazlayer_origin
        # as the list is prepared prior to autoselecting the radiobuttons
        count = dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.count()
        self.assertEqual(count, expected_hazard_layers_count)

        # step_fc_hazlayer_origin: test if the radiobuttons are autmatically
        # enabled and selected
        self.assertTrue(
            dialog.step_fc_hazlayer_origin.rbHazLayerFromCanvas.isEnabled())
        self.assertTrue(
            dialog.step_fc_hazlayer_origin.rbHazLayerFromCanvas.isChecked())

        # step_fc_hazlayer_origin: press ok
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas: press ok
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas: test the lstCanvasExpLayers state
        # Note this step is tested prior to step_fc_explayer_origin
        # as the list is prepared prior to autoselecting the radiobuttons
        count = dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.count()
        self.assertEqual(count, expected_exposure_layers_count)

        # step_fc_explayer_origin: test if the radiobuttons are automatically
        # enabled and selected
        self.assertTrue(
            dialog.step_fc_explayer_origin.rbExpLayerFromCanvas.isEnabled())
        self.assertTrue(
            dialog.step_fc_explayer_origin.rbExpLayerFromCanvas.isChecked())

        # step_fc_explayer_origin: press ok
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas: press ok
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas: test the lstCanvasAggLayers state
        # Note this step is tested prior to step_fc_agglayer_origin
        # as the list is prepared prior to auto selecting the radio buttons
        count = dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.count()
        self.assertEqual(count, expected_aggregation_layers_count)

        # step_fc_agglayer_origin: test if the radio buttons are automatically
        # enabled and selected
        self.assertFalse(
            dialog.step_fc_agglayer_origin.rbAggLayerFromCanvas.isEnabled())
        self.assertTrue(
            dialog.step_fc_agglayer_origin.rbAggLayerFromBrowser.isChecked())

        # step_fc_agglayer_origin: switch to no aggregation and press ok
        self.check_current_step(dialog.step_fc_agglayer_origin)
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.click()
        dialog.pbnNext.click()

        # step_fc_extent: switch to layer's extent and press ok
        self.check_current_step(dialog.step_fc_extent)
        dialog.step_fc_extent.extent_dialog.hazard_exposure_only.click()
        dialog.pbnNext.click()

        # step_fc_params: press ok (already covered by the relevant test)
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnNext.click()

        # step_fc_summary: test minimum needs text
        # summaries = dialog.lblSummary.text().split('<br/>')

        # #TODO: temporarily disable minimum needs test as they seem
        # #te be removed from params
        # minneeds = [s for s in summaries
        #            if expected_summary_key.upper() in s.upper()]
        # self.assertTrue(minneeds)
        # self.assertTrue(expected_summary_value_fragment.upper()
        #                in minneeds[0].upper())

        # step_fc_summary: run analysis
        dialog.pbnNext.click()

        # No longer valid for impact data.
        # step_fc_analysis: test the html output
        # report_path = dialog.wvResults.report_path
        # size = os.stat(report_path).st_size
        # self.assertTrue(
        #     (expected_report_size - tolerance < size < expected_report_size +
        #      tolerance))
        # close the wizard
        dialog.pbnNext.click()
    def test_input_function_centric_wizard_test_4(self):
        """Test keyword creation wizard called from the
           impact function centric one
        """

        chosen_if = 'FloodEvacuationRasterHazardFunction'

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        # Just the hazard layer in two copies
        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=False,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=False,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # step_fc_functions1: select functions for flood x population
        self.check_current_step(dialog.step_fc_functions1)
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(1, 1)
        dialog.pbnNext.click()

        # step_fc_functions2: select functions for raster x raster
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(0, 0)
        dialog.pbnNext.click()

        # step_fc_function: test if FloodEvacuationRasterHazardFunction
        # is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(dialog.step_fc_function.lstFunctions.count())]
        self.assertTrue(chosen_if in flood_ifs)

        # step_fc_function: select FloodEvacuationRasterHazardFunction and
        # press ok
        self.check_current_step(dialog.step_fc_function)
        chosen_if_row = flood_ifs.index(chosen_if)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_origin:
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas:
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        # Select the first layer and register it as unsuitable (Tsunmami)
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(0)
        dialog.pbnNext.click()

        # step_kw_purpose
        self.check_current_step(dialog.step_kw_purpose)
        self.select_from_list_widget(
            'Hazard', dialog.step_kw_purpose.lstCategories)
        dialog.pbnNext.click()

        # step_kw_subcategory
        self.check_current_step(dialog.step_kw_subcategory)
        self.select_from_list_widget(
            'Tsunami', dialog.step_kw_subcategory.lstSubcategories)
        dialog.pbnNext.click()

        # step_kw_hazard_category
        self.check_current_step(dialog.step_kw_hazard_category)
        self.select_from_list_widget(
            'Single event',
            dialog.step_kw_hazard_category.lstHazardCategories)
        dialog.pbnNext.click()

        # step_kw_layermode
        self.check_current_step(dialog.step_kw_layermode)
        self.select_from_list_widget(
            'Continuous', dialog.step_kw_layermode.lstLayerModes)
        dialog.pbnNext.click()

        # step_kw_unit
        self.check_current_step(dialog.step_kw_unit)
        self.select_from_list_widget(
            'Metres', dialog.step_kw_unit.lstUnits)
        dialog.pbnNext.click()

        # step_kw_source
        self.check_current_step(dialog.step_kw_source)
        dialog.pbnNext.click()

        # step_kw_title
        self.check_current_step(dialog.step_kw_title)
        dialog.pbnNext.click()

        # step_kw_summary
        self.check_current_step(dialog.step_kw_summary)
        # step back and forth
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_kw_title)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_summary)

        # Finish the keyword thread and register the unsuitable layer
        dialog.pbnNext.click()

        # Should be again in the step_fc_hazlayer_from_canvas
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        # Now there is only one layer listed - register it properly
        dialog.pbnNext.click()

        # step_kw_purpose
        self.check_current_step(dialog.step_kw_purpose)
        self.select_from_list_widget(
            'Hazard', dialog.step_kw_purpose.lstCategories)
        dialog.pbnNext.click()

        # step_kw_subcategory
        self.check_current_step(dialog.step_kw_subcategory)
        self.select_from_list_widget(
            'Flood', dialog.step_kw_subcategory.lstSubcategories)
        dialog.pbnNext.click()

        # step_kw_hazard_category
        self.check_current_step(dialog.step_kw_hazard_category)
        self.select_from_list_widget(
            'Single event',
            dialog.step_kw_hazard_category.lstHazardCategories)
        dialog.pbnNext.click()

        # step_kw_layermode
        self.check_current_step(dialog.step_kw_layermode)
        self.select_from_list_widget(
            'Continuous', dialog.step_kw_layermode.lstLayerModes)
        dialog.pbnNext.click()

        # step_kw_unit
        self.check_current_step(dialog.step_kw_unit)
        self.select_from_list_widget(
            'Metres', dialog.step_kw_unit.lstUnits)
        dialog.pbnNext.click()

        # step_kw_source
        self.check_current_step(dialog.step_kw_source)
        dialog.pbnNext.click()

        # step_kw_title
        self.check_current_step(dialog.step_kw_title)
        dialog.pbnNext.click()

        # step_kw_summary
        self.check_current_step(dialog.step_kw_summary)
        dialog.pbnNext.click()

        # Should be in the step_fc_explayer_origin
        self.check_current_step(dialog.step_fc_explayer_origin)

        dialog.pbnCancel.click()
    def test_input_function_centric_wizard_test_2(self):
        """Test the IFCW mode: """

        chosen_if = 'FloodEvacuationRasterHazardFunction'

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        # Hazard layer
        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=True,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Exposure layer
        layer = clone_raster_layer(
            name='people_allow_resampling_true',
            extension='.tif',
            include_keywords=True,
            source_directory=test_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # step_fc_functions1: select functions for flood x population
        self.check_current_step(dialog.step_fc_functions1)
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(1, 1)
        dialog.pbnNext.click()

        # step_fc_functions2: select functions for raster x raster
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(0, 0)
        dialog.pbnNext.click()

        # step_fc_function: test if FloodEvacuationRasterHazardFunction
        # is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(dialog.step_fc_function.lstFunctions.count())]
        self.assertTrue(chosen_if in flood_ifs)

        # step_fc_function: select FloodEvacuationRasterHazardFunction and
        # press ok
        self.check_current_step(dialog.step_fc_function)
        chosen_if_row = flood_ifs.index(chosen_if)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_origin:
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas:
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_explayer_origin:
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas:
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_agglayer_origin:
        self.check_current_step(dialog.step_fc_agglayer_origin)
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.click()
        dialog.pbnNext.click()

        # step_fc_extent:
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnNext.click()

        # step_fc_params:
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnNext.click()

        # step_fc_summary:
        self.check_current_step(dialog.step_fc_summary)
        dialog.pbnNext.click()

        # step_fc_analysis:
        self.check_current_step(dialog.step_fc_analysis)

        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_summary)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_agglayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_function)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_functions2)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_functions1)
        dialog.pbnCancel.click()
    def test_input_function_centric_wizard_test_3(self):
        """Test various usecases of the wizard:
           keywordless layers, disjoint layers, browsers,
           stepping back and forth ."""

        chosen_if1 = 'FloodRasterBuildingFunction'
        chosen_if2 = 'ClassifiedRasterHazardBuildingFunction'

        expected_hazard_layers_count = 2
        expected_exposure_layers_count = 2
        expected_aggregation_layers_count = 2

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        # Hazard layer without keywords
        layer = clone_raster_layer(
            name='keywordless_layer',
            extension='.tif',
            include_keywords=False,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Hazard layer - disjoint
        layer = clone_raster_layer(
            name='continuous_flood_unaligned_big_size',
            extension='.tif',
            include_keywords=True,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Hazard layer
        layer = clone_raster_layer(
            name='classified_flood_20_20',
            extension='.asc',
            include_keywords=True,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Exposure layer
        layer = clone_shp_layer(
            name='building-points',
            include_keywords=True,
            source_directory=test_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Exposure layer without keywords
        layer = clone_shp_layer(
            name='building-points',
            include_keywords=False,
            source_directory=test_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Aggregation layer
        layer = clone_shp_layer(
            name='district_osm_jakarta',
            include_keywords=True,
            source_directory=test_data_path('boundaries'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Aggregation layer without keywords
        layer = clone_shp_layer(
            name='grid_jakarta',
            include_keywords=False,
            source_directory=test_data_path('boundaries'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # step_fc_functions1: select functions for flood x structure
        self.check_current_step(dialog.step_fc_functions1)
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(3, 1)
        dialog.pbnNext.click()

        # step_fc_functions2: select functions for raster x point
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(1, 0)
        dialog.pbnNext.click()

        # step_fc_function: test if FloodRasterBuildingFunction is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(dialog.step_fc_function.lstFunctions.count())]
        self.assertTrue(chosen_if1 in flood_ifs)

        # step_fc_function: select FloodRasterBuildingFunction and
        # press ok
        self.check_current_step(dialog.step_fc_function)
        chosen_if_row = flood_ifs.index(chosen_if1)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_origin:
        self.check_current_step(dialog.step_fc_hazlayer_origin)

        # step_fc_hazlayer_from_canvas: test the lstCanvasHazLayers state
        # Note this step is tested prior to step_fc_hazlayer_origin
        # as the list is prepared prior to autoselecting the radiobuttons
        count = dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.count()
        self.assertEqual(count, expected_hazard_layers_count)

        # test if hazard browser works
        dialog.step_fc_hazlayer_origin.rbHazLayerFromBrowser.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_browser)
        # step back and continue with hazard from canvas
        dialog.pbnBack.click()
        dialog.step_fc_hazlayer_origin.rbHazLayerFromCanvas.click()
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        # Select the second (keywordless) layer in order to trigger preparing
        # the 'missing keywords' description.
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(1)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_purpose)
        dialog.pbnBack.click()
        # Now select the first (proper) layer and press ok
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(0)
        dialog.pbnNext.click()

        # step_fc_explayer_origin
        self.check_current_step(dialog.step_fc_explayer_origin)
        count = dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.count()
        self.assertEqual(count, expected_exposure_layers_count)

        # test if exposure browser works
        dialog.step_fc_explayer_origin.rbExpLayerFromBrowser.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_from_browser)
        # step back and continue with exposure from canvas
        dialog.pbnBack.click()
        dialog.step_fc_explayer_origin.rbExpLayerFromCanvas.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        # Select the second (keywordless) layer in order to trigger preparing
        # the 'missing keywords' description.
        dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.setCurrentRow(1)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_purpose)
        dialog.pbnBack.click()
        # Now select the first (proper) layer and press ok
        dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.setCurrentRow(0)
        dialog.pbnNext.click()

        # step_fc_disjoint_layers
        self.check_current_step(dialog.step_fc_disjoint_layers)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_function)

        # Select ClassifiedRasterHazardBuildingFunction
        chosen_if_row = flood_ifs.index(chosen_if2)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)

        # Select the first (proper) layer and press ok
        dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.setCurrentRow(0)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.setCurrentRow(0)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_agglayer_origin)

        # test if no aggregation works
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_extent)

        # step back and test if aggregation browser works
        dialog.pbnBack.click()
        dialog.step_fc_agglayer_origin.rbAggLayerFromBrowser.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_agglayer_from_browser)

        # step back and continue with aggregation from canvas
        dialog.pbnBack.click()
        dialog.step_fc_agglayer_origin.rbAggLayerFromCanvas.click()
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_agglayer_from_canvas)
        count = dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.count()
        self.assertEqual(count, expected_aggregation_layers_count)
        # Select the second (keywordless) layer in order to trigger preparing
        # the 'missing keywords' description.
        dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.setCurrentRow(1)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_kw_purpose)
        dialog.pbnBack.click()
        # Now select the first (proper) layer and press ok
        dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.setCurrentRow(0)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnNext.click()

        self.check_current_step(dialog.step_fc_params)
        # Step back and enter the params step again
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnNext.click()
        self.check_current_step(dialog.step_fc_summary)

        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_agglayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_agglayer_origin)
        # No need to test more backward steps (already tested in other test)
        dialog.pbnCancel.click()
Example #17
0
class Plugin(object):
    """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
        """
        # Register all the impact functions
        register_impact_functions()
        # 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_minimum_needs_config = None
        self.action_impact_merge_dlg = None
        self.key_action = None
        self.action_options = None
        self.action_keywords_wizard = None
        self.action_function_centric_wizard = None
        self.action_extent_selector = None
        self.translator = None
        self.toolbar = None
        self.wizard = 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
        # Flag indicating if toolbar should show only common icons or not
        self.full_toolbar = False
        # 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['INASAFE_LANG'] = str(new_locale)

        LOGGER.debug('%s %s %s' % (
            new_locale, QLocale.system().name(), os.environ['INASAFE_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)

    def _create_dock_toggle_action(self):
        """Create action for plugin dockable window (show/hide)."""
        # pylint: disable=W0201
        icon = resources_path('img', 'icons', 'icon.svg')
        self.action_dock = QAction(
            QIcon(icon),
            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 creation wizard
        # -------------------------------------

    def _create_keywords_wizard_action(self):
        """Create action for keywords creation wizard."""
        icon = resources_path('img', 'icons', 'show-keyword-wizard.svg')
        self.action_keywords_wizard = QAction(
            QIcon(icon),
            self.tr('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)

    def _create_analysis_wizard_action(self):
        """Create action for IF-centric wizard."""
        icon = resources_path('img', 'icons', 'show-wizard.svg')
        self.action_function_centric_wizard = QAction(
            QIcon(icon),
            self.tr('Impact Function Centric Wizard'),
            self.iface.mainWindow())
        self.action_function_centric_wizard.setStatusTip(self.tr(
            'Open InaSAFE impact function centric wizard'))
        self.action_function_centric_wizard.setWhatsThis(self.tr(
            'Open InaSAFE impact function centric wizard'))
        self.action_function_centric_wizard.setEnabled(True)
        self.action_function_centric_wizard.triggered.connect(
            self.show_function_centric_wizard)
        self.add_action(self.action_function_centric_wizard)

    def _create_options_dialog_action(self):
        """Create action for options dialog."""
        icon = resources_path('img', 'icons', 'configure-inasafe.svg')
        self.action_options = QAction(
            QIcon(icon),
            self.tr('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, add_to_toolbar=self.full_toolbar)

    def _create_minimum_needs_action(self):
        """Create action for minimum needs dialog."""
        icon = resources_path('img', 'icons', 'show-minimum-needs.svg')
        self.action_minimum_needs = QAction(
            QIcon(icon),
            self.tr('Minimum Needs Calculator'), self.iface.mainWindow())
        self.action_minimum_needs.setStatusTip(self.tr(
            'Open InaSAFE minimum needs calculator'))
        self.action_minimum_needs.setWhatsThis(self.tr(
            'Open InaSAFE minimum needs calculator'))
        self.action_minimum_needs.triggered.connect(self.show_minimum_needs)
        self.add_action(
            self.action_minimum_needs, add_to_toolbar=self.full_toolbar)

    def _create_minimum_needs_options_action(self):
        """Create action for global minimum needs dialog."""
        icon = resources_path('img', 'icons', 'show-global-minimum-needs.svg')
        self.action_minimum_needs_config = QAction(
            QIcon(icon),
            self.tr('Minimum Needs Configuration'),
            self.iface.mainWindow())
        self.action_minimum_needs_config.setStatusTip(self.tr(
            'Open InaSAFE minimum needs configuration'))
        self.action_minimum_needs_config.setWhatsThis(self.tr(
            'Open InaSAFE minimum needs configuration'))
        self.action_minimum_needs_config.triggered.connect(
            self.show_minimum_needs_configuration)
        self.add_action(
            self.action_minimum_needs_config, add_to_toolbar=self.full_toolbar)

    def _create_shakemap_converter_action(self):
        """Create action for converter dialog."""
        icon = resources_path('img', 'icons', 'show-converter-tool.svg')
        self.action_shake_converter = QAction(
            QIcon(icon),
            self.tr('Shakemap 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, add_to_toolbar=self.full_toolbar)

    def _create_batch_runner_action(self):
        """Create action for batch runner dialog."""
        icon = resources_path('img', 'icons', 'show-batch-runner.svg')
        self.action_batch_runner = QAction(
            QIcon(icon),
            self.tr('Batch Runner'), self.iface.mainWindow())
        self.action_batch_runner.setStatusTip(self.tr(
            'Open Batch Runner'))
        self.action_batch_runner.setWhatsThis(self.tr(
            'Open Batch Runner'))
        self.action_batch_runner.triggered.connect(self.show_batch_runner)
        self.add_action(
            self.action_batch_runner, add_to_toolbar=self.full_toolbar)

    def _create_save_scenario_action(self):
        """Create action for save scenario dialog."""
        icon = resources_path('img', 'icons', 'save-as-scenario.svg')
        self.action_save_scenario = QAction(
            QIcon(icon),
            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, add_to_toolbar=self.full_toolbar)

    def _create_osm_downloader_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'show-osm-download.svg')
        self.action_import_dialog = QAction(
            QIcon(icon),
            self.tr('OpenStreetMap Downloader'),
            self.iface.mainWindow())
        self.action_import_dialog.setStatusTip(self.tr(
            'OpenStreetMap Downloader'))
        self.action_import_dialog.setWhatsThis(self.tr(
            'OpenStreetMap Downloader'))
        self.action_import_dialog.triggered.connect(self.show_osm_downloader)
        self.add_action(self.action_import_dialog)

    def _create_add_osm_layer_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'add-osm-tiles-layer.svg')
        self.action_add_osm_layer = QAction(
            QIcon(icon),
            self.tr('Add OpenStreetMap Tile Layer'),
            self.iface.mainWindow())
        self.action_add_osm_layer.setStatusTip(self.tr(
            'Add OpenStreetMap Tile Layer'))
        self.action_add_osm_layer.setWhatsThis(self.tr(
            'Use this to add an OSM layer to your map. '
            'It needs internet access to function.'))
        self.action_add_osm_layer.triggered.connect(self.add_osm_layer)
        self.add_action(self.action_add_osm_layer)

    def _create_add_petajakarta_layer_action(self):
        """Create action for import OSM Dialog."""
        icon = resources_path('img', 'icons', 'add-petajakarta-layer.svg')
        self.action_add_petajakarta_layer = QAction(
            QIcon(icon),
            self.tr('Add PetaJakarta Flood Layer'),
            self.iface.mainWindow())
        self.action_add_petajakarta_layer.setStatusTip(self.tr(
            'Add PetaJakarta Flood Layer'))
        self.action_add_petajakarta_layer.setWhatsThis(self.tr(
            'Use this to add a PetaJakarta layer to your map. '
            'It needs internet access to function.'))
        self.action_add_petajakarta_layer.triggered.connect(
            self.add_petajakarta_layer)
        self.add_action(
            self.action_add_petajakarta_layer,
            add_to_toolbar=False)

    def _create_impact_merge_action(self):
        """Create action for impact layer merge Dialog."""
        icon = resources_path('img', 'icons', 'show-impact-merge.svg')
        self.action_impact_merge_dlg = QAction(
            QIcon(icon),
            self.tr('Impact Layer Merger'),
            self.iface.mainWindow())
        self.action_impact_merge_dlg.setStatusTip(self.tr(
            'Impact Layer Merger'))
        self.action_impact_merge_dlg.setWhatsThis(self.tr(
            'Impact Layer Merger'))
        self.action_impact_merge_dlg.triggered.connect(self.show_impact_merge)
        self.add_action(
            self.action_impact_merge_dlg, add_to_toolbar=self.full_toolbar)

    def _create_rubber_bands_action(self):
        """Create action for toggling rubber bands."""
        icon = resources_path('img', 'icons', 'toggle-rubber-bands.svg')
        self.action_toggle_rubberbands = QAction(
            QIcon(icon),
            self.tr('Toggle Scenario Outlines'), self.iface.mainWindow())
        message = self.tr('Toggle rubber bands showing scenario 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)

    def _create_analysis_extent_action(self):
        """Create action for analysis extent dialog."""
        icon = resources_path('img', 'icons', 'set-extents-tool.svg')
        self.action_extent_selector = QAction(
            QIcon(icon),
            self.tr('Set Analysis Area'),
            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)

    def _create_test_layers_action(self):
        """Create action for adding layers (developer mode, non final only)."""
        final_release = release_status() == 'final'
        settings = QSettings()
        self.developer_mode = settings.value(
            'inasafe/developer_mode', False, type=bool)
        if not final_release and self.developer_mode:
            icon = resources_path('img', 'icons', 'add-test-layers.svg')
            self.action_add_layers = QAction(
                QIcon(icon),
                self.tr('Add Test Layers'),
                self.iface.mainWindow())
            self.action_add_layers.setStatusTip(self.tr(
                'Add test layers'))
            self.action_add_layers.setWhatsThis(self.tr(
                'Add test layers'))
            self.action_add_layers.triggered.connect(
                self.add_test_layers)

            self.add_action(self.action_add_layers)

    def _create_dock(self):
        """Create dockwidget and tabify it with the legend."""
        # Import dock here as it needs to be imported AFTER i18n is set up
        from safe.gui.widgets.dock import Dock
        self.dock_widget = Dock(self.iface)
        self.dock_widget.setObjectName('InaSAFE-Dock')
        self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock_widget)
        legend_tab = self.iface.mainWindow().findChild(QApplication, 'Legend')
        if legend_tab:
            self.iface.mainWindow().tabifyDockWidget(
                legend_tab, self.dock_widget)
            self.dock_widget.raise_()

    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')
        self.dock_widget = None
        # Now create the actual dock
        self._create_dock()
        # And all the menu actions
        # Configuration Group
        self._create_dock_toggle_action()
        self._create_options_dialog_action()
        self._create_minimum_needs_options_action()
        self._create_analysis_extent_action()
        self._create_rubber_bands_action()
        self._add_spacer_to_menu()
        self._create_keywords_wizard_action()
        self._create_analysis_wizard_action()
        self._add_spacer_to_menu()
        self._create_osm_downloader_action()
        self._create_add_osm_layer_action()
        self._create_add_petajakarta_layer_action()
        self._create_shakemap_converter_action()
        self._create_minimum_needs_action()
        self._create_test_layers_action()
        self._add_spacer_to_menu()
        self._create_batch_runner_action()
        self._create_impact_merge_action()
        self._create_save_scenario_action()

        # 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())

    def _add_spacer_to_menu(self):
        """Create a spacer to the menu to separate action groups."""
        separator = QAction(self.iface.mainWindow())
        separator.setSeparator(True)
        self.iface.addPluginToMenu(self.tr('InaSAFE'), separator)

    def clear_modules(self):
        """Unload inasafe functions and try to return QGIS to before InaSAFE.

        .. todo:: I think this function can be removed. TS.
        """
        # next lets force remove any inasafe related modules
        modules = []
        for module in sys.modules:
            if 'inasafe' in module:
                # Check if it is really one of our modules i.e. exists in the
                # plugin directory
                tokens = module.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' % module)
                    modules.append(module)
        for module in modules:
            del (sys.modules[module])
        for module in sys.modules:
            if 'inasafe' in module:
                print module

        # 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
        if self.wizard:
            self.wizard.deleteLater()
        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)

    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 add_test_layers(self):
        """Add standard test layers."""
        from safe.test.utilities import load_standard_layers
        load_standard_layers()
        rect = QgsRectangle(106.806, -6.195, 106.837, -6.167)
        self.iface.mapCanvas().setExtent(rect)

    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.gui.tools.extent_selector_dialog import ExtentSelectorDialog

        widget = ExtentSelectorDialog(
            self.iface,
            self.iface.mainWindow(),
            extent=self.dock_widget.extent.user_extent,
            crs=self.dock_widget.extent.user_extent_crs)
        widget.clear_extent.connect(
            self.dock_widget.extent.clear_user_analysis_extent)
        widget.extent_defined.connect(
            self.dock_widget.define_user_analysis_extent)
        # This ensures that run button state is updated on dialog close
        widget.extent_selector_closed.connect(
            self.dock_widget.show_next_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.gui.tools.minimum_needs.needs_calculator_dialog import (
            NeedsCalculatorDialog
        )

        dialog = NeedsCalculatorDialog(self.iface.mainWindow())
        dialog.exec_()

    def show_minimum_needs_configuration(self):
        """Show the minimum needs dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.minimum_needs.needs_manager_dialog import (
            NeedsManagerDialog)

        dialog = NeedsManagerDialog(
            parent=self.iface.mainWindow(),
            dock=self.dock_widget)
        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.gui.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.gui.tools.options_dialog import OptionsDialog

        dialog = OptionsDialog(
            self.iface,
            self.dock_widget,
            self.iface.mainWindow())
        dialog.exec_()  # modal

    def show_keywords_wizard(self):
        """Show the keywords creation wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        if self.iface.activeLayer() is None:
            return

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since the IFCW is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_keywords_creation_mode()
        self.wizard.exec_()  # modal

    def show_function_centric_wizard(self):
        """Show the function centric wizard."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.wizard.wizard_dialog import WizardDialog

        # Don't break an existing wizard session if accidentally clicked
        if self.wizard and self.wizard.isVisible():
            return

        # Prevent spawning multiple copies since it is non modal
        if not self.wizard:
            self.wizard = WizardDialog(
                self.iface.mainWindow(),
                self.iface,
                self.dock_widget)
        self.wizard.set_function_centric_mode()
        # non-modal in order to hide for selecting user extent
        self.wizard.show()

    def show_shakemap_importer(self):
        """Show the converter dialog."""
        # import here only so that it is AFTER i18n set up
        from safe.gui.tools.shake_grid.shakemap_converter_dialog import (
            ShakemapConverterDialog)

        dialog = ShakemapConverterDialog(self.iface.mainWindow())
        dialog.exec_()  # modal

    def show_osm_downloader(self):
        """Show the OSM buildings downloader dialog."""
        from safe.gui.tools.osm_downloader_dialog import OsmDownloaderDialog

        dialog = OsmDownloaderDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def show_osm_downloader(self):
        """Show the OSM buildings downloader dialog."""
        from safe.gui.tools.osm_downloader_dialog import OsmDownloaderDialog

        dialog = OsmDownloaderDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def add_osm_layer(self):
        """Add OSM tile layer to the map.

        This uses a gdal wrapper around the OSM tile service - see the
        WorldOSM.gdal file for how it is constructed.
        """
        path = resources_path('osm', 'WorldOSM.gdal')
        layer = QgsRasterLayer(path, self.tr('OpenStreetMap'))
        registry = QgsMapLayerRegistry.instance()

        # For older versions we just add directly to the top of legend
        if QGis.QGIS_VERSION_INT < 20400:
            # True flag adds layer directly to legend
            registry.addMapLayer(layer, True)
            return
        # Otherwise try to add it as the last layer in the list
        # False flag prevents layer being added to legend
        registry.addMapLayer(layer, False)
        root = QgsProject.instance().layerTreeRoot()
        index = len(root.findLayers()) + 1
        # LOGGER.info('Inserting layer %s at position %s' % (
        #    layer.source(), index))
        root.insertLayer(index, layer)
        QgsMapLayerRegistry.instance().addMapLayer(layer)

    def add_petajakarta_layer(self):
        """Add petajakarta layer to the map.

        This uses the PetaJakarta API to fetch the latest floods in JK. See
        https://petajakarta.org/banjir/en/data/api/#aggregates
        """
        from safe.gui.tools.peta_jakarta_dialog import PetaJakartaDialog
        dialog = PetaJakartaDialog(self.iface.mainWindow(), self.iface)
        dialog.show()  # non modal

    def show_batch_runner(self):
        """Show the batch runner dialog."""
        from safe.gui.tools.batch.batch_dialog import BatchDialog

        dialog = BatchDialog(
            parent=self.iface.mainWindow(),
            iface=self.iface,
            dock=self.dock_widget)
        dialog.exec_()  # modal

    def save_scenario(self):
        """Save current scenario to text file"""
        from safe.gui.tools.save_scenario import SaveScenarioDialog

        dialog = SaveScenarioDialog(
            iface=self.iface,
            dock=self.dock_widget)
        dialog.save_scenario()

    def _disable_keyword_tools(self):
        """Internal helper to disable the keyword and wizard actions."""
        self.action_keywords_wizard.setEnabled(False)

    def layer_changed(self, layer):
        """Enable or disable keywords editor icon when active layer changes.

        :param layer: The layer that is now active.
        :type layer: QgsMapLayer
        """
        if not layer:
            self._disable_keyword_tools()
            return
        if not hasattr(layer, 'providerType'):
            self._disable_keyword_tools()
            return
        if layer.providerType() == 'wms':
            self._disable_keyword_tools()
            return
        if is_raster_layer(layer) and layer.bandCount() > 1:
            self._disable_keyword_tools()
            return

        self.action_keywords_wizard.setEnabled(True)

    def shortcut_f7(self):
        """Executed when user press F7 - will show the shakemap importer."""
        self.show_shakemap_importer()
    def test_input_function_centric_wizard(self):
        """Test the IFCW mode: FloodRasterBuildingFunction"""

        expected_test_layer_count = 2

        expected_hazards_count = 5
        # expected_exposures_count = 3
        expected_exposures_count = 4
        expected_flood_structure_functions_count = 4
        expected_raster_polygon_functions_count = 2
        expected_functions_count = 2
        chosen_if = 'FloodRasterBuildingFunction'

        expected_hazard_layers_count = 1
        expected_exposure_layers_count = 1
        expected_aggregation_layers_count = 0

        # expected_summary_key = 'minimum needs'
        # expected_summary_value_fragment = 'rice'

        # expected_report_size = 4055  # as saved on Ubuntu
        # TS : changed tolerance from 120 to 160 because above change
        # causes fail on fedora
        # AG: updated the tolerance from 160 to 190
        # MD: more tolerance please! 190 -> 200
        # tolerance = 200  # windows EOL etc

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=True,
            source_directory=test_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        layer = clone_shp_layer(
            name='buildings',
            include_keywords=True,
            source_directory=test_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Check the environment first
        self.assertIsNotNone(layer.dataProvider())

        count = len(dialog.iface.mapCanvas().layers())
        self.assertEqual(count, expected_test_layer_count)

        # step_fc_functions1: test function matrix dimensions
        col_count = dialog.step_fc_functions1.tblFunctions1.columnCount()
        self.assertEqual(col_count, expected_hazards_count)
        row_count = dialog.step_fc_functions1.tblFunctions1.rowCount()
        self.assertEqual(row_count, expected_exposures_count)

        # step_fc_functions1: test number of functions for flood x structure
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(3, 1)
        count = len(dialog.step_fc_functions1.selected_functions_1())
        self.assertEqual(count, expected_flood_structure_functions_count)

        # step_fc_functions1: press ok
        dialog.pbnNext.click()

        # step_fc_functions2: test number of functions for raster flood
        # and polygon structure
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(3, 0)

        count = len(dialog.step_fc_functions2.selected_functions_2())
        self.assertEqual(count, expected_raster_polygon_functions_count)

        # step_fc_functions2: press ok
        dialog.pbnNext.click()

        # step_fc_function: test number of available functions
        self.check_current_step(dialog.step_fc_function)
        count = dialog.step_fc_function.lstFunctions.count()
        self.assertEqual(count, expected_functions_count)

        # step_fc_function: test if chosen_if is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(count)]
        self.assertTrue(chosen_if in flood_ifs)

        # step_fc_function: select FloodRasterBuildingImpactFunction and
        # press ok
        chosen_if_row = flood_ifs.index(chosen_if)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas: test the lstCanvasHazLayers state
        # Note this step is tested prior to step_fc_hazlayer_origin
        # as the list is prepared prior to autoselecting the radiobuttons
        count = dialog.step_fc_hazlayer_from_canvas.lstCanvasHazLayers.count()
        self.assertEqual(count, expected_hazard_layers_count)

        # step_fc_hazlayer_origin: test if the radiobuttons are autmatically
        # enabled and selected
        self.assertTrue(
            dialog.step_fc_hazlayer_origin.rbHazLayerFromCanvas.isEnabled())
        self.assertTrue(
            dialog.step_fc_hazlayer_origin.rbHazLayerFromCanvas.isChecked())

        # step_fc_hazlayer_origin: press ok
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas: press ok
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas: test the lstCanvasExpLayers state
        # Note this step is tested prior to step_fc_explayer_origin
        # as the list is prepared prior to autoselecting the radiobuttons
        count = dialog.step_fc_explayer_from_canvas.lstCanvasExpLayers.count()
        self.assertEqual(count, expected_exposure_layers_count)

        # step_fc_explayer_origin: test if the radiobuttons are automatically
        # enabled and selected
        self.assertTrue(
            dialog.step_fc_explayer_origin.rbExpLayerFromCanvas.isEnabled())
        self.assertTrue(
            dialog.step_fc_explayer_origin.rbExpLayerFromCanvas.isChecked())

        # step_fc_explayer_origin: press ok
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas: press ok
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas: test the lstCanvasAggLayers state
        # Note this step is tested prior to step_fc_agglayer_origin
        # as the list is prepared prior to auto selecting the radio buttons
        count = dialog.step_fc_agglayer_from_canvas.lstCanvasAggLayers.count()
        self.assertEqual(count, expected_aggregation_layers_count)

        # step_fc_agglayer_origin: test if the radio buttons are automatically
        # enabled and selected
        self.assertFalse(
            dialog.step_fc_agglayer_origin.rbAggLayerFromCanvas.isEnabled())
        self.assertTrue(
            dialog.step_fc_agglayer_origin.rbAggLayerFromBrowser.isChecked())

        # step_fc_agglayer_origin: switch to no aggregation and press ok
        self.check_current_step(dialog.step_fc_agglayer_origin)
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.click()
        dialog.pbnNext.click()

        # step_fc_extent: switch to layer's extent and press ok
        self.check_current_step(dialog.step_fc_extent)
        dialog.step_fc_extent.extent_dialog.hazard_exposure_only.click()
        dialog.pbnNext.click()

        # step_fc_params: press ok (already covered by the relevant test)
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnNext.click()

        # step_fc_summary: test minimum needs text
        # summaries = dialog.lblSummary.text().split('<br/>')

        # #TODO: temporarily disable minimum needs test as they seem
        # #te be removed from params
        # minneeds = [s for s in summaries
        #            if expected_summary_key.upper() in s.upper()]
        # self.assertTrue(minneeds)
        # self.assertTrue(expected_summary_value_fragment.upper()
        #                in minneeds[0].upper())

        # step_fc_summary: run analysis
        dialog.pbnNext.click()

        # No longer valid for impact data.
        # step_fc_analysis: test the html output
        # report_path = dialog.wvResults.report_path
        # size = os.stat(report_path).st_size
        # self.assertTrue(
        #     (expected_report_size - tolerance < size < expected_report_size +
        #      tolerance))
        # close the wizard
        dialog.pbnNext.click()
    def test_input_function_centric_wizard_test_2(self):
        """Test the IFCW mode: """

        chosen_if = 'FloodEvacuationRasterHazardFunction'

        # Initialize dialog
        # noinspection PyTypeChecker
        dialog = WizardDialog(iface=IFACE)
        dialog.dock = self.dock
        dialog.set_function_centric_mode()
        QgsMapLayerRegistry.instance().removeAllMapLayers()

        # Load test layers
        # Hazard layer
        layer = clone_raster_layer(
            name='continuous_flood_20_20',
            extension='.asc',
            include_keywords=True,
            source_directory=standard_data_path('hazard'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # Exposure layer
        layer = clone_raster_layer(
            name='people_allow_resampling_true',
            extension='.tif',
            include_keywords=True,
            source_directory=standard_data_path('exposure'))
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().addMapLayers([layer])

        # step_fc_functions1: select functions for flood x population
        self.check_current_step(dialog.step_fc_functions1)
        dialog.step_fc_functions1.tblFunctions1.setCurrentCell(1, 1)
        dialog.pbnNext.click()

        # step_fc_functions2: select functions for raster x raster
        self.check_current_step(dialog.step_fc_functions2)
        dialog.step_fc_functions2.tblFunctions2.setCurrentCell(0, 0)
        dialog.pbnNext.click()

        # step_fc_function: test if FloodEvacuationRasterHazardFunction
        # is on the list
        role = QtCore.Qt.UserRole
        flood_ifs = [
            dialog.step_fc_function.lstFunctions.item(row).data(role)['id']
            for row in range(dialog.step_fc_function.lstFunctions.count())]
        self.assertTrue(chosen_if in flood_ifs)

        # step_fc_function: select FloodEvacuationRasterHazardFunction and
        # press ok
        self.check_current_step(dialog.step_fc_function)
        chosen_if_row = flood_ifs.index(chosen_if)
        dialog.step_fc_function.lstFunctions.setCurrentRow(chosen_if_row)
        dialog.pbnNext.click()

        # step_fc_hazlayer_origin:
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnNext.click()

        # step_fc_hazlayer_from_canvas:
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_explayer_origin:
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnNext.click()

        # step_fc_explayer_from_canvas:
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnNext.click()

        # step_fc_agglayer_origin:
        self.check_current_step(dialog.step_fc_agglayer_origin)
        dialog.step_fc_agglayer_origin.rbAggLayerNoAggregation.click()
        dialog.pbnNext.click()

        # step_fc_extent:
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnNext.click()

        # step_fc_params:
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnNext.click()

        # step_fc_summary:
        self.check_current_step(dialog.step_fc_summary)
        dialog.pbnNext.click()

        # step_fc_analysis:
        self.check_current_step(dialog.step_fc_analysis)

        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_summary)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_params)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_extent)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_agglayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_explayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_from_canvas)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_hazlayer_origin)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_function)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_functions2)
        dialog.pbnBack.click()
        self.check_current_step(dialog.step_fc_functions1)
        dialog.pbnCancel.click()