dag: rename `text` to `rev_text`
[git-cola.git] / cola / widgets / action.py
blob4331a7e8bc1cc23d4cabe38611f7f99fea9e7dbf
1 """Actions widget"""
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 from functools import partial
5 from qtpy import QtCore
6 from qtpy import QtWidgets
8 from .. import cmds
9 from .. import qtutils
10 from ..i18n import N_
11 from ..widgets import defs
12 from ..widgets import remote
13 from ..widgets import stash
14 from ..qtutils import create_button
15 from ..qtutils import connect_button
18 class QFlowLayoutWidget(QtWidgets.QFrame):
20 _horizontal = QtWidgets.QBoxLayout.LeftToRight
21 _vertical = QtWidgets.QBoxLayout.TopToBottom
23 def __init__(self, parent):
24 QtWidgets.QFrame.__init__(self, parent)
25 self._direction = self._vertical
26 self._layout = layout = QtWidgets.QBoxLayout(self._direction)
27 layout.setSpacing(defs.spacing)
28 qtutils.set_margin(layout, defs.margin)
29 self.setLayout(layout)
30 policy = QtWidgets.QSizePolicy(
31 QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum
33 self.setSizePolicy(policy)
34 self.setMinimumSize(QtCore.QSize(10, 10))
35 self.aspect_ratio = 0.8
37 def resizeEvent(self, event):
38 size = event.size()
39 if size.width() * self.aspect_ratio < size.height():
40 dxn = self._vertical
41 else:
42 dxn = self._horizontal
44 if dxn != self._direction:
45 self._direction = dxn
46 self.layout().setDirection(dxn)
49 def tooltip_button(text, layout):
50 button = create_button(text, layout=layout)
51 button.setToolTip(text)
52 return button
55 class ActionButtons(QFlowLayoutWidget):
56 def __init__(self, context, parent=None):
57 QFlowLayoutWidget.__init__(self, parent)
58 layout = self.layout()
59 self.context = context
60 self.stage_button = tooltip_button(N_('Stage'), layout)
61 self.unstage_button = tooltip_button(N_('Unstage'), layout)
62 self.refresh_button = tooltip_button(N_('Refresh'), layout)
63 self.fetch_button = tooltip_button(N_('Fetch...'), layout)
64 self.push_button = tooltip_button(N_('Push...'), layout)
65 self.pull_button = tooltip_button(N_('Pull...'), layout)
66 self.stash_button = tooltip_button(N_('Stash...'), layout)
67 self.exit_diff_mode_button = tooltip_button(N_('Exit Diff'), layout)
68 self.exit_diff_mode_button.setToolTip(N_('Exit Diff mode'))
69 self.exit_diff_mode_button.setVisible(False)
70 self.aspect_ratio = 0.4
71 layout.addStretch()
72 self.setMinimumHeight(30)
74 # Add callbacks
75 connect_button(self.refresh_button, cmds.run(cmds.Refresh, context))
76 connect_button(self.fetch_button, partial(remote.fetch, context))
77 connect_button(self.push_button, partial(remote.push, context))
78 connect_button(self.pull_button, partial(remote.pull, context))
79 connect_button(self.stash_button, partial(stash.view, context))
80 connect_button(self.stage_button, cmds.run(cmds.StageSelected, context))
81 connect_button(self.exit_diff_mode_button, cmds.run(cmds.ResetMode, context))
82 connect_button(self.unstage_button, self.unstage)
84 def unstage(self):
85 """Unstage selected files, or all files if no selection exists."""
86 context = self.context
87 paths = context.selection.staged
88 context = self.context
89 if not paths:
90 cmds.do(cmds.UnstageAll, context)
91 else:
92 cmds.do(cmds.Unstage, context, paths)
94 def set_mode(self, mode):
95 """Respond to changes to the diff mode"""
96 diff_mode = mode == self.context.model.mode_diff
97 self.exit_diff_mode_button.setVisible(diff_mode)