submodules: use better icons
[git-cola.git] / cola / widgets / submodules.py
blobe6a098bf17c854b323d8ccfc4dd43c659742c38e
1 """Provides widgets related to submodules"""
2 from __future__ import absolute_import
4 from qtpy import QtWidgets
5 from qtpy.QtCore import Qt
6 from qtpy.QtCore import Signal
8 from .. import cmds
9 from .. import core
10 from .. import qtutils
11 from .. import icons
12 from ..i18n import N_
13 from ..widgets import defs
14 from ..widgets import standard
17 class SubmodulesWidget(QtWidgets.QWidget):
19 def __init__(self, context, parent):
20 QtWidgets.QWidget.__init__(self, parent)
21 self.setToolTip(N_('Submodules'))
23 self.tree = SubmodulesTreeWidget(context, parent=self)
24 self.setFocusProxy(self.tree)
26 self.main_layout = qtutils.vbox(defs.no_margin, defs.spacing, self.tree)
27 self.setLayout(self.main_layout)
29 # Titlebar buttons
30 self.refresh_button = qtutils.create_action_button(
31 tooltip=N_('Refresh'), icon=icons.sync())
33 self.open_parent_button = qtutils.create_action_button(
34 tooltip=N_('Open Parent'), icon=icons.repo())
36 self.button_layout = qtutils.hbox(defs.no_margin, defs.spacing,
37 self.open_parent_button,
38 self.refresh_button)
39 self.corner_widget = QtWidgets.QWidget(self)
40 self.corner_widget.setLayout(self.button_layout)
41 titlebar = parent.titleBarWidget()
42 titlebar.add_corner_widget(self.corner_widget)
44 # Connections
45 qtutils.connect_button(self.refresh_button, self.tree.refresh_command)
46 qtutils.connect_button(self.open_parent_button,
47 cmds.run(cmds.OpenParentRepo, context))
50 class SubmodulesTreeWidget(standard.TreeWidget):
51 updated = Signal()
53 def __init__(self, context, parent=None):
54 standard.TreeWidget.__init__(self, parent=parent)
56 self.context = context
57 self.main_model = model = context.model
59 self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
60 self.setHeaderHidden(True)
61 # model
62 self.updated.connect(self.refresh, type=Qt.QueuedConnection)
63 model.add_observer(model.message_updated, self.updated.emit)
64 # UI
65 self._active = False
66 self.list_helper = BuildItem()
67 self.itemDoubleClicked.connect(self.tree_double_clicked)
69 def refresh_command(self):
70 # TODO how to monitor changes of submodules?
71 self.main_model._update_submodules_list()
72 self.refresh()
74 def refresh(self):
75 if not self._active:
76 return
78 items = [self.list_helper.get(entry) for entry in
79 self.main_model.submodules_list]
80 self.clear()
81 self.addTopLevelItems(items)
83 def showEvent(self, event):
84 """Defer updating widgets until the widget is visible"""
85 if not self._active:
86 self._active = True
87 self.refresh()
88 return super(SubmodulesTreeWidget, self).showEvent(event)
90 def tree_double_clicked(self, item, _column):
91 path = core.abspath(item.path)
92 cmds.do(cmds.OpenRepo, self.context, path)
95 class BuildItem(object):
97 def __init__(self):
98 self.state_folder_map = {}
99 self.state_folder_map[''] = icons.folder()
100 self.state_folder_map['+'] = icons.staged()
101 self.state_folder_map['-'] = icons.modified()
102 self.state_folder_map['U'] = icons.merge()
104 def get(self, entry):
105 """entry: same as returned from list_submodule"""
106 name = entry[2]
107 path = entry[2]
108 # TODO better tip
109 tip = path + '\n' + entry[1]
110 if entry[3]:
111 tip += '\n({0})'.format(entry[3])
112 icon = self.state_folder_map[entry[0]]
113 return SubmodulesTreeWidgetItem(name, path, tip, icon)
116 class SubmodulesTreeWidgetItem(QtWidgets.QTreeWidgetItem):
118 def __init__(self, name, path, tip, icon):
119 QtWidgets.QTreeWidgetItem.__init__(self)
120 self.path = path
122 self.setIcon(0, icon)
123 self.setText(0, name)
124 self.setToolTip(0, tip)