|
1 | 1 | from PyQt4.QtCore import *
|
2 | 2 | from PyQt4.QtGui import *
|
3 | 3 |
|
4 |
| -from qgis.core import QgsContextHelp |
| 4 | +from qgis.core import QgsApplication, QgsContextHelp |
5 | 5 |
|
6 | 6 | from gui import Ui_Dialog
|
7 | 7 | import resources
|
8 | 8 |
|
| 9 | +from repository_dialog import RepositoryDialog |
| 10 | + |
| 11 | + |
9 | 12 | class InstallerPluginGui(QDialog, Ui_Dialog):
|
10 | 13 | def __init__(self, parent, fl):
|
11 | 14 | QDialog.__init__(self, parent, fl)
|
12 | 15 |
|
| 16 | + self.default_repository_name = "Official QGIS repository" |
| 17 | + self.default_repository = "http://spatialserver.net/cgi-bin/pyqgis_plugin.rb" |
| 18 | + |
13 | 19 | self.setupUi(self)
|
14 | 20 |
|
15 |
| - def on_buttonBrowse_released(self): |
16 |
| - self.emit(SIGNAL("retrieveList(QString )"),"test" ) |
17 |
| - print "browse" |
18 |
| - |
19 |
| - def on_pbnOK_released(self): |
20 |
| - #self.hide() |
21 |
| - self.emit(SIGNAL("installPlugin(QString )"), self.linePlugin.text() ) |
22 |
| - return |
23 |
| - #self.done(1) |
| 21 | + self.connect(self.buttonBrowse, SIGNAL("clicked()"), self.getAvailablePlugins) |
| 22 | + self.connect(self.pbnOK, SIGNAL("clicked()"), self.installPlugin) |
24 | 23 |
|
| 24 | + # grab the click on the treelist |
| 25 | + self.connect(self.treePlugins, SIGNAL("itemClicked(QTreeWidgetItem *,int)"), self.treeClicked) |
| 26 | + |
| 27 | + # repositories handling |
| 28 | + self.connect(self.buttonAddRep, SIGNAL("clicked()"), self.addRepository) |
| 29 | + self.connect(self.buttonEditRep, SIGNAL("clicked()"), self.editRepository) |
| 30 | + self.connect(self.buttonDeleteRep, SIGNAL("clicked()"), self.deleteRepository) |
| 31 | + |
| 32 | + self.populateRepositories() |
| 33 | + |
| 34 | + |
25 | 35 | def on_pbnCancel_clicked(self):
|
26 | 36 | self.close()
|
| 37 | + |
| 38 | + |
| 39 | + def getAvailablePlugins(self): |
| 40 | + print "getting list of plugins" |
| 41 | + repository = self.getRepository() |
| 42 | + if not repository: |
| 43 | + return |
| 44 | + repository_url = str(repository[1]) |
| 45 | + |
| 46 | + from qgis_plugins import retrieve_list |
| 47 | + QApplication.setOverrideCursor(Qt.WaitCursor) |
| 48 | + |
| 49 | + try: |
| 50 | + pluginlist = retrieve_list(repository_url) |
| 51 | + except IOError: |
| 52 | + QApplication.restoreOverrideCursor() |
| 53 | + QMessageBox.warning(self, "Error", "Couldn't connect to the repository.") |
| 54 | + return |
| 55 | + except Exception: |
| 56 | + QApplication.restoreOverrideCursor() |
| 57 | + QMessageBox.warning(self, "Error", "Couldn't parse output from repository.") |
| 58 | + return |
| 59 | + |
| 60 | + #output = "QGIS python plugins avialable from \n%s\n" % self.repository |
| 61 | + #for p in pluginlist: |
| 62 | + # output += "\n%s ( version %s )" % (p["name"], p["version"]) |
| 63 | + # output += "\n\t%s by %s" % (p["desc"],p["author"]) |
| 64 | + #self.gui.txtAvailable.setText(output) |
| 65 | + self.treePlugins.clear() |
| 66 | + for p in pluginlist: |
| 67 | + a = QTreeWidgetItem(self.treePlugins) |
| 68 | + a.setText(0,p["name"]) |
| 69 | + a.setText(1,p["version"]) |
| 70 | + a.setText(2,p["desc"]) |
| 71 | + a.setText(3,p["author"]) |
| 72 | + |
| 73 | + QApplication.restoreOverrideCursor() |
| 74 | + |
| 75 | + # resize the columns |
| 76 | + # plugin name |
| 77 | + self.treePlugins.resizeColumnToContents(0); |
| 78 | + # version |
| 79 | + self.treePlugins.resizeColumnToContents(1); |
| 80 | + # author/contributor |
| 81 | + self.treePlugins.resizeColumnToContents(3); |
| 82 | + # description |
| 83 | + self.treePlugins.setColumnWidth(2, 560); |
| 84 | + |
| 85 | + |
| 86 | + def installPlugin(self): |
| 87 | + """ installs currently selected plugin """ |
| 88 | + plugin = self.linePlugin.text() |
| 89 | + repository = self.getRepository() |
| 90 | + if not repository: |
| 91 | + return |
| 92 | + repository_url = str(repository[1]) |
| 93 | + plugindir = str(QgsApplication.qgisSettingsDirPath()) + "/python/plugins" |
| 94 | + |
| 95 | + QApplication.setOverrideCursor(Qt.WaitCursor) |
| 96 | + from qgis_plugins import retrieve_list, install_plugin |
| 97 | + print "install_plugin",plugin,plugindir,repository_url |
| 98 | + result = install_plugin(plugin, plugindir, repository_url) |
| 99 | + QApplication.restoreOverrideCursor() |
| 100 | + |
| 101 | + if result[0]: |
| 102 | + QMessageBox.information(self, "Plugin installed successfully", result[1]) |
| 103 | + else: |
| 104 | + QMessageBox.warning(self, "Plugin installation failed", result[1]) |
| 105 | + |
| 106 | + |
| 107 | + def treeClicked(self, item, col): |
| 108 | + self.linePlugin.setText(item.text(0)) |
| 109 | + |
| 110 | + |
| 111 | + def getRepository(self): |
| 112 | + """ returns Name and URL of the current repository as a tuple or None if no repository is selected """ |
| 113 | + if self.comboRepositories.currentIndex() == -1: |
| 114 | + return None |
| 115 | + |
| 116 | + settings = QSettings() |
| 117 | + reposGroup = "/Qgis/plugin-repos" |
| 118 | + reposName = self.comboRepositories.currentText() |
| 119 | + reposURL = settings.value(reposGroup+"/"+reposName+"/url", QVariant()).toString() |
| 120 | + return (reposName, reposURL) |
| 121 | + |
| 122 | + |
| 123 | + def populateRepositories(self): |
| 124 | + """ populate repository combo box from the settings """ |
| 125 | + self.comboRepositories.clear() |
| 126 | + |
| 127 | + settings = QSettings() |
| 128 | + reposGroup = "/Qgis/plugin-repos" |
| 129 | + settings.beginGroup(reposGroup) |
| 130 | + |
| 131 | + # add the default repository when there isn't any... |
| 132 | + if len(settings.childGroups()) == 0: |
| 133 | + settings.setValue(self.default_repository_name+"/url", QVariant(self.default_repository)) |
| 134 | + |
| 135 | + for key in settings.childGroups(): |
| 136 | + self.comboRepositories.addItem(key) |
| 137 | + |
| 138 | + settings.endGroup() |
| 139 | + |
| 140 | + |
| 141 | + def addRepository(self): |
| 142 | + """ add repository button has been clicked """ |
| 143 | + print "add" |
| 144 | + dlg = RepositoryDialog(self) |
| 145 | + if not dlg.exec_(): |
| 146 | + return |
| 147 | + |
| 148 | + settings = QSettings() |
| 149 | + reposGroup = "/Qgis/plugin-repos" |
| 150 | + settings.beginGroup(reposGroup) |
| 151 | + |
| 152 | + reposName = dlg.editName.text() |
| 153 | + reposURL = dlg.editURL.text() |
| 154 | + print "name: "+reposName |
| 155 | + print "url: "+reposURL |
| 156 | + |
| 157 | + # add to settings |
| 158 | + settings.setValue(reposName+"/url", QVariant(reposURL)) |
| 159 | + |
| 160 | + # add to combobox |
| 161 | + self.comboRepositories.addItem(reposName) |
| 162 | + |
| 163 | + |
| 164 | + def editRepository(self): |
| 165 | + """ edit repository button has been clicked """ |
| 166 | + print "edit" |
| 167 | + |
| 168 | + current = self.comboRepositories.currentIndex() |
| 169 | + if current == -1: |
| 170 | + return |
| 171 | + |
| 172 | + (reposName, reposURL) = self.getRepository() |
| 173 | + |
| 174 | + dlg = RepositoryDialog(self) |
| 175 | + dlg.editName.setText(reposName) |
| 176 | + dlg.editURL.setText(reposURL) |
| 177 | + if not dlg.exec_(): |
| 178 | + return |
| 179 | + |
| 180 | + settings = QSettings() |
| 181 | + reposGroup = "/Qgis/plugin-repos" |
| 182 | + settings.beginGroup(reposGroup) |
| 183 | + |
| 184 | + # first delete old setting |
| 185 | + settings.remove(reposName) |
| 186 | + |
| 187 | + # and create new one |
| 188 | + settings.setValue(dlg.editName.text()+"/url", QVariant(dlg.editURL.text())) |
| 189 | + |
| 190 | + # update the name if it has been changed |
| 191 | + self.comboRepositories.setItemText(current, dlg.editName.text()) |
| 192 | + |
| 193 | + |
| 194 | + def deleteRepository(self): |
| 195 | + """ delete repository button has been clicked """ |
| 196 | + print "delete" |
| 197 | + |
| 198 | + current = self.comboRepositories.currentIndex() |
| 199 | + if current == -1: |
| 200 | + return |
| 201 | + |
| 202 | + settings = QSettings() |
| 203 | + reposGroup = "/Qgis/plugin-repos" |
| 204 | + settings.beginGroup(reposGroup) |
| 205 | + |
| 206 | + # delete from settings |
| 207 | + reposName = self.comboRepositories.currentText() |
| 208 | + settings.remove(reposName) |
| 209 | + |
| 210 | + # delete from combo box |
| 211 | + self.comboRepositories.removeItem(current) |
0 commit comments