|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 - 2022 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 PyQt6.QtCore import pyqtSlot, QUrl |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from EricWidgets.EricCompleters import EricDirCompleter |
|
14 from EricWidgets import EricFileDialog |
|
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 = EricDirCompleter( |
|
56 self.submoduleUrlCombo) |
|
57 self.submoduleDirCompleter = EricDirCompleter( |
|
58 self.submoduleDirEdit) |
|
59 |
|
60 ipath = ( |
|
61 Preferences.getMultiProject("Workspace") or |
|
62 Utilities.getHomeDir() |
|
63 ) |
|
64 self.__initPaths = [ |
|
65 Utilities.fromNativeSeparators(ipath), |
|
66 Utilities.fromNativeSeparators(ipath) + "/", |
|
67 ] |
|
68 |
|
69 self.buttonBox.button( |
|
70 QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
|
71 |
|
72 msh = self.minimumSizeHint() |
|
73 self.resize(max(self.width(), msh.width()), msh.height()) |
|
74 |
|
75 @pyqtSlot(str) |
|
76 def on_submoduleUrlCombo_editTextChanged(self, txt): |
|
77 """ |
|
78 Private slot to handle changes of the submodule repository URL. |
|
79 |
|
80 @param txt current text of the combo box |
|
81 @type str |
|
82 """ |
|
83 enable = False |
|
84 vcsUrlEnable = False |
|
85 |
|
86 if txt: |
|
87 url = QUrl.fromUserInput(txt) |
|
88 if url.isValid(): |
|
89 if url.scheme() in ConfigGitSchemes: |
|
90 enable = True |
|
91 vcsUrlEnable = url.scheme() == "file" |
|
92 elif ':' in txt: |
|
93 # assume scp like repository URL |
|
94 enable = True |
|
95 else: |
|
96 vcsUrlEnable = True |
|
97 |
|
98 self.buttonBox.button( |
|
99 QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
100 self.submoduleUrlButton.setEnabled(vcsUrlEnable) |
|
101 |
|
102 @pyqtSlot() |
|
103 def on_submoduleUrlButton_clicked(self): |
|
104 """ |
|
105 Private slot to display a directory selection dialog. |
|
106 """ |
|
107 directory = EricFileDialog.getExistingDirectory( |
|
108 self, |
|
109 self.tr("Select Submodule Repository Directory"), |
|
110 self.submoduleUrlCombo.currentText(), |
|
111 EricFileDialog.ShowDirsOnly) |
|
112 |
|
113 if directory: |
|
114 self.submoduleUrlCombo.setEditText( |
|
115 Utilities.toNativeSeparators(directory)) |
|
116 |
|
117 @pyqtSlot() |
|
118 def on_submoduleUrlClearHistoryButton_clicked(self): |
|
119 """ |
|
120 Private slot to clear the history of entered repository URLs. |
|
121 """ |
|
122 currentUrl = self.submoduleUrlCombo.currentText() |
|
123 self.submoduleUrlCombo.clear() |
|
124 self.submoduleUrlCombo.setEditText(currentUrl) |
|
125 |
|
126 self.__saveHistory() |
|
127 |
|
128 @pyqtSlot() |
|
129 def on_submoduleDirButton_clicked(self): |
|
130 """ |
|
131 Private slot to display a directory selection dialog. |
|
132 """ |
|
133 directory = EricFileDialog.getExistingDirectory( |
|
134 self, |
|
135 self.tr("Select Submodule Directory"), |
|
136 self.submoduleDirEdit.text(), |
|
137 EricFileDialog.ShowDirsOnly) |
|
138 |
|
139 if directory: |
|
140 self.submoduleDirEdit.setText( |
|
141 Utilities.toNativeSeparators(directory)) |
|
142 |
|
143 def getData(self): |
|
144 """ |
|
145 Public method to get the entered data. |
|
146 |
|
147 @return tuple containing the repository URL, optional branch name, |
|
148 optional logical name, optional submodule path and a flag |
|
149 indicating to enforce the operation |
|
150 @rtype tuple of (str, str, str, str, bool) |
|
151 """ |
|
152 self.__saveHistory() |
|
153 |
|
154 path = self.submoduleDirEdit.text() |
|
155 if path: |
|
156 path = self.__getRelativePath(path) |
|
157 |
|
158 return ( |
|
159 self.submoduleUrlCombo.currentText().replace("\\", "/"), |
|
160 self.branchEdit.text(), |
|
161 self.nameEdit.text(), |
|
162 path, |
|
163 self.forceCheckBox.isChecked(), |
|
164 ) |
|
165 |
|
166 def __saveHistory(self): |
|
167 """ |
|
168 Private method to save the repository URL history. |
|
169 """ |
|
170 url = self.submoduleUrlCombo.currentText() |
|
171 submoduleUrlHistory = [] |
|
172 for index in range(self.submoduleUrlCombo.count()): |
|
173 submoduleUrlHistory.append(self.submoduleUrlCombo.itemText(index)) |
|
174 if url not in submoduleUrlHistory: |
|
175 submoduleUrlHistory.insert(0, url) |
|
176 |
|
177 # max. list sizes is hard coded to 20 entries |
|
178 newSubmoduleUrlHistory = [url for url in submoduleUrlHistory if url] |
|
179 if len(newSubmoduleUrlHistory) > 20: |
|
180 newSubmoduleUrlHistory = newSubmoduleUrlHistory[:20] |
|
181 |
|
182 self.__vcs.getPlugin().setPreferences( |
|
183 "RepositoryUrlHistory", newSubmoduleUrlHistory) |
|
184 |
|
185 def __getRelativePath(self, path): |
|
186 """ |
|
187 Private method to convert a file path to a relative path. |
|
188 |
|
189 @param path file or directory name to convert |
|
190 @type str |
|
191 @return relative path or unchanged path, if path doesn't |
|
192 belong to the project |
|
193 @rtype str |
|
194 """ |
|
195 if path == self.__repodir: |
|
196 return "" |
|
197 elif ( |
|
198 Utilities.normcasepath(Utilities.toNativeSeparators(path)) |
|
199 .startswith(Utilities.normcasepath( |
|
200 Utilities.toNativeSeparators(self.__repodir + "/"))) |
|
201 ): |
|
202 relpath = path[len(self.__repodir):] |
|
203 if relpath.startswith(("/", "\\")): |
|
204 relpath = relpath[1:] |
|
205 return relpath |
|
206 else: |
|
207 return path |