python-git: handle filenames with embedded quotes in their names
[git-cola.git] / ugit / qtutils.py
blobe1d22f3d34db296e11074914c9002fa28c7b87a2
1 import os
2 from PyQt4 import QtCore
3 from PyQt4 import QtGui
4 from PyQt4.QtGui import QClipboard
5 from PyQt4.QtGui import QFileDialog
6 from PyQt4.QtGui import QIcon
7 from PyQt4.QtGui import QListWidgetItem
8 from PyQt4.QtGui import QMessageBox
10 from ugit import utils
12 LOGGER = None
14 def log(output, quiet=True, doraise=False):
15 if not LOGGER: return
16 LOGGER.log(output)
17 if quiet: return
18 LOGGER.show()
19 if not doraise: return
20 LOGGER.raise_()
22 def input(msg, title=None):
23 if title is None:
24 title = msg
25 parent = QtGui.qApp.activeWindow()
26 result = QtGui.QInputDialog.getText(parent, msg, title)
27 return (unicode(result[0]), result[1])
29 def close_log_window():
30 LOGGER.hide()
31 LOGGER.done(0)
33 def show_output(output):
34 if not output: return
35 log(output, quiet=False)
37 def toggle_log_window():
38 if not LOGGER: return
39 if LOGGER.isVisible():
40 LOGGER.hide()
41 else:
42 LOGGER.show()
43 LOGGER.raise_()
45 def create_listwidget_item(text, filename):
46 icon = QIcon(filename)
47 item = QListWidgetItem()
48 item.setIcon(icon)
49 item.setText(text)
50 return item
52 def information(title, message=None):
53 '''Launches a QMessageBox information with the
54 provided title and message.'''
55 if message is None:
56 message = title
57 QMessageBox.information(QtGui.qApp.activeWindow(), title, message)
59 def get_selected_row(list_widget):
60 '''Returns a(row_number, is_selected) tuple for a QListWidget.'''
61 row = list_widget.currentRow()
62 item = list_widget.item(row)
63 selected = item is not None and item.isSelected()
64 return(row, selected)
66 def get_selection_list(listwidget, items):
67 '''Returns an array of model items that correspond to
68 the selected QListWidget indices.'''
69 selected = []
70 itemcount = listwidget.count()
71 widgetitems = [ listwidget.item(idx) for idx in range(itemcount) ]
73 for item, widgetitem in zip(items, widgetitems):
74 if widgetitem.isSelected():
75 selected.append(item)
76 return selected
78 def get_selected_item(list_widget, items):
79 row, selected = get_selected_row(list_widget)
80 if selected and row < len(items):
81 return items[row]
82 else:
83 return None
85 def open_dialog(parent, title, filename=None):
86 qstr = QFileDialog.getOpenFileName(
87 parent, parent.tr(title), filename)
88 return unicode(qstr)
90 def opendir_dialog(parent, title, directory):
91 qstr = QFileDialog.getExistingDirectory(
92 parent, directory, parent.tr(title),
93 QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks)
94 return unicode(qstr)
96 def save_dialog(parent, title, filename=None):
97 qstr = QFileDialog.getSaveFileName(
98 parent, parent.tr(title), filename)
99 return unicode(qstr)
101 def dir_dialog(parent, title, directory):
102 directory = QFileDialog.getExistingDirectory(
103 parent, title, directory)
104 return unicode(directory)
106 def get_qicon(filename):
107 icon = utils.get_icon(filename)
108 return QIcon(icon)
110 def question(parent, title, message, default=True):
111 '''Launches a QMessageBox question with the provided title and message.
112 Passing "default=False" will make "No" the default choice.'''
113 yes = QMessageBox.Yes
114 no = QMessageBox.No
115 buttons = yes | no
116 if default:
117 default = yes
118 else:
119 default = no
120 result = QMessageBox.question(parent,
121 title, message, buttons, default)
122 return result == QMessageBox.Yes
124 def set_clipboard(text):
125 QtGui.qApp.clipboard().setText(text, QClipboard.Clipboard)
126 QtGui.qApp.clipboard().setText(text, QClipboard.Selection)
128 def add_items(widget, items):
129 for item in items: widget.addItem(item)
131 def set_items(widget, items):
132 widget.clear()
133 add_items(widget, items)
135 def tr(txt):
136 trtext = unicode(QtGui.qApp.tr(txt))
137 if trtext.endswith('@@verb'):
138 trtext = trtext.replace('@@verb','')
139 if trtext.endswith('@@noun'):
140 trtext = trtext.replace('@@noun','')
141 return trtext
143 def create_item(filename, staged, untracked=False):
144 '''Given a filename, return a QListWidgetItem suitable
145 for adding to a QListWidget. "staged" and "untracked"
146 controls whether to use the appropriate icons.'''
147 if staged:
148 if os.path.exists(filename):
149 icon_file = utils.get_icon('staged.png')
150 else:
151 icon_file = utils.get_icon('removed.png')
152 elif untracked:
153 icon_file = utils.get_icon('untracked.png')
154 else:
155 icon_file = utils.get_file_icon(filename)
156 return create_listwidget_item(filename, icon_file)
158 def create_txt_item(txt):
159 item = QListWidgetItem()
160 item.setText(txt)
161 return item
163 def update_listwidget(widget, items, staged=True,
164 untracked=False, append=False):
165 '''Populate a QListWidget with custom icon items.'''
166 if not append: widget.clear()
167 add_items( widget, [ create_item(i, staged, untracked) for i in items ])
169 def set_listwidget_strings(widget, items):
170 widget.clear()
171 add_items( widget, [ create_txt_item(i) for i in items ])