Beispiel #1
0
class Timeperd(WizardWidget):  #

    drag_label = "Time Period of Content <timeperd>"
    acceptable_tags = ['timeperd']

    def build_ui(self):
        """
        Build and modify this widget's GUI
        Returns
        -------
        None
        """
        self.ui = UI_timeperd.Ui_Form()
        self.ui.setupUi(self)
        self.setup_dragdrop(self)

        self.single_date = FGDCDate(label='    Single Date ',
                                    fgdc_name='fgdc_caldate')
        self.ui.fgdc_sngdate.layout().insertWidget(0, self.single_date)

        self.range_start_date = FGDCDate(label='Start  ',
                                         fgdc_name='fgdc_begdate')
        self.range_end_date = FGDCDate(label='End  ', fgdc_name='fgdc_enddate')
        self.ui.layout_daterange.addWidget(self.range_start_date)
        self.ui.layout_daterange.addWidget(self.range_end_date)

        date_widget_kwargs = {
            'show_format': False,
            'label': 'Individual Date   ',
            'fgdc_name': 'fgdc_caldate',
            'parent_fgdc_name': 'fgdc_sngdate'
        }

        self.multi_dates = RepeatingElement(widget=FGDCDate,
                                            widget_kwargs=date_widget_kwargs)

        self.multi_dates.add_another()
        self.switch_primary()

    def connect_events(self):
        """
        Connect the appropriate GUI components with the corresponding functions
        Returns
        -------
        None
        """
        self.ui.radio_single.toggled.connect(self.switch_primary)
        self.ui.radio_range.toggled.connect(self.switch_primary)
        self.ui.radio_multiple.toggled.connect(self.switch_primary)

    def switch_primary(self):
        """
        Switches form to reflect either organization or person primary
        Returns
        -------
        None
        """
        if self.ui.radio_single.isChecked():
            self.findChild(QStackedWidget, "fgdc_timeinfo").setCurrentIndex(0)
            self.ui.fgdc_sngdate.show()
            self.ui.fgdc_rngdates.hide()
            self.ui.fgdc_mdattim.hide()
            self.ui.fgdc_mdattim.layout().removeWidget(self.multi_dates)
        elif self.ui.radio_range.isChecked():
            self.findChild(QStackedWidget, "fgdc_timeinfo").setCurrentIndex(1)
            self.ui.fgdc_rngdates.hide()
            self.ui.fgdc_rngdates.show()
            self.ui.fgdc_mdattim.hide()
            self.ui.fgdc_mdattim.layout().removeWidget(self.multi_dates)
        elif self.ui.radio_multiple.isChecked():
            self.findChild(QStackedWidget, "fgdc_timeinfo").setCurrentIndex(2)
            self.ui.fgdc_sngdate.hide()
            self.ui.fgdc_rngdates.hide()
            self.ui.fgdc_mdattim.layout().addWidget(self.multi_dates)
            self.ui.fgdc_mdattim.show()

    def _to_xml(self):
        """
        encapsulates the QTabWidget text for Metadata Time in an element tag
        Returns
        -------
        timeperd element tag in xml tree
        """
        timeperd = xml_utils.xml_node('timeperd')
        timeinfo = xml_utils.xml_node("timeinfo", parent_node=timeperd)
        tabIndex = self.ui.fgdc_timeinfo.currentIndex()

        if tabIndex == 0:
            sngdate = xml_utils.xml_node("sngdate", parent_node=timeinfo)
            caldate = xml_utils.xml_node('caldate',
                                         parent_node=sngdate,
                                         text=self.single_date.get_date())
        if tabIndex == 1:
            rngdates = xml_utils.xml_node("rngdates", parent_node=timeinfo)
            begdate = xml_utils.xml_node("begdate",
                                         parent_node=rngdates,
                                         text=self.range_start_date.get_date())
            enddate = xml_utils.xml_node("enddate",
                                         parent_node=rngdates,
                                         text=self.range_end_date.get_date())
        if tabIndex == 2:
            mdattim = xml_utils.xml_node("mdattim", parent_node=timeinfo)

            for single_date in self.multi_dates.get_widgets():

                single_date_node = xml_utils.xml_node("sngdate",
                                                      parent_node=mdattim)

                caldate = xml_utils.xml_node('caldate',
                                             parent_node=single_date_node,
                                             text=single_date.get_date())

        current = xml_utils.xml_node('current',
                                     parent_node=timeperd,
                                     text=self.ui.fgdc_current.currentText())

        return timeperd

    def _from_xml(self, timeperd):
        """
        parses the xml code into the relevant timeperd elements
        Parameters
        ----------
        metadata_date - the xml element timeperd and its contents
        Returns
        -------
        None
        """
        try:
            if timeperd.tag == 'timeperd':

                if timeperd.findall("current"):
                    current_text = timeperd.findtext("current")
                    current_box = self.findChild(QComboBox, 'fgdc_current')
                    current_box.setCurrentText(current_text)
                else:
                    pass

                timeinfo_stack = self.ui.fgdc_timeinfo
                if timeperd.xpath("timeinfo/rngdates"):
                    self.ui.radio_range.setChecked(True)
                    timeinfo_stack.setCurrentIndex(1)

                    begdate = timeperd.findtext("timeinfo/rngdates/begdate")
                    self.range_start_date.set_date(begdate)

                    enddate = timeperd.findtext("timeinfo/rngdates/enddate")
                    self.range_end_date.set_date(enddate)

                elif timeperd.xpath("timeinfo/mdattim"):
                    self.ui.radio_multiple.setChecked(True)
                    timeinfo_stack.setCurrentIndex(2)

                    self.multi_dates.clear_widgets(add_another=False)
                    for caldate in timeperd.xpath(
                            'timeinfo/mdattim/sngdate/caldate'):
                        date_widget = self.multi_dates.add_another()
                        date_widget.set_date(caldate.text)

                elif timeperd.xpath("timeinfo/sngdate"):
                    self.ui.radio_single.setChecked(True)
                    timeinfo_stack.setCurrentIndex(0)

                    sngdate = timeperd.findtext("timeinfo/sngdate/caldate")
                    self.single_date.set_date(sngdate)
                else:
                    pass

            else:
                print("The tag is not timeperd")
        except KeyError:
            pass
Beispiel #2
0
class MetaInfo(WizardWidget):

    drag_label = "Metadata Information <metainfo>"
    acceptable_tags = ['metainfo', 'cntinfo', 'ptcontact']

    ui_class = UI_metainfo.Ui_fgdc_metainfo

    def __init__(self, root_widget=None):
        super(self.__class__, self).__init__()
        self.root_widget = root_widget

    def build_ui(self):

        self.ui = self.ui_class()
        self.ui.setupUi(self)

        self.setup_dragdrop(self)

        self.contactinfo = ContactInfo(parent=self)
        self.metd = FGDCDate(parent=self, fgdc_name='fgdc_metd')

        self.ui.help_metd.layout().addWidget(self.metd)

        self.ui.fgdc_metc.layout().addWidget(self.contactinfo)

    def connect_events(self):
        self.ui.fgdc_metstdn.currentTextChanged.connect(self.update_metstdv)
        self.ui.fgdc_metstdv.currentIndexChanged.connect(self.update_metstdn)
        self.ui.button_use_dataset.clicked.connect(self.pull_datasetcontact)

    def update_metstdn(self):
        if self.ui.fgdc_metstdv.currentText() == 'FGDC-STD-001-1998':
            self.ui.fgdc_metstdn.setCurrentIndex(0)
            self.root_widget.switch_schema('fgdc')
        elif self.ui.fgdc_metstdv.currentText() == 'FGDC-STD-001.1-1999':
            self.ui.fgdc_metstdn.setCurrentIndex(1)
            self.root_widget.switch_schema('bdp')

    def update_metstdv(self):
        if 'biological' in self.ui.fgdc_metstdn.currentText().lower() or \
           'bdp' in self.ui.fgdc_metstdn.currentText().lower():
            self.ui.fgdc_metstdv.setCurrentIndex(1)
            self.root_widget.switch_schema('bdp')
        else:
            self.ui.fgdc_metstdv.setCurrentIndex(0)
            self.root_widget.switch_schema('fgdc')

    def pull_datasetcontact(self):
        self.contactinfo.from_xml(self.root_widget.idinfo.ptcontac.to_xml())

    def to_xml(self):
        # add code here to translate the form into xml representation
        metainfo_node = xml_utils.xml_node('metainfo')
        metd = xml_utils.xml_node('metd', text=self.metd.get_date(),
                                  parent_node=metainfo_node)

        if self.original_xml is not None:
            metrd = xml_utils.search_xpath(self.original_xml, 'metrd')
            if metrd is not None:
                metrd.tail = None
                metainfo_node.append(deepcopy(metrd))
        if self.original_xml is not None:
            metfrd = xml_utils.search_xpath(self.original_xml, 'metfrd')
            if metfrd is not None:
                metfrd.tail = None
                metainfo_node.append(deepcopy(metfrd))

        metc = xml_utils.xml_node('metc', parent_node=metainfo_node)
        cntinfo = self.contactinfo.to_xml()
        metc.append(cntinfo)

        metstdn = xml_utils.xml_node('metstdn',
                                     text=self.ui.fgdc_metstdn.currentText(),
                                     parent_node=metainfo_node)
        metstdv = xml_utils.xml_node('metstdv',
                                     text=self.ui.fgdc_metstdv.currentText(),
                                     parent_node=metainfo_node)

        if self.original_xml is not None:
            mettc = xml_utils.search_xpath(self.original_xml, 'mettc')
            if mettc is not None:
                mettc.tail = None
                metainfo_node.append(deepcopy(mettc))
        if self.original_xml is not None:
            metac = xml_utils.search_xpath(self.original_xml, 'metac')
            if metac is not None:
                metac.tail = None
                metainfo_node.append(deepcopy(metac))


        if self.original_xml is not None:
            metuc = xml_utils.search_xpath(self.original_xml, 'metuc')
            if metuc is not None:
                metuc_str = xml_utils.get_text_content(self.original_xml, 'metuc')
                metuc = xml_utils.xml_node('metuc',
                                           text=metuc_str,
                                           parent_node=metainfo_node)

        if self.original_xml is not None:
            metsi = xml_utils.search_xpath(self.original_xml, 'metsi')
            if metsi is not None:
                metsi.tail = None
                metainfo_node.append(deepcopy(metsi))

            metextns = xml_utils.search_xpath(self.original_xml, 'metextns')
            if metextns is not None:
                metextns.tail = None
                metainfo_node.append(deepcopy(metextns))

        return metainfo_node

    def from_xml(self, xml_metainfo):

        if xml_metainfo.tag == 'metainfo':
            self.original_xml = xml_metainfo

            if xml_metainfo.xpath('metc/cntinfo'):
                self.contactinfo.from_xml(xml_metainfo.xpath('metc/cntinfo')[0])

            if xml_metainfo.xpath('metstdn'):
                standard = xml_utils.get_text_content(xml_metainfo, 'metstdn')
                self.ui.fgdc_metstdn.setCurrentText(standard)
                # switch wizard content to reflect the standard in this record
                if "biological" in standard.lower() \
                        or 'bdp' in standard.lower():
                    self.root_widget.switch_schema('bdp')
                else:
                    self.root_widget.switch_schema('fgdc')

            metstdv = xml_utils.get_text_content(xml_metainfo, 'metstdv')
            self.ui.fgdc_metstdv.setCurrentText(metstdv)

            metd = xml_utils.get_text_content(xml_metainfo, 'metd')
            self.metd.set_date(metd)
        elif xml_metainfo.tag in ['ptcontac', 'cntinfo']:
            if xml_metainfo.tag == 'ptcontac':
                xml_metainfo = xml_utils.search_xpath(xml_metainfo, 'cntinfo')
            self.contactinfo.from_xml(xml_metainfo)