Death to .ui: Add a 'BranchCompareView' class
[git-cola.git] / cola / views / __init__.py
blobb0e6fa734b77c00ac8c4b455c2550d73becd3097
1 """This module creates simple wrapper classes around the auto-generated
2 .ui classes.
3 """
6 import sys
7 import time
9 from PyQt4 import QtCore
10 from PyQt4 import QtGui
11 from PyQt4.QtGui import QDialog
12 from PyQt4.QtGui import QListWidget
13 from PyQt4.QtGui import qApp
14 from PyQt4.QtCore import SIGNAL
16 from cola import core
17 from cola.views.standard import create_standard_view
18 from cola.views.bookmark import BookmarkView
19 from cola.views.option import OptionsView
20 from cola.views.syntax import DiffSyntaxHighlighter
22 try:
23 from cola.gui.combo import Ui_combo
24 from cola.gui.compare import Ui_compare
25 from cola.gui.createbranch import Ui_createbranch
26 from cola.gui.items import Ui_items
27 from cola.gui.remote import Ui_remote
28 from cola.gui.stash import Ui_stash
29 except ImportError:
30 sys.stderr.write('\nThe cola gui modules have not been built.\n'
31 'Try running "make" in the cola source tree.\n')
32 sys.exit(-1)
35 from cola.views.compare import BranchCompareView
36 CreateBranchView = create_standard_view(Ui_createbranch, QDialog)
37 StashView = create_standard_view(Ui_stash, QDialog)
38 CompareView = create_standard_view(Ui_compare, QDialog)
40 class ItemView(object):
41 def __init__(self, parent, title="", items=[], dblclick=None):
42 self.setWindowTitle(title)
43 self.items_widget.addItems(items)
44 if dblclick and type(self.items_widget) is QListWidget:
45 self.connect(self.items_widget,
46 SIGNAL('itemDoubleClicked(QListWidgetItem*)'),
47 dblclick)
48 def idx(self):
49 return 0
50 def selected(self):
51 geom = qApp.desktop().screenGeometry()
52 width = geom.width()
53 height = geom.height()
54 if self.parent():
55 x = self.parent().x() + self.parent().width()/2 - self.width()/2
56 y = self.parent().y() + self.parent().height()/3 - self.height()/2
57 self.move(x, y)
58 self.show()
59 if self.exec_() == QDialog.Accepted:
60 return self.value()
61 else:
62 return None
64 ComboViewBase = create_standard_view(Ui_combo, QDialog, ItemView)
65 class ComboView(ComboViewBase, ItemView):
66 """A dialog for choosing branches."""
67 def idx(self):
68 return self.items_widget.currentIndex()
69 def value(self):
70 return str(self.items_widget.currentText())
72 ListViewBase = create_standard_view(Ui_items, QDialog, ItemView)
73 class ListView(ListViewBase, ItemView):
74 """A dialog for an item from a list."""
75 def idx(self):
76 return self.items_widget.currentRow()
77 def value(self):
78 item = self.items_widget.currentItem()
79 if not item:
80 return None
81 return str(item.text())
84 RemoteViewBase = create_standard_view(Ui_remote, QDialog)
85 class RemoteView(RemoteViewBase):
86 """Dialog used by Fetch, Push and Pull"""
88 def __init__(self, parent, action):
89 """Customizes the dialog based on the remote action
90 """
91 RemoteViewBase.__init__(self, parent)
92 if action:
93 self.action_button.setText(action.title())
94 self.setWindowTitle(action.title())
95 if action == 'pull':
96 self.tags_checkbox.hide()
97 self.ffwd_only_checkbox.hide()
98 self.local_label.hide()
99 self.local_branch.hide()
100 self.local_branches.hide()
101 if action != 'pull':
102 self.rebase_checkbox.hide()
104 def select_first_remote(self):
105 """Selects the first remote in the list view"""
106 return self.select_remote(0)
108 def select_remote(self, idx):
109 """Selects a remote by index"""
110 item = self.remotes.item(idx)
111 if item:
112 self.remotes.setItemSelected(item, True)
113 self.remotes.setCurrentItem(item)
114 self.remotename.setText(item.text())
115 return True
116 else:
117 return False
119 def select_local_branch(self, idx):
120 """Selects a local branch by index in the list view"""
121 item = self.local_branches.item(idx)
122 if not item:
123 return False
124 self.local_branches.setItemSelected(item, True)
125 self.local_branches.setCurrentItem(item)
126 self.local_branch.setText(item.text())
127 return True