Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/dialogs/qsidebar.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qsidebar_p.h" | - | ||||||||||||
35 | #include "qfilesystemmodel.h" | - | ||||||||||||
36 | - | |||||||||||||
37 | #ifndef QT_NO_FILEDIALOG | - | ||||||||||||
38 | - | |||||||||||||
39 | #include <qaction.h> | - | ||||||||||||
40 | #include <qurl.h> | - | ||||||||||||
41 | #include <qmenu.h> | - | ||||||||||||
42 | #include <qmimedata.h> | - | ||||||||||||
43 | #include <qevent.h> | - | ||||||||||||
44 | #include <qdebug.h> | - | ||||||||||||
45 | #include <qfileiconprovider.h> | - | ||||||||||||
46 | #include <qfiledialog.h> | - | ||||||||||||
47 | - | |||||||||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||||||||
49 | - | |||||||||||||
50 | void QSideBarDelegate::initStyleOption(QStyleOptionViewItem *option, | - | ||||||||||||
51 | const QModelIndex &index) const | - | ||||||||||||
52 | { | - | ||||||||||||
53 | QStyledItemDelegate::initStyleOption(option,index); | - | ||||||||||||
54 | QVariant value = index.data(QUrlModel::EnabledRole); | - | ||||||||||||
55 | if (value.isValid()) {
| 0 | ||||||||||||
56 | //If the bookmark/entry is not enabled then we paint it in gray | - | ||||||||||||
57 | if (!qvariant_cast<bool>(value))
| 0 | ||||||||||||
58 | option->state &= ~QStyle::State_Enabled; never executed: option->state &= ~QStyle::State_Enabled; | 0 | ||||||||||||
59 | } never executed: end of block | 0 | ||||||||||||
60 | } never executed: end of block | 0 | ||||||||||||
61 | - | |||||||||||||
62 | /*! | - | ||||||||||||
63 | \internal | - | ||||||||||||
64 | \class QUrlModel | - | ||||||||||||
65 | QUrlModel lets you have indexes from a QFileSystemModel to a list. When QFileSystemModel | - | ||||||||||||
66 | changes them QUrlModel will automatically update. | - | ||||||||||||
67 | - | |||||||||||||
68 | Example usage: File dialog sidebar and combo box | - | ||||||||||||
69 | */ | - | ||||||||||||
70 | QUrlModel::QUrlModel(QObject *parent) : QStandardItemModel(parent), showFullPath(false), fileSystemModel(0) | - | ||||||||||||
71 | { | - | ||||||||||||
72 | } never executed: end of block | 0 | ||||||||||||
73 | - | |||||||||||||
74 | /*! | - | ||||||||||||
75 | \reimp | - | ||||||||||||
76 | */ | - | ||||||||||||
77 | QStringList QUrlModel::mimeTypes() const | - | ||||||||||||
78 | { | - | ||||||||||||
79 | return QStringList(QLatin1String("text/uri-list")); never executed: return QStringList(QLatin1String("text/uri-list")); | 0 | ||||||||||||
80 | } | - | ||||||||||||
81 | - | |||||||||||||
82 | /*! | - | ||||||||||||
83 | \reimp | - | ||||||||||||
84 | */ | - | ||||||||||||
85 | Qt::ItemFlags QUrlModel::flags(const QModelIndex &index) const | - | ||||||||||||
86 | { | - | ||||||||||||
87 | Qt::ItemFlags flags = QStandardItemModel::flags(index); | - | ||||||||||||
88 | if (index.isValid()) {
| 0 | ||||||||||||
89 | flags &= ~Qt::ItemIsEditable; | - | ||||||||||||
90 | // ### some future version could support "moving" urls onto a folder | - | ||||||||||||
91 | flags &= ~Qt::ItemIsDropEnabled; | - | ||||||||||||
92 | } never executed: end of block | 0 | ||||||||||||
93 | - | |||||||||||||
94 | if (index.data(Qt::DecorationRole).isNull())
| 0 | ||||||||||||
95 | flags &= ~Qt::ItemIsEnabled; never executed: flags &= ~Qt::ItemIsEnabled; | 0 | ||||||||||||
96 | - | |||||||||||||
97 | return flags; never executed: return flags; | 0 | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | /*! | - | ||||||||||||
101 | \reimp | - | ||||||||||||
102 | */ | - | ||||||||||||
103 | QMimeData *QUrlModel::mimeData(const QModelIndexList &indexes) const | - | ||||||||||||
104 | { | - | ||||||||||||
105 | QList<QUrl> list; | - | ||||||||||||
106 | for (int i = 0; i < indexes.count(); ++i) {
| 0 | ||||||||||||
107 | if (indexes.at(i).column() == 0)
| 0 | ||||||||||||
108 | list.append(indexes.at(i).data(UrlRole).toUrl()); never executed: list.append(indexes.at(i).data(UrlRole).toUrl()); | 0 | ||||||||||||
109 | } never executed: end of block | 0 | ||||||||||||
110 | QMimeData *data = new QMimeData(); | - | ||||||||||||
111 | data->setUrls(list); | - | ||||||||||||
112 | return data; never executed: return data; | 0 | ||||||||||||
113 | } | - | ||||||||||||
114 | - | |||||||||||||
115 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
116 | - | |||||||||||||
117 | /*! | - | ||||||||||||
118 | Decide based upon the data if it should be accepted or not | - | ||||||||||||
119 | - | |||||||||||||
120 | We only accept dirs and not files | - | ||||||||||||
121 | */ | - | ||||||||||||
122 | bool QUrlModel::canDrop(QDragEnterEvent *event) | - | ||||||||||||
123 | { | - | ||||||||||||
124 | if (!event->mimeData()->formats().contains(mimeTypes().first()))
| 0 | ||||||||||||
125 | return false; never executed: return false; | 0 | ||||||||||||
126 | - | |||||||||||||
127 | const QList<QUrl> list = event->mimeData()->urls(); | - | ||||||||||||
128 | for (int i = 0; i < list.count(); ++i) {
| 0 | ||||||||||||
129 | QModelIndex idx = fileSystemModel->index(list.at(0).toLocalFile()); | - | ||||||||||||
130 | if (!fileSystemModel->isDir(idx))
| 0 | ||||||||||||
131 | return false; never executed: return false; | 0 | ||||||||||||
132 | } never executed: end of block | 0 | ||||||||||||
133 | return true; never executed: return true; | 0 | ||||||||||||
134 | } | - | ||||||||||||
135 | - | |||||||||||||
136 | /*! | - | ||||||||||||
137 | \reimp | - | ||||||||||||
138 | */ | - | ||||||||||||
139 | bool QUrlModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
140 | int row, int column, const QModelIndex &parent) | - | ||||||||||||
141 | { | - | ||||||||||||
142 | if (!data->formats().contains(mimeTypes().first()))
| 0 | ||||||||||||
143 | return false; never executed: return false; | 0 | ||||||||||||
144 | Q_UNUSED(action); | - | ||||||||||||
145 | Q_UNUSED(column); | - | ||||||||||||
146 | Q_UNUSED(parent); | - | ||||||||||||
147 | addUrls(data->urls(), row); | - | ||||||||||||
148 | return true; never executed: return true; | 0 | ||||||||||||
149 | } | - | ||||||||||||
150 | - | |||||||||||||
151 | #endif // QT_NO_DRAGANDDROP | - | ||||||||||||
152 | - | |||||||||||||
153 | /*! | - | ||||||||||||
154 | \reimp | - | ||||||||||||
155 | - | |||||||||||||
156 | If the role is the UrlRole then handle otherwise just pass to QStandardItemModel | - | ||||||||||||
157 | */ | - | ||||||||||||
158 | bool QUrlModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||
159 | { | - | ||||||||||||
160 | if (value.type() == QVariant::Url) {
| 0 | ||||||||||||
161 | QUrl url = value.toUrl(); | - | ||||||||||||
162 | QModelIndex dirIndex = fileSystemModel->index(url.toLocalFile()); | - | ||||||||||||
163 | //On windows the popup display the "C:\", convert to nativeSeparators | - | ||||||||||||
164 | if (showFullPath)
| 0 | ||||||||||||
165 | QStandardItemModel::setData(index, QDir::toNativeSeparators(fileSystemModel->data(dirIndex, QFileSystemModel::FilePathRole).toString())); never executed: QStandardItemModel::setData(index, QDir::toNativeSeparators(fileSystemModel->data(dirIndex, QFileSystemModel::FilePathRole).toString())); | 0 | ||||||||||||
166 | else { | - | ||||||||||||
167 | QStandardItemModel::setData(index, QDir::toNativeSeparators(fileSystemModel->data(dirIndex, QFileSystemModel::FilePathRole).toString()), Qt::ToolTipRole); | - | ||||||||||||
168 | QStandardItemModel::setData(index, fileSystemModel->data(dirIndex).toString()); | - | ||||||||||||
169 | } never executed: end of block | 0 | ||||||||||||
170 | QStandardItemModel::setData(index, fileSystemModel->data(dirIndex, Qt::DecorationRole), | - | ||||||||||||
171 | Qt::DecorationRole); | - | ||||||||||||
172 | QStandardItemModel::setData(index, url, UrlRole); | - | ||||||||||||
173 | return true; never executed: return true; | 0 | ||||||||||||
174 | } | - | ||||||||||||
175 | return QStandardItemModel::setData(index, value, role); never executed: return QStandardItemModel::setData(index, value, role); | 0 | ||||||||||||
176 | } | - | ||||||||||||
177 | - | |||||||||||||
178 | void QUrlModel::setUrl(const QModelIndex &index, const QUrl &url, const QModelIndex &dirIndex) | - | ||||||||||||
179 | { | - | ||||||||||||
180 | setData(index, url, UrlRole); | - | ||||||||||||
181 | if (url.path().isEmpty()) {
| 0 | ||||||||||||
182 | setData(index, fileSystemModel->myComputer()); | - | ||||||||||||
183 | setData(index, fileSystemModel->myComputer(Qt::DecorationRole), Qt::DecorationRole); | - | ||||||||||||
184 | } else { never executed: end of block | 0 | ||||||||||||
185 | QString newName; | - | ||||||||||||
186 | if (showFullPath) {
| 0 | ||||||||||||
187 | //On windows the popup display the "C:\", convert to nativeSeparators | - | ||||||||||||
188 | newName = QDir::toNativeSeparators(dirIndex.data(QFileSystemModel::FilePathRole).toString()); | - | ||||||||||||
189 | } else { never executed: end of block | 0 | ||||||||||||
190 | newName = dirIndex.data().toString(); | - | ||||||||||||
191 | } never executed: end of block | 0 | ||||||||||||
192 | - | |||||||||||||
193 | QIcon newIcon = qvariant_cast<QIcon>(dirIndex.data(Qt::DecorationRole)); | - | ||||||||||||
194 | if (!dirIndex.isValid()) {
| 0 | ||||||||||||
195 | const QFileIconProvider *provider = fileSystemModel->iconProvider(); | - | ||||||||||||
196 | if (provider)
| 0 | ||||||||||||
197 | newIcon = provider->icon(QFileIconProvider::Folder); never executed: newIcon = provider->icon(QFileIconProvider::Folder); | 0 | ||||||||||||
198 | newName = QFileInfo(url.toLocalFile()).fileName(); | - | ||||||||||||
199 | if (!invalidUrls.contains(url))
| 0 | ||||||||||||
200 | invalidUrls.append(url); never executed: invalidUrls.append(url); | 0 | ||||||||||||
201 | //The bookmark is invalid then we set to false the EnabledRole | - | ||||||||||||
202 | setData(index, false, EnabledRole); | - | ||||||||||||
203 | } else { never executed: end of block | 0 | ||||||||||||
204 | //The bookmark is valid then we set to true the EnabledRole | - | ||||||||||||
205 | setData(index, true, EnabledRole); | - | ||||||||||||
206 | } never executed: end of block | 0 | ||||||||||||
207 | - | |||||||||||||
208 | // Make sure that we have at least 32x32 images | - | ||||||||||||
209 | const QSize size = newIcon.actualSize(QSize(32,32)); | - | ||||||||||||
210 | if (size.width() < 32) {
| 0 | ||||||||||||
211 | QPixmap smallPixmap = newIcon.pixmap(QSize(32, 32)); | - | ||||||||||||
212 | newIcon.addPixmap(smallPixmap.scaledToWidth(32, Qt::SmoothTransformation)); | - | ||||||||||||
213 | } never executed: end of block | 0 | ||||||||||||
214 | - | |||||||||||||
215 | if (index.data().toString() != newName)
| 0 | ||||||||||||
216 | setData(index, newName); never executed: setData(index, newName); | 0 | ||||||||||||
217 | QIcon oldIcon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole)); | - | ||||||||||||
218 | if (oldIcon.cacheKey() != newIcon.cacheKey())
| 0 | ||||||||||||
219 | setData(index, newIcon, Qt::DecorationRole); never executed: setData(index, newIcon, Qt::DecorationRole); | 0 | ||||||||||||
220 | } never executed: end of block | 0 | ||||||||||||
221 | } | - | ||||||||||||
222 | - | |||||||||||||
223 | void QUrlModel::setUrls(const QList<QUrl> &list) | - | ||||||||||||
224 | { | - | ||||||||||||
225 | removeRows(0, rowCount()); | - | ||||||||||||
226 | invalidUrls.clear(); | - | ||||||||||||
227 | watching.clear(); | - | ||||||||||||
228 | addUrls(list, 0); | - | ||||||||||||
229 | } never executed: end of block | 0 | ||||||||||||
230 | - | |||||||||||||
231 | /*! | - | ||||||||||||
232 | Add urls \a list into the list at \a row. If move then movie | - | ||||||||||||
233 | existing ones to row. | - | ||||||||||||
234 | - | |||||||||||||
235 | \sa dropMimeData() | - | ||||||||||||
236 | */ | - | ||||||||||||
237 | void QUrlModel::addUrls(const QList<QUrl> &list, int row, bool move) | - | ||||||||||||
238 | { | - | ||||||||||||
239 | if (row == -1)
| 0 | ||||||||||||
240 | row = rowCount(); never executed: row = rowCount(); | 0 | ||||||||||||
241 | row = qMin(row, rowCount()); | - | ||||||||||||
242 | for (int i = list.count() - 1; i >= 0; --i) {
| 0 | ||||||||||||
243 | QUrl url = list.at(i); | - | ||||||||||||
244 | if (!url.isValid() || url.scheme() != QLatin1String("file"))
| 0 | ||||||||||||
245 | continue; never executed: continue; | 0 | ||||||||||||
246 | //this makes sure the url is clean | - | ||||||||||||
247 | const QString cleanUrl = QDir::cleanPath(url.toLocalFile()); | - | ||||||||||||
248 | if (!cleanUrl.isEmpty())
| 0 | ||||||||||||
249 | url = QUrl::fromLocalFile(cleanUrl); never executed: url = QUrl::fromLocalFile(cleanUrl); | 0 | ||||||||||||
250 | - | |||||||||||||
251 | for (int j = 0; move && j < rowCount(); ++j) {
| 0 | ||||||||||||
252 | QString local = index(j, 0).data(UrlRole).toUrl().toLocalFile(); | - | ||||||||||||
253 | #if defined(Q_OS_WIN) | - | ||||||||||||
254 | const Qt::CaseSensitivity cs = Qt::CaseInsensitive; | - | ||||||||||||
255 | #else | - | ||||||||||||
256 | const Qt::CaseSensitivity cs = Qt::CaseSensitive; | - | ||||||||||||
257 | #endif | - | ||||||||||||
258 | if (!cleanUrl.compare(local, cs)) {
| 0 | ||||||||||||
259 | removeRow(j); | - | ||||||||||||
260 | if (j <= row)
| 0 | ||||||||||||
261 | row--; never executed: row--; | 0 | ||||||||||||
262 | break; never executed: break; | 0 | ||||||||||||
263 | } | - | ||||||||||||
264 | } never executed: end of block | 0 | ||||||||||||
265 | row = qMax(row, 0); | - | ||||||||||||
266 | QModelIndex idx = fileSystemModel->index(cleanUrl); | - | ||||||||||||
267 | if (!fileSystemModel->isDir(idx))
| 0 | ||||||||||||
268 | continue; never executed: continue; | 0 | ||||||||||||
269 | insertRows(row, 1); | - | ||||||||||||
270 | setUrl(index(row, 0), url, idx); | - | ||||||||||||
271 | watching.append(qMakePair(idx, cleanUrl)); | - | ||||||||||||
272 | } never executed: end of block | 0 | ||||||||||||
273 | } never executed: end of block | 0 | ||||||||||||
274 | - | |||||||||||||
275 | /*! | - | ||||||||||||
276 | Return the complete list of urls in a QList. | - | ||||||||||||
277 | */ | - | ||||||||||||
278 | QList<QUrl> QUrlModel::urls() const | - | ||||||||||||
279 | { | - | ||||||||||||
280 | QList<QUrl> list; | - | ||||||||||||
281 | const int numRows = rowCount(); | - | ||||||||||||
282 | list.reserve(numRows); | - | ||||||||||||
283 | for (int i = 0; i < numRows; ++i)
| 0 | ||||||||||||
284 | list.append(data(index(i, 0), UrlRole).toUrl()); never executed: list.append(data(index(i, 0), UrlRole).toUrl()); | 0 | ||||||||||||
285 | return list; never executed: return list; | 0 | ||||||||||||
286 | } | - | ||||||||||||
287 | - | |||||||||||||
288 | /*! | - | ||||||||||||
289 | QFileSystemModel to get index's from, clears existing rows | - | ||||||||||||
290 | */ | - | ||||||||||||
291 | void QUrlModel::setFileSystemModel(QFileSystemModel *model) | - | ||||||||||||
292 | { | - | ||||||||||||
293 | if (model == fileSystemModel)
| 0 | ||||||||||||
294 | return; never executed: return; | 0 | ||||||||||||
295 | if (fileSystemModel != 0) {
| 0 | ||||||||||||
296 | disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
297 | this, SLOT(dataChanged(QModelIndex,QModelIndex))); | - | ||||||||||||
298 | disconnect(model, SIGNAL(layoutChanged()), | - | ||||||||||||
299 | this, SLOT(layoutChanged())); | - | ||||||||||||
300 | disconnect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), | - | ||||||||||||
301 | this, SLOT(layoutChanged())); | - | ||||||||||||
302 | } never executed: end of block | 0 | ||||||||||||
303 | fileSystemModel = model; | - | ||||||||||||
304 | if (fileSystemModel != 0) {
| 0 | ||||||||||||
305 | connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
306 | this, SLOT(dataChanged(QModelIndex,QModelIndex))); | - | ||||||||||||
307 | connect(model, SIGNAL(layoutChanged()), | - | ||||||||||||
308 | this, SLOT(layoutChanged())); | - | ||||||||||||
309 | connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), | - | ||||||||||||
310 | this, SLOT(layoutChanged())); | - | ||||||||||||
311 | } never executed: end of block | 0 | ||||||||||||
312 | clear(); | - | ||||||||||||
313 | insertColumns(0, 1); | - | ||||||||||||
314 | } never executed: end of block | 0 | ||||||||||||
315 | - | |||||||||||||
316 | /* | - | ||||||||||||
317 | If one of the index's we are watching has changed update our internal data | - | ||||||||||||
318 | */ | - | ||||||||||||
319 | void QUrlModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) | - | ||||||||||||
320 | { | - | ||||||||||||
321 | QModelIndex parent = topLeft.parent(); | - | ||||||||||||
322 | for (int i = 0; i < watching.count(); ++i) {
| 0 | ||||||||||||
323 | QModelIndex index = watching.at(i).first; | - | ||||||||||||
324 | if (index.model() && topLeft.model()) {
| 0 | ||||||||||||
325 | Q_ASSERT(index.model() == topLeft.model()); | - | ||||||||||||
326 | } never executed: end of block | 0 | ||||||||||||
327 | if ( index.row() >= topLeft.row()
| 0 | ||||||||||||
328 | && index.row() <= bottomRight.row()
| 0 | ||||||||||||
329 | && index.column() >= topLeft.column()
| 0 | ||||||||||||
330 | && index.column() <= bottomRight.column()
| 0 | ||||||||||||
331 | && index.parent() == parent) {
| 0 | ||||||||||||
332 | changed(watching.at(i).second); | - | ||||||||||||
333 | } never executed: end of block | 0 | ||||||||||||
334 | } never executed: end of block | 0 | ||||||||||||
335 | } never executed: end of block | 0 | ||||||||||||
336 | - | |||||||||||||
337 | /*! | - | ||||||||||||
338 | Re-get all of our data, anything could have changed! | - | ||||||||||||
339 | */ | - | ||||||||||||
340 | void QUrlModel::layoutChanged() | - | ||||||||||||
341 | { | - | ||||||||||||
342 | QStringList paths; | - | ||||||||||||
343 | const int numPaths = watching.count(); | - | ||||||||||||
344 | paths.reserve(numPaths); | - | ||||||||||||
345 | for (int i = 0; i < numPaths; ++i)
| 0 | ||||||||||||
346 | paths.append(watching.at(i).second); never executed: paths.append(watching.at(i).second); | 0 | ||||||||||||
347 | watching.clear(); | - | ||||||||||||
348 | for (int i = 0; i < numPaths; ++i) {
| 0 | ||||||||||||
349 | QString path = paths.at(i); | - | ||||||||||||
350 | QModelIndex newIndex = fileSystemModel->index(path); | - | ||||||||||||
351 | watching.append(QPair<QModelIndex, QString>(newIndex, path)); | - | ||||||||||||
352 | if (newIndex.isValid())
| 0 | ||||||||||||
353 | changed(path); never executed: changed(path); | 0 | ||||||||||||
354 | } never executed: end of block | 0 | ||||||||||||
355 | } never executed: end of block | 0 | ||||||||||||
356 | - | |||||||||||||
357 | /*! | - | ||||||||||||
358 | The following path changed data update our copy of that data | - | ||||||||||||
359 | - | |||||||||||||
360 | \sa layoutChanged(), dataChanged() | - | ||||||||||||
361 | */ | - | ||||||||||||
362 | void QUrlModel::changed(const QString &path) | - | ||||||||||||
363 | { | - | ||||||||||||
364 | for (int i = 0; i < rowCount(); ++i) {
| 0 | ||||||||||||
365 | QModelIndex idx = index(i, 0); | - | ||||||||||||
366 | if (idx.data(UrlRole).toUrl().toLocalFile() == path) {
| 0 | ||||||||||||
367 | setData(idx, idx.data(UrlRole).toUrl()); | - | ||||||||||||
368 | } never executed: end of block | 0 | ||||||||||||
369 | } never executed: end of block | 0 | ||||||||||||
370 | } never executed: end of block | 0 | ||||||||||||
371 | - | |||||||||||||
372 | QSidebar::QSidebar(QWidget *parent) : QListView(parent) | - | ||||||||||||
373 | { | - | ||||||||||||
374 | } never executed: end of block | 0 | ||||||||||||
375 | - | |||||||||||||
376 | void QSidebar::setModelAndUrls(QFileSystemModel *model, const QList<QUrl> &newUrls) | - | ||||||||||||
377 | { | - | ||||||||||||
378 | // ### TODO make icon size dynamic | - | ||||||||||||
379 | setIconSize(QSize(24,24)); | - | ||||||||||||
380 | setUniformItemSizes(true); | - | ||||||||||||
381 | urlModel = new QUrlModel(this); | - | ||||||||||||
382 | urlModel->setFileSystemModel(model); | - | ||||||||||||
383 | setModel(urlModel); | - | ||||||||||||
384 | setItemDelegate(new QSideBarDelegate(this)); | - | ||||||||||||
385 | - | |||||||||||||
386 | connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
387 | this, SLOT(clicked(QModelIndex))); | - | ||||||||||||
388 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
389 | setDragDropMode(QAbstractItemView::DragDrop); | - | ||||||||||||
390 | #endif | - | ||||||||||||
391 | setContextMenuPolicy(Qt::CustomContextMenu); | - | ||||||||||||
392 | connect(this, SIGNAL(customContextMenuRequested(QPoint)), | - | ||||||||||||
393 | this, SLOT(showContextMenu(QPoint))); | - | ||||||||||||
394 | urlModel->setUrls(newUrls); | - | ||||||||||||
395 | setCurrentIndex(this->model()->index(0,0)); | - | ||||||||||||
396 | } never executed: end of block | 0 | ||||||||||||
397 | - | |||||||||||||
398 | QSidebar::~QSidebar() | - | ||||||||||||
399 | { | - | ||||||||||||
400 | } | - | ||||||||||||
401 | - | |||||||||||||
402 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
403 | void QSidebar::dragEnterEvent(QDragEnterEvent *event) | - | ||||||||||||
404 | { | - | ||||||||||||
405 | if (urlModel->canDrop(event))
| 0 | ||||||||||||
406 | QListView::dragEnterEvent(event); never executed: QListView::dragEnterEvent(event); | 0 | ||||||||||||
407 | } never executed: end of block | 0 | ||||||||||||
408 | #endif // QT_NO_DRAGANDDROP | - | ||||||||||||
409 | - | |||||||||||||
410 | QSize QSidebar::sizeHint() const | - | ||||||||||||
411 | { | - | ||||||||||||
412 | if (model())
| 0 | ||||||||||||
413 | return QListView::sizeHintForIndex(model()->index(0, 0)) + QSize(2 * frameWidth(), 2 * frameWidth()); never executed: return QListView::sizeHintForIndex(model()->index(0, 0)) + QSize(2 * frameWidth(), 2 * frameWidth()); | 0 | ||||||||||||
414 | return QListView::sizeHint(); never executed: return QListView::sizeHint(); | 0 | ||||||||||||
415 | } | - | ||||||||||||
416 | - | |||||||||||||
417 | void QSidebar::selectUrl(const QUrl &url) | - | ||||||||||||
418 | { | - | ||||||||||||
419 | disconnect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
420 | this, SLOT(clicked(QModelIndex))); | - | ||||||||||||
421 | - | |||||||||||||
422 | selectionModel()->clear(); | - | ||||||||||||
423 | for (int i = 0; i < model()->rowCount(); ++i) {
| 0 | ||||||||||||
424 | if (model()->index(i, 0).data(QUrlModel::UrlRole).toUrl() == url) {
| 0 | ||||||||||||
425 | selectionModel()->select(model()->index(i, 0), QItemSelectionModel::Select); | - | ||||||||||||
426 | break; never executed: break; | 0 | ||||||||||||
427 | } | - | ||||||||||||
428 | } never executed: end of block | 0 | ||||||||||||
429 | - | |||||||||||||
430 | connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
431 | this, SLOT(clicked(QModelIndex))); | - | ||||||||||||
432 | } never executed: end of block | 0 | ||||||||||||
433 | - | |||||||||||||
434 | #ifndef QT_NO_MENU | - | ||||||||||||
435 | /*! | - | ||||||||||||
436 | \internal | - | ||||||||||||
437 | - | |||||||||||||
438 | \sa removeEntry() | - | ||||||||||||
439 | */ | - | ||||||||||||
440 | void QSidebar::showContextMenu(const QPoint &position) | - | ||||||||||||
441 | { | - | ||||||||||||
442 | QList<QAction *> actions; | - | ||||||||||||
443 | if (indexAt(position).isValid()) {
| 0 | ||||||||||||
444 | QAction *action = new QAction(QFileDialog::tr("Remove"), this); | - | ||||||||||||
445 | if (indexAt(position).data(QUrlModel::UrlRole).toUrl().path().isEmpty())
| 0 | ||||||||||||
446 | action->setEnabled(false); never executed: action->setEnabled(false); | 0 | ||||||||||||
447 | connect(action, SIGNAL(triggered()), this, SLOT(removeEntry())); | - | ||||||||||||
448 | actions.append(action); | - | ||||||||||||
449 | } never executed: end of block | 0 | ||||||||||||
450 | if (actions.count() > 0)
| 0 | ||||||||||||
451 | QMenu::exec(actions, mapToGlobal(position)); never executed: QMenu::exec(actions, mapToGlobal(position)); | 0 | ||||||||||||
452 | } never executed: end of block | 0 | ||||||||||||
453 | #endif // QT_NO_MENU | - | ||||||||||||
454 | - | |||||||||||||
455 | /*! | - | ||||||||||||
456 | \internal | - | ||||||||||||
457 | - | |||||||||||||
458 | \sa showContextMenu() | - | ||||||||||||
459 | */ | - | ||||||||||||
460 | void QSidebar::removeEntry() | - | ||||||||||||
461 | { | - | ||||||||||||
462 | QList<QModelIndex> idxs = selectionModel()->selectedIndexes(); | - | ||||||||||||
463 | QList<QPersistentModelIndex> indexes; | - | ||||||||||||
464 | const int numIndexes = idxs.count(); | - | ||||||||||||
465 | indexes.reserve(numIndexes); | - | ||||||||||||
466 | for (int i = 0; i < numIndexes; i++)
| 0 | ||||||||||||
467 | indexes.append(idxs.at(i)); never executed: indexes.append(idxs.at(i)); | 0 | ||||||||||||
468 | - | |||||||||||||
469 | for (int i = 0; i < numIndexes; ++i) {
| 0 | ||||||||||||
470 | if (!indexes.at(i).data(QUrlModel::UrlRole).toUrl().path().isEmpty())
| 0 | ||||||||||||
471 | model()->removeRow(indexes.at(i).row()); never executed: model()->removeRow(indexes.at(i).row()); | 0 | ||||||||||||
472 | } never executed: end of block | 0 | ||||||||||||
473 | } never executed: end of block | 0 | ||||||||||||
474 | - | |||||||||||||
475 | /*! | - | ||||||||||||
476 | \internal | - | ||||||||||||
477 | - | |||||||||||||
478 | \sa goToUrl() | - | ||||||||||||
479 | */ | - | ||||||||||||
480 | void QSidebar::clicked(const QModelIndex &index) | - | ||||||||||||
481 | { | - | ||||||||||||
482 | QUrl url = model()->index(index.row(), 0).data(QUrlModel::UrlRole).toUrl(); | - | ||||||||||||
483 | emit goToUrl(url); | - | ||||||||||||
484 | selectUrl(url); | - | ||||||||||||
485 | } never executed: end of block | 0 | ||||||||||||
486 | - | |||||||||||||
487 | /*! | - | ||||||||||||
488 | \reimp | - | ||||||||||||
489 | Don't automatically select something | - | ||||||||||||
490 | */ | - | ||||||||||||
491 | void QSidebar::focusInEvent(QFocusEvent *event) | - | ||||||||||||
492 | { | - | ||||||||||||
493 | QAbstractScrollArea::focusInEvent(event); | - | ||||||||||||
494 | viewport()->update(); | - | ||||||||||||
495 | } never executed: end of block | 0 | ||||||||||||
496 | - | |||||||||||||
497 | /*! | - | ||||||||||||
498 | \reimp | - | ||||||||||||
499 | */ | - | ||||||||||||
500 | bool QSidebar::event(QEvent * event) | - | ||||||||||||
501 | { | - | ||||||||||||
502 | if (event->type() == QEvent::KeyRelease) {
| 0 | ||||||||||||
503 | QKeyEvent* ke = (QKeyEvent*) event; | - | ||||||||||||
504 | if (ke->key() == Qt::Key_Delete) {
| 0 | ||||||||||||
505 | removeEntry(); | - | ||||||||||||
506 | return true; never executed: return true; | 0 | ||||||||||||
507 | } | - | ||||||||||||
508 | } never executed: end of block | 0 | ||||||||||||
509 | return QListView::event(event); never executed: return QListView::event(event); | 0 | ||||||||||||
510 | } | - | ||||||||||||
511 | - | |||||||||||||
512 | QT_END_NAMESPACE | - | ||||||||||||
513 | - | |||||||||||||
514 | #include "moc_qsidebar_p.cpp" | - | ||||||||||||
515 | - | |||||||||||||
516 | #endif | - | ||||||||||||
Source code | Switch to Preprocessed file |