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