"Fossies" - the Fresh Open Source Software Archive

Member "cutter-1.9.0/src/widgets/ClassesWidget.h" (6 Sep 2019, 5688 Bytes) of package /linux/privat/cutter-1.9.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ClassesWidget.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.8.3_vs_1.9.0.

    1 #ifndef CLASSESWIDGET_H
    2 #define CLASSESWIDGET_H
    3 
    4 #include <memory>
    5 
    6 #include "core/Cutter.h"
    7 #include "CutterDockWidget.h"
    8 
    9 #include <QAbstractListModel>
   10 #include <QSortFilterProxyModel>
   11 
   12 namespace Ui {
   13 class ClassesWidget;
   14 }
   15 
   16 class QTreeWidget;
   17 class QTreeWidgetItem;
   18 class MainWindow;
   19 class ClassesWidget;
   20 
   21 /**
   22  * @brief Common abstract base class for Bin and Anal classes models
   23  */
   24 class ClassesModel: public QAbstractItemModel
   25 {
   26 public:
   27     enum Columns { NAME = 0, TYPE, OFFSET, VTABLE, COUNT };
   28 
   29     /**
   30      * @brief values for TypeRole data
   31      */
   32     enum class RowType { Class = 0, Base, VTable, Method, Field };
   33 
   34     /**
   35      * @brief Offset role of data for QModelIndex
   36      *
   37      * will contain values of type RVA
   38      */
   39     static const int OffsetRole = Qt::UserRole;
   40 
   41     /**
   42      * @brief Name role of data for QModelIndex
   43      *
   44      * will contain values of QString, used for sorting,
   45      * as well as identifying classes and methods
   46      */
   47     static const int NameRole = Qt::UserRole + 1;
   48 
   49     /**
   50      * @brief Type role of data for QModelIndex
   51      *
   52      * will contain values of RowType
   53      */
   54     static const int TypeRole = Qt::UserRole + 2;
   55 
   56     /**
   57      * @brief VTable role of data for QModelIndex
   58      *
   59      * will contain values of type long long for sorting
   60      * by vtable offset
   61      */
   62     static const int VTableRole = Qt::UserRole + 3;
   63 
   64     explicit ClassesModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {}
   65 
   66     QVariant headerData(int section, Qt::Orientation orientation,
   67                         int role = Qt::DisplayRole) const override;
   68 };
   69 
   70 Q_DECLARE_METATYPE(ClassesModel::RowType)
   71 
   72 class BinClassesModel: public ClassesModel
   73 {
   74     Q_OBJECT
   75 
   76 private:
   77     QList<BinClassDescription> classes;
   78 
   79     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
   80 
   81     QModelIndex parent(const QModelIndex &index) const override;
   82     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
   83 
   84     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
   85     QVariant data(const QModelIndex &index, int role) const override;
   86 
   87 public:
   88     explicit BinClassesModel(QObject *parent = nullptr);
   89     void setClasses(const QList<BinClassDescription> &classes);
   90 };
   91 
   92 
   93 class AnalClassesModel: public ClassesModel
   94 {
   95 Q_OBJECT
   96 
   97 private:
   98     /**
   99      * @brief List entry below a class
  100      *
  101      * This roughly corresponds to attributes of r2 anal classes, which means it is not an attribute in the sense of
  102      * a class member variable, but any kind of sub-info associated with the class.
  103      * This struct in particular is used to provide a model for the list entries below a class.
  104      */
  105     struct Attribute
  106     {
  107         enum class Type { VTable, Base, Method };
  108         Type type;
  109         QVariant data;
  110 
  111         Attribute() = default;
  112         Attribute(Type type, const QVariant &data) : type(type), data(data) {}
  113     };
  114 
  115     /**
  116      * This must always stay sorted alphabetically.
  117      */
  118     QList<QString> classes;
  119 
  120     RefreshDeferrer *refreshDeferrer;
  121 
  122     /**
  123      * @brief Cache for class attributes
  124      *
  125      * Maps class names to a list of Attributes.
  126      * This is filled only when the attributes of a specific class are requested.
  127      * (i.e. the user expands the class in the QTreeView)
  128      *
  129      * This must be a pointer instead of just a QMap, because it has to be modified
  130      * in methods that are defined as const by QAbstractItemModel.
  131      */
  132     std::unique_ptr<QMap<QString, QVector<Attribute>>> attrs;
  133 
  134     const QVector<Attribute> &getAttrs(const QString &cls) const;
  135 
  136     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
  137 
  138     QModelIndex parent(const QModelIndex &index) const override;
  139     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  140     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
  141 
  142     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  143     QVariant data(const QModelIndex &index, int role) const override;
  144 
  145 public:
  146     explicit AnalClassesModel(CutterDockWidget *parent);
  147 
  148 public slots:
  149     void refreshAll();
  150     void classNew(const QString &cls);
  151     void classDeleted(const QString &cls);
  152     void classRenamed(const QString &oldName, const QString &newName);
  153     void classAttrsChanged(const QString &cls);
  154 };
  155 
  156 
  157 
  158 class ClassesSortFilterProxyModel : public QSortFilterProxyModel
  159 {
  160     Q_OBJECT
  161 
  162 public:
  163     explicit ClassesSortFilterProxyModel(QObject *parent = nullptr);
  164 
  165 protected:
  166     bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
  167     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
  168     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
  169 };
  170 
  171 
  172 
  173 class ClassesWidget : public CutterDockWidget
  174 {
  175     Q_OBJECT
  176 
  177 public:
  178     explicit ClassesWidget(MainWindow *main, QAction *action = nullptr);
  179     ~ClassesWidget();
  180 
  181 private slots:
  182     void on_classesTreeView_doubleClicked(const QModelIndex &index);
  183 
  184     void on_seekToVTableAction_triggered();
  185     void on_addMethodAction_triggered();
  186     void on_editMethodAction_triggered();
  187     void on_newClassAction_triggered();
  188     void on_deleteClassAction_triggered();
  189     void on_renameClassAction_triggered();
  190 
  191     void showContextMenu(const QPoint &pt);
  192 
  193     void refreshClasses();
  194 
  195 private:
  196     enum class Source { BIN, ANAL };
  197 
  198     Source getSource();
  199 
  200     std::unique_ptr<Ui::ClassesWidget> ui;
  201 
  202     BinClassesModel *bin_model = nullptr;
  203     AnalClassesModel *anal_model = nullptr;
  204     ClassesSortFilterProxyModel *proxy_model;
  205 };
  206 
  207 
  208 #endif // CLASSESWIDGET_H