File indexing completed on 2024-04-28 04:37:21

0001 /*
0002     SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PROBLEMSTORENODE_H
0008 #define KDEVPLATFORM_PROBLEMSTORENODE_H
0009 
0010 #include <QString>
0011 #include <interfaces/iproblem.h>
0012 #include <shell/shellexport.h>
0013 
0014 namespace KDevelop
0015 {
0016 
0017 /**
0018  * @brief Base class for ProblemStoreNode classes, which together make up a tree structure with label or problem leaves.
0019  *
0020  * When adding a child the node is automatically reparented.
0021  *
0022  * Usage:
0023  * @code
0024  * ProblemStoreNode *root = new ProblemStoreNode();
0025  * root->addChild(new ProblemStoreNode());
0026  * root->addChild(new ProblemStoreNode());
0027  * root->addChild(new ProblemStoreNode());
0028  * root->count(); // Returns 3
0029  * @endcode
0030  *
0031  */
0032 class KDEVPLATFORMSHELL_EXPORT ProblemStoreNode
0033 {
0034 public:
0035     explicit ProblemStoreNode(ProblemStoreNode *parent = nullptr)
0036     {
0037         m_parent = parent;
0038     }
0039 
0040     virtual ~ProblemStoreNode()
0041     {
0042         clear();
0043     }
0044 
0045     /// Clear the children nodes
0046     void clear()
0047     {
0048         qDeleteAll(m_children);
0049         m_children.clear();
0050     }
0051 
0052     /// Tells if the node is a root node.
0053     /// A node is considered a root node (in this context), when it has no parent
0054     bool isRoot() const
0055     {
0056         if(!m_parent)
0057             return true;
0058         else
0059             return false;
0060     }
0061 
0062     /// Returns the index of this node in the parent's child list.
0063     int index()
0064     {
0065         if(!m_parent)
0066             return -1;
0067 
0068         const QVector<ProblemStoreNode*> &children = m_parent->children();
0069         return children.indexOf(this);
0070     }
0071 
0072     /// Returns the parent of this node
0073     ProblemStoreNode* parent() const{
0074         return m_parent;
0075     }
0076 
0077     /// Sets the parent of this node
0078     void setParent(ProblemStoreNode *parent)
0079     {
0080         m_parent = parent;
0081     }
0082 
0083     /// Returns the number of children nodes
0084     int count() const
0085     {
0086         return m_children.count();
0087     }
0088 
0089     /// Returns a particular child node
0090     ProblemStoreNode* child(int row) const
0091     {
0092         return m_children[row];
0093     }
0094 
0095     /// Returns the list of children nodes
0096     const QVector<ProblemStoreNode*>& children() const{
0097         return m_children;
0098     }
0099 
0100     /// Adds a child node, and reparents the child
0101     void addChild(ProblemStoreNode *child)
0102     {
0103         m_children.push_back(child);
0104         child->setParent(this);
0105     }
0106 
0107     /// Returns the label of this node, if there's one
0108     virtual QString label() const{
0109         return QString();
0110     }
0111 
0112     /// Returns the node's stored problem, if there's such
0113     virtual IProblem::Ptr problem() const{
0114         return IProblem::Ptr(nullptr);
0115     }
0116 
0117 private:
0118     /// The parent node
0119     ProblemStoreNode *m_parent;
0120 
0121     /// Children nodes
0122     QVector<ProblemStoreNode*> m_children;
0123 };
0124 
0125 
0126 ///////////////////////////////////////////////////////////////////////////////////////////////////////
0127 
0128 /**
0129  * @brief A ProblemStoreNode that contains a label. For example: Label for severity or path grouping of problem nodes.
0130  *
0131  * Usage:
0132  * @code
0133  * ProblemStoreNode *root = new ProblemStoreNode();
0134  * ...
0135  * root->addChild(new LabelNode(root, QStringLiteral("ERROR")));
0136  * root->children().last()->label(); // "ERROR"
0137  * @endcode
0138  *
0139  */
0140 class KDEVPLATFORMSHELL_EXPORT LabelNode : public ProblemStoreNode
0141 {
0142 public:
0143     explicit LabelNode(ProblemStoreNode *parent = nullptr, const QString &l = QString())
0144         : ProblemStoreNode(parent)
0145         , m_label(l)
0146     {
0147     }
0148 
0149     ~LabelNode() override
0150     {
0151     }
0152 
0153     QString label() const override { return m_label; }
0154 
0155     /// Sets the label
0156     void setLabel(const QString &s){ m_label = s; }
0157 
0158 private:
0159     /// The label
0160     QString m_label;
0161 };
0162 
0163 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
0164 
0165 /**
0166  * @brief A ProblemStoreNode that contains a problem. For example: as part of a problem list in a severity or path group.
0167  *
0168  * Usage:
0169  * @code
0170  * IProblem::Ptr problem1(new DetectedProblem());
0171  * ...
0172  * label->addChild(new ProblemNode(label, problem1));
0173  * label->children().last()->problem(); // Provides problem1
0174  * @endcode
0175  *
0176  */
0177 class KDEVPLATFORMSHELL_EXPORT ProblemNode : public ProblemStoreNode
0178 {
0179 public:
0180     explicit ProblemNode(ProblemStoreNode *parent = nullptr, const IProblem::Ptr &problem = IProblem::Ptr(nullptr))
0181         : ProblemStoreNode(parent)
0182         , m_problem(problem)
0183     {
0184     }
0185 
0186     ~ProblemNode() override
0187     {
0188     }
0189 
0190     IProblem::Ptr problem() const override {
0191         return m_problem;
0192     }
0193 
0194     /// Sets the problem
0195     void setProblem(const IProblem::Ptr &problem){
0196         m_problem = problem;
0197     }
0198 
0199 
0200 private:
0201     /// The problem
0202     IProblem::Ptr m_problem;
0203 };
0204 
0205 }
0206 
0207 #endif
0208