Пример #1
0
    def __init__(self):
        super().__init__()
        self.data = None
        self.domainmodels = []
        self.unmatched = []

        top = self.controlArea

        def _radioChanged():
            self.mainArea.setVisible(self.is_decoding == 0
                                     and len(self.unmatched))
            self.commit()

        modes = gui.radioButtons(top,
                                 self,
                                 'is_decoding',
                                 callback=_radioChanged)

        gui.appendRadioButton(
            modes,
            '&Encode region names into geographical coordinates:',
            insertInto=top)
        box = gui.indentedBox(top)
        model = DomainModel(parent=self,
                            valid_types=(StringVariable, DiscreteVariable))
        self.domainmodels.append(model)

        combo = gui.comboBox(box,
                             self,
                             'str_attr',
                             label='Region identifier:',
                             orientation=Qt.Horizontal,
                             callback=self.region_attr_changed,
                             sendSelectedValue=True,
                             model=model)
        gui.comboBox(box,
                     self,
                     'str_type',
                     label='Identifier type:',
                     orientation=Qt.Horizontal,
                     items=tuple(self.ID_TYPE.keys()),
                     callback=lambda: self.commit(),
                     sendSelectedValue=True)

        # Select first mode if any of its combos are changed
        for combo in box.findChildren(QComboBox):
            combo.activated.connect(lambda: setattr(self, 'is_decoding', 0))

        gui.appendRadioButton(modes,
                              '&Decode latitude and longitude into regions:',
                              insertInto=top)
        box = gui.indentedBox(top)
        model = DomainModel(parent=self, valid_types=ContinuousVariable)
        self.domainmodels.append(model)
        combo = gui.comboBox(box,
                             self,
                             'lat_attr',
                             label='Latitude:',
                             orientation=Qt.Horizontal,
                             callback=lambda: self.commit(),
                             sendSelectedValue=True,
                             model=model)
        combo = gui.comboBox(box,
                             self,
                             'lon_attr',
                             label='Longitude:',
                             orientation=Qt.Horizontal,
                             callback=lambda: self.commit(),
                             sendSelectedValue=True,
                             model=model)
        gui.comboBox(
            box,
            self,
            'admin',
            label='Administrative level:',
            orientation=Qt.Horizontal,
            callback=lambda: self.commit(),
            items=
            ('Country',
             '1st-level subdivision (state, region, province, municipality, ...)',
             '2nd-level subdivisions (1st-level & US counties)'),
        )

        # Select second mode if any of its combos are changed
        for combo in box.findChildren(QComboBox):
            combo.activated.connect(lambda: setattr(self, 'is_decoding', 1))

        gui.checkBox(
            top,
            self,
            'append_features',
            label='E&xtend coded data with additional region properties',
            callback=lambda: self.commit(),
            toolTip='Extend coded data with region properties, such as'
            'ISO codes, continent, subregion, region type, '
            'economy type, FIPS/HASC codes, region capital etc. as available.')

        gui.auto_commit(self.controlArea, self, 'autocommit', '&Apply')
        gui.rubber(self.controlArea)

        model = self.replacementsModel = PyTableModel(self.replacements,
                                                      parent=self,
                                                      editable=[False, True])
        view = gui.TableView(self,
                             sortingEnabled=False,
                             selectionMode=gui.TableView.NoSelection,
                             editTriggers=gui.TableView.AllEditTriggers)
        view.horizontalHeader().setResizeMode(QHeaderView.Stretch)
        view.verticalHeader().setSectionResizeMode(0)
        view.setModel(model)

        owwidget = self

        class TableItemDelegate(QItemDelegate):
            def createEditor(self, parent, options, index):
                nonlocal owwidget
                edit = QLineEdit(parent)
                wordlist = [''] + ToLatLon.valid_values(
                    owwidget.ID_TYPE[owwidget.str_type])
                edit.setCompleter(
                    QCompleter(wordlist,
                               edit,
                               caseSensitivity=Qt.CaseInsensitive,
                               filterMode=Qt.MatchContains))

                def save_and_commit():
                    if edit.text() and edit.text() in wordlist:
                        model = index.model()
                        pindex = QPersistentModelIndex(index)
                        if pindex.isValid():
                            new_index = pindex.sibling(pindex.row(),
                                                       pindex.column())
                            save = model.setData(new_index, edit.text(),
                                                 Qt.EditRole)
                            if save:
                                owwidget.commit()
                                return
                    edit.clear()

                edit.editingFinished.connect(save_and_commit)
                return edit

        view.setItemDelegate(TableItemDelegate())
        model.setHorizontalHeaderLabels(
            ['Unmatched Identifier', 'Custom Replacement'])
        box = gui.vBox(self.mainArea)
        self.info_str = ' /'
        gui.label(box, self, 'Unmatched identifiers: %(info_str)s')
        box.layout().addWidget(view)
        self.mainArea.setVisible(self.is_decoding == 0)
Пример #2
0
    def __init__(self):
        super().__init__()
        self.data = None
        self.domainmodels = []

        top = self.controlArea

        def _radioChanged():
            self.mainArea.setVisible(self.is_decoding == 0)
            self.commit()

        modes = gui.radioButtons(top,
                                 self,
                                 'is_decoding',
                                 callback=_radioChanged)

        gui.appendRadioButton(
            modes,
            '&Encode region names into geographical coordinates:',
            insertInto=top)
        box = gui.indentedBox(top)
        model = DomainModel(parent=self,
                            valid_types=(StringVariable, DiscreteVariable))
        self.domainmodels.append(model)

        def _region_attr_changed():
            if self.data is None:
                return

            # Auto-detect the type of region in the attribute and set its combo
            values = self._get_data_values()
            func = ToLatLon.detect_input(values)
            str_type = next((k for k, v in self.ID_TYPE.items() if v == func),
                            None)
            if str_type is not None and str_type != self.str_type:
                self.str_type = str_type

            self.commit()

        combo = gui.comboBox(box,
                             self,
                             'str_attr',
                             label='Region identifier:',
                             orientation=Qt.Horizontal,
                             callback=_region_attr_changed,
                             sendSelectedValue=True)
        combo.setModel(model)
        gui.comboBox(box,
                     self,
                     'str_type',
                     label='Identifier type:',
                     orientation=Qt.Horizontal,
                     items=tuple(self.ID_TYPE.keys()),
                     callback=lambda: self.commit(),
                     sendSelectedValue=True)

        # Select first mode if any of its combos are changed
        for combo in box.findChildren(QComboBox):
            combo.currentIndexChanged.connect(
                lambda: setattr(self, 'is_decoding', 0))

        gui.appendRadioButton(modes,
                              '&Decode latitude and longitude into regions:',
                              insertInto=top)
        box = gui.indentedBox(top)
        model = DomainModel(parent=self, valid_types=ContinuousVariable)
        self.domainmodels.append(model)
        combo = gui.comboBox(box,
                             self,
                             'lat_attr',
                             label='Latitude:',
                             orientation=Qt.Horizontal,
                             callback=lambda: self.commit(),
                             sendSelectedValue=True)
        combo.setModel(model)
        combo = gui.comboBox(box,
                             self,
                             'lon_attr',
                             label='Longitude:',
                             orientation=Qt.Horizontal,
                             callback=lambda: self.commit(),
                             sendSelectedValue=True)
        combo.setModel(model)
        gui.comboBox(
            box,
            self,
            'admin',
            label='Administrative level:',
            orientation=Qt.Horizontal,
            callback=lambda: self.commit(),
            items=
            ('Country',
             '1st-level subdivision (state, region, province, municipality, ...)',
             '2nd-level subdivisions (1st-level & US counties)'),
        )

        # Select second mode if any of its combos are changed
        for combo in box.findChildren(QComboBox):
            combo.currentIndexChanged.connect(
                lambda: setattr(self, 'is_decoding', 1))

        gui.checkBox(
            top,
            self,
            'append_features',
            label='E&xtend coded data with additional region properties',
            callback=lambda: self.commit(),
            toolTip='Extend coded data with region properties, such as'
            'ISO codes, continent, subregion, region type, '
            'economy type, FIPS/HASC codes, region capital etc. as available.')

        gui.auto_commit(self.controlArea, self, 'autocommit', '&Apply')

        model = self.replacementsModel = PyTableModel(self.replacements,
                                                      parent=self,
                                                      editable=[False, True])
        view = gui.TableView(self,
                             sortingEnabled=False,
                             selectionMode=gui.TableView.NoSelection,
                             editTriggers=gui.TableView.AllEditTriggers)
        view.horizontalHeader().setSectionResizeMode(0)
        view.verticalHeader().setSectionResizeMode(0)
        view.setModel(model)

        owwidget = self

        class EditorFactory(QItemEditorFactory):
            def createEditor(self, p_int, parent):
                nonlocal owwidget
                edit = QLineEdit(parent)
                wordlist = [''] + ToLatLon.valid_values(
                    owwidget.ID_TYPE[owwidget.str_type])
                edit.setCompleter(
                    QCompleter(wordlist,
                               edit,
                               caseSensitivity=Qt.CaseInsensitive,
                               filterMode=Qt.MatchContains))
                return edit

        self.factory = EditorFactory()
        view.itemDelegate().setItemEditorFactory(self.factory)
        model.setHorizontalHeaderLabels(
            ['Unmatched Identifier', 'Custom Replacement'])
        box = gui.vBox(self.mainArea)
        self.info_str = ' /'
        gui.label(box, self, 'Unmatched identifiers: %(info_str)s')
        box.layout().addWidget(view)
        self.mainArea.setVisible(self.is_decoding == 0)