"Fossies" - the Fresh Open Source Software Archive

Member "cutter-1.8.2/src/widgets/ClassesWidget.h" (20 May 2019, 5481 Bytes) of package /linux/privat/cutter-1.8.2.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 last Fossies "Diffs" side-by-side code changes report: 1.7.4_vs_1.8.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     explicit ClassesModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {}
   57 
   58     QVariant headerData(int section, Qt::Orientation orientation,
   59                         int role = Qt::DisplayRole) const override;
   60 };
   61 
   62 Q_DECLARE_METATYPE(ClassesModel::RowType)
   63 
   64 class BinClassesModel: public ClassesModel
   65 {
   66     Q_OBJECT
   67 
   68 private:
   69     QList<BinClassDescription> classes;
   70 
   71     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
   72 
   73     QModelIndex parent(const QModelIndex &index) const override;
   74     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
   75 
   76     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
   77     QVariant data(const QModelIndex &index, int role) const override;
   78 
   79 public:
   80     explicit BinClassesModel(QObject *parent = nullptr);
   81     void setClasses(const QList<BinClassDescription> &classes);
   82 };
   83 
   84 
   85 class AnalClassesModel: public ClassesModel
   86 {
   87 Q_OBJECT
   88 
   89 private:
   90     /**
   91      * @brief List entry below a class
   92      *
   93      * This roughly corresponds to attributes of r2 anal classes, which means it is not an attribute in the sense of
   94      * a class member variable, but any kind of sub-info associated with the class.
   95      * This struct in particular is used to provide a model for the list entries below a class.
   96      */
   97     struct Attribute
   98     {
   99         enum class Type { VTable, Base, Method };
  100         Type type;
  101         QVariant data;
  102 
  103         Attribute() = default;
  104         Attribute(Type type, const QVariant &data) : type(type), data(data) {}
  105     };
  106 
  107     /**
  108      * This must always stay sorted alphabetically.
  109      */
  110     QList<QString> classes;
  111 
  112     RefreshDeferrer *refreshDeferrer;
  113 
  114     /**
  115      * @brief Cache for class attributes
  116      *
  117      * Maps class names to a list of Attributes.
  118      * This is filled only when the attributes of a specific class are requested.
  119      * (i.e. the user expands the class in the QTreeView)
  120      *
  121      * This must be a pointer instead of just a QMap, because it has to be modified
  122      * in methods that are defined as const by QAbstractItemModel.
  123      */
  124     std::unique_ptr<QMap<QString, QVector<Attribute>>> attrs;
  125 
  126     const QVector<Attribute> &getAttrs(const QString &cls) const;
  127 
  128     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
  129 
  130     QModelIndex parent(const QModelIndex &index) const override;
  131     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  132     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
  133 
  134     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  135     QVariant data(const QModelIndex &index, int role) const override;
  136 
  137 public:
  138     explicit AnalClassesModel(CutterDockWidget *parent);
  139 
  140 public slots:
  141     void refreshAll();
  142     void classNew(const QString &cls);
  143     void classDeleted(const QString &cls);
  144     void classRenamed(const QString &oldName, const QString &newName);
  145     void classAttrsChanged(const QString &cls);
  146 };
  147 
  148 
  149 
  150 class ClassesSortFilterProxyModel : public QSortFilterProxyModel
  151 {
  152     Q_OBJECT
  153 
  154 public:
  155     explicit ClassesSortFilterProxyModel(QObject *parent = nullptr);
  156 
  157 protected:
  158     bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
  159     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
  160     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
  161 };
  162 
  163 
  164 
  165 class ClassesWidget : public CutterDockWidget
  166 {
  167     Q_OBJECT
  168 
  169 public:
  170     explicit ClassesWidget(MainWindow *main, QAction *action = nullptr);
  171     ~ClassesWidget();
  172 
  173 private slots:
  174     void on_classesTreeView_doubleClicked(const QModelIndex &index);
  175 
  176     void on_seekToVTableAction_triggered();
  177     void on_addMethodAction_triggered();
  178     void on_editMethodAction_triggered();
  179     void on_newClassAction_triggered();
  180     void on_deleteClassAction_triggered();
  181     void on_renameClassAction_triggered();
  182 
  183     void showContextMenu(const QPoint &pt);
  184 
  185     void refreshClasses();
  186 
  187 private:
  188     enum class Source { BIN, ANAL };
  189 
  190     Source getSource();
  191 
  192     std::unique_ptr<Ui::ClassesWidget> ui;
  193 
  194     BinClassesModel *bin_model = nullptr;
  195     AnalClassesModel *anal_model = nullptr;
  196     ClassesSortFilterProxyModel *proxy_model;
  197 };
  198 
  199 
  200 #endif // CLASSESWIDGET_H