qsidebar.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/dialogs/qsidebar.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7void QSideBarDelegate::initStyleOption(QStyleOptionViewItem *option,-
8 const QModelIndex &index) const-
9{-
10 QStyledItemDelegate::initStyleOption(option,index);-
11 QVariant value = index.data(QUrlModel::EnabledRole);-
12 if (value.isValid()) {-
13-
14 if (!qvariant_cast<bool>(value))-
15 option->state &= ~QStyle::State_Enabled;-
16 }-
17}-
18QUrlModel::QUrlModel(QObject *parent) : QStandardItemModel(parent), showFullPath(false), fileSystemModel(0)-
19{-
20}-
21-
22-
23-
24-
25QStringList QUrlModel::mimeTypes() const-
26{-
27 return QStringList(QLatin1String("text/uri-list"));-
28}-
29-
30-
31-
32-
33Qt::ItemFlags QUrlModel::flags(const QModelIndex &index) const-
34{-
35 Qt::ItemFlags flags = QStandardItemModel::flags(index);-
36 if (index.isValid()) {-
37 flags &= ~Qt::ItemIsEditable;-
38-
39 flags &= ~Qt::ItemIsDropEnabled;-
40 }-
41-
42 if (index.data(Qt::DecorationRole).isNull())-
43 flags &= ~Qt::ItemIsEnabled;-
44-
45 return flags;-
46}-
47-
48-
49-
50-
51QMimeData *QUrlModel::mimeData(const QModelIndexList &indexes) const-
52{-
53 QList<QUrl> list;-
54 for (int i = 0; i <const auto &index : indexes.count(); ++i) {-
55 if (indexesindex
index.column() == 0Description
TRUEnever evaluated
FALSEnever evaluated
.at(i).column() == 0
index.column() == 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
56 list.append(indexesindex.at(i).data(UrlRole).toUrl());
never executed: list.append(index.data(UrlRole).toUrl());
0
57 }
never executed: end of block
0
58 QMimeData *data = new QMimeData();-
59 data->setUrls(list);-
60 return
never executed: return data;
data;
never executed: return data;
0
61}-
62bool QUrlModel::canDrop(QDragEnterEvent *event)-
63{-
64 if (!event->mimeData()->formats().contains(mimeTypes().firstconstFirst())
!event->mimeDa....constFirst())Description
TRUEnever evaluated
FALSEnever evaluated
)
0
65 return
never executed: return false;
false;
never executed: return false;
0
66-
67 const QList<QUrl> list = event->mimeData()->urls();-
68 for (int i = 0; i <const auto &url : list.count(); ++i) {-
69 const QModelIndex idx = fileSystemModel->index(listurl.at(0).toLocalFile());-
70 if (!fileSystemModel->isDir(idx)
!fileSystemModel->isDir(idx)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
71 return
never executed: return false;
false;
never executed: return false;
0
72 }
never executed: end of block
0
73 return
never executed: return true;
true;
never executed: return true;
0
74}-
75-
76-
77-
78-
79bool QUrlModel::dropMimeData(const QMimeData *data, Qt::DropAction action,-
80 int row, int column, const QModelIndex &parent)-
81{-
82 if (!data->formats().contains(mimeTypes().firstconstFirst())
!data->formats....constFirst())Description
TRUEnever evaluated
FALSEnever evaluated
)
0
83 return
never executed: return false;
false;
never executed: return false;
0
84 (void)action;;-
85 (void)column;;-
86 (void)parent;;-
87 addUrls(data->urls(), row);-
88 return
never executed: return true;
true;
never executed: return true;
0
89}-
90bool QUrlModel::setData(const QModelIndex &index, const QVariant &value, int role)-
91{-
92 if (value.type() == QVariant::Url) {-
93 QUrl url = value.toUrl();-
94 QModelIndex dirIndex = fileSystemModel->index(url.toLocalFile());-
95-
96 if (showFullPath)-
97 QStandardItemModel::setData(index, QDir::toNativeSeparators(fileSystemModel->data(dirIndex, QFileSystemModel::FilePathRole).toString()));-
98 else {-
99 QStandardItemModel::setData(index, QDir::toNativeSeparators(fileSystemModel->data(dirIndex, QFileSystemModel::FilePathRole).toString()), Qt::ToolTipRole);-
100 QStandardItemModel::setData(index, fileSystemModel->data(dirIndex).toString());-
101 }-
102 QStandardItemModel::setData(index, fileSystemModel->data(dirIndex, Qt::DecorationRole),-
103 Qt::DecorationRole);-
104 QStandardItemModel::setData(index, url, UrlRole);-
105 return true;-
106 }-
107 return QStandardItemModel::setData(index, value, role);-
108}-
109-
110void QUrlModel::setUrl(const QModelIndex &index, const QUrl &url, const QModelIndex &dirIndex)-
111{-
112 setData(index, url, UrlRole);-
113 if (url.path().isEmpty()) {-
114 setData(index, fileSystemModel->myComputer());-
115 setData(index, fileSystemModel->myComputer(Qt::DecorationRole), Qt::DecorationRole);-
116 } else {-
117 QString newName;-
118 if (showFullPath) {-
119-
120 newName = QDir::toNativeSeparators(dirIndex.data(QFileSystemModel::FilePathRole).toString());-
121 } else {-
122 newName = dirIndex.data().toString();-
123 }-
124-
125 QIcon newIcon = qvariant_cast<QIcon>(dirIndex.data(Qt::DecorationRole));-
126 if (!dirIndex.isValid()) {-
127 const QFileIconProvider *provider = fileSystemModel->iconProvider();-
128 if (provider)-
129 newIcon = provider->icon(QFileIconProvider::Folder);-
130 newName = QFileInfo(url.toLocalFile()).fileName();-
131 if (!invalidUrls.contains(url))-
132 invalidUrls.append(url);-
133-
134 setData(index, false, EnabledRole);-
135 } else {-
136-
137 setData(index, true, EnabledRole);-
138 }-
139-
140-
141 const QSize size = newIcon.actualSize(QSize(32,32));-
142 if (size.width() < 32) {-
143 QPixmap smallPixmap = newIcon.pixmap(QSize(32, 32));-
144 newIcon.addPixmap(smallPixmap.scaledToWidth(32, Qt::SmoothTransformation));-
145 }-
146-
147 if (index.data().toString() != newName)-
148 setData(index, newName);-
149 QIcon oldIcon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));-
150 if (oldIcon.cacheKey() != newIcon.cacheKey())-
151 setData(index, newIcon, Qt::DecorationRole);-
152 }-
153}-
154-
155void QUrlModel::setUrls(const QList<QUrl> &list)-
156{-
157 removeRows(0, rowCount());-
158 invalidUrls.clear();-
159 watching.clear();-
160 addUrls(list, 0);-
161}-
162-
163-
164-
165-
166-
167-
168-
169void QUrlModel::addUrls(const QList<QUrl> &list, int row, bool move)-
170{-
171 if (row == -1)-
172 row = rowCount();-
173 row = qMin(row, rowCount());-
174 for (int i = list.count() - 1; i >= 0; --i) {-
175 QUrl url = list.at(i);-
176 if (!url.isValid() || url.scheme() != QLatin1String("file"))-
177 continue;-
178-
179 const QString cleanUrl = QDir::cleanPath(url.toLocalFile());-
180 if (!cleanUrl.isEmpty())-
181 url = QUrl::fromLocalFile(cleanUrl);-
182-
183 for (int j = 0; move && j < rowCount(); ++j) {-
184 QString local = index(j, 0).data(UrlRole).toUrl().toLocalFile();-
185-
186-
187-
188 const Qt::CaseSensitivity cs = Qt::CaseSensitive;-
189-
190 if (!cleanUrl.compare(local, cs)) {-
191 removeRow(j);-
192 if (j <= row)-
193 row--;-
194 break;-
195 }-
196 }-
197 row = qMax(row, 0);-
198 QModelIndex idx = fileSystemModel->index(cleanUrl);-
199 if (!fileSystemModel->isDir(idx))-
200 continue;-
201 insertRows(row, 1);-
202 setUrl(index(row, 0), url, idx);-
203 watching.append(qMakePair(idx, cleanUrl));-
204 }-
205}-
206-
207-
208-
209-
210QList<QUrl> QUrlModel::urls() const-
211{-
212 QList<QUrl> list;-
213 const int numRows = rowCount();-
214 list.reserve(numRows);-
215 for (int i = 0; i < numRows; ++i)-
216 list.append(data(index(i, 0), UrlRole).toUrl());-
217 return list;-
218}-
219-
220-
221-
222-
223void QUrlModel::setFileSystemModel(QFileSystemModel *model)-
224{-
225 if (model == fileSystemModel)-
226 return;-
227 if (fileSystemModel != 0) {-
228 disconnect(model, qFlagLocation("2""dataChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "296""302"),-
229 this, qFlagLocation("1""dataChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "297""303"));-
230 disconnect(model, qFlagLocation("2""layoutChanged()" "\0" __FILE__ ":" "298""304"),-
231 this, qFlagLocation("1""layoutChanged()" "\0" __FILE__ ":" "299""305"));-
232 disconnect(model, qFlagLocation("2""rowsRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "300""306"),-
233 this, qFlagLocation("1""layoutChanged()" "\0" __FILE__ ":" "301""307"));-
234 }-
235 fileSystemModel = model;-
236 if (fileSystemModel != 0) {-
237 connect(model, qFlagLocation("2""dataChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "305""311"),-
238 this, qFlagLocation("1""dataChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "306""312"));-
239 connect(model, qFlagLocation("2""layoutChanged()" "\0" __FILE__ ":" "307""313"),-
240 this, qFlagLocation("1""layoutChanged()" "\0" __FILE__ ":" "308""314"));-
241 connect(model, qFlagLocation("2""rowsRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "309""315"),-
242 this, qFlagLocation("1""layoutChanged()" "\0" __FILE__ ":" "310""316"));-
243 }-
244 clear();-
245 insertColumns(0, 1);-
246}-
247-
248-
249-
250-
251void QUrlModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)-
252{-
253 QModelIndex parent = topLeft.parent();-
254 for (int i = 0; i < watching.count(); ++i) {-
255 QModelIndex index = watching.at(i).first;-
256 if (index.model() && topLeft.model()) {-
257 ((!(index.model() == topLeft.model())) ? qt_assert("index.model() == topLeft.model()",__FILE__,325331) : qt_noop());-
258 }-
259 if ( index.row() >= topLeft.row()-
260 && index.row() <= bottomRight.row()-
261 && index.column() >= topLeft.column()-
262 && index.column() <= bottomRight.column()-
263 && index.parent() == parent) {-
264 changed(watching.at(i).second);-
265 }-
266 }-
267}-
268-
269-
270-
271-
272void QUrlModel::layoutChanged()-
273{-
274 QStringList paths;-
275 const int numPaths = watching.count();-
276 paths.reserve(numPaths);-
277 for (int i = 0; i < numPaths; ++i)-
278 paths.append(watching.at(i).second);-
279 watching.clear();-
280 for (int i = 0; i < numPaths; ++i) {-
281 QString path = paths.at(i);-
282 QModelIndex newIndex = fileSystemModel->index(path);-
283 watching.append(QPair<QModelIndex, QString>(newIndex, path));-
284 if (newIndex.isValid())-
285 changed(path);-
286 }-
287}-
288-
289-
290-
291-
292-
293-
294void QUrlModel::changed(const QString &path)-
295{-
296 for (int i = 0; i < rowCount(); ++i) {-
297 QModelIndex idx = index(i, 0);-
298 if (idx.data(UrlRole).toUrl().toLocalFile() == path) {-
299 setData(idx, idx.data(UrlRole).toUrl());-
300 }-
301 }-
302}-
303-
304QSidebar::QSidebar(QWidget *parent) : QListView(parent)-
305{-
306}-
307-
308void QSidebar::setModelAndUrls(QFileSystemModel *model, const QList<QUrl> &newUrls)-
309{-
310-
311 setIconSize(QSize(24,24));-
312 setUniformItemSizes(true);-
313 urlModel = new QUrlModel(this);-
314 urlModel->setFileSystemModel(model);-
315 setModel(urlModel);-
316 setItemDelegate(new QSideBarDelegate(this));-
317-
318 connect(selectionModel(), qFlagLocation("2""currentChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "386""392"),-
319 this, qFlagLocation("1""clicked(QModelIndex)" "\0" __FILE__ ":" "387""393"));-
320-
321 setDragDropMode(QAbstractItemView::DragDrop);-
322-
323 setContextMenuPolicy(Qt::CustomContextMenu);-
324 connect(this, qFlagLocation("2""customContextMenuRequested(QPoint)" "\0" __FILE__ ":" "392""398"),-
325 this, qFlagLocation("1""showContextMenu(QPoint)" "\0" __FILE__ ":" "393""399"));-
326 urlModel->setUrls(newUrls);-
327 setCurrentIndex(this->model()->index(0,0));-
328}-
329-
330QSidebar::~QSidebar()-
331{-
332}-
333-
334-
335void QSidebar::dragEnterEvent(QDragEnterEvent *event)-
336{-
337 if (urlModel->canDrop(event))-
338 QListView::dragEnterEvent(event);-
339}-
340-
341-
342QSize QSidebar::sizeHint() const-
343{-
344 if (model())-
345 return QListView::sizeHintForIndex(model()->index(0, 0)) + QSize(2 * frameWidth(), 2 * frameWidth());-
346 return QListView::sizeHint();-
347}-
348-
349void QSidebar::selectUrl(const QUrl &url)-
350{-
351 disconnect(selectionModel(), qFlagLocation("2""currentChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "419""425"),-
352 this, qFlagLocation("1""clicked(QModelIndex)" "\0" __FILE__ ":" "420""426"));-
353-
354 selectionModel()->clear();-
355 for (int i = 0; i < model()->rowCount(); ++i) {-
356 if (model()->index(i, 0).data(QUrlModel::UrlRole).toUrl() == url) {-
357 selectionModel()->select(model()->index(i, 0), QItemSelectionModel::Select);-
358 break;-
359 }-
360 }-
361-
362 connect(selectionModel(), qFlagLocation("2""currentChanged(QModelIndex,QModelIndex)" "\0" __FILE__ ":" "430""436"),-
363 this, qFlagLocation("1""clicked(QModelIndex)" "\0" __FILE__ ":" "431""437"));-
364}-
365-
366-
367-
368-
369-
370-
371-
372void QSidebar::showContextMenu(const QPoint &position)-
373{-
374 QList<QAction *> actions;-
375 if (indexAt(position).isValid()) {-
376 QAction *action = new QAction(QFileDialog::tr("Remove"), this);-
377 if (indexAt(position).data(QUrlModel::UrlRole).toUrl().path().isEmpty())-
378 action->setEnabled(false);-
379 connect(action, qFlagLocation("2""triggered()" "\0" __FILE__ ":" "447""453"), this, qFlagLocation("1""removeEntry()" "\0" __FILE__ ":" "447""453"));-
380 actions.append(action);-
381 }-
382 if (actions.count() > 0)-
383 QMenu::exec(actions, mapToGlobal(position));-
384}-
385-
386-
387-
388-
389-
390-
391-
392void QSidebar::removeEntry()-
393{-
394 QList<QModelIndex> idxs = selectionModel()->selectedIndexes();-
395 QList<QPersistentModelIndex> indexes;-
396 const int numIndexes = idxs.count();-
397 indexes.reserve(numIndexes);-
398 for (int i = 0; i < numIndexes; i++)-
399 indexes.append(idxs.at(i));-
400-
401 for (int i = 0; i < numIndexes; ++i) {-
402 if (!indexes.at(i).data(QUrlModel::UrlRole).toUrl().path().isEmpty())-
403 model()->removeRow(indexes.at(i).row());-
404 }-
405}-
406-
407-
408-
409-
410-
411-
412void QSidebar::clicked(const QModelIndex &index)-
413{-
414 QUrl url = model()->index(index.row(), 0).data(QUrlModel::UrlRole).toUrl();-
415 goToUrl(url);-
416 selectUrl(url);-
417}-
418-
419-
420-
421-
422-
423void QSidebar::focusInEvent(QFocusEvent *event)-
424{-
425 QAbstractScrollArea::focusInEvent(event);-
426 viewport()->update();-
427}-
428-
429-
430-
431-
432bool QSidebar::event(QEvent * event)-
433{-
434 if (event->type() == QEvent::KeyRelease) {-
435 QKeyEvent* ke = (QKeyEvent*) event;-
436 if (ke->key() == Qt::Key_Delete) {-
437 removeEntry();-
438 return true;-
439 }-
440 }-
441 return QListView::event(event);-
442}-
443-
444-
445-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9