Always close the logging window on exit
[ugit.git] / ugitlibs / qtutils.py
blob195e4f13cca47d65b067189741325f96a69a9a6c
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 import views
11 import utils
13 LOGGER = None
15 def log(output, quiet=True, doraise=False):
16 if not LOGGER: return
17 LOGGER.log(output)
18 if quiet: return
19 LOGGER.show()
20 if not doraise: return
21 LOGGER.raise_()
23 def close_log_window():
24 LOGGER.hide()
25 LOGGER.done(0)
27 def show_output(output):
28 if not output: return
29 log(output, quiet=False)
31 def toggle_log_window():
32 if not LOGGER: return
33 if LOGGER.isVisible():
34 LOGGER.hide()
35 else:
36 LOGGER.show()
37 LOGGER.raise_()
39 def create_listwidget_item(text, filename):
40 icon = QIcon(filename)
41 item = QListWidgetItem()
42 item.setIcon(icon)
43 item.setText(text)
44 return item
46 def information(parent, title, message):
47 '''Launches a QMessageBox information with the
48 provided title and message.'''
49 QMessageBox.information(parent, title, message)
51 def get_selected_row(list_widget):
52 '''Returns a(row_number, is_selected) tuple for a QListWidget.'''
53 row = list_widget.currentRow()
54 item = list_widget.item(row)
55 selected = item is not None and item.isSelected()
56 return(row, selected)
58 def get_selection_list(list_widget, items):
59 '''Returns an array of model items that correspond to
60 the selected QListWidget indices.'''
61 selected = []
62 for idx in range(list_widget.count()):
63 item = list_widget.item(idx)
64 if item.isSelected():
65 selected.append(items[idx])
66 return selected
68 def get_selected_item(list_widget, items):
69 selected = get_selection_list(list_widget, items)
70 if not selected: return None
71 return selected[0]
73 def open_dialog(parent, title, filename=None):
74 qstr = QFileDialog.getOpenFileName(
75 parent, parent.tr(title), filename)
76 return unicode(qstr)
78 def save_dialog(parent, title, filename=None):
79 qstr = QFileDialog.getSaveFileName(
80 parent, parent.tr(title), filename)
81 return unicode(qstr)
83 def dir_dialog(parent, title, directory):
84 directory = QFileDialog.getExistingDirectory(
85 parent, title, directory)
86 return unicode(directory)
88 def get_qicon(filename):
89 icon = utils.get_icon(filename)
90 return QIcon(icon)
92 def question(parent, title, message, default=True):
93 '''Launches a QMessageBox question with the provided title and message.
94 Passing "default=False" will make "No" the default choice.'''
95 yes = QMessageBox.Yes
96 no = QMessageBox.No
97 buttons = yes | no
98 if default:
99 default = yes
100 else:
101 default = no
102 result = QMessageBox.question(parent,
103 title, message, buttons, default)
104 return result == QMessageBox.Yes
106 def set_clipboard(text):
107 QtGui.qApp.clipboard().setText(text, QClipboard.Clipboard)
108 QtGui.qApp.clipboard().setText(text, QClipboard.Selection)
110 def add_items(widget, items):
111 for item in items: widget.addItem(item)
113 def set_items(widget, items):
114 widget.clear()
115 add_items(widget, items)
117 def tr(txt):
118 trtext = unicode(QtGui.qApp.tr(txt))
119 if trtext.endswith('@@verb'):
120 trtext = trtext.replace('@@verb','')
121 if trtext.endswith('@@noun'):
122 trtext = trtext.replace('@@noun','')
123 return trtext
125 def create_item(filename, staged, untracked=False):
126 '''Given a filename, return a QListWidgetItem suitable
127 for adding to a QListWidget. "staged" controls whether
128 to use icons for the staged or unstaged list widget.'''
129 if staged:
130 if os.path.exists(filename):
131 icon_file = utils.get_icon('staged.png')
132 else:
133 icon_file = utils.get_icon('removed.png')
134 elif untracked:
135 icon_file = utils.get_icon('untracked.png')
136 else:
137 icon_file = utils.get_file_icon(filename)
138 return create_listwidget_item(filename, icon_file)
140 def update_listwidget(widget, items, staged=True,
141 untracked=False, append=False):
142 '''Populate a QListWidget with custom icon items.'''
143 if not append: widget.clear()
144 add_items( widget, [ create_item(i, staged, untracked) for i in items ])