Merged branchmenu into master
[ugit.git] / py / qtutils.py
blob65308e9f55f890711726125a7967fa9f07f7535e
1 from PyQt4 import QtGui
2 from PyQt4.QtGui import QClipboard
3 from PyQt4.QtGui import QIcon
4 from PyQt4.QtGui import QListWidgetItem
5 from PyQt4.QtGui import QMessageBox
6 from PyQt4.QtGui import QPixmap
7 from views import GitCommandDialog
9 def create_listwidget_item (text, filename):
10 icon = QIcon (QPixmap (filename))
11 item = QListWidgetItem()
12 item.setIcon (icon)
13 item.setText (text)
14 return item
16 def information (parent, title, message):
17 '''Launches a QMessageBox information with the
18 provided title and message.'''
19 QMessageBox.information(parent, title, message)
21 def get_selected_row (list_widget):
22 '''Returns a (row_number, is_selected) tuple for a QListWidget.'''
23 row = list_widget.currentRow()
24 item = list_widget.item (row)
25 selected = item is not None and item.isSelected()
26 return (row, selected)
28 def get_selection_from_list (list_widget, items):
29 '''Returns an array of model items that correspond to
30 the selected QListWidget indices.'''
31 selected = []
32 for idx in range (list_widget.count()):
33 item = list_widget.item (idx)
34 if item.isSelected():
35 selected.append (items[idx])
36 return selected
39 def question (parent, title, message, default=True):
40 '''Launches a QMessageBox question with the provided title and message.
41 Passing "default=False" will make "No" the default choice.'''
42 yes = QMessageBox.Yes
43 no = QMessageBox.No
44 buttons = yes | no
46 if default:
47 default = yes
48 else:
49 default = no
51 result = QMessageBox.question (parent,
52 title, message, buttons, default)
53 return result == QMessageBox.Yes
55 def set_clipboard (text):
56 QtGui.qApp.clipboard().setText (text, QClipboard.Clipboard)
57 QtGui.qApp.clipboard().setText (text, QClipboard.Selection)
59 def show_command_output (parent, output):
60 dialog = GitCommandDialog (parent, output=output)
61 dialog.show()
62 dialog.exec_()