Repository navigation
Expand file tree
/
Copy pathprojectsettingsdialog.cpp
More file actions
126 lines (105 loc) · 3.57 KB
/
Copy pathprojectsettingsdialog.cpp
File metadata and controls
126 lines (105 loc) · 3.57 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "projectsettingsdialog.h"
#include "ui_projectsettingsdialog.h"
#include <QComboBox>
#include <QAbstractItemDelegate>
#include <QItemDelegate>
#include <QStyledItemDelegate>
#include <QStyleOptionComplex>
#include <QDebug>
#include "project.h"
#include "timelineevent.h"
#include "eventtypemodel.h"
class EventPropertyDelegate : public QStyledItemDelegate
{
public:
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/,
const QModelIndex& /*index*/) const
{
QComboBox* editor = new QComboBox(parent);
static const QMetaType::Type types[] = {
QMetaType::QString,
QMetaType::Double,
QMetaType::Int,
QMetaType::Bool
};
for (int i = 0; i < sizeof(types) / sizeof(types[0]); i++) {
editor->addItem(QMetaType::typeName(types[i]),
QVariant((int)types[i]));
}
return editor;
}
void setEditorData(QWidget* editorGeneric, const QModelIndex& index) const
{
QComboBox* editor = (QComboBox*)editorGeneric;
QString typeName = QMetaType::typeName((QMetaType::Type)index.data().toInt());
editor->setCurrentText(typeName);
editor->showPopup();
}
void setModelData(QWidget* editorGeneric, QAbstractItemModel* model, const QModelIndex &index) const
{
QComboBox* editor = (QComboBox*)editorGeneric;
int type = QMetaType::type(editor->currentText().toUtf8().constData());
qDebug() << "setting type to " << type;
model->setData(index, type);
}
void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (index.data().isNull()) {
QStyledItemDelegate::paint(painter, option, index);
return;
}
QStyleOptionComboBox cb;
cb.rect = option.rect;
cb.state = option.state;
cb.frame = true;
cb.currentText = QMetaType::typeName(index.data().toInt());
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &cb, painter);
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &cb, painter);
}
};
ProjectSettingsDialog::ProjectSettingsDialog(const Project* project, QWidget *parent) :
QDialog(parent),
ui(new Ui::ProjectSettingsDialog)
{
ui->setupUi(this);
mEventTypeModel = new EventTypeModel();
ui->tree_eventType->setModel(mEventTypeModel);
ui->tree_eventType->setItemDelegateForColumn(1, new EventPropertyDelegate());
init(project);
}
ProjectSettingsDialog::~ProjectSettingsDialog()
{
delete ui;
}
void ProjectSettingsDialog::init(const Project* project)
{
for (auto it = project->eventTypes().begin(); it != project->eventTypes().end(); it++) {
mEventTypeModel->add(*it);
}
}
void ProjectSettingsDialog::apply(Project* project)
{
auto newEventTypes = mEventTypeModel->toVector();
project->setEventTypes(newEventTypes);
}
void ProjectSettingsDialog::insertEventType()
{
mEventTypeModel->addEventType("New Event Type");
}
void ProjectSettingsDialog::insertProperty()
{
auto current = ui->tree_eventType->currentIndex();
if (!current.isValid())
return;
QModelIndex parent;
if (current.parent().isValid())
parent = current.parent();
else
parent = current;
mEventTypeModel->addProperty(parent, "New Property", QMetaType::QString, QVariant(""));
}
void ProjectSettingsDialog::removeEventTypeOrProperty()
{
mEventTypeModel->remove(ui->tree_eventType->currentIndex());
}