fetch: add support for the traditional FETCH_HEAD behavior
[git-cola.git] / cola / widgets / action.py
blobce91a60e1d1ccfa8f1e9184a1bcdb5e977658bc2
1 """Actions widget"""
2 from functools import partial
4 from qtpy import QtCore
5 from qtpy import QtWidgets
7 from .. import cmds
8 from .. import qtutils
9 from ..i18n import N_
10 from ..widgets import defs
11 from ..widgets import remote
12 from ..widgets import stash
13 from ..qtutils import create_button
14 from ..qtutils import connect_button
17 class QFlowLayoutWidget(QtWidgets.QFrame):
18 _horizontal = QtWidgets.QBoxLayout.LeftToRight
19 _vertical = QtWidgets.QBoxLayout.TopToBottom
21 def __init__(self, parent):
22 QtWidgets.QFrame.__init__(self, parent)
23 self._direction = self._vertical
24 self._layout = layout = QtWidgets.QBoxLayout(self._direction)
25 layout.setSpacing(defs.spacing)
26 qtutils.set_margin(layout, defs.margin)
27 self.setLayout(layout)
28 policy = QtWidgets.QSizePolicy(
29 QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum
31 self.setSizePolicy(policy)
32 self.setMinimumSize(QtCore.QSize(10, 10))
33 self.aspect_ratio = 0.8
35 def resizeEvent(self, event):
36 size = event.size()
37 if size.width() * self.aspect_ratio < size.height():
38 dxn = self._vertical
39 else:
40 dxn = self._horizontal
42 if dxn != self._direction:
43 self._direction = dxn
44 self.layout().setDirection(dxn)
47 def tooltip_button(text, layout, tooltip=''):
48 """Convenience wrapper around qtutils.create_button()"""
49 return create_button(text, layout=layout, tooltip=tooltip)
52 class ActionButtons(QFlowLayoutWidget):
53 def __init__(self, context, parent=None):
54 QFlowLayoutWidget.__init__(self, parent)
55 layout = self.layout()
56 self.context = context
57 self.stage_button = tooltip_button(
58 N_('Stage'), layout, tooltip=N_('Stage changes using "git add"')
60 self.unstage_button = tooltip_button(
61 N_('Unstage'), layout, tooltip=N_('Unstage changes using "git reset"')
63 self.refresh_button = tooltip_button(N_('Refresh'), layout)
64 self.fetch_button = tooltip_button(
65 N_('Fetch...'),
66 layout,
67 tooltip=N_('Fetch from one or more remotes using "git fetch"'),
69 self.push_button = tooltip_button(
70 N_('Push...'), layout, N_('Push to one or more remotes using "git push"')
72 self.pull_button = tooltip_button(
73 N_('Pull...'), layout, tooltip=N_('Integrate changes using "git pull"')
75 self.stash_button = tooltip_button(
76 N_('Stash...'),
77 layout,
78 tooltip=N_('Temporarily stash away uncommitted changes using "git stash"'),
80 self.exit_diff_mode_button = tooltip_button(
81 N_('Exit Diff'), layout, tooltip=N_('Exit Diff mode')
83 self.exit_diff_mode_button.setVisible(False)
84 self.aspect_ratio = 0.4
85 layout.addStretch()
86 self.setMinimumHeight(30)
88 # Add callbacks
89 connect_button(self.refresh_button, cmds.run(cmds.Refresh, context))
90 connect_button(self.fetch_button, partial(remote.fetch, context))
91 connect_button(self.push_button, partial(remote.push, context))
92 connect_button(self.pull_button, partial(remote.pull, context))
93 connect_button(self.stash_button, partial(stash.view, context))
94 connect_button(self.stage_button, cmds.run(cmds.StageSelected, context))
95 connect_button(self.exit_diff_mode_button, cmds.run(cmds.ResetMode, context))
96 connect_button(self.unstage_button, self.unstage)
98 def unstage(self):
99 """Unstage selected files, or all files if no selection exists."""
100 context = self.context
101 paths = context.selection.staged
102 if not paths:
103 cmds.do(cmds.UnstageAll, context)
104 else:
105 cmds.do(cmds.Unstage, context, paths)
107 def set_mode(self, mode):
108 """Respond to changes to the diff mode"""
109 diff_mode = mode == self.context.model.mode_diff
110 self.exit_diff_mode_button.setVisible(diff_mode)