-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
112 lines (95 loc) · 2.36 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto g1 = root.add("Simple types");
g1->add("bool");
g1->add("int");
g1->add("float");
g1->add("double");
auto g2 = root.add("Structured types");
auto g3 = g2->add("Array");
g3->add("Static");
g3->add("Dynamic");
g2->add("Class");
m_adapter.reset(new VirtualPartAdapter(root));
m_treeModel.reset(new VirtualTreeModel(m_adapter.get()));
ui->treeView->setModel(m_treeModel.get());
}
MainWindow::~MainWindow()
{
ui->treeView->setModel(nullptr);
delete ui;
}
VirtualPartAdapter::VirtualPartAdapter(Part &root) : m_root(root)
{
}
QVariant VirtualPartAdapter::data(void *item, int role)
{
auto value = getValue(item);
if (role == Qt::DisplayRole)
{
return value->name;
}
else
{
return QVariant();
}
}
void *VirtualPartAdapter::getItemParent(void *item)
{
return item ? reinterpret_cast<Part*>(item)->parent : nullptr;
}
void *VirtualPartAdapter::getItem(void *parent, int index)
{
return getValue(parent)->subParts[index].get();
}
Part * VirtualPartAdapter::getValue(void * data)
{
return data ? reinterpret_cast<Part*>(data) : &m_root;
}
int VirtualPartAdapter::getItemsCount(void *parent)
{
return static_cast<int>(getValue(parent)->subParts.size());
}
void MainWindow::on_actionAdd_group_triggered()
{
// we can update data inside beginUpdate / endUpdate block
m_adapter->beginUpdate();
Part* cur = currentPart();
cur = cur ? cur : &root;
auto g1 = cur->add("NewType");
g1->add("my class");
g1->add("my struct");
m_adapter->endUpdate();
ui->treeView->setCurrentIndex(m_treeModel->getItemIndex(g1));
}
void MainWindow::on_actionRemove_current_triggered()
{
Part *part = currentPart();
if (part && part != &root)
{
// we can simply notify view about changes
m_treeModel->QueuedUpdate();
part->parent->remove(part);
}
}
Part *MainWindow::currentPart()
{
return m_adapter->getValue(m_treeModel->getItem(ui->treeView->currentIndex()));
}
void MainWindow::on_actionRemove_model_triggered()
{
m_treeModel->setModelAdapter(nullptr);
}
void MainWindow::on_actionAssign_adapter_triggered()
{
m_treeModel->setModelAdapter(m_adapter.get());
}
void MainWindow::on_actionRemove_adapter_triggered()
{
m_treeModel->setModelAdapter(nullptr);
}