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
14 def log(output
, quiet
=True, doraise
=False):
19 if not doraise
: return
25 def input(msg
, title
=None):
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():
36 def show_output(output
, **kwargs
):
38 log(output
, quiet
=False)
40 def toggle_log_window():
42 if LOGGER
.isVisible():
48 def create_listwidget_item(text
, filename
):
49 icon
= QIcon(filename
)
50 item
= QListWidgetItem()
55 def information(title
, message
=None):
56 """Launches a QMessageBox information with the
57 provided title and message."""
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()
69 def get_selection_list(listwidget
, items
):
70 """Returns an array of model items that correspond to
71 the selected QListWidget indices."""
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():
81 def get_selected_item(list_widget
, items
):
82 row
, selected
= get_selected_row(list_widget
)
83 if selected
and row
< len(items
):
88 def open_dialog(parent
, title
, filename
=None):
89 qstr
= QFileDialog
.getOpenFileName(parent
, parent
.tr(title
), filename
)
92 def opendir_dialog(parent
, title
, directory
):
93 flags
= QtGui
.QFileDialog
.ShowDirsOnly | QtGui
.QFileDialog
.DontResolveSymlinks
94 qstr
= QFileDialog
.getExistingDirectory(parent
, directory
,
99 def save_dialog(parent
, title
, filename
=None):
100 qstr
= QFileDialog
.getSaveFileName(parent
, parent
.tr(title
), filename
)
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
)
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
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
):
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
])