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