1 | /* |
2 | This file is part of the KDE Libraries |
3 | SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> |
4 | SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef KPAGEVIEW_P_H |
10 | #define KPAGEVIEW_P_H |
11 | |
12 | #include "kpageview.h" |
13 | |
14 | #include <QAbstractItemDelegate> |
15 | #include <QAbstractProxyModel> |
16 | #include <QGridLayout> |
17 | #include <QLineEdit> |
18 | #include <QListView> |
19 | #include <QPointer> |
20 | #include <QStackedWidget> |
21 | #include <QTimer> |
22 | #include <QTreeView> |
23 | #include <ktitlewidget.h> |
24 | |
25 | class KPageStackedWidget : public QStackedWidget |
26 | { |
27 | Q_OBJECT |
28 | public: |
29 | explicit KPageStackedWidget(QWidget *parent = nullptr) |
30 | : QStackedWidget(parent) |
31 | { |
32 | } |
33 | |
34 | void setMinimumSize(const QSize &size) |
35 | { |
36 | mMinimumSize = size; |
37 | } |
38 | |
39 | QSize minimumSizeHint() const override |
40 | { |
41 | return mMinimumSize.expandedTo(otherSize: QStackedWidget::minimumSizeHint()); |
42 | } |
43 | |
44 | private: |
45 | QSize mMinimumSize; |
46 | }; |
47 | |
48 | class KPageViewPrivate |
49 | { |
50 | Q_DECLARE_PUBLIC(KPageView) |
51 | |
52 | public: |
53 | virtual ~KPageViewPrivate() = default; |
54 | |
55 | protected: |
56 | KPageViewPrivate(KPageView *); |
57 | |
58 | KPageView *q_ptr; |
59 | |
60 | // data |
61 | QAbstractItemModel *model; |
62 | KPageView::FaceType faceType; |
63 | |
64 | // gui |
65 | QGridLayout *layout; |
66 | KPageStackedWidget *stack; |
67 | KTitleWidget *titleWidget; |
68 | QWidget *defaultWidget; |
69 | QWidget *searchLineEditContainer; |
70 | QLineEdit *searchLineEdit; |
71 | QFrame *separatorLine; |
72 | QTimer searchTimer; |
73 | |
74 | QAbstractItemView *view; |
75 | |
76 | QPointer<QWidget> ; |
77 | QPointer<QWidget> ; |
78 | |
79 | void updateTitleWidget(const QModelIndex &index); |
80 | |
81 | void updateSelection(); |
82 | void cleanupPages(); |
83 | QList<QWidget *> collectPages(const QModelIndex &parent = QModelIndex()); |
84 | KPageView::FaceType detectAutoFace() const; |
85 | KPageView::FaceType effectiveFaceType() const; |
86 | |
87 | // private slots |
88 | void rebuildGui(); |
89 | void modelChanged(); |
90 | void dataChanged(const QModelIndex &, const QModelIndex &); |
91 | void pageSelected(const QItemSelection &, const QItemSelection &); |
92 | bool hasSearchableView() const; |
93 | |
94 | private: |
95 | void onSearchTextChanged(); |
96 | void init(); |
97 | |
98 | QMetaObject::Connection m_dataChangedConnection; |
99 | QMetaObject::Connection m_layoutChangedConnection; |
100 | QMetaObject::Connection m_selectionChangedConnection; |
101 | QList<class SearchMatchOverlay *> m_searchMatchOverlays; |
102 | }; |
103 | |
104 | namespace KDEPrivate |
105 | { |
106 | class KPageListViewDelegate; |
107 | class KPageListViewProxy; |
108 | |
109 | class KPagePlainView : public QAbstractItemView |
110 | { |
111 | Q_OBJECT |
112 | public: |
113 | explicit KPagePlainView(QWidget *parent = nullptr); |
114 | |
115 | QModelIndex indexAt(const QPoint &point) const override; |
116 | void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override; |
117 | QRect visualRect(const QModelIndex &index) const override; |
118 | |
119 | protected: |
120 | QModelIndex moveCursor(QAbstractItemView::CursorAction, Qt::KeyboardModifiers) override; |
121 | int horizontalOffset() const override; |
122 | int verticalOffset() const override; |
123 | bool isIndexHidden(const QModelIndex &) const override; |
124 | void setSelection(const QRect &, QFlags<QItemSelectionModel::SelectionFlag>) override; |
125 | QRegion visualRegionForSelection(const QItemSelection &) const override; |
126 | }; |
127 | |
128 | class KPageListView : public QListView |
129 | { |
130 | Q_OBJECT |
131 | |
132 | public: |
133 | explicit KPageListView(QWidget *parent = nullptr); |
134 | ~KPageListView() override; |
135 | |
136 | void setModel(QAbstractItemModel *model) override; |
137 | void setFlexibleWidth(bool flexibleWidth); |
138 | |
139 | protected: |
140 | void changeEvent(QEvent *event) override; |
141 | |
142 | private Q_SLOTS: |
143 | void updateWidth(); |
144 | |
145 | private: |
146 | bool m_flexibleWidth = false; |
147 | }; |
148 | |
149 | class KPageTreeView : public QTreeView |
150 | { |
151 | Q_OBJECT |
152 | |
153 | public: |
154 | explicit KPageTreeView(QWidget *parent = nullptr); |
155 | |
156 | void setModel(QAbstractItemModel *model) override; |
157 | |
158 | private Q_SLOTS: |
159 | void updateWidth(); |
160 | |
161 | private: |
162 | void expandItems(const QModelIndex &index = QModelIndex()); |
163 | }; |
164 | |
165 | class KPageTabbedView : public QAbstractItemView |
166 | { |
167 | Q_OBJECT |
168 | |
169 | public: |
170 | explicit KPageTabbedView(QWidget *parent = nullptr); |
171 | ~KPageTabbedView() override; |
172 | |
173 | void setModel(QAbstractItemModel *model) override; |
174 | |
175 | QModelIndex indexAt(const QPoint &point) const override; |
176 | void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override; |
177 | QRect visualRect(const QModelIndex &index) const override; |
178 | |
179 | QSize minimumSizeHint() const override; |
180 | |
181 | protected: |
182 | QModelIndex moveCursor(QAbstractItemView::CursorAction, Qt::KeyboardModifiers) override; |
183 | int horizontalOffset() const override; |
184 | int verticalOffset() const override; |
185 | bool isIndexHidden(const QModelIndex &) const override; |
186 | void setSelection(const QRect &, QFlags<QItemSelectionModel::SelectionFlag>) override; |
187 | QRegion visualRegionForSelection(const QItemSelection &) const override; |
188 | |
189 | private Q_SLOTS: |
190 | void currentPageChanged(int); |
191 | void layoutChanged(); |
192 | void dataChanged(const QModelIndex &, const QModelIndex &, const QList<int> &roles) override; |
193 | |
194 | private: |
195 | QTabWidget *mTabWidget; |
196 | }; |
197 | |
198 | class KPageListViewDelegate : public QAbstractItemDelegate |
199 | { |
200 | Q_OBJECT |
201 | |
202 | public: |
203 | explicit KPageListViewDelegate(QObject *parent = nullptr); |
204 | |
205 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; |
206 | QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; |
207 | |
208 | private: |
209 | void drawFocus(QPainter *, const QStyleOptionViewItem &, const QRect &) const; |
210 | }; |
211 | |
212 | /** |
213 | * We need this proxy model to map the leaves of a tree-like model |
214 | * to a one-level list model. |
215 | */ |
216 | class KPageListViewProxy : public QAbstractProxyModel |
217 | { |
218 | Q_OBJECT |
219 | |
220 | public: |
221 | explicit KPageListViewProxy(QObject *parent = nullptr); |
222 | ~KPageListViewProxy() override; |
223 | |
224 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
225 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
226 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
227 | QModelIndex parent(const QModelIndex &) const override; |
228 | QVariant data(const QModelIndex &index, int role) const override; |
229 | QModelIndex mapFromSource(const QModelIndex &index) const override; |
230 | QModelIndex mapToSource(const QModelIndex &index) const override; |
231 | |
232 | public Q_SLOTS: |
233 | void rebuildMap(); |
234 | |
235 | private: |
236 | void addMapEntry(const QModelIndex &); |
237 | |
238 | QList<QModelIndex> mList; |
239 | }; |
240 | |
241 | class SelectionModel : public QItemSelectionModel |
242 | { |
243 | Q_OBJECT |
244 | |
245 | public: |
246 | SelectionModel(QAbstractItemModel *model, QObject *parent); |
247 | |
248 | public Q_SLOTS: |
249 | void clear() override; |
250 | void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) override; |
251 | void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) override; |
252 | }; |
253 | |
254 | } |
255 | |
256 | #endif |
257 | |