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
14 def log(output
, quiet
=True, doraise
=False):
19 if not doraise
: return
22 def input(msg
, title
=None):
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():
33 def show_output(output
):
35 log(output
, quiet
=False)
37 def toggle_log_window():
39 if LOGGER
.isVisible():
45 def create_listwidget_item(text
, filename
):
46 icon
= QIcon(filename
)
47 item
= QListWidgetItem()
52 def information(title
, message
=None):
53 '''Launches a QMessageBox information with the
54 provided title and message.'''
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()
66 def get_selection_list(listwidget
, items
):
67 '''Returns an array of model items that correspond to
68 the selected QListWidget indices.'''
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():
78 def get_selected_item(list_widget
, items
):
79 row
, selected
= get_selected_row(list_widget
)
80 if selected
and row
< len(items
):
85 def open_dialog(parent
, title
, filename
=None):
86 qstr
= QFileDialog
.getOpenFileName(
87 parent
, parent
.tr(title
), filename
)
90 def opendir_dialog(parent
, title
, directory
):
91 qstr
= QFileDialog
.getExistingDirectory(
92 parent
, directory
, parent
.tr(title
),
93 QtGui
.QFileDialog
.ShowDirsOnly | QtGui
.QFileDialog
.DontResolveSymlinks
)
96 def save_dialog(parent
, title
, filename
=None):
97 qstr
= QFileDialog
.getSaveFileName(
98 parent
, parent
.tr(title
), filename
)
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
)
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
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
):
133 add_items(widget
, items
)
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','')
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.'''
148 if os
.path
.exists(filename
):
149 icon_file
= utils
.get_icon('staged.png')
151 icon_file
= utils
.get_icon('removed.png')
153 icon_file
= utils
.get_icon('untracked.png')
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()
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
):
171 add_items( widget
, [ create_txt_item(i
) for i
in items
])