Finished the option gui
[ugit.git] / ugitlibs / qtutils.py
blob8e1befc8762a50864c5572916e85e6e723e4cd84
1 from PyQt4 import QtCore
2 from PyQt4 import QtGui
3 from PyQt4.QtGui import QClipboard
4 from PyQt4.QtGui import QFileDialog
5 from PyQt4.QtGui import QIcon
6 from PyQt4.QtGui import QListWidgetItem
7 from PyQt4.QtGui import QMessageBox
8 from PyQt4.QtGui import QPixmap
10 import views
11 import utils
13 def create_listwidget_item(text, filename):
14 icon = QIcon(QPixmap(filename))
15 item = QListWidgetItem()
16 item.setIcon(icon)
17 item.setText(text)
18 return item
20 def information(parent, title, message):
21 '''Launches a QMessageBox information with the
22 provided title and message.'''
23 QMessageBox.information(parent, title, message)
25 def get_selected_row(list_widget):
26 '''Returns a(row_number, is_selected) tuple for a QListWidget.'''
27 row = list_widget.currentRow()
28 item = list_widget.item(row)
29 selected = item is not None and item.isSelected()
30 return(row, selected)
32 def get_selection_list(list_widget, items):
33 '''Returns an array of model items that correspond to
34 the selected QListWidget indices.'''
35 selected = []
36 for idx in range(list_widget.count()):
37 item = list_widget.item(idx)
38 if item.isSelected():
39 selected.append(items[idx])
40 return selected
42 def get_selected_item(list_widget, items):
43 selected = get_selection_list(list_widget, items)
44 if not selected: return None
45 return selected[0]
47 def open_dialog(parent, title, filename=None):
48 qstr = QFileDialog.getOpenFileName(
49 parent, parent.tr(title), filename)
50 return unicode(qstr)
52 def save_dialog(parent, title, filename=None):
53 qstr = QFileDialog.getSaveFileName(
54 parent, parent.tr(title), filename)
55 return unicode(qstr)
57 def dir_dialog(parent, title, directory):
58 directory = QFileDialog.getExistingDirectory(
59 parent, title, directory)
60 return unicode(directory)
62 def question(parent, title, message, default=True):
63 '''Launches a QMessageBox question with the provided title and message.
64 Passing "default=False" will make "No" the default choice.'''
65 yes = QMessageBox.Yes
66 no = QMessageBox.No
67 buttons = yes | no
68 if default:
69 default = yes
70 else:
71 default = no
72 result = QMessageBox.question(parent,
73 title, message, buttons, default)
74 return result == QMessageBox.Yes
76 def set_clipboard(text):
77 QtGui.qApp.clipboard().setText(text, QClipboard.Clipboard)
78 QtGui.qApp.clipboard().setText(text, QClipboard.Selection)
80 def add_items(widget, items):
81 for item in items: widget.addItem(item)
83 def set_items(widget, items):
84 widget.clear()
85 add_items(widget, items)
87 def show_output(parent, output):
88 if not output: return
89 dialog = views.OutputGUI(parent, output=output)
90 dialog.show()
91 dialog.exec_()
93 def tr(txt):
94 trtext = unicode(QtGui.qApp.tr(txt))
95 if trtext.endswith('@@verb'):
96 trtext = trtext.replace('@@verb','')
97 if trtext.endswith('@@noun'):
98 trtext = trtext.replace('@@noun','')
99 return trtext
101 def create_item(filename, staged, untracked=False):
102 '''Given a filename, return a QListWidgetItem suitable
103 for adding to a QListWidget. "staged" controls whether
104 to use icons for the staged or unstaged list widget.'''
105 if staged:
106 icon_file = utils.get_staged_icon(filename)
107 elif untracked:
108 icon_file = utils.get_untracked_icon()
109 else:
110 icon_file = utils.get_icon(filename)
111 return create_listwidget_item(filename, icon_file)
113 def update_listwidget(widget, items, staged=True,
114 untracked=False, append=False):
115 '''Populate a QListWidget with custom icon items.'''
116 if not append: widget.clear()
117 add_items( widget, [ create_item(i, staged, untracked) for i in items ])