Skip to content

Commit acb054b

Browse files
committed
Get a working model for a QTreeView
1 parent 95815d6 commit acb054b

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

src/sliderulestab.cpp

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,51 @@
55

66
namespace Ripes {
77

8-
ISAInstructionsModel::ISAInstructionsModel(QObject *parent) {}
8+
InstructionItem::InstructionItem(const InstructionBase &instruction,
9+
InstructionItem *parent)
10+
: m_instruction(instruction), m_parent(parent) {
11+
if (!parent)
12+
m_children.push_back(std::make_unique<InstructionItem>(instruction, this));
13+
}
14+
15+
InstructionItem *InstructionItem::child() {
16+
return m_children.size() > 0 ? m_children.at(0).get() : nullptr;
17+
}
18+
19+
const InstructionBase &InstructionItem::data() const { return m_instruction; }
920

10-
int ISAInstructionsModel::rowCount(const QModelIndex &) const { return 0; }
21+
InstructionItem *InstructionItem::parent() { return m_parent; }
1122

12-
int ISAInstructionsModel::columnCount(const QModelIndex &) const { return 0; }
23+
bool InstructionItem::hasParent() const { return m_parent; }
24+
25+
ISAInstructionsModel::ISAInstructionsModel(QObject *parent) {
26+
for (const auto &instr : ProcessorHandler::currentISA()->instructions()) {
27+
m_instrItems.push_back(std::make_unique<InstructionItem>(*instr.get()));
28+
}
29+
}
30+
31+
int ISAInstructionsModel::rowCount(const QModelIndex &parent) const {
32+
if (parent.isValid()) {
33+
return parent.parent().isValid() ? 0 : 1;
34+
}
35+
return m_instrItems.size();
36+
}
37+
38+
int ISAInstructionsModel::columnCount(const QModelIndex &) const { return 1; }
1339

1440
QVariant ISAInstructionsModel::data(const QModelIndex &index, int role) const {
15-
return {};
41+
if (!index.isValid() || role != Qt::DisplayRole) {
42+
return {};
43+
}
44+
45+
const auto *item =
46+
static_cast<const InstructionItem *>(index.internalPointer());
47+
return !item->hasParent() ? item->data().name() : item->data().description();
48+
}
49+
50+
Qt::ItemFlags ISAInstructionsModel::flags(const QModelIndex &index) const {
51+
return index.isValid() ? QAbstractItemModel::flags(index)
52+
: Qt::ItemFlags(Qt::NoItemFlags);
1653
}
1754

1855
QVariant ISAInstructionsModel::headerData(int section,
@@ -21,6 +58,37 @@ QVariant ISAInstructionsModel::headerData(int section,
2158
return {};
2259
}
2360

61+
QModelIndex ISAInstructionsModel::index(int row, int column,
62+
const QModelIndex &parent) const {
63+
if (!hasIndex(row, column, parent)) {
64+
return {};
65+
}
66+
67+
if (parent.isValid()) {
68+
if (auto *child =
69+
static_cast<InstructionItem *>(parent.internalPointer())->child()) {
70+
return createIndex(row, column, child);
71+
}
72+
} else {
73+
if (static_cast<size_t>(row) < m_instrItems.size()) {
74+
return createIndex(row, column, m_instrItems.at(row).get());
75+
}
76+
}
77+
78+
return {};
79+
}
80+
81+
QModelIndex ISAInstructionsModel::parent(const QModelIndex &index) const {
82+
if (!index.isValid()) {
83+
return {};
84+
}
85+
86+
auto *item = static_cast<InstructionItem *>(index.internalPointer());
87+
auto *parent = item->parent();
88+
89+
return parent ? createIndex(0, 0, parent) : QModelIndex{};
90+
}
91+
2492
SliderulesTab::SliderulesTab(QToolBar *toolbar, QWidget *parent)
2593
: RipesTab(toolbar, parent), ui(new Ui::SliderulesTab) {
2694
ui->setupUi(this);
@@ -30,6 +98,9 @@ SliderulesTab::SliderulesTab(QToolBar *toolbar, QWidget *parent)
3098
mainExtBox = ui->mainExtBox;
3199
baseExtCheckBox = ui->baseExtCheckBox;
32100

101+
m_isaModel = std::make_unique<ISAInstructionsModel>();
102+
ui->encodingTable->setModel(m_isaModel.get());
103+
33104
for (const auto &familyName : ISAFamilyNames) {
34105
isaFamilyBox->addItem(familyName.second,
35106
QVariant(static_cast<int>(familyName.first)));

src/sliderulestab.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ namespace Ui {
1919
class SliderulesTab;
2020
}
2121

22-
class ISAInstructionsModel : public QStandardItemModel {
22+
class InstructionItem {
23+
public:
24+
explicit InstructionItem(const InstructionBase &instruction,
25+
InstructionItem *parent = nullptr);
26+
27+
InstructionItem *child();
28+
// int childCount() const;
29+
const InstructionBase &data() const;
30+
InstructionItem *parent();
31+
bool hasParent() const;
32+
33+
private:
34+
std::vector<std::unique_ptr<InstructionItem>> m_children;
35+
const InstructionBase &m_instruction;
36+
InstructionItem *m_parent;
37+
};
38+
39+
class ISAInstructionsModel : public QAbstractItemModel {
2340
Q_OBJECT
2441
public:
2542
ISAInstructionsModel(QObject *parent = nullptr);
@@ -29,8 +46,12 @@ class ISAInstructionsModel : public QStandardItemModel {
2946
virtual int columnCount(const QModelIndex &) const override;
3047
virtual QVariant data(const QModelIndex &index,
3148
int role = Qt::DisplayRole) const override;
49+
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
3250
virtual QVariant headerData(int section, Qt::Orientation orientation,
3351
int role = Qt::DisplayRole) const override;
52+
virtual QModelIndex index(int row, int column,
53+
const QModelIndex &parent = {}) const override;
54+
virtual QModelIndex parent(const QModelIndex &index) const override;
3455

3556
// const ISAInfoBase *isaInfo() const;
3657
// const ISAInfoBase *prevISAInfo() const;
@@ -46,6 +67,8 @@ class ISAInstructionsModel : public QStandardItemModel {
4667
protected:
4768
// std::shared_ptr<const ISAInfoBase> m_isaInfo = nullptr;
4869
// std::shared_ptr<const ISAInfoBase> m_prevIsaInfo = nullptr;
70+
71+
std::vector<std::unique_ptr<InstructionItem>> m_instrItems;
4972
};
5073

5174
class SliderulesTab : public RipesTab {

0 commit comments

Comments
 (0)