|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter the data to add a submodule. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, QUrl |
|
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
14 |
|
15 from E5Gui.E5Completers import E5DirCompleter |
|
16 from E5Gui import E5FileDialog |
|
17 |
|
18 from .Ui_GitSubmoduleAddDialog import Ui_GitSubmoduleAddDialog |
|
19 from .Config import ConfigGitSchemes |
|
20 |
|
21 import Utilities |
|
22 import Preferences |
|
23 import UI.PixmapCache |
|
24 |
|
25 |
|
26 class GitSubmoduleAddDialog(QDialog, Ui_GitSubmoduleAddDialog): |
|
27 """ |
|
28 Class implementing a dialog to enter the data to add a submodule. |
|
29 """ |
|
30 def __init__(self, vcs, repodir, parent=None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param vcs reference to the version control object |
|
35 @type Git |
|
36 @param repodir directory containing the superproject |
|
37 @type str |
|
38 @param parent reference to the parent widget |
|
39 @type QWidget |
|
40 """ |
|
41 super(GitSubmoduleAddDialog, self).__init__(parent) |
|
42 self.setupUi(self) |
|
43 |
|
44 self.__vcs = vcs |
|
45 self.__repodir = repodir |
|
46 |
|
47 self.submoduleDirButton.setIcon(UI.PixmapCache.getIcon("open.png")) |
|
48 self.submoduleUrlButton.setIcon(UI.PixmapCache.getIcon("open.png")) |
|
49 self.submoduleUrlClearHistoryButton.setIcon( |
|
50 UI.PixmapCache.getIcon("editDelete.png")) |
|
51 |
|
52 submoduleUrlHistory = self.__vcs.getPlugin().getPreferences( |
|
53 "RepositoryUrlHistory") |
|
54 self.submoduleUrlCombo.addItems(submoduleUrlHistory) |
|
55 self.submoduleUrlCombo.setEditText("") |
|
56 |
|
57 self.submoduleUrlDirCompleter = E5DirCompleter(self.submoduleUrlCombo) |
|
58 self.submoduleDirCompleter = E5DirCompleter(self.submoduleDirEdit) |
|
59 |
|
60 ipath = Preferences.getMultiProject("Workspace") or \ |
|
61 Utilities.getHomeDir() |
|
62 self.__initPaths = [ |
|
63 Utilities.fromNativeSeparators(ipath), |
|
64 Utilities.fromNativeSeparators(ipath) + "/", |
|
65 ] |
|
66 |
|
67 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
68 |
|
69 msh = self.minimumSizeHint() |
|
70 self.resize(max(self.width(), msh.width()), msh.height()) |
|
71 |
|
72 @pyqtSlot(str) |
|
73 def on_submoduleUrlCombo_editTextChanged(self, txt): |
|
74 """ |
|
75 Private slot to handle changes of the submodule repository URL. |
|
76 |
|
77 @param txt current text of the combo box |
|
78 @type str |
|
79 """ |
|
80 url = QUrl.fromUserInput(txt) |
|
81 enable = url.isValid() and url.scheme() in ConfigGitSchemes |
|
82 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable) |
|
83 |
|
84 self.submoduleUrlButton.setEnabled(url.scheme() == "file" or |
|
85 len(txt) == 0) |
|
86 |
|
87 @pyqtSlot() |
|
88 def on_submoduleUrlButton_clicked(self): |
|
89 """ |
|
90 Private slot to display a directory selection dialog. |
|
91 """ |
|
92 directory = E5FileDialog.getExistingDirectory( |
|
93 self, |
|
94 self.tr("Select Submodule Repository Directory"), |
|
95 self.submoduleUrlCombo.currentText(), |
|
96 E5FileDialog.Options(E5FileDialog.ShowDirsOnly)) |
|
97 |
|
98 if directory: |
|
99 self.submoduleUrlCombo.setEditText( |
|
100 Utilities.toNativeSeparators(directory)) |
|
101 |
|
102 @pyqtSlot() |
|
103 def on_submoduleUrlClearHistoryButton_clicked(self): |
|
104 """ |
|
105 Private slot to clear the history of entered repository URLs. |
|
106 """ |
|
107 currentUrl = self.submoduleUrlCombo.currentText() |
|
108 self.submoduleUrlCombo.clear() |
|
109 self.submoduleUrlCombo.setEditText(currentUrl) |
|
110 |
|
111 self.__saveHistory() |
|
112 |
|
113 @pyqtSlot() |
|
114 def on_submoduleDirButton_clicked(self): |
|
115 """ |
|
116 Private slot to display a directory selection dialog. |
|
117 """ |
|
118 directory = E5FileDialog.getExistingDirectory( |
|
119 self, |
|
120 self.tr("Select Submodule Directory"), |
|
121 self.submoduleDirEdit.text(), |
|
122 E5FileDialog.Options(E5FileDialog.ShowDirsOnly)) |
|
123 |
|
124 if directory: |
|
125 self.submoduleDirEdit.setText( |
|
126 Utilities.toNativeSeparators(directory)) |
|
127 |
|
128 def getData(self): |
|
129 """ |
|
130 Public method to get the entered data. |
|
131 |
|
132 @return tuple containing the repository URL, optional branch name, |
|
133 optional logical name, optional submodule path and a flag |
|
134 indicating to enforce the operation |
|
135 @rtype tuple of (str, str, str, str, bool) |
|
136 """ |
|
137 self.__saveHistory() |
|
138 |
|
139 url = QUrl.fromUserInput( |
|
140 self.submoduleUrlCombo.currentText().replace("\\", "/")) |
|
141 path = self.submoduleDirEdit.text() |
|
142 if path: |
|
143 path = self.__getRelativePath(path) |
|
144 |
|
145 return ( |
|
146 url.toString(QUrl.None_), |
|
147 self.branchEdit.text(), |
|
148 self.nameEdit.text(), |
|
149 path, |
|
150 self.forceCheckBox.isChecked(), |
|
151 ) |
|
152 |
|
153 def __saveHistory(self): |
|
154 """ |
|
155 Private method to save the repository URL history. |
|
156 """ |
|
157 url = self.submoduleUrlCombo.currentText() |
|
158 submoduleUrlHistory = [] |
|
159 for index in range(self.submoduleUrlCombo.count()): |
|
160 submoduleUrlHistory.append(self.submoduleUrlCombo.itemText(index)) |
|
161 if url not in submoduleUrlHistory: |
|
162 submoduleUrlHistory.insert(0, url) |
|
163 |
|
164 # max. list sizes is hard coded to 20 entries |
|
165 newSubmoduleUrlHistory = [url for url in submoduleUrlHistory if url] |
|
166 if len(newSubmoduleUrlHistory) > 20: |
|
167 newSubmoduleUrlHistory = newSubmoduleUrlHistory[:20] |
|
168 |
|
169 self.__vcs.getPlugin().setPreferences( |
|
170 "RepositoryUrlHistory", newSubmoduleUrlHistory) |
|
171 |
|
172 def __getRelativePath(self, path): |
|
173 """ |
|
174 Private method to convert a file path to a relative path. |
|
175 |
|
176 @param path file or directory name to convert |
|
177 @type str |
|
178 @return relative path or unchanged path, if path doesn't |
|
179 belong to the project |
|
180 @rtype str |
|
181 """ |
|
182 if path == self.__repodir: |
|
183 return "" |
|
184 elif Utilities.normcasepath(Utilities.toNativeSeparators(path))\ |
|
185 .startswith(Utilities.normcasepath( |
|
186 Utilities.toNativeSeparators(self.__repodir + "/"))): |
|
187 relpath = path[len(self.__repodir):] |
|
188 if relpath.startswith(("/", "\\")): |
|
189 relpath = relpath[1:] |
|
190 return relpath |
|
191 else: |
|
192 return path |