prefs: apply flake8 suggestions
[git-cola.git] / cola / widgets / action.py
blob7d4db339cf5d4665a286b50bd8db9c128db367d4
1 """The "Actions" widget"""
2 from __future__ import division, absolute_import, unicode_literals
4 from PyQt4 import QtCore
5 from PyQt4 import QtGui
7 from cola import cmds
8 from cola.i18n import N_
9 from cola.models.selection import selection_model
10 from cola.widgets import defs
11 from cola.widgets import remote
12 from cola.widgets import stash
13 from cola.qtutils import create_button
14 from cola.qtutils import connect_button
17 class QFlowLayoutWidget(QtGui.QWidget):
19 _horizontal = QtGui.QBoxLayout.LeftToRight
20 _vertical = QtGui.QBoxLayout.TopToBottom
22 def __init__(self, parent):
23 QtGui.QWidget.__init__(self, parent)
24 self._direction = self._vertical
25 self._layout = layout = QtGui.QBoxLayout(self._direction)
26 layout.setSpacing(defs.spacing)
27 layout.setMargin(defs.margin)
28 self.setLayout(layout)
29 policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum,
30 QtGui.QSizePolicy.Minimum)
31 self.setSizePolicy(policy)
32 self.setMinimumSize(QtCore.QSize(1, 1))
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):
48 button = create_button(text, layout=layout)
49 button.setToolTip(text)
50 return button
53 class ActionButtons(QFlowLayoutWidget):
54 def __init__(self, parent=None):
55 QFlowLayoutWidget.__init__(self, parent)
56 layout = self.layout()
57 self.stage_button = tooltip_button(N_('Stage'), layout)
58 self.unstage_button = tooltip_button(N_('Unstage'), layout)
59 self.refresh_button = tooltip_button(N_('Refresh'), layout)
60 self.fetch_button = tooltip_button(N_('Fetch...'), layout)
61 self.push_button = tooltip_button(N_('Push...'), layout)
62 self.pull_button = tooltip_button(N_('Pull...'), layout)
63 self.stash_button = tooltip_button(N_('Stash...'), layout)
64 self.aspect_ratio = 0.4
65 layout.addStretch()
66 self.setMinimumHeight(30)
68 # Add callbacks
69 connect_button(self.refresh_button, cmds.run(cmds.Refresh))
70 connect_button(self.fetch_button, remote.fetch)
71 connect_button(self.push_button, remote.push)
72 connect_button(self.pull_button, remote.pull)
73 connect_button(self.stash_button, stash.stash)
74 connect_button(self.stage_button, self.stage)
75 connect_button(self.unstage_button, self.unstage)
77 def stage(self):
78 """Stage selected files, or all files if no selection exists."""
79 paths = selection_model().unstaged
80 if not paths:
81 cmds.do(cmds.StageModified)
82 else:
83 cmds.do(cmds.Stage, paths)
85 def unstage(self):
86 """Unstage selected files, or all files if no selection exists."""
87 paths = selection_model().staged
88 if not paths:
89 cmds.do(cmds.UnstageAll)
90 else:
91 cmds.do(cmds.Unstage, paths)