win32: Avoid super for portability reasons
[git-cola.git] / cola / widgets / standard.py
blobd02dbb3c1f8011029c555f14e033847187a3cc78
1 from PyQt4 import QtGui
2 from PyQt4 import QtCore
3 from PyQt4.QtCore import Qt
4 from PyQt4.QtCore import SIGNAL
7 def create_widget_class(Base):
8 class Widget(Base):
9 # Mix-in for standard view operations
10 def __init__(self, parent=None):
11 super(Widget, self).__init__(parent)
13 def show(self):
14 """Automatically centers dialogs"""
15 if self.parent():
16 left = self.parent().x()
17 width = self.parent().width()
18 center_x = left + width/2
20 x = center_x - self.width()/2
21 y = self.parent().y()
23 self.move(x, y)
24 # Call the base Qt show()
25 Base.show(self)
27 def name(self):
28 """Returns the name of the view class"""
29 return self.__class__.__name__.lower()
31 def apply_state(self, state):
32 """Imports data for view save/restore"""
33 try:
34 self.resize(state['width'], state['height'])
35 except:
36 pass
37 try:
38 self.move(state['x'], state['y'])
39 except:
40 pass
42 def export_state(self):
43 """Exports data for view save/restore"""
44 return {
45 'x': self.x(),
46 'y': self.y(),
47 'width': self.width(),
48 'height': self.height(),
51 return Widget
54 def create_tree_class(Base):
55 class Tree(Base):
56 def __init__(self, parent=None):
57 super(Tree, self).__init__(parent)
58 self.setAlternatingRowColors(True)
59 self.setUniformRowHeights(True)
60 self.setAllColumnsShowFocus(True)
61 self.setAnimated(True)
63 def key_pressed(self):
64 pass
66 def keyPressEvent(self, event):
67 """
68 Override keyPressEvent to allow LeftArrow to work on non-directories.
70 When LeftArrow is pressed on a file entry or an unexpanded directory,
71 then move the current index to the parent directory.
73 This simplifies navigation using the keyboard.
74 For power-users, we support Vim keybindings ;-P
76 """
77 # Check whether the item is expanded before calling the base class
78 # keyPressEvent otherwise we end up collapsing and changing the
79 # current index in one shot, which we don't want to do.
80 index = self.currentIndex()
81 was_expanded = self.isExpanded(index)
82 was_collapsed = not was_expanded
84 # Vim keybindings...
85 # Rewrite the event before marshalling to QTreeView.event()
86 key = event.key()
88 # Remap 'H' to 'Left'
89 if key == Qt.Key_H:
90 event = QtGui.QKeyEvent(event.type(),
91 Qt.Key_Left,
92 event.modifiers())
93 # Remap 'J' to 'Down'
94 elif key == Qt.Key_J:
95 event = QtGui.QKeyEvent(event.type(),
96 Qt.Key_Down,
97 event.modifiers())
98 # Remap 'K' to 'Up'
99 elif key == Qt.Key_K:
100 event = QtGui.QKeyEvent(event.type(),
101 Qt.Key_Up,
102 event.modifiers())
103 # Remap 'L' to 'Right'
104 elif key == Qt.Key_L:
105 event = QtGui.QKeyEvent(event.type(),
106 Qt.Key_Right,
107 event.modifiers())
109 # Re-read the event key to take the remappings into account
110 key = event.key()
112 result = super(Tree, self).keyPressEvent(event)
114 # Let others hook in here before we change the indexes
115 self.emit(SIGNAL('indexAboutToChange()'))
117 # Try to select the first item if the model index is invalid
118 if not index.isValid():
119 index = self.model().index(0, 0, QtCore.QModelIndex())
120 if index.isValid():
121 self.setCurrentIndex(index)
123 # Automatically select the first entry when expanding a directory
124 elif (key == Qt.Key_Right and was_collapsed and
125 self.isExpanded(index)):
126 index = self.moveCursor(self.MoveDown, event.modifiers())
127 self.setCurrentIndex(index)
129 # Process non-root entries with valid parents only.
130 elif key == Qt.Key_Left and index.parent().isValid():
132 # File entries have rowCount() == 0
133 if self.model().itemFromIndex(index).rowCount() == 0:
134 self.setCurrentIndex(index.parent())
136 # Otherwise, do this for collapsed directories only
137 elif was_collapsed:
138 self.setCurrentIndex(index.parent())
140 return result
141 # Tree is a closure over "Base"
142 return Tree
145 Widget = create_widget_class(QtGui.QWidget)
146 Dialog = create_widget_class(QtGui.QDialog)
147 MainWindow = create_widget_class(QtGui.QMainWindow)
149 TreeView = create_tree_class(QtGui.QTreeView)
150 TreeWidget = create_tree_class(QtGui.QTreeWidget)