widgets: pylint tweaks, docstrings and use queued connections
[git-cola.git] / cola / widgets / submodules.py
blobb83d1af061653ac090723e5ef30a668179624773
1 """Provides widgets related to submodules"""
2 from __future__ import absolute_import, division, print_function, unicode_literals
4 from qtpy import QtWidgets
5 from qtpy.QtCore import Qt
6 from qtpy.QtCore import Signal
8 from .. import cmds
9 from .. import compat
10 from .. import core
11 from .. import qtutils
12 from .. import icons
13 from ..i18n import N_
14 from ..widgets import defs
15 from ..widgets import standard
16 from ..widgets import text
19 def add_submodule(context, parent):
20 """Add a new submodule"""
21 dlg = AddSubmodule(parent)
22 dlg.show()
23 if dlg.exec_() == standard.Dialog.Accepted:
24 cmd = dlg.get(context)
25 cmd.do()
28 class SubmodulesWidget(QtWidgets.QFrame):
29 def __init__(self, context, parent):
30 super(SubmodulesWidget, self).__init__(parent)
31 self.context = context
32 self.setToolTip(N_('Submodules'))
34 self.tree = SubmodulesTreeWidget(context, parent=self)
35 self.setFocusProxy(self.tree)
37 self.main_layout = qtutils.vbox(defs.no_margin, defs.spacing, self.tree)
38 self.setLayout(self.main_layout)
40 # Titlebar buttons
41 self.add_button = qtutils.create_action_button(
42 tooltip=N_('Add Submodule'), icon=icons.add()
44 self.refresh_button = qtutils.create_action_button(
45 tooltip=N_('Refresh'), icon=icons.sync()
48 self.open_parent_button = qtutils.create_action_button(
49 tooltip=N_('Open Parent'), icon=icons.repo()
52 self.button_layout = qtutils.hbox(
53 defs.no_margin,
54 defs.spacing,
55 self.add_button,
56 self.open_parent_button,
57 self.refresh_button,
59 self.corner_widget = QtWidgets.QWidget(self)
60 self.corner_widget.setLayout(self.button_layout)
61 titlebar = parent.titleBarWidget()
62 titlebar.add_corner_widget(self.corner_widget)
64 # Connections
65 qtutils.connect_button(self.add_button, self.add_submodule)
66 qtutils.connect_button(self.refresh_button, self.tree.update_model.emit)
67 qtutils.connect_button(
68 self.open_parent_button, cmds.run(cmds.OpenParentRepo, context)
71 def add_submodule(self):
72 add_submodule(self.context, self)
75 class AddSubmodule(standard.Dialog):
76 """Add a new submodule"""
78 def __init__(self, parent):
79 super(AddSubmodule, self).__init__(parent=parent)
81 hint = N_('git://git.example.com/repo.git')
82 tooltip = N_('Submodule URL (can be relative, ex: ../repo.git)')
83 self.url_text = text.HintedDefaultLineEdit(hint, tooltip=tooltip, parent=self)
85 hint = N_('path/to/submodule')
86 tooltip = N_('Submodule path within the current repository (optional)')
87 self.path_text = text.HintedDefaultLineEdit(hint, tooltip=tooltip, parent=self)
89 hint = N_('Branch name')
90 tooltip = N_('Submodule branch to track (optional)')
91 self.branch_text = text.HintedDefaultLineEdit(
92 hint, tooltip=tooltip, parent=self
95 self.depth_spinbox = standard.SpinBox(
96 mini=0, maxi=compat.maxint, value=0, parent=self
98 self.depth_spinbox.setToolTip(
99 N_(
100 'Create a shallow clone with history truncated to the '
101 'specified number of revisions. 0 performs a full clone.'
105 hint = N_('Reference URL')
106 tooltip = N_('Reference repository to use when cloning (optional)')
107 self.reference_text = text.HintedDefaultLineEdit(
108 hint, tooltip=tooltip, parent=self
111 self.add_button = qtutils.ok_button(N_('Add Submodule'), enabled=False)
112 self.close_button = qtutils.close_button()
114 self.form_layout = qtutils.form(
115 defs.no_margin,
116 defs.button_spacing,
117 (N_('URL'), self.url_text),
118 (N_('Path'), self.path_text),
119 (N_('Branch'), self.branch_text),
120 (N_('Depth'), self.depth_spinbox),
121 (N_('Reference Repository'), self.reference_text),
123 self.button_layout = qtutils.hbox(
124 defs.no_margin,
125 defs.button_spacing,
126 qtutils.STRETCH,
127 self.close_button,
128 self.add_button,
131 self.main_layout = qtutils.vbox(
132 defs.large_margin, defs.spacing, self.form_layout, self.button_layout
134 self.setLayout(self.main_layout)
135 self.init_size(parent=qtutils.active_window())
136 # pylint: disable=no-member
137 self.url_text.textChanged.connect(lambda x: self._update_widgets())
138 qtutils.connect_button(self.add_button, self.accept)
139 qtutils.connect_button(self.close_button, self.close)
141 def _update_widgets(self):
142 value = self.url_text.value()
143 self.add_button.setEnabled(bool(value))
145 def get(self, context):
146 return cmds.SubmoduleAdd(
147 context,
148 self.url_text.value(),
149 self.path_text.value(),
150 self.branch_text.value(),
151 self.depth_spinbox.value(),
152 self.reference_text.value(),
156 # pylint: disable=too-many-ancestors
157 class SubmodulesTreeWidget(standard.TreeWidget):
158 update_model = Signal()
160 def __init__(self, context, parent=None):
161 standard.TreeWidget.__init__(self, parent=parent)
163 model = context.model
164 self.context = context
166 self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
167 self.setHeaderHidden(True)
168 # UI
169 self._active = False
170 self.list_helper = BuildItem()
171 # Connections
172 # pylint: disable=no-member
173 self.itemDoubleClicked.connect(self.tree_double_clicked)
174 model.submodules_changed.connect(self.refresh, type=Qt.QueuedConnection)
175 self.update_model.connect(
176 model.update_submodules_list, type=Qt.QueuedConnection
179 def refresh(self):
180 if not self._active:
181 return
182 submodules = self.context.model.submodules_list
183 items = [self.list_helper.get(entry) for entry in submodules]
184 self.clear()
185 if items:
186 self.addTopLevelItems(items)
188 def showEvent(self, event):
189 """Defer updating widgets until the widget is visible"""
190 if not self._active:
191 self._active = True
192 self.update_model.emit()
193 return super(SubmodulesTreeWidget, self).showEvent(event)
195 def tree_double_clicked(self, item, _column):
196 path = core.abspath(item.path)
197 cmds.do(cmds.OpenRepo, self.context, path)
200 class BuildItem(object):
201 def __init__(self):
202 self.state_folder_map = {}
203 self.state_folder_map[''] = icons.folder()
204 self.state_folder_map['+'] = icons.staged()
205 self.state_folder_map['-'] = icons.modified()
206 self.state_folder_map['U'] = icons.merge()
208 def get(self, entry):
209 """entry: same as returned from list_submodule"""
210 name = entry[2]
211 path = entry[2]
212 # TODO better tip
213 tip = path + '\n' + entry[1]
214 if entry[3]:
215 tip += '\n({0})'.format(entry[3])
216 icon = self.state_folder_map[entry[0]]
217 return SubmodulesTreeWidgetItem(name, path, tip, icon)
220 class SubmodulesTreeWidgetItem(QtWidgets.QTreeWidgetItem):
221 def __init__(self, name, path, tip, icon):
222 QtWidgets.QTreeWidgetItem.__init__(self)
223 self.path = path
225 self.setIcon(0, icon)
226 self.setText(0, name)
227 self.setToolTip(0, tip)