Esempio n. 1
0
    def showPluginsSearchOptions(self):
        '''
        Loads the search options of all the selected plugins and populates the relevant UI elements
        with input fields for the string options and checkboxes for the boolean options
        '''
        pl = []
        for pluginName in list(
                set([target['pluginName'] for target in self.ProjectWizardSelectedTargetsTable.targets])):
            plugin = self.PluginManager.getPluginByName(pluginName, 'Input')
            self.enabledPlugins.append(plugin)
            pl.append(plugin)
            '''
            Build the configuration page from the available saerch options
            and add the page to the stackwidget
            '''
            page = QWidget()
            page.setObjectName(_fromUtf8('searchconfig_page_' + plugin.name))
            scroll = QScrollArea()
            scroll.setWidgetResizable(True)
            layout = QVBoxLayout()
            titleLabel = QLabel(_fromUtf8(plugin.name + self.trUtf8(' Search Options')))
            layout.addWidget(titleLabel)
            vboxWidget = QWidget()
            vboxWidget.setObjectName(_fromUtf8('searchconfig_vboxwidget_container_' + plugin.name))
            vbox = QGridLayout()
            vbox.setObjectName(_fromUtf8('searchconfig_vbox_container_' + plugin.name))
            gridLayoutRowIndex = 0
            '''
            Load the String options first
            '''
            pluginStringOptions = plugin.plugin_object.readConfiguration('search_string_options')[1]
            if pluginStringOptions:
                for idx, item in enumerate(pluginStringOptions.keys()):
                    itemLabel = plugin.plugin_object.getLabelForKey(item)
                    label = QLabel()
                    label.setObjectName(_fromUtf8('searchconfig_string_label_' + item))
                    label.setText(itemLabel)
                    vbox.addWidget(label, idx, 0)
                    value = QLineEdit()
                    value.setObjectName(_fromUtf8('searchconfig_string_value_' + item))
                    value.setText(pluginStringOptions[item])
                    vbox.addWidget(value, idx, 1)
                    gridLayoutRowIndex = idx + 1
            '''
            Load the boolean options 
            '''
            pluginBooleanOptions = plugin.plugin_object.readConfiguration('search_boolean_options')[1]
            if pluginBooleanOptions:
                for idx, item in enumerate(pluginBooleanOptions.keys()):
                    itemLabel = plugin.plugin_object.getLabelForKey(item)
                    cb = QCheckBox(itemLabel)
                    cb.setObjectName(_fromUtf8('searchconfig_boolean_label_' + item))
                    if pluginBooleanOptions[item] == 'True':
                        cb.toggle()
                    vbox.addWidget(cb, gridLayoutRowIndex + idx, 0)
            # If there are no search options just show a message
            if not pluginBooleanOptions and not pluginStringOptions:
                label = QLabel()
                label.setObjectName(_fromUtf8('no_search_config_options'))
                label.setText(self.trUtf8('This plugin does not offer any search options.'))
                vbox.addWidget(label, 0, 0)

            vboxWidget.setLayout(vbox)
            scroll.setWidget(vboxWidget)
            layout.addWidget(scroll)
            layout.addStretch(1)
            page.setLayout(layout)
            self.ui.searchConfiguration.addWidget(page)

        self.ui.searchConfiguration.setCurrentIndex(0)

        self.SearchConfigPluginConfigurationListModel = PluginConfigurationListModel(pl, self)
        self.SearchConfigPluginConfigurationListModel.checkPluginConfiguration()
        self.ui.personProjectWizardSearchConfigPluginsList.setModel(self.SearchConfigPluginConfigurationListModel)
        self.ui.personProjectWizardSearchConfigPluginsList.clicked.connect(self.changePluginConfigurationPage)
Esempio n. 2
0
 def showPluginsSearchOptions(self):
     '''
     Loads the search options of all the selected plugins and populates the relevant UI elements
     with input fields for the string options and checkboxes for the boolean options
     '''
     pl = []
     for pluginName in list(set([target['pluginName'] for target in self.ProjectWizardSelectedTargetsTable.targets])):
         plugin = self.PluginManager.getPluginByName(pluginName, 'Input')
         self.enabledPlugins.append(plugin)
         pl.append(plugin)
         '''
         Build the configuration page from the available saerch options
         and add the page to the stackwidget
         '''
         page = QWidget()
         page.setObjectName(_fromUtf8('searchconfig_page_' + plugin.name))
         scroll = QScrollArea()
         scroll.setWidgetResizable(True)
         layout = QVBoxLayout()
         titleLabel = QLabel(_fromUtf8(plugin.name + self.trUtf8(' Search Options')))
         layout.addWidget(titleLabel)    
         vboxWidget = QWidget()
         vboxWidget.setObjectName(_fromUtf8('searchconfig_vboxwidget_container_' + plugin.name))
         vbox = QGridLayout()
         vbox.setObjectName(_fromUtf8('searchconfig_vbox_container_' + plugin.name))
         gridLayoutRowIndex = 0
         '''
         Load the String options first
         '''
         pluginStringOptions = plugin.plugin_object.readConfiguration('search_string_options')[1]
         if pluginStringOptions:
             for idx, item in enumerate(pluginStringOptions.keys()):
                 itemLabel = plugin.plugin_object.getLabelForKey(item)
                 label = QLabel()
                 label.setObjectName(_fromUtf8('searchconfig_string_label_' + item))
                 label.setText(itemLabel)
                 vbox.addWidget(label, idx, 0)
                 value = QLineEdit()
                 value.setObjectName(_fromUtf8('searchconfig_string_value_' + item))
                 value.setText(pluginStringOptions[item])
                 vbox.addWidget(value, idx, 1)
                 gridLayoutRowIndex = idx + 1
         '''
         Load the boolean options 
         '''
         pluginBooleanOptions = plugin.plugin_object.readConfiguration('search_boolean_options')[1]
         if pluginBooleanOptions:
             for idx, item in enumerate(pluginBooleanOptions.keys()):
                 itemLabel = plugin.plugin_object.getLabelForKey(item)
                 cb = QCheckBox(itemLabel)
                 cb.setObjectName(_fromUtf8('searchconfig_boolean_label_' + item))
                 if pluginBooleanOptions[item] == 'True':
                     cb.toggle()
                 vbox.addWidget(cb, gridLayoutRowIndex + idx, 0)
         #If there are no search options just show a message 
         if not pluginBooleanOptions and not pluginStringOptions:
             label = QLabel()
             label.setObjectName(_fromUtf8('no_search_config_options'))
             label.setText(self.trUtf8('This plugin does not offer any search options.'))
             vbox.addWidget(label,0,0)
         
         vboxWidget.setLayout(vbox)
         scroll.setWidget(vboxWidget)
         layout.addWidget(scroll)
         layout.addStretch(1)
         page.setLayout(layout)
         self.ui.searchConfiguration.addWidget(page)
         
         
     self.ui.searchConfiguration.setCurrentIndex(0)   
         
     self.SearchConfigPluginConfigurationListModel = PluginConfigurationListModel(pl, self)
     self.SearchConfigPluginConfigurationListModel.checkPluginConfiguration()
     self.ui.personProjectWizardSearchConfigPluginsList.setModel(self.SearchConfigPluginConfigurationListModel)
     self.ui.personProjectWizardSearchConfigPluginsList.clicked.connect(self.changePluginConfigurationPage)
class LDSConfigPage(QWizardPage):
    def __init__(self, parent=None,key=None):
        super(LDSConfigPage, self).__init__(parent)
        
        self.parent = parent 
        self.key = key#'lds'
        
        try:
            (ldsurl,ldskey,ldssvc,ldsver,ldsfmt,ldscql) = self.parent.mfr.readLDSConfig()
        except:
            (ldsurl,ldskey,ldssvc,ldsver,ldsfmt,ldscql) = (None,)*6
        
        self.setTitle(self.parent.plist.get(self.key)[1]+' Configuration Options')
        self.setSubTitle('Here you can enter a name for your custom configuration file, your LDS API key and required output. Also select whether you want to configure a proxy or enable password encryption')

        QToolTip.setFont(QFont('SansSerif', 10))
        
        #labels
        fileLabel = QLabel('User Config File')
        keyLabel = QLabel('LDS API Key')
        destLabel = QLabel('Output Type')
        internalLabel = QLabel('Save Layer-Config in DB')
        self.warnLabel = QLabel('!!!')
        encryptionLabel = QLabel('Enable Password Protection')
        serviceLabel = QLabel('Service Type')
        versionLabel = QLabel('Service Version')
        
        
        infoLinkLabel = QLabel('<a href="http://www.linz.govt.nz/about-linz/linz-data-service/features/how-to-use-web-services">LDS API Information Page</a>')
        infoLinkLabel.setOpenExternalLinks(True);
        keyLinkLabel = QLabel('<a href="http://data.linz.govt.nz/my/api/">LDS API Key</a>')
        keyLinkLabel.setOpenExternalLinks(True);

        #edit boxes
        self.fileEdit = QLineEdit(self.parent.uchint)
        self.fileEdit.setToolTip('Name of user config file (without .conf suffix)')
        self.keyEdit = QLineEdit(ldskey)
        self.keyEdit.setToolTip('This is your LDS API key. If you have an account you can copy your key from here <a href="http://data.linz.govt.nz/my/api/">http://data.linz.govt.nz/my/api/</a>')
               
        #dropdown
        self.destSelect = QComboBox()
        self.destSelect.setToolTip('Choose from one of four possible output destinations')
        self.destSelect.addItem('')
        for itemkey in ('pg','ms','fg','sl'):
            itemindex = self.parent.plist.get(itemkey)[0]
            itemdata = self.parent.plist.get(itemkey)[1]
            self.destSelect.addItem(itemdata, itemindex)
            if itemdata == self.parent.sechint:
                self.destSelect.setCurrentIndex(itemindex)
                
        self.serviceSelect = QComboBox()
        self.serviceSelect.setToolTip('Choose from WFS (or one day, WMS)')
        for itemkey in ('','WFS','WMS','WMTS'):
            self.serviceSelect.addItem(itemkey)
            self.serviceSelect.setCurrentIndex(0)        
            
        self.versionSelect = QComboBox()
        self.versionSelect.setToolTip('Choose service Version')
        for itemkey in ('','1.0.0','1.1.0','2.0.0'):
            self.versionSelect.addItem(itemkey)
            self.versionSelect.setCurrentIndex(0)
        
        
        self.keyEdit.setValidator(QRegExpValidator(QRegExp("[a-fA-F0-9]{32}", re.IGNORECASE), self))
        
        #checkbox
        self.internalEnable = QCheckBox()
        self.internalEnable.setToolTip('Enable saving layer-config (per layer config and progress settings) internally')
        self.internalEnable.toggle()
        self.internalEnable.setChecked(True)
        self.internalEnable.stateChanged.connect(self.setWarn)
        
        self.encryptionEnable = QCheckBox()
        self.encryptionEnable.setToolTip('Encrypt any passwords saved to user config file')

        
        self.registerField(self.key+"file",self.fileEdit)
        self.registerField(self.key+"apikey",self.keyEdit)
        self.registerField(self.key+"dest",self.destSelect,"currentIndex")
        self.registerField(self.key+"internal",self.internalEnable)
        self.registerField(self.key+"encryption",self.encryptionEnable)

        #grid
        grid = QGridLayout()
        grid.setSpacing(10)
        
        grid.addWidget(fileLabel, 1, 0)
        grid.addWidget(self.fileEdit, 1, 2)
        #grid.addWidget(cfileButton, 1, 3)        
        
        grid.addWidget(keyLabel, 2, 0)
        grid.addWidget(self.keyEdit, 2, 2)
        
        grid.addWidget(destLabel, 3, 0)
        grid.addWidget(self.destSelect, 3, 2)
        
        grid.addWidget(internalLabel, 4, 0)
        grid.addWidget(self.internalEnable, 4, 2)
        #if self.internalEnable.checkState(): grid.addWidget(intwarnLabel, 4, 4)
        
        grid.addWidget(encryptionLabel, 5, 0)
        grid.addWidget(self.encryptionEnable, 5, 2)
        
        svgrid = QGridLayout()
        svgrid.addWidget(serviceLabel, 0, 0) 
        svgrid.addWidget(self.serviceSelect, 0, 2) 
        svgrid.addWidget(versionLabel, 1, 0) 
        svgrid.addWidget(self.versionSelect, 1, 2)
        
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addLayout(svgrid)

        #layout       
        vbox = QVBoxLayout()
        vbox.addLayout(grid)
        #vbox.addLayout(hbox)
        vbox.addStretch(1)
        vbox.addWidget(self.warnLabel)
        vbox.addWidget(keyLinkLabel)
        vbox.addWidget(infoLinkLabel)
        
        self.setLayout(vbox)
        
    #def selectConfFile(self):
    #    self.fileEdit.setText(QFileDialog.getOpenFileName())

    def nextId(self):
        #if self.field(self.key+"proxy").toBool():
        return self.parent.plist.get('proxy')[0]
        #return int(self.field(self.key+"dest").toString())
        
    def setWarn(self):
        if self.internalEnable.checkState(): 
            ldslog.warn('Warning, Internal config selected')
            self.warnLabel.setText('!!!')
            QApplication.processEvents()
        else:
            ldslog.warn('External config selected')
            self.warnLabel.setText('^_^')
            QApplication.processEvents()
class geobricks_qgis_plugin_faostat:

    def __init__(self, iface):
        self.iface = iface
        self.layout = QVBoxLayout()
        self.cbGroups = QComboBox()
        self.cbDomains = QComboBox()
        self.cbElements = QComboBox()
        self.cbItems = QComboBox()
        self.download_folder = QLineEdit()
        try:
            if self.last_download_folder is not None:
                self.download_folder.setText(self.last_download_folder)
        except:
            self.last_download_folder = None
        self.download_folder_button = QPushButton(self.tr('...'))
        self.download_folder_button.clicked.connect(self.select_output_file)
        self.progress = QProgressBar()
        self.add_to_canvas = QCheckBox(self.tr('Add output layer to canvas'))
        self.start_download_button = QPushButton(self.tr('Start Download'))
        self.start_download_button.clicked.connect(self.download_data)
        self.progress_label = QLabel('<b>' + self.tr('Progress') + '</b>')
        self.bar = QgsMessageBar()
        self.plugin_dir = os.path.dirname(__file__)
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir,
            'i18n',
            'geobricks_qgis_plugin_faostat_{}.qm'.format(locale))
        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)
            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)
        self.dlg = geobricks_qgis_plugin_faostatDialog()
        self.actions = []
        self.menu = self.tr('FAOSTAT Data Downloader')
        self.toolbar = self.iface.addToolBar('geobricks_qgis_plugin_faostat')
        self.toolbar.setObjectName('geobricks_qgis_plugin_faostat')
        self.initialized = False

    def run(self):

        # Build UI
        self.build_ui()

        # Populate domains
        groups = get_groups()
        for group in groups:
            self.cbGroups.addItem(group['label'], group['code'])

        # Test message bar
        self.bar.pushMessage(None, str(len(groups)) + self.tr(' groups added'), level=QgsMessageBar.INFO)

    def build_ui(self):

        # Reset layout
        self.layout = QVBoxLayout()

        # Groups
        lbl_0 = QLabel('<b>' + self.tr('Groups') + '</b>')
        self.cbGroups.addItem(self.tr('Please select a groups...'))
        self.cbGroups.activated[str].connect(self.on_groups_change)

        # Domains
        lbl_1 = QLabel('<b>' + self.tr('Domains') + '</b>')
        self.cbDomains.addItem(self.tr('Please select a group to populate this combo-box...'))
        self.cbDomains.activated[str].connect(self.on_domain_change)

        # Elements
        lbl_2 = QLabel('<b>' + self.tr('Elements') + '</b>')
        self.cbElements.addItem(self.tr('Please select a domain to populate this combo-box...'))

        # Items
        lbl_3 = QLabel('<b>' + self.tr('Items') + '</b>')
        self.cbItems.addItem(self.tr('Please select a domain to populate this combo-box...'))

        # Download Folder
        lbl_4 = QLabel('<b>' + self.tr('Download Folder') + '</b>')
        download_folder_widget = QWidget()
        download_folder_layout = QHBoxLayout()
        download_folder_widget.setLayout(download_folder_layout)
        download_folder_layout.addWidget(self.download_folder)
        download_folder_layout.addWidget(self.download_folder_button)

        # Progress bar
        self.progress.setValue(0)

        # Add to canvas
        self.add_to_canvas.toggle()

        # Message bar
        self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.layout.addWidget(self.bar)

        # Add widgets to layout
        self.layout.addWidget(lbl_0)
        self.layout.addWidget(self.cbGroups)
        self.layout.addWidget(lbl_1)
        self.layout.addWidget(self.cbDomains)
        self.layout.addWidget(lbl_2)
        self.layout.addWidget(self.cbElements)
        self.layout.addWidget(lbl_3)
        self.layout.addWidget(self.cbItems)
        self.layout.addWidget(lbl_4)
        self.layout.addWidget(download_folder_widget)
        self.layout.addWidget(self.add_to_canvas)
        self.layout.addWidget(self.start_download_button)
        self.layout.addWidget(self.progress_label)
        self.layout.addWidget(self.progress)

        # Set layout
        self.dlg.setLayout(self.layout)

        # Show dialog
        self.dlg.show()

    def download_data(self):

        # Get user selection
        group_code = self.cbGroups.itemData(self.cbGroups.currentIndex())
        domain_code = self.cbDomains.itemData(self.cbDomains.currentIndex())
        element_code = self.cbElements.itemData(self.cbElements.currentIndex())
        item_code = self.cbItems.itemData(self.cbItems.currentIndex())
        download_folder = self.download_folder.text()

        # Check selection
        if group_code is None:
            self.bar.pushMessage(None, self.tr('Please select a group'), level=QgsMessageBar.CRITICAL)
        elif domain_code is None:
            self.bar.pushMessage(None, self.tr('Please select a domain'), level=QgsMessageBar.CRITICAL)
        elif element_code is None:
            self.bar.pushMessage(None, self.tr('Please select an element'), level=QgsMessageBar.CRITICAL)
        elif item_code is None:
            self.bar.pushMessage(None, self.tr('Please select an item'), level=QgsMessageBar.CRITICAL)
        elif download_folder is None or len(download_folder) == 0:
            self.bar.pushMessage(None, self.tr('Please select a download folder'), level=QgsMessageBar.CRITICAL)
        else:
            # Get data
            data = get_data(domain_code, element_code, item_code)
            # Notify the user
            self.bar.pushMessage(None, self.tr('Downloaded rows: ') + str(len(data)), level=QgsMessageBar.INFO)
            # Layer name
            layer_name = self.cbItems.currentText().replace(' ', '_') + '_' + self.cbElements.currentText().replace(' ', '_')
            folder_name = os.path.join(download_folder, group_code, domain_code)
            if not os.path.exists(folder_name):
                os.makedirs(folder_name)
            # Copy template layer
            output_file = copy_layer(folder_name, layer_name)
            layer = QgsVectorLayer(output_file, 'layer_name', 'ogr')
            # Add all the years to the layer
            feature_idx = 64
            year_to_be_shown = 2014
            number_of_nulls = 0
            for year in range(2014, 1960, -1):
                progress = (1 + (feature_idx - 64)) * 1.86
                self.progress.setValue(progress)
                self.progress_label.setText('<b>' + self.tr('Progress') + ': ' + '</b> ' + self.tr('Adding Year ') + str(year))
                year_data = self.get_year_data(data, year)
                layer.dataProvider().addAttributes([QgsField(str(year), QVariant.Double)])
                if len(year_data) > 0:
                    layer.startEditing()
                    for feature in layer.getFeatures():
                        if feature['FAOSTAT'] is not None:
                            feature_code = str(feature['FAOSTAT'])
                            for d in year_data:
                                data_code = str(d['code'])
                                if data_code == feature_code:
                                    value = d['value']
                                    layer.changeAttributeValue(feature.id(), (feature_idx), float(value))
                                    tmp_feature = QgsFeature()
                                    tmp_feature.setAttributes([float(value)])
                                    layer.dataProvider().addFeatures([tmp_feature])
                                    if value is None:
                                        number_of_nulls += 1
                    layer.commitChanges()
                else:
                    year_to_be_shown -= 1
                feature_idx += 1
            # Add layer to canvas
            if self.add_to_canvas.isChecked():
                renderer = self.create_join_renderer(layer, str(year_to_be_shown), 11, QgsGraduatedSymbolRendererV2.Pretty)
                l = QgsVectorLayer(output_file, layer_name + '(' + str(year_to_be_shown) + ')', 'ogr')
                r = renderer.clone()
                r.setClassAttribute(str(year_to_be_shown))
                l.setRendererV2(r)
                QgsMapLayerRegistry.instance().addMapLayer(l)
                self.iface.legendInterface().setLayerVisible(l, True)
            # Close pop-up
            self.dlg.close()

    def create_join_renderer(self, layer, field, classes, mode, color='PuBu'):
        symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
        style = QgsStyleV2().defaultStyle()
        colorRamp = style.colorRampRef(color)
        renderer = QgsGraduatedSymbolRendererV2.createRenderer(layer, field, classes, mode, symbol, colorRamp)
        label_format = self.create_join_label_format(2)
        renderer.setLabelFormat(label_format)
        return renderer

    def create_join_label_format(self, precision):
        format = QgsRendererRangeV2LabelFormat()
        template = '%1 - %2'
        format.setFormat(template)
        format.setPrecision(precision)
        format.setTrimTrailingZeroes(True)
        return format

    def get_year_data(self, data, year):
        out = []
        for d in data:
            if d['year'] == str(year):
                out.append(d)
        return out

    def on_groups_change(self, text):

        # Get selected group code
        group_code = self.cbGroups.itemData(self.cbGroups.currentIndex())

        # Update domains list
        domains = get_domains(group_code)
        self.cbDomains.clear()
        self.cbDomains.addItem(self.tr('Please select a domain'))
        for domain in domains:
            self.cbDomains.addItem(domain['label'], domain['code'])

    def on_domain_change(self, text):

        # Get selected domain code
        domain_code = self.cbDomains.itemData(self.cbDomains.currentIndex())

        # Check domain code
        if domain_code is not None:

            # Update elements list
            try:
                elements = get_elements(domain_code)
                self.cbElements.clear()
                self.cbElements.addItem(self.tr('Please select an element'))
                for element in elements:
                    self.cbElements.addItem(element['label'], element['code'])
            except ValueError:
                self.bar.pushMessage(None, self.tr('No elements available for this domain. Please select another domain.'), level=QgsMessageBar.CRITICAL)

            # Update items list
            try:
                items = get_items(domain_code)
                self.cbItems.clear()
                self.cbItems.addItem(self.tr('Please select an item'))
                for item in items:
                    self.cbItems.addItem(item['label'], item['code'])
            except:
                self.bar.pushMessage(None, self.tr('No items available for this domain. Please select another domain.'), level=QgsMessageBar.CRITICAL)

        else:
            self.bar.pushMessage(None, self.tr('No domain selected. Please select a domain.'), level=QgsMessageBar.CRITICAL)

    def tr(self, message):
        return QCoreApplication.translate('geobricks_qgis_plugin_faostat', message)

    def add_action(self, icon_path, text, callback, enabled_flag=True, add_to_menu=True, add_to_toolbar=True,
                   status_tip=None, whats_this=None, parent=None):
        icon = QIcon(icon_path)
        action = QAction(icon, text, parent)
        action.triggered.connect(callback)
        action.setEnabled(enabled_flag)
        if status_tip is not None:
            action.setStatusTip(status_tip)
        if whats_this is not None:
            action.setWhatsThis(whats_this)
        if add_to_toolbar:
            self.toolbar.addAction(action)
        if add_to_menu:
            self.iface.addPluginToMenu(
                self.menu,
                action)
        self.actions.append(action)
        return action

    def initGui(self):
        icon_path = ':/plugins/geobricks_qgis_plugin_faostat/icon.png'
        self.add_action(
            icon_path,
            text=self.tr('FAOSTAT Data Downloader'),
            callback=self.run,
            parent=self.iface.mainWindow())

    def unload(self):
        for action in self.actions:
            self.iface.removePluginMenu(
                self.tr(self.tr('FAOSTAT Data Downloader')),
                action)
            self.iface.removeToolBarIcon(action)
        del self.toolbar

    def select_output_file(self):
        filename = QFileDialog.getExistingDirectory(self.dlg, self.tr('Select Folder'))
        self.last_download_folder = filename
        self.download_folder.setText(self.last_download_folder)
class GithubRepoWizardPage(QWizardPage):
    def __init__(self, github, parent=None):
        super(GithubRepoWizardPage, self).__init__(
            parent,
            title="Github Repository",
            subTitle="Configure the new Github repository")
        
        self.github = github
        
        # moreButton

        self.moreButton = QPushButton(
                "More",
                checkable=True,
                clicked=self.more)
        spacer = QWidget()
        spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        moreButtonHBox = QHBoxLayout()
        moreButtonHBox.addWidget(spacer)
        moreButtonHBox.addWidget(self.moreButton)

        #  LineEdits

        self.nameEdit = QLineEdit(textChanged=self.update)
        self.nameEdit.setValidator(QRegExpValidator(
                QRegExp(r'[a-zA-Z0-9-_]+[a-zA-Z0-9-_]*')))
        self.descriptionEdit = QLineEdit(textChanged=self.update)
        self.homepageEdit = QLineEdit(textChanged=self.update)
        
        # CheckBox

        self.privateCheckBox = QCheckBox(stateChanged=self.update)
        self.initCheckBox = QCheckBox(stateChanged=self.update)
        self.hasWikiCheckBox = QCheckBox(stateChanged=self.update)
        self.hasDownloadsCheckBox = QCheckBox(stateChanged=self.update)
        self.hasIssuesCheckBox = QCheckBox(stateChanged=self.update)
        
        # gitignoreComboBox

        self.gitignoreComboBox = QComboBox(currentIndexChanged=self.update)
        self.gitignoreComboBox.addItem('None')
        for i in gitignore_types(self.github):
            self.gitignoreComboBox.addItem(i)
            
        hbox2 = QHBoxLayout()
        hbox2.addWidget(QLabel(
            'Initialize this repository with a README and .gitignore'))
        hbox2.addWidget(self.initCheckBox)
        
        # Extension Form

        self.form_extension = QFormLayout()
        self.form_extension.addRow("Homepage", self.homepageEdit)  
        self.form_extension.addRow("Has wiki", self.hasWikiCheckBox)
        self.form_extension.addRow("Has issues", self.hasIssuesCheckBox)
        self.form_extension.addRow("Has downloads", self.hasDownloadsCheckBox)

        # Extension

        self.extension = QWidget()
        self.extension.setLayout(self.form_extension)

        # Form

        self.form = QFormLayout()
        self.form.addRow("Name: ", self.nameEdit)
        self.form.addRow("Description: ", self.descriptionEdit)
        self.form.addRow('Private', self.privateCheckBox)
        self.form.addRow(hbox2)
        self.form.addRow('Add .gitignore', self.gitignoreComboBox)
        self.form.addRow(moreButtonHBox)
        self.form.addRow(self.extension)
        
        # Layout

        self.mainLayout = QVBoxLayout()
        self.mainLayout.addLayout(self.form)
        self.setLayout(self.mainLayout)
    
        # Fields

        self.registerField('name*', self.nameEdit)
        self.registerField('description', self.descriptionEdit)
        self.registerField('private', self.privateCheckBox)
        self.registerField('auto_init', self.initCheckBox)
        self.registerField('gitignore', self.gitignoreComboBox, 'currentText')
        self.registerField('homepage', self.homepageEdit)
        self.registerField('has_issues', self.hasIssuesCheckBox)
        self.registerField('has_downloads', self.hasDownloadsCheckBox)
        self.registerField('has_wiki', self.hasWikiCheckBox)
        
        # Setup

        self.hasWikiCheckBox.toggle()
        self.hasDownloadsCheckBox.toggle()
        self.hasIssuesCheckBox.toggle()
        if not self.github.get_user().plan:
            self.privateCheckBox.setEnabled(False)
        
        self.extension.hide()

    def update(self):

        if self.initCheckBox.isChecked():
            self.gitignoreComboBox.setEnabled(True)
        else:
            self.gitignoreComboBox.setEnabled(False)

    def more(self):
            
        if self.moreButton.isChecked():
            self.moreButton.setText("Less")
            self.extension.show()
            self.wizard().resize(self.wizard().sizeHint())
        else:
            self.moreButton.setText("More")
            self.extension.hide()
            size = self.sizeHint()
            wizard_size = self.wizard().sizeHint()
            self.wizard().resize(wizard_size.width(), size.height())
Esempio n. 6
0
class geobricks_qgis_plugin_faostat:
    def __init__(self, iface):
        self.iface = iface
        self.layout = QVBoxLayout()
        self.cbGroups = QComboBox()
        self.cbDomains = QComboBox()
        self.cbElements = QComboBox()
        self.cbItems = QComboBox()
        self.download_folder = QLineEdit()
        try:
            if self.last_download_folder is not None:
                self.download_folder.setText(self.last_download_folder)
        except:
            self.last_download_folder = None
        self.download_folder_button = QPushButton(self.tr('...'))
        self.download_folder_button.clicked.connect(self.select_output_file)
        self.progress = QProgressBar()
        self.add_to_canvas = QCheckBox(self.tr('Add output layer to canvas'))
        self.start_download_button = QPushButton(self.tr('Start Download'))
        self.start_download_button.clicked.connect(self.download_data)
        self.progress_label = QLabel('<b>' + self.tr('Progress') + '</b>')
        self.bar = QgsMessageBar()
        self.plugin_dir = os.path.dirname(__file__)
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(
            self.plugin_dir, 'i18n',
            'geobricks_qgis_plugin_faostat_{}.qm'.format(locale))
        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)
            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)
        self.dlg = geobricks_qgis_plugin_faostatDialog()
        self.actions = []
        self.menu = self.tr('FAOSTAT Data Downloader')
        self.toolbar = self.iface.addToolBar('geobricks_qgis_plugin_faostat')
        self.toolbar.setObjectName('geobricks_qgis_plugin_faostat')
        self.initialized = False

    def run(self):

        # Build UI
        self.build_ui()

        # Populate domains
        groups = get_groups()
        for group in groups:
            self.cbGroups.addItem(group['label'], group['code'])

        # Test message bar
        self.bar.pushMessage(None,
                             str(len(groups)) + self.tr(' groups added'),
                             level=QgsMessageBar.INFO)

    def build_ui(self):

        # Reset layout
        self.layout = QVBoxLayout()

        # Groups
        lbl_0 = QLabel('<b>' + self.tr('Groups') + '</b>')
        self.cbGroups.addItem(self.tr('Please select a groups...'))
        self.cbGroups.activated[str].connect(self.on_groups_change)

        # Domains
        lbl_1 = QLabel('<b>' + self.tr('Domains') + '</b>')
        self.cbDomains.addItem(
            self.tr('Please select a group to populate this combo-box...'))
        self.cbDomains.activated[str].connect(self.on_domain_change)

        # Elements
        lbl_2 = QLabel('<b>' + self.tr('Elements') + '</b>')
        self.cbElements.addItem(
            self.tr('Please select a domain to populate this combo-box...'))

        # Items
        lbl_3 = QLabel('<b>' + self.tr('Items') + '</b>')
        self.cbItems.addItem(
            self.tr('Please select a domain to populate this combo-box...'))

        # Download Folder
        lbl_4 = QLabel('<b>' + self.tr('Download Folder') + '</b>')
        download_folder_widget = QWidget()
        download_folder_layout = QHBoxLayout()
        download_folder_widget.setLayout(download_folder_layout)
        download_folder_layout.addWidget(self.download_folder)
        download_folder_layout.addWidget(self.download_folder_button)

        # Progress bar
        self.progress.setValue(0)

        # Add to canvas
        self.add_to_canvas.toggle()

        # Message bar
        self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.layout.addWidget(self.bar)

        # Add widgets to layout
        self.layout.addWidget(lbl_0)
        self.layout.addWidget(self.cbGroups)
        self.layout.addWidget(lbl_1)
        self.layout.addWidget(self.cbDomains)
        self.layout.addWidget(lbl_2)
        self.layout.addWidget(self.cbElements)
        self.layout.addWidget(lbl_3)
        self.layout.addWidget(self.cbItems)
        self.layout.addWidget(lbl_4)
        self.layout.addWidget(download_folder_widget)
        self.layout.addWidget(self.add_to_canvas)
        self.layout.addWidget(self.start_download_button)
        self.layout.addWidget(self.progress_label)
        self.layout.addWidget(self.progress)

        # Set layout
        self.dlg.setLayout(self.layout)

        # Show dialog
        self.dlg.show()

    def download_data(self):

        # Get user selection
        group_code = self.cbGroups.itemData(self.cbGroups.currentIndex())
        domain_code = self.cbDomains.itemData(self.cbDomains.currentIndex())
        element_code = self.cbElements.itemData(self.cbElements.currentIndex())
        item_code = self.cbItems.itemData(self.cbItems.currentIndex())
        download_folder = self.download_folder.text()

        # Check selection
        if group_code is None:
            self.bar.pushMessage(None,
                                 self.tr('Please select a group'),
                                 level=QgsMessageBar.CRITICAL)
        elif domain_code is None:
            self.bar.pushMessage(None,
                                 self.tr('Please select a domain'),
                                 level=QgsMessageBar.CRITICAL)
        elif element_code is None:
            self.bar.pushMessage(None,
                                 self.tr('Please select an element'),
                                 level=QgsMessageBar.CRITICAL)
        elif item_code is None:
            self.bar.pushMessage(None,
                                 self.tr('Please select an item'),
                                 level=QgsMessageBar.CRITICAL)
        elif download_folder is None or len(download_folder) == 0:
            self.bar.pushMessage(None,
                                 self.tr('Please select a download folder'),
                                 level=QgsMessageBar.CRITICAL)
        else:
            # Get data
            data = get_data(domain_code, element_code, item_code)
            # Notify the user
            self.bar.pushMessage(None,
                                 self.tr('Downloaded rows: ') + str(len(data)),
                                 level=QgsMessageBar.INFO)
            # Layer name
            layer_name = self.cbItems.currentText().replace(
                ' ', '_') + '_' + self.cbElements.currentText().replace(
                    ' ', '_')
            folder_name = os.path.join(download_folder, group_code,
                                       domain_code)
            if not os.path.exists(folder_name):
                os.makedirs(folder_name)
            # Copy template layer
            output_file = copy_layer(folder_name, layer_name)
            layer = QgsVectorLayer(output_file, 'layer_name', 'ogr')
            # Add all the years to the layer
            feature_idx = 64
            year_to_be_shown = 2014
            number_of_nulls = 0
            for year in range(2014, 1960, -1):
                progress = (1 + (feature_idx - 64)) * 1.86
                self.progress.setValue(progress)
                self.progress_label.setText('<b>' + self.tr('Progress') +
                                            ': ' + '</b> ' +
                                            self.tr('Adding Year ') +
                                            str(year))
                year_data = self.get_year_data(data, year)
                layer.dataProvider().addAttributes(
                    [QgsField(str(year), QVariant.Double)])
                if len(year_data) > 0:
                    layer.startEditing()
                    for feature in layer.getFeatures():
                        if feature['FAOSTAT'] is not None:
                            feature_code = str(feature['FAOSTAT'])
                            for d in year_data:
                                data_code = str(d['code'])
                                if data_code == feature_code:
                                    value = d['value']
                                    layer.changeAttributeValue(
                                        feature.id(), (feature_idx),
                                        float(value))
                                    tmp_feature = QgsFeature()
                                    tmp_feature.setAttributes([float(value)])
                                    layer.dataProvider().addFeatures(
                                        [tmp_feature])
                                    if value is None:
                                        number_of_nulls += 1
                    layer.commitChanges()
                else:
                    year_to_be_shown -= 1
                feature_idx += 1
            # Add layer to canvas
            if self.add_to_canvas.isChecked():
                renderer = self.create_join_renderer(
                    layer, str(year_to_be_shown), 11,
                    QgsGraduatedSymbolRendererV2.Pretty)
                l = QgsVectorLayer(
                    output_file,
                    layer_name + '(' + str(year_to_be_shown) + ')', 'ogr')
                r = renderer.clone()
                r.setClassAttribute(str(year_to_be_shown))
                l.setRendererV2(r)
                QgsMapLayerRegistry.instance().addMapLayer(l)
                self.iface.legendInterface().setLayerVisible(l, True)
            # Close pop-up
            self.dlg.close()

    def create_join_renderer(self, layer, field, classes, mode, color='PuBu'):
        symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
        style = QgsStyleV2().defaultStyle()
        colorRamp = style.colorRampRef(color)
        renderer = QgsGraduatedSymbolRendererV2.createRenderer(
            layer, field, classes, mode, symbol, colorRamp)
        label_format = self.create_join_label_format(2)
        renderer.setLabelFormat(label_format)
        return renderer

    def create_join_label_format(self, precision):
        format = QgsRendererRangeV2LabelFormat()
        template = '%1 - %2'
        format.setFormat(template)
        format.setPrecision(precision)
        format.setTrimTrailingZeroes(True)
        return format

    def get_year_data(self, data, year):
        out = []
        for d in data:
            if d['year'] == str(year):
                out.append(d)
        return out

    def on_groups_change(self, text):

        # Get selected group code
        group_code = self.cbGroups.itemData(self.cbGroups.currentIndex())

        # Update domains list
        domains = get_domains(group_code)
        self.cbDomains.clear()
        self.cbDomains.addItem(self.tr('Please select a domain'))
        for domain in domains:
            self.cbDomains.addItem(domain['label'], domain['code'])

    def on_domain_change(self, text):

        # Get selected domain code
        domain_code = self.cbDomains.itemData(self.cbDomains.currentIndex())

        # Check domain code
        if domain_code is not None:

            # Update elements list
            try:
                elements = get_elements(domain_code)
                self.cbElements.clear()
                self.cbElements.addItem(self.tr('Please select an element'))
                for element in elements:
                    self.cbElements.addItem(element['label'], element['code'])
            except ValueError:
                self.bar.pushMessage(
                    None,
                    self.
                    tr('No elements available for this domain. Please select another domain.'
                       ),
                    level=QgsMessageBar.CRITICAL)

            # Update items list
            try:
                items = get_items(domain_code)
                self.cbItems.clear()
                self.cbItems.addItem(self.tr('Please select an item'))
                for item in items:
                    self.cbItems.addItem(item['label'], item['code'])
            except:
                self.bar.pushMessage(
                    None,
                    self.
                    tr('No items available for this domain. Please select another domain.'
                       ),
                    level=QgsMessageBar.CRITICAL)

        else:
            self.bar.pushMessage(
                None,
                self.tr('No domain selected. Please select a domain.'),
                level=QgsMessageBar.CRITICAL)

    def tr(self, message):
        return QCoreApplication.translate('geobricks_qgis_plugin_faostat',
                                          message)

    def add_action(self,
                   icon_path,
                   text,
                   callback,
                   enabled_flag=True,
                   add_to_menu=True,
                   add_to_toolbar=True,
                   status_tip=None,
                   whats_this=None,
                   parent=None):
        icon = QIcon(icon_path)
        action = QAction(icon, text, parent)
        action.triggered.connect(callback)
        action.setEnabled(enabled_flag)
        if status_tip is not None:
            action.setStatusTip(status_tip)
        if whats_this is not None:
            action.setWhatsThis(whats_this)
        if add_to_toolbar:
            self.toolbar.addAction(action)
        if add_to_menu:
            self.iface.addPluginToMenu(self.menu, action)
        self.actions.append(action)
        return action

    def initGui(self):
        icon_path = ':/plugins/geobricks_qgis_plugin_faostat/icon.png'
        self.add_action(icon_path,
                        text=self.tr('FAOSTAT Data Downloader'),
                        callback=self.run,
                        parent=self.iface.mainWindow())

    def unload(self):
        for action in self.actions:
            self.iface.removePluginMenu(
                self.tr(self.tr('FAOSTAT Data Downloader')), action)
            self.iface.removeToolBarIcon(action)
        del self.toolbar

    def select_output_file(self):
        filename = QFileDialog.getExistingDirectory(self.dlg,
                                                    self.tr('Select Folder'))
        self.last_download_folder = filename
        self.download_folder.setText(self.last_download_folder)
Esempio n. 7
0
class GithubRepoWizardPage(QWizardPage):
    def __init__(self, github, parent=None):
        super(GithubRepoWizardPage,
              self).__init__(parent,
                             title="Github Repository",
                             subTitle="Configure the new Github repository")

        self.github = github

        # moreButton

        self.moreButton = QPushButton("More",
                                      checkable=True,
                                      clicked=self.more)
        spacer = QWidget()
        spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        moreButtonHBox = QHBoxLayout()
        moreButtonHBox.addWidget(spacer)
        moreButtonHBox.addWidget(self.moreButton)

        #  LineEdits

        self.nameEdit = QLineEdit(textChanged=self.update)
        self.nameEdit.setValidator(
            QRegExpValidator(QRegExp(r'[a-zA-Z0-9-_]+[a-zA-Z0-9-_]*')))
        self.descriptionEdit = QLineEdit(textChanged=self.update)
        self.homepageEdit = QLineEdit(textChanged=self.update)

        # CheckBox

        self.privateCheckBox = QCheckBox(stateChanged=self.update)
        self.initCheckBox = QCheckBox(stateChanged=self.update)
        self.hasWikiCheckBox = QCheckBox(stateChanged=self.update)
        self.hasDownloadsCheckBox = QCheckBox(stateChanged=self.update)
        self.hasIssuesCheckBox = QCheckBox(stateChanged=self.update)

        # gitignoreComboBox

        self.gitignoreComboBox = QComboBox(currentIndexChanged=self.update)
        self.gitignoreComboBox.addItem('None')
        for i in gitignore_types(self.github):
            self.gitignoreComboBox.addItem(i)

        hbox2 = QHBoxLayout()
        hbox2.addWidget(
            QLabel('Initialize this repository with a README and .gitignore'))
        hbox2.addWidget(self.initCheckBox)

        # Extension Form

        self.form_extension = QFormLayout()
        self.form_extension.addRow("Homepage", self.homepageEdit)
        self.form_extension.addRow("Has wiki", self.hasWikiCheckBox)
        self.form_extension.addRow("Has issues", self.hasIssuesCheckBox)
        self.form_extension.addRow("Has downloads", self.hasDownloadsCheckBox)

        # Extension

        self.extension = QWidget()
        self.extension.setLayout(self.form_extension)

        # Form

        self.form = QFormLayout()
        self.form.addRow("Name: ", self.nameEdit)
        self.form.addRow("Description: ", self.descriptionEdit)
        self.form.addRow('Private', self.privateCheckBox)
        self.form.addRow(hbox2)
        self.form.addRow('Add .gitignore', self.gitignoreComboBox)
        self.form.addRow(moreButtonHBox)
        self.form.addRow(self.extension)

        # Layout

        self.mainLayout = QVBoxLayout()
        self.mainLayout.addLayout(self.form)
        self.setLayout(self.mainLayout)

        # Fields

        self.registerField('name*', self.nameEdit)
        self.registerField('description', self.descriptionEdit)
        self.registerField('private', self.privateCheckBox)
        self.registerField('auto_init', self.initCheckBox)
        self.registerField('gitignore', self.gitignoreComboBox, 'currentText')
        self.registerField('homepage', self.homepageEdit)
        self.registerField('has_issues', self.hasIssuesCheckBox)
        self.registerField('has_downloads', self.hasDownloadsCheckBox)
        self.registerField('has_wiki', self.hasWikiCheckBox)

        # Setup

        self.hasWikiCheckBox.toggle()
        self.hasDownloadsCheckBox.toggle()
        self.hasIssuesCheckBox.toggle()
        if not self.github.get_user().plan:
            self.privateCheckBox.setEnabled(False)

        self.extension.hide()

    def update(self):

        if self.initCheckBox.isChecked():
            self.gitignoreComboBox.setEnabled(True)
        else:
            self.gitignoreComboBox.setEnabled(False)

    def more(self):

        if self.moreButton.isChecked():
            self.moreButton.setText("Less")
            self.extension.show()
            self.wizard().resize(self.wizard().sizeHint())
        else:
            self.moreButton.setText("More")
            self.extension.hide()
            size = self.sizeHint()
            wizard_size = self.wizard().sizeHint()
            self.wizard().resize(wizard_size.width(), size.height())