File indexing completed on 2024-04-28 03:59:10

0001 /*
0002     This file is part of the KDE Libraries
0003     SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KPAGEVIEW_H
0009 #define KPAGEVIEW_H
0010 
0011 #include <kwidgetsaddons_export.h>
0012 
0013 #include <QWidget>
0014 #include <memory>
0015 
0016 class KPageModel;
0017 
0018 class QAbstractItemDelegate;
0019 class QAbstractItemView;
0020 class QModelIndex;
0021 class KPageViewPrivate;
0022 class QAbstractItemModel;
0023 
0024 /**
0025  * @class KPageView kpageview.h KPageView
0026  *
0027  * @short A base class which can handle multiple pages.
0028  *
0029  * This class provides a widget base class which handles multiple
0030  * pages and allows the user to switch between these pages in
0031  * different ways.
0032  *
0033  * Currently, @p Auto, @p Plain, @p List, @p Tree, @p FlatList and
0034  * @p Tabbed face types are available (cmp. KPageWidget).
0035  *
0036  * <b>Example:</b>\n
0037  *
0038  * \code
0039  *  KPageModel *model = new MyPageModel();
0040  *
0041  *  auto view = new KPageView(this);
0042  *  view->setModel(model);
0043  *
0044  *  view->setFaceType(KPageView::List);
0045  * \endcode
0046  *
0047  * @author Tobias Koenig (tokoe@kde.org)
0048  */
0049 class KWIDGETSADDONS_EXPORT KPageView : public QWidget
0050 {
0051     Q_OBJECT
0052     Q_PROPERTY(QWidget *pageHeader READ pageHeader WRITE setPageHeader)
0053     Q_PROPERTY(QWidget *pageFooter READ pageFooter WRITE setPageFooter)
0054     Q_PROPERTY(FaceType faceType READ faceType WRITE setFaceType)
0055     Q_DECLARE_PRIVATE(KPageView)
0056 
0057 public:
0058     /**
0059      * This enum is used to decide which type of navigation view
0060      * shall be used in the page view.
0061      */
0062     enum FaceType {
0063         /**
0064          * Depending on the number of pages in the model,
0065          * the @c Plain (one page), the @c List (several pages)
0066          * or the @c Tree face (nested pages) will be used.
0067          * This is the default face type.
0068          */
0069         Auto,
0070         /**
0071          * No navigation view will be visible and only the
0072          * first page of the model will be shown.
0073          */
0074         Plain,
0075         /**
0076          * An icon list is used as navigation view
0077          */
0078         List,
0079         /**
0080          * A tree list is used as navigation view
0081          */
0082         Tree,
0083         /**
0084          * A tab widget is used as navigation view
0085          */
0086         Tabbed,
0087         /**
0088          * A flat list with small icons is used as navigation view
0089          */
0090         FlatList,
0091     };
0092     Q_ENUM(FaceType)
0093 
0094     /**
0095      * Creates a page view with given parent.
0096      */
0097     explicit KPageView(QWidget *parent = nullptr);
0098 
0099     /**
0100      * Destroys the page view.
0101      */
0102     ~KPageView() override;
0103 
0104     /**
0105      * Sets the @p model of the page view.
0106      *
0107      * The model has to provide data for the roles defined in KPageModel::Role.
0108      */
0109     void setModel(QAbstractItemModel *model);
0110 
0111     /**
0112      * Returns the model of the page view.
0113      */
0114     QAbstractItemModel *model() const;
0115 
0116     /**
0117      * Sets the face type of the page view.
0118      */
0119     void setFaceType(FaceType faceType);
0120 
0121     /**
0122      * Returns the face type of the page view.
0123      */
0124     FaceType faceType() const;
0125 
0126     /**
0127      * Sets the page with @param index to be the current page and emits
0128      * the signal currentPageChanged.
0129      */
0130     void setCurrentPage(const QModelIndex &index);
0131 
0132     /**
0133      * Returns the index for the current page or an invalid index
0134      * if no current page exists.
0135      */
0136     QModelIndex currentPage() const;
0137 
0138     /**
0139      * Sets the item @param delegate which can be used customize
0140      * the page view.
0141      */
0142     void setItemDelegate(QAbstractItemDelegate *delegate);
0143 
0144     /**
0145      * Returns the item delegate of the page view.
0146      */
0147     QAbstractItemDelegate *itemDelegate() const;
0148 
0149     /**
0150      * Sets the @p widget which will be shown when a page is selected
0151      * that has no own widget set.
0152      */
0153     void setDefaultWidget(QWidget *widget);
0154 
0155     /**
0156      * Set a widget as the header for this Page view
0157      * It will replace the standard page title
0158      * @since 5.61
0159      */
0160     void setPageHeader(QWidget *header);
0161 
0162     /**
0163      * Widget of the header for this page view
0164      * @since 5.61
0165      */
0166     QWidget *pageHeader() const;
0167 
0168     /**
0169      * Set a widget as the footer for this Page view
0170      * @since 5.61
0171      */
0172     void setPageFooter(QWidget *footer);
0173 
0174     /**
0175      * Widget of the footer for this page view
0176      * @since 5.61
0177      */
0178     QWidget *pageFooter() const;
0179 
0180 Q_SIGNALS:
0181     /**
0182      * This signal is emitted whenever the current page changes.
0183      * The previous page index is replaced by the current index.
0184      */
0185     void currentPageChanged(const QModelIndex &current, const QModelIndex &previous);
0186 
0187 protected:
0188     /**
0189      * Returns the navigation view, depending on the current
0190      * face type.
0191      *
0192      * This method can be reimplemented to provide custom
0193      * navigation views.
0194      */
0195     virtual QAbstractItemView *createView();
0196 
0197     /**
0198      * Returns whether the page header should be visible.
0199      *
0200      * This method can be reimplemented for adapting custom
0201      * views.
0202      */
0203     virtual bool showPageHeader() const;
0204 
0205     /**
0206      * Returns the position where the navigation view should be
0207      * located according to the page stack.
0208      *
0209      * This method can be reimplemented for adapting custom
0210      * views.
0211      */
0212     virtual Qt::Alignment viewPosition() const;
0213 
0214     KWIDGETSADDONS_NO_EXPORT KPageView(KPageViewPrivate &dd, QWidget *parent);
0215 
0216 protected:
0217     std::unique_ptr<class KPageViewPrivate> const d_ptr;
0218 };
0219 
0220 #endif