File indexing completed on 2024-05-12 15:27:33

0001 /***************************************************************************
0002 File                 : TreeModel.h
0003 Project              : LabPlot
0004 Description          : This is an abstract treemodel which can be used by a treeview
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2019 Martin Marmsoler (martin.marmsoler@gmail.com)
0007 
0008 ***************************************************************************/
0009 
0010 /***************************************************************************
0011 *                                                                         *
0012 *  This program is free software; you can redistribute it and/or modify   *
0013 *  it under the terms of the GNU General Public License as published by   *
0014 *  the Free Software Foundation; either version 2 of the License, or      *
0015 *  (at your option) any later version.                                    *
0016 *                                                                         *
0017 *  This program is distributed in the hope that it will be useful,        *
0018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020 *  GNU General Public License for more details.                           *
0021 *                                                                         *
0022 *   You should have received a copy of the GNU General Public License     *
0023 *   along with this program; if not, write to the Free Software           *
0024 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025 *   Boston, MA  02110-1301  USA                                           *
0026 *                                                                         *
0027 ***************************************************************************/
0028 
0029 #ifndef TREEMODEL_H
0030 #define TREEMODEL_H
0031 
0032 #include <QAbstractItemModel>
0033 #include <QColor>
0034 
0035 /*!
0036  * \brief The TreeItem class
0037  * Item in the treemodel
0038  */
0039 class TreeItem {
0040 public:
0041     explicit TreeItem(const QVector<QVariant>& data, TreeItem* parent = 0);
0042     ~TreeItem();
0043 
0044     TreeItem* child(int number);
0045     int childCount() const;
0046     int columnCount() const;
0047     QVariant data(int column) const;
0048     QVariant backgroundColor() const;
0049     bool insertChildren(int position, int count, int columns);
0050     bool insertColumns(int position, int columns);
0051     TreeItem *parent();
0052     bool removeChildren(int position, int count);
0053     bool removeColumns(int position, int columns);
0054     int childNumber() const;
0055     bool setData(int column, const QVariant& value);
0056     bool setBackgroundColor(int column, const QVariant& value);
0057 
0058 private:
0059     QList<TreeItem*> childItems;
0060     QVector<QVariant> itemData;
0061     QColor m_backgroundColor{QColor(0,0,0,0)};
0062     TreeItem *parentItem{nullptr};
0063 };
0064 /*!
0065  * \brief The TreeModel class
0066  * This is an abstract treemodel which can be used by a treeview
0067  */
0068 class TreeModel : public QAbstractItemModel {
0069     Q_OBJECT
0070 
0071 public:
0072     explicit TreeModel(const QStringList& headers, QObject* parent = nullptr);
0073     ~TreeModel();
0074     QVariant treeData(const int row, const int column, const QModelIndex& parent = QModelIndex(), const int role = Qt::EditRole);
0075     QVariant data(const QModelIndex&, int role) const override;
0076     QVariant headerData(int section, Qt::Orientation orientation,
0077                         int role = Qt::DisplayRole) const override;
0078 
0079     QModelIndex index(int row, int column,
0080                         const QModelIndex& parent = QModelIndex()) const override;
0081     QModelIndex parent(const QModelIndex&) const override;
0082 
0083     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0084     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0085 
0086     Qt::ItemFlags flags(const QModelIndex&) const override;
0087     bool setTreeData(const QVariant& data, const int row, const int column,
0088                         const QModelIndex& parent = QModelIndex(), int role = Qt::EditRole);
0089     bool setData(const QModelIndex&, const QVariant& value,
0090                     int role = Qt::EditRole) override;
0091     int compareStrings(const QString& value, const int row, const int column, const QModelIndex& parent = QModelIndex());
0092     bool setHeaderData(int section, Qt::Orientation orientation,
0093                         const QVariant& value, int role = Qt::EditRole) override;
0094 
0095     bool insertColumns(int position, int columns,
0096                         const QModelIndex& parent = QModelIndex()) override;
0097     bool removeColumns(int position, int columns,
0098                         const QModelIndex& parent = QModelIndex()) override;
0099     bool insertRows(int position, int rows,
0100                     const QModelIndex& parent = QModelIndex()) override;
0101     bool removeRows(int position, int rows,
0102                     const QModelIndex& parent = QModelIndex()) override;
0103 
0104 private:
0105     TreeItem* getItem(const QModelIndex&) const;
0106     TreeItem* rootItem{nullptr};
0107 };
0108 
0109 #endif // TREEMODEL_H