cola: include GitPython to make installation simpler for users
[git-cola.git] / cola / qtutils.py
blob471264cab8b19cc3a3b93df517be965b267f41ec
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 cola 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 raise_logger()
22 def raise_logger():
23 LOGGER.raise_()
25 def input(msg, title=None):
26 if title is None:
27 title = msg
28 parent = QtGui.qApp.activeWindow()
29 result = QtGui.QInputDialog.getText(parent, msg, title)
30 return (unicode(result[0]), result[1])
32 def close_log_window():
33 LOGGER.hide()
34 LOGGER.done(0)
36 def show_output(output, **kwargs):
37 if not output: return
38 log(output, quiet=False)
40 def toggle_log_window():
41 if not LOGGER: return
42 if LOGGER.isVisible():
43 LOGGER.hide()
44 else:
45 LOGGER.show()
46 LOGGER.raise_()
48 def create_listwidget_item(text, filename):
49 icon = QIcon(filename)
50 item = QListWidgetItem()
51 item.setIcon(icon)
52 item.setText(text)
53 return item
55 def information(title, message=None):
56 """Launches a QMessageBox information with the
57 provided title and message."""
58 if message is None:
59 message = title
60 QMessageBox.information(QtGui.qApp.activeWindow(), title, message)
62 def get_selected_row(list_widget):
63 """Returns a(row_number, is_selected) tuple for a QListWidget."""
64 row = list_widget.currentRow()
65 item = list_widget.item(row)
66 selected = item is not None and item.isSelected()
67 return(row, selected)
69 def get_selection_list(listwidget, items):
70 """Returns an array of model items that correspond to
71 the selected QListWidget indices."""
72 selected = []
73 itemcount = listwidget.count()
74 widgetitems = [ listwidget.item(idx) for idx in range(itemcount) ]
76 for item, widgetitem in zip(items, widgetitems):
77 if widgetitem.isSelected():
78 selected.append(item)
79 return selected
81 def get_selected_item(list_widget, items):
82 row, selected = get_selected_row(list_widget)
83 if selected and row < len(items):
84 return items[row]
85 else:
86 return None
88 def open_dialog(parent, title, filename=None):
89 qstr = QFileDialog.getOpenFileName(parent, parent.tr(title), filename)
90 return unicode(qstr)
92 def opendir_dialog(parent, title, directory):
93 flags = QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks
94 qstr = QFileDialog.getExistingDirectory(parent, directory,
95 parent.tr(title),
96 flags)
97 return unicode(qstr)
99 def save_dialog(parent, title, filename=None):
100 qstr = QFileDialog.getSaveFileName(parent, parent.tr(title), filename)
101 return unicode(qstr)
103 def dir_dialog(parent, title, directory):
104 directory = QFileDialog.getExistingDirectory(parent, title, directory)
105 return unicode(directory)
107 def get_qicon(filename):
108 icon = utils.get_icon(filename)
109 return QIcon(icon)
111 def question(parent, title, message, default=True):
112 """Launches a QMessageBox question with the provided title and message.
113 Passing "default=False" will make "No" the default choice."""
114 yes = QMessageBox.Yes
115 no = QMessageBox.No
116 buttons = yes | no
117 if default:
118 default = yes
119 else:
120 default = no
121 result = QMessageBox.question(parent, 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 ])