core: make getcwd() fail-safe
[git-cola.git] / cola / widgets / submodules.py
blob76a769609b8b702eec9f7bc01783b816fa3d3e14
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.QFrame):
19 def __init__(self, context, parent):
20 QtWidgets.QFrame.__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,
46 context.model.update_submodules_list)
47 qtutils.connect_button(self.open_parent_button,
48 cmds.run(cmds.OpenParentRepo, context))
51 class SubmodulesTreeWidget(standard.TreeWidget):
52 updated = Signal()
54 def __init__(self, context, parent=None):
55 standard.TreeWidget.__init__(self, parent=parent)
57 self.context = context
58 self.main_model = model = context.model
60 self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
61 self.setHeaderHidden(True)
62 # UI
63 self._active = False
64 self.list_helper = BuildItem()
65 self.itemDoubleClicked.connect(self.tree_double_clicked)
66 # Connections
67 self.updated.connect(self.refresh, type=Qt.QueuedConnection)
68 model.add_observer(model.message_submodules_changed,
69 self.updated.emit)
71 def refresh(self):
72 if not self._active:
73 return
75 items = [self.list_helper.get(entry) for entry in
76 self.main_model.submodules_list]
77 self.clear()
78 self.addTopLevelItems(items)
80 def showEvent(self, event):
81 """Defer updating widgets until the widget is visible"""
82 if not self._active:
83 self._active = True
84 self.refresh()
85 return super(SubmodulesTreeWidget, self).showEvent(event)
87 def tree_double_clicked(self, item, _column):
88 path = core.abspath(item.path)
89 cmds.do(cmds.OpenRepo, self.context, path)
92 class BuildItem(object):
94 def __init__(self):
95 self.state_folder_map = {}
96 self.state_folder_map[''] = icons.folder()
97 self.state_folder_map['+'] = icons.staged()
98 self.state_folder_map['-'] = icons.modified()
99 self.state_folder_map['U'] = icons.merge()
101 def get(self, entry):
102 """entry: same as returned from list_submodule"""
103 name = entry[2]
104 path = entry[2]
105 # TODO better tip
106 tip = path + '\n' + entry[1]
107 if entry[3]:
108 tip += '\n({0})'.format(entry[3])
109 icon = self.state_folder_map[entry[0]]
110 return SubmodulesTreeWidgetItem(name, path, tip, icon)
113 class SubmodulesTreeWidgetItem(QtWidgets.QTreeWidgetItem):
115 def __init__(self, name, path, tip, icon):
116 QtWidgets.QTreeWidgetItem.__init__(self)
117 self.path = path
119 self.setIcon(0, icon)
120 self.setText(0, name)
121 self.setToolTip(0, tip)