File indexing completed on 2024-05-19 04:38:51

0001 /*
0002     SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org>
0003     SPDX-FileCopyrightText: 2008 Vladimir Prus <ghost@cs.msu.su>
0004     SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KDEVPLATFORM_VARIABLECOLLECTION_H
0010 #define KDEVPLATFORM_VARIABLECOLLECTION_H
0011 
0012 #include <QPointer>
0013 
0014 #include <KLocalizedString>
0015 #include <KTextEditor/TextHintInterface>
0016 
0017 #include <debugger/debuggerexport.h>
0018 #include "../util/treemodel.h"
0019 #include "../util/treeitem.h"
0020 #include "../../interfaces/idocument.h"
0021 #include "../../interfaces/idebugcontroller.h"
0022 
0023 namespace KDevMI { namespace GDB {
0024     class GdbTest;
0025 }
0026 }
0027 
0028 namespace KDevelop
0029 {
0030 class VariableToolTip;
0031 class IDebugSession;
0032 
0033 class KDEVPLATFORMDEBUGGER_EXPORT Variable : public TreeItem
0034 {
0035     Q_OBJECT
0036     friend class KDevMI::GDB::GdbTest;
0037 public:
0038 protected:
0039     Variable(TreeModel* model, TreeItem* parent,
0040              const QString& expression,
0041              const QString& display = {});
0042 
0043 public:
0044     enum format_t { Natural, Binary, Octal, Decimal, Hexadecimal };
0045     static format_t str2format(const QString& str);
0046     static QString format2str(format_t format);
0047 
0048     QString expression() const;
0049     bool inScope() const;
0050     void setInScope(bool v);
0051     void setValue(const QString &v);
0052     QString value() const;
0053     void setType(const QString& type);
0054     QString type() const;
0055     void setTopLevel(bool v);
0056     void setShowError(bool v);
0057     bool showError();
0058 
0059     using TreeItem::setHasMore;
0060     using TreeItem::setHasMoreInitial;
0061     using TreeItem::appendChild;
0062     using TreeItem::deleteChildren;
0063     using TreeItem::isExpanded;
0064     using TreeItem::parent;
0065 
0066     using TreeItem::model;
0067 
0068     ~Variable() override;
0069 
0070     /* Connects this variable to debugger, fetching the current value and
0071        otherwise preparing this variable to be displayed everywhere.  
0072        The attempt may fail, for example if the expression is invalid.
0073        Calls slot 'callbackMethod' in 'callback' to notify of the result.
0074        The slot should be taking 'bool ok' parameter.  */
0075     virtual void attachMaybe(QObject *callback = nullptr, const char *callbackMethod = nullptr) = 0;
0076 
0077     virtual bool canSetFormat() const { return false; }
0078 
0079     void setFormat(format_t format);
0080     format_t format() const { return m_format; }
0081     virtual void formatChanged();
0082 
0083     // get/set 'changed' state, if the variable changed it will be highlighted
0084     bool isChanged() const { return m_changed; }
0085     void setChanged(bool c);
0086     void resetChanged();
0087 
0088 public Q_SLOTS:
0089     void die();
0090 
0091 protected:
0092     bool topLevel() const { return m_topLevel; }
0093 
0094 private: // TreeItem overrides
0095     QVariant data(int column, int role) const override;
0096 
0097 private:
0098     bool isPotentialProblematicValue() const;
0099 
0100     QString m_expression;
0101     bool m_inScope;
0102     bool m_topLevel;
0103     bool m_changed;
0104     bool m_showError;
0105 
0106     format_t m_format;
0107 };
0108 
0109 class KDEVPLATFORMDEBUGGER_EXPORT TooltipRoot : public TreeItem
0110 {
0111     Q_OBJECT
0112 public:
0113     explicit TooltipRoot(TreeModel* model)
0114     : TreeItem(model)
0115     {}
0116 
0117     void init(Variable *var)
0118     {
0119         appendChild(var);
0120     }
0121 
0122     void fetchMoreChildren() override {}
0123 };
0124 
0125 class KDEVPLATFORMDEBUGGER_EXPORT Watches : public TreeItem
0126 {
0127     Q_OBJECT
0128     friend class KDevMI::GDB::GdbTest;
0129 public:
0130     Watches(TreeModel* model, TreeItem* parent);
0131     Variable* add(const QString& expression);
0132 
0133     void reinstall();
0134 
0135     Variable *addFinishResult(const QString& convenienceVarible);
0136     void removeFinishResult();
0137     void resetChanged();
0138 
0139     using TreeItem::childCount;
0140     friend class VariableCollection;
0141     friend class IVariableController;
0142 private:
0143 
0144     QVariant data(int column, int role) const override;
0145 
0146     void fetchMoreChildren() override {}
0147 
0148     Variable* finishResult_;
0149 };
0150 
0151 class KDEVPLATFORMDEBUGGER_EXPORT Locals : public TreeItem
0152 {
0153     Q_OBJECT
0154 public:
0155     Locals(TreeModel* model, TreeItem* parent, const QString &name);
0156     QList<Variable*> updateLocals(const QStringList& locals);
0157     void resetChanged();
0158 
0159     using TreeItem::deleteChildren;
0160     using TreeItem::setHasMore;
0161 
0162     friend class VariableCollection;
0163     friend class IVariableController;
0164     
0165 private:
0166     void fetchMoreChildren() override {}
0167 };
0168 
0169 class KDEVPLATFORMDEBUGGER_EXPORT VariablesRoot : public TreeItem
0170 {
0171     Q_OBJECT
0172 public:
0173     explicit VariablesRoot(TreeModel* model);
0174 
0175     Watches *watches() const { return m_watches; }
0176     Locals *locals(const QString &name = QStringLiteral("Locals"));
0177     QHash<QString, Locals*> allLocals() const;
0178 
0179     void fetchMoreChildren() override {}
0180 
0181     void resetChanged();
0182 
0183 private:
0184     Watches *m_watches;
0185     QHash<QString, Locals*> m_locals;
0186 };
0187 
0188 class VariableProvider : public KTextEditor::TextHintProvider
0189 {
0190 public:
0191     explicit VariableProvider(VariableCollection* collection);
0192     QString textHint(KTextEditor::View* view, const KTextEditor::Cursor& position) override;
0193 
0194 private:
0195     VariableCollection* m_collection;
0196 };
0197 
0198 class KDEVPLATFORMDEBUGGER_EXPORT VariableCollection : public TreeModel
0199 {
0200     Q_OBJECT
0201 
0202 public:
0203     enum Column {
0204         NameColumn,
0205         ValueColumn,
0206         TypeColumn
0207     };
0208 
0209     explicit VariableCollection(IDebugController* parent);
0210     ~VariableCollection() override;
0211 
0212     VariablesRoot* root() const { return m_universe; }
0213     Watches* watches() const { return m_universe->watches(); }
0214     Locals* locals(const QString &name = QString()) const;
0215     QHash<QString, Locals*> allLocals() const { return m_universe->allLocals(); }
0216 
0217 public Q_SLOTS:
0218     void variableWidgetShown();
0219     void variableWidgetHidden();
0220 
0221 private Q_SLOTS:
0222     void updateAutoUpdate(KDevelop::IDebugSession* session = nullptr);
0223 
0224     void textDocumentCreated( KDevelop::IDocument*);
0225     void viewCreated(KTextEditor::Document*, KTextEditor::View*);
0226 
0227 private:
0228     VariablesRoot* m_universe;
0229     QPointer<VariableToolTip> m_activeTooltip;
0230     bool m_widgetVisible;
0231 
0232     friend class VariableProvider;
0233     VariableProvider m_textHintProvider;
0234 
0235     QVector<KTextEditor::View*> m_textHintProvidedViews;
0236 };
0237 
0238 }
0239 
0240 #endif