Plugins/VcsPlugins/vcsGit/GitPushDialog.py

changeset 6020
baf6da1ae288
child 6048
82ad8ec9548c
equal deleted inserted replaced
6019:58ecdaf0b789 6020:baf6da1ae288
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to enter data for a Push operation.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot, Qt
13 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QComboBox
14
15 from .Ui_GitPushDialog import Ui_GitPushDialog
16
17
18 class GitPushDialog(QDialog, Ui_GitPushDialog):
19 """
20 Class implementing a dialog to enter data for a Push operation.
21 """
22 PushColumn = 0
23 LocalBranchColumn = 1
24 RemoteBranchColumn = 2
25 ForceColumn = 3
26
27 def __init__(self, vcs, repodir, parent=None):
28 """
29 Constructor
30
31 @param vcs reference to the git object
32 @param repodir directory name of the local repository (string)
33 @param parent reference to the parent widget (QWidget)
34 """
35 super(GitPushDialog, self).__init__(parent)
36 self.setupUi(self)
37
38 self.__vcs = vcs
39 self.__repodir = repodir
40
41 remoteUrlsList = self.__vcs.gitGetRemoteUrlsList(self.__repodir)
42 self.__repos = {name: url for name, url in remoteUrlsList}
43
44 remoteBranches = self.__vcs.gitGetBranchesList(self.__repodir,
45 remotes=True)
46 self.__remotes = {}
47 for remoteBranch in remoteBranches:
48 repo, branch = remoteBranch.rsplit("/", 2)[-2:]
49 if repo not in self.__remotes:
50 self.__remotes[repo] = []
51 self.__remotes[repo].append(branch)
52
53 self.__localBranches = self.__vcs.gitGetBranchesList(self.__repodir,
54 withMaster=True)
55
56 self.remotesComboBox.addItems(sorted(self.__repos.keys()))
57
58 for localBranch in self.__localBranches:
59 itm = QTreeWidgetItem(self.branchesTree, ["", localBranch, "", ""])
60 combo = QComboBox()
61 combo.setEditable(True)
62 combo.setSizeAdjustPolicy(QComboBox.AdjustToContents)
63 self.branchesTree.setItemWidget(
64 itm, GitPushDialog.RemoteBranchColumn, combo)
65
66 self.__resizeColumns()
67
68 self.branchesTree.header().setSortIndicator(
69 GitPushDialog.LocalBranchColumn, Qt.AscendingOrder)
70
71 self.forceWarningLabel.setVisible(False)
72
73 index = self.remotesComboBox.findText("origin")
74 if index == -1:
75 index = 0
76 self.remotesComboBox.setCurrentIndex(index)
77
78 def __resizeColumns(self):
79 """
80 Private slot to adjust the column sizes.
81 """
82 for col in range(self.branchesTree.columnCount()):
83 self.branchesTree.resizeColumnToContents(col)
84
85 @pyqtSlot(str)
86 def on_remotesComboBox_currentTextChanged(self, txt):
87 """
88 Private slot to handle changes of the selected repository.
89
90 @param txt current text of the combo box (string)
91 """
92 self.remoteEdit.setText(self.__repos[txt])
93
94 for row in range(self.branchesTree.topLevelItemCount()):
95 itm = self.branchesTree.topLevelItem(row)
96 localBranch = itm.text(GitPushDialog.LocalBranchColumn)
97 combo = self.branchesTree.itemWidget(
98 itm, GitPushDialog.RemoteBranchColumn)
99 combo.clear()
100 combo.addItems([""] + sorted(self.__remotes[txt]))
101 index = combo.findText(localBranch)
102 if index != -1:
103 combo.setCurrentIndex(index)
104 itm.setCheckState(GitPushDialog.PushColumn, Qt.Checked)
105 else:
106 itm.setCheckState(GitPushDialog.PushColumn, Qt.Unchecked)
107 itm.setCheckState(GitPushDialog.ForceColumn, Qt.Unchecked)
108
109 self.__resizeColumns()
110
111 @pyqtSlot(QTreeWidgetItem, int)
112 def on_branchesTree_itemChanged(self, item, column):
113 """
114 Private slot handling changes of a branch item.
115
116 @param item reference to the changed item (QTreeWidgetItem)
117 @param column changed column (integer)
118 """
119 if column == GitPushDialog.PushColumn:
120 # step 1: set the item's remote branch, if it is empty
121 if item.checkState(GitPushDialog.PushColumn) == Qt.Checked and \
122 (self.branchesTree.itemWidget(
123 item,
124 GitPushDialog.RemoteBranchColumn)
125 .currentText() == ""):
126 self.branchesTree.itemWidget(
127 item, GitPushDialog.RemoteBranchColumn).setEditText(
128 item.text(GitPushDialog.LocalBranchColumn))
129
130 # step 2: count checked items
131 checkedItemsCount = 0
132 for row in range(self.branchesTree.topLevelItemCount()):
133 itm = self.branchesTree.topLevelItem(row)
134 if itm.checkState(GitPushDialog.PushColumn) == Qt.Checked:
135 checkedItemsCount += 1
136 if checkedItemsCount == len(self.__localBranches):
137 self.selectAllCheckBox.setCheckState(Qt.Checked)
138 elif checkedItemsCount == 0:
139 self.selectAllCheckBox.setCheckState(Qt.Unchecked)
140 else:
141 self.selectAllCheckBox.setCheckState(Qt.PartiallyChecked)
142
143 elif column == GitPushDialog.ForceColumn:
144 forceItemsCount = 0
145 for row in range(self.branchesTree.topLevelItemCount()):
146 itm = self.branchesTree.topLevelItem(row)
147 if itm.checkState(GitPushDialog.ForceColumn) == Qt.Checked:
148 forceItemsCount += 1
149 self.forceWarningLabel.setVisible(forceItemsCount > 0)
150
151 @pyqtSlot(int)
152 def on_selectAllCheckBox_stateChanged(self, state):
153 """
154 Private slot to select/deselect all branch items.
155
156 @param state check state of the check box (Qt.CheckState)
157 """
158 if state != Qt.PartiallyChecked:
159 for row in range(self.branchesTree.topLevelItemCount()):
160 itm = self.branchesTree.topLevelItem(row)
161 if itm.checkState(GitPushDialog.PushColumn) != state:
162 itm.setCheckState(GitPushDialog.PushColumn, state)
163
164 def getData(self):
165 """
166 Public method to get the entered data.
167
168 @return remote name, list of branches to be pushed,
169 a flag indicating to push tags as well, a flag indicating
170 to set tracking information and the push method for submodules
171 @rtype tuple of (str, list of str, bool, bool, str)
172 """
173 refspecs = []
174 for row in range(self.branchesTree.topLevelItemCount()):
175 itm = self.branchesTree.topLevelItem(row)
176 force = itm.checkState(GitPushDialog.ForceColumn) == Qt.Checked
177 if itm.checkState(GitPushDialog.PushColumn) == Qt.Checked:
178 localBranch = itm.text(GitPushDialog.LocalBranchColumn)
179 remoteBranch = self.branchesTree.itemWidget(
180 itm, GitPushDialog.RemoteBranchColumn).currentText()
181 refspecs.append("{0}{1}:{2}".format(
182 "+" if force else "", localBranch, remoteBranch))
183
184 # submodule stuff (--recurse-submodules=)
185 if self.submodulesOnDemandButton.isChecked():
186 submodulesPush = "on-demand"
187 elif self.submodulesCheckButton.isChecked():
188 submodulesPush = "check"
189 elif self.submodulesOnlyButton.isChecked():
190 submodulesPush = "only"
191 else:
192 submodulesPush = "no"
193
194 return (
195 self.remotesComboBox.currentText(),
196 refspecs,
197 self.tagsCheckBox.isChecked(),
198 self.trackingCheckBox.isChecked(),
199 submodulesPush,
200 )

eric ide

mercurial