|
| 1 | +#include "AstModel.h" |
| 2 | +#include <qbrush.h> |
| 3 | + |
| 4 | + |
| 5 | +AstModel::AstModel(GenericAstNode *data, QObject *parent): |
| 6 | + QAbstractItemModel(parent), |
| 7 | + rootItem(data) |
| 8 | +{ |
| 9 | +} |
| 10 | + |
| 11 | +AstModel::~AstModel() |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +int AstModel::columnCount(const QModelIndex &parent) const |
| 16 | +{ |
| 17 | + return 1; |
| 18 | +} |
| 19 | + |
| 20 | +QVariant AstModel::data(const QModelIndex &index, int role) const |
| 21 | +{ |
| 22 | + if (!index.isValid()) |
| 23 | + return QVariant(); |
| 24 | + |
| 25 | + if (role != Qt::DisplayRole && role != Qt::ForegroundRole && role != Qt::NodeRole) |
| 26 | + return QVariant(); |
| 27 | + |
| 28 | + auto item = static_cast<GenericAstNode*>(index.internalPointer()); |
| 29 | + switch (role) |
| 30 | + { |
| 31 | + case Qt::DisplayRole: |
| 32 | + return QVariant(QString::fromStdString(item->name)); |
| 33 | + case Qt::ForegroundRole: |
| 34 | + switch (item->getColor()) |
| 35 | + { |
| 36 | + case 0: |
| 37 | + return QVariant(QBrush(Qt::GlobalColor::darkBlue)); |
| 38 | + case 1: |
| 39 | + return QVariant(QBrush(Qt::GlobalColor::darkGreen)); |
| 40 | + default: |
| 41 | + return QVariant(QBrush(Qt::GlobalColor::black)); |
| 42 | + } |
| 43 | + case Qt::NodeRole: |
| 44 | + return QVariant::fromValue(item); |
| 45 | + } |
| 46 | + return QVariant(QString::fromStdString(item->name)); |
| 47 | +} |
| 48 | + |
| 49 | +Qt::ItemFlags AstModel::flags(const QModelIndex &index) const |
| 50 | +{ |
| 51 | + if (!index.isValid()) |
| 52 | + return 0; |
| 53 | + |
| 54 | + return QAbstractItemModel::flags(index); |
| 55 | +} |
| 56 | + |
| 57 | +QVariant AstModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 58 | +{ |
| 59 | + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) |
| 60 | + return QVariant("Test"); |
| 61 | + |
| 62 | + return QVariant(); |
| 63 | +} |
| 64 | + |
| 65 | +QModelIndex AstModel::index(int row, int column, const QModelIndex &parent) const |
| 66 | +{ |
| 67 | + if (!hasIndex(row, column, parent)) |
| 68 | + return QModelIndex(); |
| 69 | + |
| 70 | + |
| 71 | + if (!parent.isValid()) |
| 72 | + { |
| 73 | + return rootIndex(); |
| 74 | + } |
| 75 | + |
| 76 | + auto parentItem = static_cast<GenericAstNode*>(parent.internalPointer()); |
| 77 | + auto &childItem = parentItem->myChidren[row]; |
| 78 | + if (childItem) |
| 79 | + return createIndex(row, column, childItem.get()); |
| 80 | + else |
| 81 | + return QModelIndex(); |
| 82 | +} |
| 83 | + |
| 84 | +QModelIndex AstModel::rootIndex() const |
| 85 | +{ |
| 86 | + return createIndex(0, 0, rootItem); |
| 87 | +} |
| 88 | + |
| 89 | +QModelIndex AstModel::parent(const QModelIndex &index) const |
| 90 | +{ |
| 91 | + if (!index.isValid()) |
| 92 | + return QModelIndex(); |
| 93 | + |
| 94 | + GenericAstNode *childItem = static_cast<GenericAstNode*>(index.internalPointer()); |
| 95 | + if (childItem == rootItem || childItem->myParent == nullptr) |
| 96 | + return QModelIndex(); |
| 97 | + |
| 98 | + GenericAstNode *parentItem = childItem->myParent; |
| 99 | + |
| 100 | + if (parentItem == rootItem) |
| 101 | + return rootIndex(); |
| 102 | + auto grandFather = parentItem->myParent; |
| 103 | + auto parentRow = grandFather == nullptr ? |
| 104 | + 0 : |
| 105 | + grandFather->findChildIndex(parentItem); |
| 106 | + |
| 107 | + return createIndex(parentRow, 0, parentItem); |
| 108 | +} |
| 109 | + |
| 110 | +int AstModel::rowCount(const QModelIndex &parent) const |
| 111 | +{ |
| 112 | + GenericAstNode *parentItem; |
| 113 | + if (parent.column() > 0) |
| 114 | + return 0; |
| 115 | + |
| 116 | + if (parent.isValid()) |
| 117 | + { |
| 118 | + parentItem = static_cast<GenericAstNode*>(parent.internalPointer()); |
| 119 | + return parentItem->myChidren.size(); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + return 1; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +bool AstModel::hasChildren(const QModelIndex &parent) const |
| 128 | +{ |
| 129 | + GenericAstNode *parentItem; |
| 130 | + if (parent.column() > 0) |
| 131 | + return false; |
| 132 | + |
| 133 | + if (parent.isValid()) |
| 134 | + parentItem = static_cast<GenericAstNode*>(parent.internalPointer()); |
| 135 | + else |
| 136 | + parentItem = rootItem; |
| 137 | + |
| 138 | + return !parentItem->myChidren.empty(); |
| 139 | + |
| 140 | +} |
| 141 | + |
0 commit comments