File indexing completed on 2025-01-05 03:58:34

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-06-21
0007  * Description : A simple model to hold a tree structure.
0008  *
0009  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010-2014 by Michael G. Hansen <mike at mghansen dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_SIMPLE_TREE_MODEL_H
0017 #define DIGIKAM_SIMPLE_TREE_MODEL_H
0018 
0019 // Qt includes
0020 
0021 #include <QAbstractItemModel>
0022 
0023 // Local includes
0024 
0025 #include "digikam_export.h"
0026 
0027 namespace Digikam
0028 {
0029 
0030 class DIGIKAM_EXPORT SimpleTreeModel : public QAbstractItemModel
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035 
0036     class Item
0037     {
0038     public:
0039 
0040         explicit Item()
0041           : dataColumns(),
0042             parent     (nullptr),
0043             children   ()
0044         {
0045         }
0046 
0047         ~Item()
0048         {
0049             qDeleteAll(children);
0050         }
0051 
0052     public:
0053 
0054         QString                     data;
0055 
0056     private:
0057 
0058         QList<QMap<int, QVariant> > dataColumns;
0059         Item*                       parent;
0060         QList<Item*>                children;
0061 
0062         friend class SimpleTreeModel;
0063     };
0064 
0065     explicit SimpleTreeModel(const int columnCount, QObject* const parent = nullptr);
0066     ~SimpleTreeModel()                                                                                    override;
0067 
0068     /// QAbstractItemModel:
0069     int columnCount(const QModelIndex& parent = QModelIndex())                                      const override;
0070     bool setData(const QModelIndex& index, const QVariant& value, int role)                               override;
0071     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole)                             const override;
0072     QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex())               const override;
0073     QModelIndex parent(const QModelIndex& index)                                                    const override;
0074     int rowCount(const QModelIndex& parent = QModelIndex())                                         const override;
0075     bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)         override;
0076     QVariant headerData(int section, Qt::Orientation orientation, int role)                         const override;
0077     Qt::ItemFlags flags(const QModelIndex& index)                                                   const override;
0078 
0079     Item* addItem(Item* const parentItem = nullptr, const int rowNumber = -1);
0080     Item* indexToItem(const QModelIndex& itemIndex)                                                 const;
0081     Item* rootItem()                                                                                const;
0082     QModelIndex itemToIndex(const Item* const item)                                                 const;
0083 
0084 private:
0085 
0086     class Private;
0087     Private* const d;
0088 };
0089 
0090 } // namespace Digikam
0091 
0092 #endif // DIGIKAM_SIMPLE_TREE_MODEL_H