Exemplo n.º 1
0
    def UpdateDialog(self,
                     map=None,
                     query=None,
                     cats=None,
                     fid=-1,
                     action=None):
        """Update dialog

        :param map: name of vector map
        :param query:
        :param cats:
        :param fid: feature id
        :param action: add, update, display or None

        :return: True if updated
        :return: False
        """
        if action:
            self.action = action
            if action == "display":
                enabled = False
            else:
                enabled = True
            self.closeDialog.Enable(enabled)
            self.FindWindowById(wx.ID_OK).Enable(enabled)

        if map:
            self.map = map
            # get layer/table/column information
            self.mapDBInfo = VectorDBInfo(self.map)

        if not self.mapDBInfo:
            return False

        self.mapDBInfo.Reset()

        layers = self.mapDBInfo.layers.keys()  # get available layers

        # id of selected line
        if query:  # select by position
            data = self.mapDBInfo.SelectByPoint(query[0], query[1])
            self.cats = {}
            if data and "Layer" in data:
                idx = 0
                for layer in data["Layer"]:
                    layer = int(layer)
                    if data["Id"][idx] is not None:
                        tfid = int(data["Id"][idx])
                    else:
                        tfid = 0  # Area / Volume
                    if tfid not in self.cats:
                        self.cats[tfid] = {}
                    if layer not in self.cats[tfid]:
                        self.cats[tfid][layer] = []
                    cat = int(data["Category"][idx])
                    self.cats[tfid][layer].append(cat)
                    idx += 1
        else:
            self.cats = cats

        if fid > 0:
            self.fid = fid
        elif len(self.cats.keys()) > 0:
            self.fid = list(self.cats.keys())[0]
        else:
            self.fid = -1

        if len(self.cats.keys()) == 1:
            self.fidMulti.Show(False)
            self.fidText.Show(True)
            if self.fid > 0:
                self.fidText.SetLabel("%d" % self.fid)
            else:
                self.fidText.SetLabel(_("Unknown"))
        else:
            self.fidMulti.Show(True)
            self.fidText.Show(False)
            choices = []
            for tfid in self.cats.keys():
                choices.append(str(tfid))
            self.fidMulti.SetItems(choices)
            self.fidMulti.SetStringSelection(str(self.fid))

        # reset notebook
        self.notebook.DeleteAllPages()

        for layer in layers:  # for each layer
            if not query:  # select by layer/cat
                if self.fid > 0 and layer in self.cats[self.fid]:
                    for cat in self.cats[self.fid][layer]:
                        nselected = self.mapDBInfo.SelectFromTable(
                            layer,
                            where="%s=%d" %
                            (self.mapDBInfo.layers[layer]["key"], cat),
                        )
                else:
                    nselected = 0

            # if nselected <= 0 and self.action != "add":
            #    continue # nothing selected ...

            if self.action == "add":
                if nselected <= 0:
                    if layer in self.cats[self.fid]:
                        table = self.mapDBInfo.layers[layer]["table"]
                        key = self.mapDBInfo.layers[layer]["key"]
                        columns = self.mapDBInfo.tables[table]
                        for name in columns.keys():
                            if name == key:
                                for cat in self.cats[self.fid][layer]:
                                    self.mapDBInfo.tables[table][name][
                                        "values"].append(cat)
                            else:
                                self.mapDBInfo.tables[table][name][
                                    "values"].append(None)
                else:  # change status 'add' -> 'update'
                    self.action = "update"

            table = self.mapDBInfo.layers[layer]["table"]
            key = self.mapDBInfo.layers[layer]["key"]
            columns = self.mapDBInfo.tables[table]

            for idx in range(len(columns[key]["values"])):
                for name in columns.keys():
                    if name == key:
                        cat = int(columns[name]["values"][idx])
                        break

                # use scrolled panel instead (and fix initial max height of the
                # window to 480px)
                panel = scrolled.ScrolledPanel(parent=self.notebook,
                                               id=wx.ID_ANY,
                                               size=(-1, 150))
                panel.SetupScrolling(scroll_x=False)

                self.notebook.AddPage(
                    page=panel,
                    text=" %s %d / %s %d" %
                    (_("Layer"), layer, _("Category"), cat),
                )

                # notebook body
                border = wx.BoxSizer(wx.VERTICAL)

                flexSizer = wx.FlexGridSizer(cols=3, hgap=3, vgap=3)
                flexSizer.AddGrowableCol(2)
                # columns (sorted by index)
                names = [""] * len(columns.keys())
                for name in columns.keys():
                    names[columns[name]["index"]] = name

                for name in names:
                    if name == key:  # skip key column (category)
                        continue

                    vtype = columns[name]["type"].lower()
                    ctype = columns[name]["ctype"]

                    if columns[name]["values"][idx] is not None:
                        if not isinstance(columns[name]["ctype"],
                                          six.string_types):
                            value = str(columns[name]["values"][idx])
                        else:
                            value = columns[name]["values"][idx]
                    else:
                        value = ""

                    colName = StaticText(parent=panel,
                                         id=wx.ID_ANY,
                                         label=name)
                    colType = StaticText(parent=panel,
                                         id=wx.ID_ANY,
                                         label="[%s]:" % vtype)
                    colValue = TextCtrl(parent=panel,
                                        id=wx.ID_ANY,
                                        value=value)
                    colValue.SetName(name)
                    if ctype == int:
                        colValue.SetValidator(IntegerValidator())
                    elif ctype == float:
                        colValue.SetValidator(FloatValidator())

                    self.Bind(wx.EVT_TEXT, self.OnSQLStatement, colValue)
                    if self.action == "display":
                        colValue.SetWindowStyle(wx.TE_READONLY)

                    flexSizer.Add(colName,
                                  proportion=0,
                                  flag=wx.ALIGN_CENTER_VERTICAL)
                    flexSizer.Add(
                        colType,
                        proportion=0,
                        flag=wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT,
                    )
                    flexSizer.Add(
                        colValue,
                        proportion=1,
                        flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL,
                    )
                    # add widget reference to self.columns
                    columns[name]["ids"].append(
                        colValue.GetId())  # name, type, values, id
                # for each attribute (including category) END
                border.Add(flexSizer,
                           proportion=1,
                           flag=wx.ALL | wx.EXPAND,
                           border=5)
                panel.SetSizer(border)
            # for each category END
        # for each layer END

        if self.notebook.GetPageCount() == 0:
            self.noFoundMsg.Show(True)
        else:
            self.noFoundMsg.Show(False)

        self.Layout()

        return True