1 """Provides widgets related to submodules"""
3 from qtpy
import QtWidgets
4 from qtpy
.QtCore
import Qt
5 from qtpy
.QtCore
import Signal
10 from .. import qtutils
13 from ..widgets
import defs
14 from ..widgets
import standard
15 from ..widgets
import text
18 def add_submodule(context
, parent
):
19 """Add a new submodule"""
20 dlg
= AddSubmodule(parent
)
22 if dlg
.exec_() == standard
.Dialog
.Accepted
:
23 cmd
= dlg
.get(context
)
27 class SubmodulesWidget(QtWidgets
.QFrame
):
28 def __init__(self
, context
, parent
):
29 super().__init
__(parent
)
30 self
.context
= context
31 self
.setToolTip(N_('Submodules'))
33 self
.tree
= SubmodulesTreeWidget(context
, parent
=self
)
34 self
.setFocusProxy(self
.tree
)
36 self
.main_layout
= qtutils
.vbox(defs
.no_margin
, defs
.spacing
, self
.tree
)
37 self
.setLayout(self
.main_layout
)
40 self
.add_button
= qtutils
.create_action_button(
41 tooltip
=N_('Add Submodule'), icon
=icons
.add()
43 self
.refresh_button
= qtutils
.create_action_button(
44 tooltip
=N_('Refresh'), icon
=icons
.sync()
47 self
.open_parent_button
= qtutils
.create_action_button(
48 tooltip
=N_('Open Parent'), icon
=icons
.repo()
51 self
.button_layout
= qtutils
.hbox(
55 self
.open_parent_button
,
58 self
.corner_widget
= QtWidgets
.QWidget(self
)
59 self
.corner_widget
.setLayout(self
.button_layout
)
60 titlebar
= parent
.titleBarWidget()
61 titlebar
.add_corner_widget(self
.corner_widget
)
64 qtutils
.connect_button(self
.add_button
, self
.add_submodule
)
65 qtutils
.connect_button(self
.refresh_button
, self
.tree
.update_model
.emit
)
66 qtutils
.connect_button(
67 self
.open_parent_button
, cmds
.run(cmds
.OpenParentRepo
, context
)
70 def add_submodule(self
):
71 add_submodule(self
.context
, self
)
74 class AddSubmodule(standard
.Dialog
):
75 """Add a new submodule"""
77 def __init__(self
, parent
):
78 super().__init
__(parent
=parent
)
80 hint
= N_('git://git.example.com/repo.git')
81 tooltip
= N_('Submodule URL (can be relative, ex: ../repo.git)')
82 self
.url_text
= text
.HintedDefaultLineEdit(hint
, tooltip
=tooltip
, parent
=self
)
84 hint
= N_('path/to/submodule')
85 tooltip
= N_('Submodule path within the current repository (optional)')
86 self
.path_text
= text
.HintedDefaultLineEdit(hint
, tooltip
=tooltip
, parent
=self
)
88 hint
= N_('Branch name')
89 tooltip
= N_('Submodule branch to track (optional)')
90 self
.branch_text
= text
.HintedDefaultLineEdit(
91 hint
, tooltip
=tooltip
, parent
=self
94 self
.depth_spinbox
= standard
.SpinBox(
95 mini
=0, maxi
=compat
.maxint
, value
=0, parent
=self
97 self
.depth_spinbox
.setToolTip(
99 'Create a shallow clone with history truncated to the '
100 'specified number of revisions. 0 performs a full clone.'
104 hint
= N_('Reference URL')
105 tooltip
= N_('Reference repository to use when cloning (optional)')
106 self
.reference_text
= text
.HintedDefaultLineEdit(
107 hint
, tooltip
=tooltip
, parent
=self
110 self
.add_button
= qtutils
.ok_button(N_('Add Submodule'), enabled
=False)
111 self
.close_button
= qtutils
.close_button()
113 self
.form_layout
= qtutils
.form(
116 (N_('URL'), self
.url_text
),
117 (N_('Path'), self
.path_text
),
118 (N_('Branch'), self
.branch_text
),
119 (N_('Depth'), self
.depth_spinbox
),
120 (N_('Reference Repository'), self
.reference_text
),
122 self
.button_layout
= qtutils
.hbox(
130 self
.main_layout
= qtutils
.vbox(
131 defs
.large_margin
, defs
.spacing
, self
.form_layout
, self
.button_layout
133 self
.setLayout(self
.main_layout
)
134 self
.init_size(parent
=qtutils
.active_window())
136 self
.url_text
.textChanged
.connect(lambda x
: self
._update
_widgets
())
137 qtutils
.connect_button(self
.add_button
, self
.accept
)
138 qtutils
.connect_button(self
.close_button
, self
.close
)
140 def _update_widgets(self
):
141 value
= self
.url_text
.value()
142 self
.add_button
.setEnabled(bool(value
))
144 def get(self
, context
):
145 return cmds
.SubmoduleAdd(
147 self
.url_text
.value(),
148 self
.path_text
.value(),
149 self
.branch_text
.value(),
150 self
.depth_spinbox
.value(),
151 self
.reference_text
.value(),
155 class SubmodulesTreeWidget(standard
.TreeWidget
):
156 update_model
= Signal()
158 def __init__(self
, context
, parent
=None):
159 standard
.TreeWidget
.__init
__(self
, parent
=parent
)
161 model
= context
.model
162 self
.context
= context
164 self
.setSelectionMode(QtWidgets
.QAbstractItemView
.SingleSelection
)
165 self
.setHeaderHidden(True)
168 self
.list_helper
= BuildItem()
170 self
.itemDoubleClicked
.connect(self
.tree_double_clicked
)
171 model
.submodules_changed
.connect(self
.refresh
, type=Qt
.QueuedConnection
)
172 self
.update_model
.connect(
173 model
.update_submodules_list
, type=Qt
.QueuedConnection
179 submodules
= self
.context
.model
.submodules_list
180 items
= [self
.list_helper
.get(entry
) for entry
in submodules
]
183 self
.addTopLevelItems(items
)
185 def showEvent(self
, event
):
186 """Defer updating widgets until the widget is visible"""
189 self
.update_model
.emit()
190 return super().showEvent(event
)
192 def tree_double_clicked(self
, item
, _column
):
193 path
= core
.abspath(item
.path
)
194 cmds
.do(cmds
.OpenRepo
, self
.context
, path
)
199 self
.state_folder_map
= {}
200 self
.state_folder_map
[''] = icons
.folder()
201 self
.state_folder_map
['+'] = icons
.staged()
202 self
.state_folder_map
['-'] = icons
.modified()
203 self
.state_folder_map
['U'] = icons
.merge()
205 def get(self
, entry
):
206 """entry: same as returned from list_submodule"""
209 tip
= path
+ '\n' + entry
[1]
211 tip
+= f
'\n({entry[3]})'
212 icon
= self
.state_folder_map
[entry
[0]]
213 return SubmodulesTreeWidgetItem(name
, path
, tip
, icon
)
216 class SubmodulesTreeWidgetItem(QtWidgets
.QTreeWidgetItem
):
217 def __init__(self
, name
, path
, tip
, icon
):
218 QtWidgets
.QTreeWidgetItem
.__init
__(self
)
221 self
.setIcon(0, icon
)
222 self
.setText(0, name
)
223 self
.setToolTip(0, tip
)