|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Package implementing the configuration page. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import pyqtSlot, Qt, QSortFilterProxyModel, QStringListModel |
|
15 |
|
16 from E5Gui import E5FileDialog |
|
17 |
|
18 from Preferences.ConfigurationPages.ConfigurationPageBase import \ |
|
19 ConfigurationPageBase |
|
20 from .Ui_PipPage import Ui_PipPage |
|
21 |
|
22 from .. import DefaultIndexUrl |
|
23 |
|
24 import Utilities |
|
25 |
|
26 |
|
27 class PipPage(ConfigurationPageBase, Ui_PipPage): |
|
28 """ |
|
29 Class implementing the configuration page. |
|
30 """ |
|
31 def __init__(self, plugin): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param plugin reference to the plugin object |
|
36 """ |
|
37 super(PipPage, self).__init__() |
|
38 self.setupUi(self) |
|
39 self.setObjectName("PipPage") |
|
40 |
|
41 self.__plugin = plugin |
|
42 |
|
43 self.__model = QStringListModel(self) |
|
44 self.__proxyModel = QSortFilterProxyModel(self) |
|
45 self.__proxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive) |
|
46 self.__proxyModel.setSourceModel(self.__model) |
|
47 self.stringList.setModel(self.__proxyModel) |
|
48 |
|
49 self.removeButton.clicked.connect(self.stringList.removeSelected) |
|
50 self.removeAllButton.clicked.connect(self.stringList.removeAll) |
|
51 |
|
52 self.indexLabel.setText(self.tr( |
|
53 '<b>Note:</b> Leave empty to use the default index URL (' |
|
54 '<a href="{0}">{0}</a>).') |
|
55 .format(DefaultIndexUrl)) |
|
56 |
|
57 # set initial values |
|
58 self.__model.setStringList( |
|
59 self.__plugin.getPreferences("PipExecutables")) |
|
60 self.__model.sort(0) |
|
61 self.indexEdit.setText(self.__plugin.getPreferences("PipSearchIndex")) |
|
62 |
|
63 @pyqtSlot() |
|
64 def on_addButton_clicked(self): |
|
65 """ |
|
66 Private slot used to add an executable to the list. |
|
67 """ |
|
68 executable = E5FileDialog.getOpenFileName( |
|
69 self, |
|
70 self.tr("Add pip executable"), |
|
71 os.path.expanduser("~"), |
|
72 "") |
|
73 executable = Utilities.toNativeSeparators(executable) |
|
74 if executable != "" and executable not in self.__model.stringList(): |
|
75 self.__model.insertRow(self.__model.rowCount()) |
|
76 self.__model.setData( |
|
77 self.__model.index(self.__model.rowCount() - 1), executable) |
|
78 self.__model.sort(0) |
|
79 |
|
80 @pyqtSlot() |
|
81 def on_defaultListButton_clicked(self): |
|
82 """ |
|
83 Private slot to load the default list of pip executables. |
|
84 """ |
|
85 self.stringList.removeAll() |
|
86 defaults = self.__plugin.getDefaultPipExecutables() |
|
87 for executable in defaults: |
|
88 self.__model.insertRow(self.__model.rowCount()) |
|
89 self.__model.setData( |
|
90 self.__model.index(self.__model.rowCount() - 1), executable) |
|
91 self.__model.sort(0) |
|
92 |
|
93 def save(self): |
|
94 """ |
|
95 Public slot to save the pip configuration. |
|
96 """ |
|
97 executables = self.__model.stringList()[:] |
|
98 self.__plugin.setPreferences( |
|
99 "PipExecutables", executables) |
|
100 self.__plugin.setPreferences( |
|
101 "PipSearchIndex", self.indexEdit.text().strip()) |
|
102 if executables: |
|
103 if self.__plugin.getPreferences("CurrentPipExecutable") \ |
|
104 not in executables: |
|
105 self.__plugin.setPreferences( |
|
106 "CurrentPipExecutable", "") |
|
107 else: |
|
108 # reset the current executable if none have been configured |
|
109 self.__plugin.setPreferences( |
|
110 "CurrentPipExecutable", "") |