1 # Copyright (c) 2008 David Aguilar
3 from PyQt4
import QtCore
4 from PyQt4
import QtGui
5 from PyQt4
.QtCore
import Qt
6 from PyQt4
.QtGui
import QClipboard
7 from PyQt4
.QtGui
import QFileDialog
8 from PyQt4
.QtGui
import QIcon
9 from PyQt4
.QtGui
import QTreeWidget
10 from PyQt4
.QtGui
import QListWidgetItem
11 from PyQt4
.QtGui
import QMessageBox
13 from cola
import utils
17 def log(output
, quiet
=True, doraise
=False):
31 def input(msg
, title
=None):
34 parent
= QtGui
.qApp
.activeWindow()
35 result
= QtGui
.QInputDialog
.getText(parent
, msg
, title
)
36 return (unicode(result
[0]), result
[1])
38 def close_log_window():
42 def show_output(output
, **kwargs
):
44 log(output
, quiet
=False)
46 def toggle_log_window():
48 if LOGGER
.isVisible():
54 def create_listwidget_item(text
, filename
):
55 icon
= QIcon(filename
)
56 item
= QListWidgetItem()
61 def information(title
, message
=None):
62 """Launches a QMessageBox information with the
63 provided title and message."""
66 QMessageBox
.information(QtGui
.qApp
.activeWindow(), title
, message
)
68 def get_selected_treeitem(tree_widget
):
69 """Returns a(id_number, is_selected) for a QTreeWidget."""
72 item
= tree_widget
.currentItem()
74 id_number
= item
.data(0, Qt
.UserRole
).toInt()[0]
76 return(id_number
, selected
)
78 def get_selected_row(list_widget
):
79 """Returns a(row_number, is_selected) tuple for a QListWidget."""
80 row
= list_widget
.currentRow()
81 item
= list_widget
.item(row
)
82 selected
= item
is not None and item
.isSelected()
85 def get_selection_list(listwidget
, items
):
86 """Returns an array of model items that correspond to
87 the selected QListWidget indices."""
89 itemcount
= listwidget
.count()
90 widgetitems
= [ listwidget
.item(idx
) for idx
in range(itemcount
) ]
92 for item
, widgetitem
in zip(items
, widgetitems
):
93 if widgetitem
.isSelected():
97 def get_selected_item(list_widget
, items
):
98 row
, selected
= get_selected_row(list_widget
)
99 if selected
and row
< len(items
):
104 def open_dialog(parent
, title
, filename
=None):
105 qstr
= QFileDialog
.getOpenFileName(parent
, parent
.tr(title
), filename
)
108 def opendir_dialog(parent
, title
, directory
):
109 flags
= QtGui
.QFileDialog
.ShowDirsOnly | QtGui
.QFileDialog
.DontResolveSymlinks
110 qstr
= QFileDialog
.getExistingDirectory(parent
, parent
.tr(title
),
115 def save_dialog(parent
, title
, filename
=''):
116 return unicode(QFileDialog
.getSaveFileName(parent
,
120 def new_dir_dialog(parent
, title
, filename
=''):
121 return unicode(QFileDialog
.getSaveFileName(parent
,
125 parent
.tr('New Directory ()')))
127 def dir_dialog(parent
, title
, directory
):
128 directory
= QFileDialog
.getExistingDirectory(parent
, parent
.tr(title
), directory
)
129 return unicode(directory
)
131 def get_icon(filename
):
132 icon
= utils
.get_icon(filename
)
135 def question(parent
, title
, message
, default
=True):
136 """Launches a QMessageBox question with the provided title and message.
137 Passing "default=False" will make "No" the default choice."""
138 yes
= QMessageBox
.Yes
145 result
= QMessageBox
.question(parent
, title
, message
, buttons
, default
)
146 return result
== QMessageBox
.Yes
148 def set_clipboard(text
):
149 QtGui
.qApp
.clipboard().setText(text
, QClipboard
.Clipboard
)
150 QtGui
.qApp
.clipboard().setText(text
, QClipboard
.Selection
)
152 def set_selected_item(widget
, idx
):
153 if type(widget
) is QTreeWidget
:
154 item
= widget
.topLevelItem(idx
)
156 widget
.setItemSelected(item
, True)
157 widget
.setCurrentItem(item
)
159 def add_items(widget
, items
):
160 for item
in items
: widget
.addItem(item
)
162 def set_items(widget
, items
):
164 add_items(widget
, items
)
167 return unicode(QtGui
.qApp
.translate('', txt
))
169 def create_item(filename
, staged
, untracked
=False):
170 """Given a filename, return a QListWidgetItem suitable
171 for adding to a QListWidget. "staged" and "untracked"
172 controls whether to use the appropriate icons."""
174 if os
.path
.exists(filename
.encode('utf-8')):
175 icon_file
= utils
.get_icon('staged.png')
177 icon_file
= utils
.get_icon('removed.png')
179 icon_file
= utils
.get_icon('untracked.png')
181 icon_file
= utils
.get_file_icon(filename
)
182 return create_listwidget_item(filename
, icon_file
)
184 def create_txt_item(txt
):
185 item
= QListWidgetItem()
189 def update_listwidget(widget
, items
, staged
=True,
190 untracked
=False, append
=False):
191 """Populate a QListWidget with custom icon items."""
192 if not append
: widget
.clear()
193 add_items(widget
, [ create_item(i
, staged
, untracked
) for i
in items
])
195 def set_listwidget_strings(widget
, items
):
197 add_items(widget
, [ create_txt_item(i
) for i
in items
])