File indexing completed on 2024-05-05 04:38:46

0001 /*
0002     SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_MULTILEVELLISTVIEW_H
0008 #define KDEVPLATFORM_MULTILEVELLISTVIEW_H
0009 
0010 #include <QWidget>
0011 #include "utilexport.h"
0012 
0013 class QTreeView;
0014 class QModelIndex;
0015 class QAbstractItemModel;
0016 
0017 namespace KDevelop {
0018 class MultiLevelListViewPrivate;
0019 
0020 /**
0021  * A view for displaying a tree structure in a series of list views.
0022  *
0023  * A MultiLevelListView can have any number of levels, with one list view for each level.
0024  * Selecting an item at one level causes that item to become the root of the next level.
0025  *
0026  * For compatibility and convenience, this class has methods and signals similar to those of
0027  * QAbstractItemView, such as setModel(), setRootIndex() and currentIndexChanged().
0028  */
0029 class KDEVPLATFORMUTIL_EXPORT MultiLevelListView : public QWidget
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     enum LastLevelViewMode {
0035         SubTrees,      ///< Shows complete subtree for each child. Only leafs are selectable.
0036         DirectChildren ///< Shows only the direct childs.
0037     };
0038     Q_ENUM(LastLevelViewMode)
0039 
0040     /**
0041      * Creates a new MultiLevelListView with parent @p parent.
0042      *
0043      * Call setLevels() afterwards to set the number of list views.
0044      *
0045      * @param parent parent widget
0046      * @param f window flags, passed to QWidget
0047      */
0048     explicit MultiLevelListView(QWidget* parent = nullptr, Qt::WindowFlags f = {});
0049     /**
0050      * Default destructor
0051      */
0052     ~MultiLevelListView() override;
0053 
0054     /**
0055      * @return the number of list view
0056      */
0057     int levels() const;
0058     /**
0059      * Sets the number of levels, i.e. the number of list views visible, to @p levels
0060      * @param levels the new number of levels
0061      */
0062     void setLevels(int levels);
0063 
0064     /**
0065      * @return the model displayed by this view, or 0 if none was set
0066      * @sa QAbstractItemView::model()
0067      */
0068     QAbstractItemModel* model() const;
0069     /**
0070      * Sets the model to be displayed by this view.
0071      *
0072      * @param model the model to be displayed
0073      * @sa QAbstractItemView::setModel()
0074      */
0075     void setModel(QAbstractItemModel* model);
0076     /**
0077      * Provides access to the QTreeView objects used internally.
0078      * Returns the view for level @p level of the tree structure.
0079      *
0080      * @param level the level of the tree structure shown by the returned view
0081      */
0082     QTreeView* viewForLevel(int level) const;
0083 
0084     /**
0085      * The current index of the view.
0086      *
0087      * The current index is determined as the current index of the last list view.
0088      *
0089      * @sa QAbstractItemView::currentIndex()
0090      */
0091     QModelIndex currentIndex() const;
0092 
0093     /**
0094      * Adds the widget @p widget under the list view for level @p level.
0095      * This function can be used to insert custom widgets into the view hierarchy.
0096      *
0097      * @param level specifies where to place the widget
0098      * @param widget the widget to add
0099     */
0100     void addWidget(int level, QWidget* widget);
0101 
0102     void setHeaderLabels(const QStringList& labels);
0103 
0104     /**
0105      * Set the view mode of the view for the last level.
0106      * Default is @c SubTrees.
0107      */
0108     void setLastLevelViewMode(LastLevelViewMode mode);
0109 
0110 Q_SIGNALS:
0111     /**
0112      * Notified that the current index has changed from @p previous to @p current
0113      *
0114      * @param current the new current index
0115      * @param previous the previous index
0116      *
0117      * @sa currentIndex(), QItemSelectionModel::currentChanged()
0118      */
0119     void currentIndexChanged(const QModelIndex& current, const QModelIndex& previous);
0120 
0121 public Q_SLOTS:
0122     /**
0123      * Sets the root index of the entire view to @p index.
0124      *
0125      * @sa QAbstractItemView::setRootIndex()
0126      */
0127     void setRootIndex(const QModelIndex& index);
0128     /**
0129      * Sets the current index to @p index.
0130      *
0131      * @sa currentIndex(), QAbstractItemView::setCurrentIndex()
0132      */
0133     void setCurrentIndex(const QModelIndex& index);
0134 
0135 private:
0136     const QScopedPointer<class MultiLevelListViewPrivate> d_ptr;
0137     Q_DECLARE_PRIVATE(MultiLevelListView)
0138     friend class MultiLevelListViewPrivate;
0139     Q_PRIVATE_SLOT(d_func(), void ensureViewSelected(QTreeView * view))
0140 };
0141 
0142 }
0143 
0144 #endif // KDEVPLATFORM_MULTILEVELLISTVIEW_H