File indexing completed on 2024-05-12 04:37:37

0001 /*
0002     SPDX-FileCopyrightText: 2008 Vladimir Prus <ghost@cs.msu.su>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006  
0007 #ifndef KDEVPLATFORM_TREEITEM_H
0008 #define KDEVPLATFORM_TREEITEM_H
0009 
0010 #include <QVariant>
0011 #include <QVector>
0012 #include <QIcon>
0013 
0014 #include <iostream>
0015 
0016 #include <debugger/debuggerexport.h>
0017 
0018 namespace KDevelop {
0019 
0020 class TreeModel;
0021 
0022 class KDEVPLATFORMDEBUGGER_EXPORT TreeItem: public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     ~TreeItem() override;
0027 
0028 // FIXME: should be protected
0029 public: // Methods that the derived classes should implement
0030 
0031     /** Fetches more children, and adds them by calling appendChild.
0032         The amount of children to fetch is up to the implementation.
0033         After fetching, should call setHasMore.  */
0034     virtual void fetchMoreChildren()=0;
0035 
0036     virtual void setColumn(int index, const QVariant& data) { Q_UNUSED(index); Q_UNUSED(data); }
0037     void emitAllChildrenFetched();
0038 
0039 protected: // Interface for derived classes
0040 
0041     /** Creates a tree item with the specified data.  
0042         FIXME: do we actually have to have the model 
0043         pointer.
0044      */
0045     explicit TreeItem(TreeModel* model, TreeItem *parent = nullptr);
0046 
0047     /** Set the data to be shown for the item itself.  */
0048     void setData(const QVector<QVariant> &data);
0049 
0050     /** Adds a new child and notifies the interested parties.  
0051         Clears the "hasMore" flag.  */
0052     void appendChild(TreeItem *child, bool initial = false);
0053 
0054     void insertChild(int position, TreeItem *child, bool initial = false);
0055 
0056     void removeChild(int index);
0057 
0058     void removeSelf();
0059 
0060     void deleteChildren();
0061 
0062     /** Report change in data of this item.  */
0063     void reportChange();
0064     void reportChange(int column);
0065 
0066     /** Clears all children.  */
0067     void clear();
0068 
0069     /** Sets a flag that tells if we have some more children that are
0070         not fetched yet.  */
0071     void setHasMore(bool more);
0072 
0073     void setHasMoreInitial(bool more);
0074 
0075     TreeModel* model() { return model_; }
0076 
0077     bool isExpanded() const { return expanded_; }
0078 
0079 Q_SIGNALS:
0080     void expanded();
0081     void collapsed();
0082     void allChildrenFetched();
0083 
0084 protected: // Backend implementation of Model/View
0085     friend class TreeModel;
0086 
0087     TreeItem *child(int row);
0088     int childCount() const;
0089     int columnCount() const;
0090     virtual QVariant data(int column, int role) const;
0091     int row() const;
0092     TreeItem *parent();
0093     bool hasMore() const { return more_; }
0094     void setExpanded(bool b);
0095 
0096     virtual void clicked() {}
0097     virtual QVariant icon(int column) const;
0098 
0099 protected:
0100     QVector<TreeItem*> childItems;
0101     QVector<QVariant> itemData;
0102     TreeItem *parentItem;
0103     TreeModel *model_;
0104     bool more_;
0105     TreeItem *ellipsis_;
0106     bool expanded_;
0107 };
0108 
0109 }
0110 
0111 #endif