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
15 def log(output
, quiet
=True, doraise
=False):
20 if not doraise
: return
23 def close_log_window():
27 def show_output(output
):
29 log(output
, quiet
=False)
31 def toggle_log_window():
33 if LOGGER
.isVisible():
39 def create_listwidget_item(text
, filename
):
40 icon
= QIcon(filename
)
41 item
= QListWidgetItem()
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()
58 def get_selection_list(listwidget
, items
):
59 '''Returns an array of model items that correspond to
60 the selected QListWidget indices.'''
62 itemcount
= listwidget
.count()
63 widgetitems
= [ listwidget
.item(idx
) for idx
in range(itemcount
) ]
65 for item
, widgetitem
in zip(items
, widgetitems
):
66 if widgetitem
.isSelected():
70 def get_selected_item(list_widget
, items
):
71 row
, selected
= get_selected_row(list_widget
)
72 if selected
and row
< len(items
):
77 def open_dialog(parent
, title
, filename
=None):
78 qstr
= QFileDialog
.getOpenFileName(
79 parent
, parent
.tr(title
), filename
)
82 def save_dialog(parent
, title
, filename
=None):
83 qstr
= QFileDialog
.getSaveFileName(
84 parent
, parent
.tr(title
), filename
)
87 def dir_dialog(parent
, title
, directory
):
88 directory
= QFileDialog
.getExistingDirectory(
89 parent
, title
, directory
)
90 return unicode(directory
)
92 def get_qicon(filename
):
93 icon
= utils
.get_icon(filename
)
96 def question(parent
, title
, message
, default
=True):
97 '''Launches a QMessageBox question with the provided title and message.
98 Passing "default=False" will make "No" the default choice.'''
106 result
= QMessageBox
.question(parent
,
107 title
, message
, buttons
, default
)
108 return result
== QMessageBox
.Yes
110 def set_clipboard(text
):
111 QtGui
.qApp
.clipboard().setText(text
, QClipboard
.Clipboard
)
112 QtGui
.qApp
.clipboard().setText(text
, QClipboard
.Selection
)
114 def add_items(widget
, items
):
115 for item
in items
: widget
.addItem(item
)
117 def set_items(widget
, items
):
119 add_items(widget
, items
)
122 trtext
= unicode(QtGui
.qApp
.tr(txt
))
123 if trtext
.endswith('@@verb'):
124 trtext
= trtext
.replace('@@verb','')
125 if trtext
.endswith('@@noun'):
126 trtext
= trtext
.replace('@@noun','')
129 def create_item(filename
, staged
, untracked
=False):
130 '''Given a filename, return a QListWidgetItem suitable
131 for adding to a QListWidget. "staged" and "untracked"
132 controls whether to use the appropriate icons.'''
134 if os
.path
.exists(filename
):
135 icon_file
= utils
.get_icon('staged.png')
137 icon_file
= utils
.get_icon('removed.png')
139 icon_file
= utils
.get_icon('untracked.png')
141 icon_file
= utils
.get_file_icon(filename
)
142 return create_listwidget_item(filename
, icon_file
)
144 def update_listwidget(widget
, items
, staged
=True,
145 untracked
=False, append
=False):
146 '''Populate a QListWidget with custom icon items.'''
147 if not append
: widget
.clear()
148 add_items( widget
, [ create_item(i
, staged
, untracked
) for i
in items
])