コード例 #1
0
 def update_dock_title(self, context: LinzRedistrictingContext):
     """
     Refreshes the dock title text, using the settings from a supplied
     context.
     :param context: redistricting context
     """
     self.setWindowTitle(
         self.tr('Redistricting - {} - {}').format(
             context.get_name_for_current_task(),
             context.get_name_for_current_scenario()))
 def testNameForTask(self):
     """
     Test retrieving friendly name for task
     """
     self.assertEqual(
         LinzRedistrictingContext.get_name_for_task(
             LinzRedistrictingContext.TASK_GN), 'General (North Island)')
     self.assertEqual(
         LinzRedistrictingContext.get_name_for_task(
             LinzRedistrictingContext.TASK_GS), 'General (South Island)')
     self.assertEqual(
         LinzRedistrictingContext.get_name_for_task(
             LinzRedistrictingContext.TASK_M), 'Māori')
コード例 #3
0
    def __init__(self,
                 registry: LinzElectoralDistrictRegistry,
                 context: LinzRedistrictingContext,
                 parent=None):
        super().__init__(parent)
        self.existing_names = list(registry.district_titles().keys())
        self.existing_codes = [
            f['code'] for f in registry.source_layer.getFeatures()
        ]

        self.setWindowTitle(
            self.tr('Create New {} Electorate').format(
                context.get_name_for_current_task()))

        dialog_layout = QVBoxLayout()
        label = QLabel(
            self.tr('Enter name for new {} electorate:').format(
                context.get_name_for_current_task()))
        dialog_layout.addWidget(label)

        self.name_line_edit = QLineEdit()
        self.name_line_edit.setPlaceholderText('Electorate name')
        dialog_layout.addWidget(self.name_line_edit)

        label = QLabel(self.tr('Enter code for new electorate:'))
        dialog_layout.addWidget(label)

        self.code_line_edit = QLineEdit()
        self.code_line_edit.setPlaceholderText('Electorate code')
        dialog_layout.addWidget(self.code_line_edit)

        self.feedback_label = QLabel()
        dialog_layout.addWidget(self.feedback_label)

        self.button_box = QDialogButtonBox(QDialogButtonBox.Ok
                                           | QDialogButtonBox.Cancel)
        dialog_layout.addWidget(self.button_box)
        self.button_box.rejected.connect(self.reject)
        self.button_box.accepted.connect(self.accept)

        self.name_line_edit.textChanged.connect(self.name_changed)
        self.code_line_edit.textChanged.connect(self.code_changed)

        self.setLayout(dialog_layout)
        self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
コード例 #4
0
 def testConstruct(self):
     """
     Test constructing dock
     """
     scenario_layer = make_scenario_layer()
     scenario_registry = ScenarioRegistry(source_layer=scenario_layer,
                                          id_field='id',
                                          name_field='name',
                                          meshblock_electorate_layer=None)
     context = LinzRedistrictingContext(scenario_registry=scenario_registry)
     widget = LinzRedistrictingDockWidget(context=context, iface=IFACE)
     self.assertIsNotNone(widget)
 def testNameForCurrentTask(self):
     """
     Test retrieving friendly name for currenttask
     """
     scenario_layer = make_scenario_layer()
     scenario_registry = ScenarioRegistry(source_layer=scenario_layer,
                                          id_field='id',
                                          name_field='name',
                                          meshblock_electorate_layer=None)
     context = LinzRedistrictingContext(scenario_registry=scenario_registry)
     context.task = LinzRedistrictingContext.TASK_GN
     self.assertEqual(context.get_name_for_current_task(),
                      'General (North Island)')
     context.task = LinzRedistrictingContext.TASK_GS
     self.assertEqual(context.get_name_for_current_task(),
                      'General (South Island)')
     context.task = LinzRedistrictingContext.TASK_M
     self.assertEqual(context.get_name_for_current_task(), 'Māori')
 def testBasic(self):
     """
     Test getters/settings
     """
     scenario_layer = make_scenario_layer()
     scenario_registry = ScenarioRegistry(source_layer=scenario_layer,
                                          id_field='id',
                                          name_field='name',
                                          meshblock_electorate_layer=None)
     context = LinzRedistrictingContext(scenario_registry=scenario_registry)
     self.assertIn(context.task, (LinzRedistrictingContext.TASK_GN,
                                  LinzRedistrictingContext.TASK_GS,
                                  LinzRedistrictingContext.TASK_M))
     context.task = LinzRedistrictingContext.TASK_GS
     self.assertEqual(context.task, LinzRedistrictingContext.TASK_GS)
     self.assertIsNotNone(context.scenario)
     context.scenario = 10
     self.assertEqual(context.scenario, 10)
     context.set_scenario(3)
     self.assertEqual(context.scenario, 3)
コード例 #7
0
 def testTitle(self):
     """
     Test title updates
     """
     scenario_layer = make_scenario_layer()
     scenario_registry = ScenarioRegistry(source_layer=scenario_layer,
                                          id_field='id',
                                          name_field='name',
                                          meshblock_electorate_layer=None)
     context = LinzRedistrictingContext(scenario_registry=scenario_registry)
     context.task = LinzRedistrictingContext.TASK_GS
     context.scenario = 1
     widget = LinzRedistrictingDockWidget(context=context, iface=IFACE)
     self.assertEqual(
         widget.windowTitle(),
         'Redistricting - General (South Island) - Scenario 1')
     context.task = LinzRedistrictingContext.TASK_GN
     context.scenario = 3
     widget.update_dock_title(context=context)
     self.assertEqual(
         widget.windowTitle(),
         'Redistricting - General (North Island) - scenario 3')
コード例 #8
0
    def testDialog(self):
        """
        Test dialog functionality
        """
        scenario_layer = make_scenario_layer()
        scenario_registry = ScenarioRegistry(source_layer=scenario_layer,
                                             id_field='id',
                                             name_field='name',
                                             meshblock_electorate_layer=None)

        layer = QgsVectorLayer(
            "Point?crs=EPSG:4326&field=fld1:string&field=code:string&field=type:string&field=estimated_pop:int&field=deprecated:int&field=stats_nz_pop:int&field=stats_nz_var_20:int&field=stats_nz_var_23:int&field=scenario_id:int",
            "source", "memory")
        f = QgsFeature()
        f.setAttributes(["test4", "xtest1", 'GN'])
        f2 = QgsFeature()
        f2.setAttributes(["test2", "xtest3", 'GS'])
        f3 = QgsFeature()
        f3.setAttributes(["test3", "xtest3", 'M'])
        layer.dataProvider().addFeatures([f, f2, f3])
        quota_layer = make_quota_layer()

        reg = LinzElectoralDistrictRegistry(source_layer=layer,
                                            quota_layer=quota_layer,
                                            electorate_type='',
                                            source_field='fld1',
                                            title_field='fld1')

        context = LinzRedistrictingContext(scenario_registry=scenario_registry)
        dlg = CreateElectorateDialog(registry=reg, context=context)
        self.assertIsNotNone(dlg)

        self.assertFalse(
            dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())

        dlg.name_line_edit.setText('new district')
        dlg.code_line_edit.setText('new code')
        self.assertEqual(dlg.name(), 'new district')
        self.assertEqual(dlg.code(), 'new code')

        # dupe name
        dlg.name_line_edit.setText('test4')
        self.assertFalse(
            dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        self.assertIn('already exists', dlg.feedback_label.text())
        dlg.name_line_edit.setText('test99')
        self.assertTrue(dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        self.assertFalse(dlg.feedback_label.text())
        dlg.name_line_edit.setText('')
        self.assertFalse(
            dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        self.assertIn('must be entered', dlg.feedback_label.text())
        dlg.name_line_edit.setText('test99')
        self.assertTrue(dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        self.assertFalse(dlg.feedback_label.text())

        # dupe code
        dlg.code_line_edit.setText('xtest1')
        self.assertFalse(
            dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        self.assertIn('already exists', dlg.feedback_label.text())
        dlg.code_line_edit.setText('test99')
        self.assertTrue(dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        self.assertFalse(dlg.feedback_label.text())
        dlg.code_line_edit.setText('')
        self.assertIn('must be entered', dlg.feedback_label.text())
        self.assertFalse(
            dlg.button_box.button(QDialogButtonBox.Ok).isEnabled())
        dlg.code_line_edit.setText('test99')
        self.assertFalse(dlg.feedback_label.text())