File indexing completed on 2024-05-19 05:08:30

0001 /*
0002     SPDX-FileCopyrightText: 2019 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef COLUMNSELECTOR_H
0007 #define COLUMNSELECTOR_H
0008 
0009 #include "kmm_base_widgets_export.h"
0010 
0011 // ----------------------------------------------------------------------------
0012 // QT Includes
0013 
0014 #include <QObject>
0015 #include <QTreeView>
0016 #include <QTableView>
0017 #include <QString>
0018 #include <QPoint>
0019 #include <QVector>
0020 
0021 class ColumnSelectorPrivate;
0022 
0023 // ----------------------------------------------------------------------------
0024 // KDE Includes
0025 
0026 // ----------------------------------------------------------------------------
0027 // Project Includes
0028 
0029 /**
0030  * This class takes care of the selection of visible columns of a tree view and
0031  * their sizes and stores the selection in the global application configuration.
0032  *
0033  * The @a parent tree view must have a model attached that allows to
0034  * extract the maximum number of columns. The header names found in
0035  * the model are displayed in a menu when the user clicks on the
0036  * header with the right mouse button.
0037  *
0038  * @author Thomas Baumgart
0039  */
0040 class KMM_BASE_WIDGETS_EXPORT ColumnSelector : public QObject
0041 {
0042     Q_OBJECT
0043     Q_DISABLE_COPY(ColumnSelector)
0044 
0045 public:
0046     /**
0047      * Creates a ColumnSelector object
0048      *
0049      * @param view pointer to QTreeView object
0050      * @param configGroupName name of the configuration group in the rc file
0051      * @param offset offset to be added/subtracted during load/store operation for certain columns
0052      * @param columns QVector of column indeces (incl. offset) to which the @a offset should be applied
0053      *
0054      * @a offset and @a columns are only used for backward compatibility and should not be used
0055      */
0056     explicit ColumnSelector(QTreeView* view, const QString& configGroupName = QString(), int offset = 0, const QVector<int>& columns = QVector<int>());
0057 
0058     /**
0059      * Creates a ColumnSelector object
0060      *
0061      * @param view pointer to QTableView object
0062      * @param configGroupName name of the configuration group in the rc file
0063      * @param offset offset to be added/subtracted during load/store operation for certain columns
0064      * @param columns QVector of column indeces (incl. offset) to which the @a offset should be applied
0065      *
0066      * @a offset and @a columns are only used for backward compatibility and should not be used
0067      */
0068     explicit ColumnSelector(QTableView* view, const QString& configGroupName = QString(), int offset = 0, const QVector<int>& columns = QVector<int>());
0069 
0070     ~ColumnSelector();
0071 
0072     void setAlwaysHidden(QVector<int> columns);
0073     void setAlwaysVisible(QVector<int> columns);
0074     void setSelectable(QVector<int> columns);
0075     void setModel(QAbstractItemModel* model);
0076     const QAbstractItemModel* model() const;
0077     QVector<int> columns() const;
0078 
0079     /**
0080      * Set the offset to be subtracted from the actual column number
0081      * during load and store operations. This is used to maintain
0082      * backward compatibility and should not be used on new code.
0083      * @a columns contains the indexes to which the @a offset should be applied.
0084      */
0085     void setColumnOffsetForStorage();
0086 
0087     const QString& configGroupName() const;
0088 
0089     /**
0090      * This method allows to suppress the column selection
0091      * which may be required in certain scenarios.
0092      *
0093      * @note By default column selection is enabled
0094      *
0095      * @sa setColumnSelectionEnabled()
0096      */
0097     void setColumnSelectionDisabled();
0098 
0099     /**
0100      * This method allows to enable the column selection
0101      * after it has been disabled.
0102      *
0103      * @note By default column selection is enabled
0104      *
0105      * @sa setColumnSelectionDisabled()
0106      */
0107     void setColumnSelectionEnabled();
0108 
0109 protected Q_SLOTS:
0110     void slotColumnsMenu(const QPoint);
0111     void slotUpdateHeaderState();
0112 
0113 Q_SIGNALS:
0114     void columnsChanged();
0115 
0116 private:
0117     ColumnSelectorPrivate * const d_ptr;
0118     Q_DECLARE_PRIVATE(ColumnSelector)
0119 };
0120 
0121 #endif