1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSIDEBAR_H
5#define QSIDEBAR_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include <qlist.h>
20#include <qlistview.h>
21#include <qstandarditemmodel.h>
22#include <qstyleditemdelegate.h>
23#include <qurl.h>
24
25QT_REQUIRE_CONFIG(filedialog);
26
27QT_BEGIN_NAMESPACE
28
29class QFileSystemModel;
30
31class QSideBarDelegate : public QStyledItemDelegate
32{
33 public:
34 QSideBarDelegate(QWidget *parent = nullptr) : QStyledItemDelegate(parent) {}
35 void initStyleOption(QStyleOptionViewItem *option,
36 const QModelIndex &index) const override;
37};
38
39class Q_AUTOTEST_EXPORT QUrlModel : public QStandardItemModel
40{
41 Q_OBJECT
42
43public:
44 enum Roles {
45 UrlRole = Qt::UserRole + 1,
46 EnabledRole = Qt::UserRole + 2
47 };
48
49 QUrlModel(QObject *parent = nullptr);
50
51 QStringList mimeTypes() const override;
52 QMimeData *mimeData(const QModelIndexList &indexes) const override;
53#if QT_CONFIG(draganddrop)
54 bool canDrop(QDragEnterEvent *event);
55 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
56#endif
57 Qt::ItemFlags flags(const QModelIndex &index) const override;
58 bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override;
59
60 void setUrls(const QList<QUrl> &list);
61 void addUrls(const QList<QUrl> &urls, int row = -1, bool move = true);
62 QList<QUrl> urls() const;
63 void setFileSystemModel(QFileSystemModel *model);
64 bool showFullPath;
65
66private Q_SLOTS:
67 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
68 void layoutChanged();
69
70private:
71 void setUrl(const QModelIndex &index, const QUrl &url, const QModelIndex &dirIndex);
72 void changed(const QString &path);
73 void addIndexToWatch(const QString &path, const QModelIndex &index);
74 QFileSystemModel *fileSystemModel;
75 struct WatchItem {
76 QModelIndex index;
77 QString path;
78 };
79 friend class QTypeInfo<WatchItem>;
80
81 QList<WatchItem> watching;
82 QList<QUrl> invalidUrls;
83};
84Q_DECLARE_TYPEINFO(QUrlModel::WatchItem, Q_RELOCATABLE_TYPE);
85
86class Q_AUTOTEST_EXPORT QSidebar : public QListView
87{
88 Q_OBJECT
89
90Q_SIGNALS:
91 void goToUrl(const QUrl &url);
92
93public:
94 QSidebar(QWidget *parent = nullptr);
95 void setModelAndUrls(QFileSystemModel *model, const QList<QUrl> &newUrls);
96 ~QSidebar();
97
98 QSize sizeHint() const override;
99
100 void setUrls(const QList<QUrl> &list) { urlModel->setUrls(list); }
101 void addUrls(const QList<QUrl> &list, int row) { urlModel->addUrls(urls: list, row); }
102 QList<QUrl> urls() const { return urlModel->urls(); }
103
104 void selectUrl(const QUrl &url);
105
106protected:
107 bool event(QEvent * e) override;
108 void focusInEvent(QFocusEvent *event) override;
109#if QT_CONFIG(draganddrop)
110 void dragEnterEvent(QDragEnterEvent *event) override;
111#endif
112
113private Q_SLOTS:
114 void clicked(const QModelIndex &index);
115#if QT_CONFIG(menu)
116 void showContextMenu(const QPoint &position);
117#endif
118 void removeEntry();
119
120private:
121 QUrlModel *urlModel;
122};
123
124QT_END_NAMESPACE
125
126#endif // QSIDEBAR_H
127
128

source code of qtbase/src/widgets/dialogs/qsidebar_p.h