gui: refactor code so that all generated code lives in the cola.gui namespace
[git-cola.git] / cola / views / main.py
blob07f75737d07a81515d9c612412b90198537dd3f4
1 from PyQt4.QtCore import Qt
2 from PyQt4.QtCore import SIGNAL
3 from PyQt4.QtGui import QMainWindow
5 from cola import qtutils
6 from cola import syntax
7 from cola.syntax import DiffSyntaxHighlighter
8 from cola.gui.main import Ui_main
10 def CreateStandardView(uiclass, qtclass, *classes):
11 """CreateStandardView returns a class closure of uiclass and qtclass.
12 This class performs the standard setup common to all view classes."""
13 class StandardView(uiclass, qtclass):
14 def __init__(self, parent=None, *args, **kwargs):
15 qtclass.__init__(self, parent)
16 uiclass.__init__(self)
17 self.parent_view = parent
18 syntax.set_theme_properties(self)
19 self.setupUi(self)
20 self.init(parent, *args, **kwargs)
21 for cls in classes:
22 cls.init(self, parent, *args, **kwargs)
23 def init(self, parent, *args, **kwargs):
24 pass
25 def get_properties(self):
26 # user-definable color properties
27 props = {}
28 for name in syntax.default_colors:
29 props[name] = getattr(self, '_'+name)
30 return props
31 def reset_syntax(self):
32 if hasattr(self, 'syntax') and self.syntax:
33 self.syntax.set_colors(self.get_properties())
34 self.syntax.reset()
35 syntax.install_theme_properties(StandardView)
36 return StandardView
38 class View(CreateStandardView(Ui_main, QMainWindow)):
39 """The main cola interface."""
40 def init(self, parent=None):
41 self.staged.setAlternatingRowColors(True)
42 self.unstaged.setAlternatingRowColors(True)
43 self.set_display = self.display_text.setText
44 self.amend_is_checked = self.amend_radio.isChecked
45 self.action_undo = self.commitmsg.undo
46 self.action_redo = self.commitmsg.redo
47 self.action_paste = self.commitmsg.paste
48 self.action_select_all = self.commitmsg.selectAll
50 # Qt does not support noun/verbs
51 self.commit_button.setText(qtutils.tr('Commit@@verb'))
52 self.commit_menu.setTitle(qtutils.tr('Commit@@verb'))
54 self.tabifyDockWidget(self.diff_dock, self.editor_dock)
56 # Default to creating a new commit(i.e. not an amend commit)
57 self.new_commit_radio.setChecked(True)
58 self.toolbar_show_log =\
59 self.toolbar.addAction(qtutils.get_qicon('git.png'),
60 'Show/Hide Log Window')
61 self.toolbar_show_log.setEnabled(True)
63 # Diff/patch syntax highlighter
64 self.syntax = DiffSyntaxHighlighter(self.display_text.document())
66 # Handle the vertical checkbox action
67 self.connect(self.vertical_checkbox,
68 SIGNAL('clicked(bool)'),
69 self.handle_vertical_checkbox)
71 # Display the current column
72 self.connect(self.commitmsg,
73 SIGNAL('cursorPositionChanged()'),
74 self.show_current_column)
76 # Initialize the GUI to show 'Column: 00'
77 self.show_current_column()
79 def handle_vertical_checkbox(self, checked):
80 if checked:
81 self.splitter.setOrientation(Qt.Vertical)
82 else:
83 self.splitter.setOrientation(Qt.Horizontal)
85 def set_info(self, txt):
86 try:
87 translated = self.tr(unicode(txt))
88 except:
89 translated = unicode(txt)
90 self.statusBar().showMessage(translated)
91 def show_editor(self):
92 self.editor_dock.raise_()
93 def show_diff(self):
94 self.diff_dock.raise_()
96 def action_cut(self):
97 self.action_copy()
98 self.action_delete()
99 def action_copy(self):
100 cursor = self.commitmsg.textCursor()
101 selection = cursor.selection().toPlainText()
102 qtutils.set_clipboard(selection)
103 def action_delete(self):
104 self.commitmsg.textCursor().removeSelectedText()
105 def reset_checkboxes(self):
106 self.new_commit_radio.setChecked(True)
107 self.amend_radio.setChecked(False)
108 def reset_display(self):
109 self.set_display('')
110 self.set_info('')
111 def copy_display(self):
112 cursor = self.display_text.textCursor()
113 selection = cursor.selection().toPlainText()
114 qtutils.set_clipboard(selection)
115 def diff_selection(self):
116 cursor = self.display_text.textCursor()
117 offset = cursor.position()
118 selection = cursor.selection().toPlainText()
119 return offset, selection
120 def selected_line(self):
121 cursor = self.display_text.textCursor()
122 offset = cursor.position()
123 contents = unicode(self.display_text.toPlainText())
124 while (offset >= 1
125 and contents[offset-1]
126 and contents[offset-1] != '\n'):
127 offset -= 1
128 data = contents[offset:]
129 if '\n' in data:
130 line, rest = data.split('\n', 1)
131 else:
132 line = data
133 return line
134 def display(self, text):
135 self.set_display(text)
136 self.diff_dock.raise_()
137 def show_current_column(self):
138 cursor = self.commitmsg.textCursor()
139 colnum = cursor.columnNumber()
140 self.column_label.setText('Column: %02d' % colnum)