views: add a BranchCompareView class
[git-cola.git] / cola / qtutils.py
blob99ffd915767a02d829d0c1db82fc3b3610a21d67
1 # Copyright (c) 2008 David Aguilar
2 import os
3 from PyQt4 import QtCore
4 from PyQt4 import QtGui
5 from PyQt4.QtCore import Qt
6 from PyQt4.QtGui import QClipboard
7 from PyQt4.QtGui import QFileDialog
8 from PyQt4.QtGui import QIcon
9 from PyQt4.QtGui import QTreeWidget
10 from PyQt4.QtGui import QListWidgetItem
11 from PyQt4.QtGui import QMessageBox
13 from cola import utils
15 LOGGER = None
17 def log(output, quiet=True, doraise=False):
18 if not LOGGER:
19 return
20 LOGGER.log(output)
21 if quiet:
22 return
23 LOGGER.show()
24 if not doraise:
25 return
26 raise_logger()
28 def raise_logger():
29 LOGGER.raise_()
31 def input(msg, title=None):
32 if title is None:
33 title = msg
34 parent = QtGui.qApp.activeWindow()
35 result = QtGui.QInputDialog.getText(parent, msg, title)
36 return (unicode(result[0]), result[1])
38 def close_log_window():
39 LOGGER.hide()
40 LOGGER.done(0)
42 def show_output(output, **kwargs):
43 if not output: return
44 log(output, quiet=False)
46 def toggle_log_window():
47 if not LOGGER: return
48 if LOGGER.isVisible():
49 LOGGER.hide()
50 else:
51 LOGGER.show()
52 LOGGER.raise_()
54 def create_listwidget_item(text, filename):
55 icon = QIcon(filename)
56 item = QListWidgetItem()
57 item.setIcon(icon)
58 item.setText(text)
59 return item
61 def information(title, message=None):
62 """Launches a QMessageBox information with the
63 provided title and message."""
64 if message is None:
65 message = title
66 QMessageBox.information(QtGui.qApp.activeWindow(), title, message)
68 def get_selected_treeitem(tree_widget):
69 """Returns a(id_number, is_selected) for a QTreeWidget."""
70 id_number = None
71 selected = False
72 item = tree_widget.currentItem()
73 if item:
74 id_number = item.data(0, Qt.UserRole).toInt()[0]
75 selected = True
76 return(id_number, selected)
78 def get_selected_row(list_widget):
79 """Returns a(row_number, is_selected) tuple for a QListWidget."""
80 row = list_widget.currentRow()
81 item = list_widget.item(row)
82 selected = item is not None and item.isSelected()
83 return(row, selected)
85 def get_selection_list(listwidget, items):
86 """Returns an array of model items that correspond to
87 the selected QListWidget indices."""
88 selected = []
89 itemcount = listwidget.count()
90 widgetitems = [ listwidget.item(idx) for idx in range(itemcount) ]
92 for item, widgetitem in zip(items, widgetitems):
93 if widgetitem.isSelected():
94 selected.append(item)
95 return selected
97 def get_selected_item(list_widget, items):
98 row, selected = get_selected_row(list_widget)
99 if selected and row < len(items):
100 return items[row]
101 else:
102 return None
104 def open_dialog(parent, title, filename=None):
105 qstr = QFileDialog.getOpenFileName(parent, parent.tr(title), filename)
106 return unicode(qstr)
108 def opendir_dialog(parent, title, directory):
109 flags = QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks
110 qstr = QFileDialog.getExistingDirectory(parent, parent.tr(title),
111 directory,
112 flags)
113 return unicode(qstr)
115 def save_dialog(parent, title, filename=''):
116 return unicode(QFileDialog.getSaveFileName(parent,
117 parent.tr(title),
118 filename))
120 def new_dir_dialog(parent, title, filename=''):
121 return unicode(QFileDialog.getSaveFileName(parent,
122 parent.tr(title),
123 filename,
124 os.getcwd(),
125 parent.tr('New Directory ()')))
127 def dir_dialog(parent, title, directory):
128 directory = QFileDialog.getExistingDirectory(parent, parent.tr(title), directory)
129 return unicode(directory)
131 def get_icon(filename):
132 icon = utils.get_icon(filename)
133 return QIcon(icon)
135 def question(parent, title, message, default=True):
136 """Launches a QMessageBox question with the provided title and message.
137 Passing "default=False" will make "No" the default choice."""
138 yes = QMessageBox.Yes
139 no = QMessageBox.No
140 buttons = yes | no
141 if default:
142 default = yes
143 else:
144 default = no
145 result = QMessageBox.question(parent, title, message, buttons, default)
146 return result == QMessageBox.Yes
148 def set_clipboard(text):
149 QtGui.qApp.clipboard().setText(text, QClipboard.Clipboard)
150 QtGui.qApp.clipboard().setText(text, QClipboard.Selection)
152 def set_selected_item(widget, idx):
153 if type(widget) is QTreeWidget:
154 item = widget.topLevelItem(idx)
155 if item:
156 widget.setItemSelected(item, True)
157 widget.setCurrentItem(item)
159 def add_items(widget, items):
160 for item in items: widget.addItem(item)
162 def set_items(widget, items):
163 widget.clear()
164 add_items(widget, items)
166 def tr(txt):
167 return unicode(QtGui.qApp.translate('', txt))
169 def create_item(filename, staged, untracked=False):
170 """Given a filename, return a QListWidgetItem suitable
171 for adding to a QListWidget. "staged" and "untracked"
172 controls whether to use the appropriate icons."""
173 if staged:
174 if os.path.exists(filename.encode('utf-8')):
175 icon_file = utils.get_icon('staged.png')
176 else:
177 icon_file = utils.get_icon('removed.png')
178 elif untracked:
179 icon_file = utils.get_icon('untracked.png')
180 else:
181 icon_file = utils.get_file_icon(filename)
182 return create_listwidget_item(filename, icon_file)
184 def create_txt_item(txt):
185 item = QListWidgetItem()
186 item.setText(txt)
187 return item
189 def update_listwidget(widget, items, staged=True,
190 untracked=False, append=False):
191 """Populate a QListWidget with custom icon items."""
192 if not append: widget.clear()
193 add_items(widget, [ create_item(i, staged, untracked) for i in items ])
195 def set_listwidget_strings(widget, items):
196 widget.clear()
197 add_items(widget, [ create_txt_item(i) for i in items ])