-
Notifications
You must be signed in to change notification settings - Fork 1
/
TemplatePlugin.cc
169 lines (146 loc) · 4.81 KB
/
TemplatePlugin.cc
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "{{ plugin_name }}Plugin.h"
#include <iostream>
{% if filereader -%}
#include <QFileDialog>
#include <QMessageBox>
{%- endif %}
{% if cmd -%}
#include <QCommandLineParser>
#include <QCommandLineOption>
{%- endif %}
{% if add_extension -%}
#include <rbdl_wrapper.h>
{%- endif %}
{{ plugin_name }}Plugin::{{ plugin_name }}Plugin() {
parentApp = NULL;
}
{{ plugin_name }}Plugin::~{{ plugin_name }}Plugin() {
}
void {{ plugin_name }}Plugin::init(ToolkitApp* app) {
//save reference to parent ToolkitApp
parentApp = app;
{% if filereader -%}
load_file_trigger = new QAction("Load {{ plugin_name }}");
parentApp->addFileAction(load_file_trigger);
connect(load_file_trigger, SIGNAL(triggered(bool)), this, SLOT(action_load_data()));
{%- endif %}
{% if cmd -%}
QCommandLineOption {{ plugin_name | lower }}_option( QStringList() << "{{ plugin_name | lower }}",
"Load {{ plugin_name }} files <file>",
"file"
);
parentApp->addCmdOption({{ plugin_name | lower }}_option, [this](QCommandLineParser& parser){
auto data_list = parser.values("{{ plugin_name | lower }}");
{% if add_extension -%}
for (int i=0; i<data_list.size(); i++) {
if (i < parentApp->getLoadedModels()->size() ) {
auto file = data_list[i];
{{ plugin_name }}ModelExtension* ext = nullptr;
try {
ext = load{{ plugin_name }}File(file);
} catch (RigidBodyDynamics::Errors::RBDLError& e){
ToolkitApp::showExceptionDialog(e);
if (ext != nullptr)
delete ext;
continue;
}
RBDLModelWrapper* rbdl_model = parentApp->getLoadedModels()->at(i);
rbdl_model->addExtension(ext);
{% if reload -%}
model_file_map[rbdl_model] = file;
{%- endif %}
} else {
std::cout << QString("{{ plugin_name }} file %1 can not be mapped to a model ... Ignoring!").arg(data_list[i]).toStdString() << std::endl;
}
}
{% else -%}
// implement cmd function here
{%- endif %}
});
{%- endif %}
{% if settings -%}
load{{ plugin_name }}Settings();
{%- endif %}
std::cout << "{{ plugin_name }}Plugin loaded" << std::endl;
{% if reload -%}
connect(parentApp, &ToolkitApp::reloaded_model, this, &{{ plugin_name }}Plugin::reload);
{%- endif %}
}
{% if settings -%}
void {{ plugin_name }}Plugin::load{{ plugin_name }}Settings() {
parentApp->toolkit_settings.beginGroup("{{ plugin_name }}Options");
// example read/write of setting
//QVariant val = parentApp->toolkit_settings.value("marker.color");
//if (val.isNull()) {
// marker_color = QColor::fromRgbF(0., 0., 1., 1.);
// parentApp->toolkit_settings.setValue("marker.color", marker_color.rgba());
//} else {
// marker_color = QColor::fromRgba(val.toUInt());
//}
//parentApp->toolkit_settings.setType("marker.color", marker_color);
parentApp->toolkit_settings.endGroup();
}
{%- endif %}
{% if filereader -%}
void {{ plugin_name }}Plugin::action_load_data() {
if (parentApp != NULL) {
QFileDialog file_dialog (parentApp, "Select {{ plugin_name }} File");
//file_dialog.setNameFilter(tr("{{ plugin_name }} File (*.txt)"));
file_dialog.setFileMode(QFileDialog::ExistingFile);
{% if add_extension -%}
{{ plugin_name }}ModelExtension* ext;
{%- endif %}
if (file_dialog.exec()) {
QString filepath = file_dialog.selectedFiles().at(0);
{% if add_extension -%}
try {
ext = load{{ plugin_name }}File(filepath);
} catch (RigidBodyDynamics::Errors::RBDLError& e){
ToolkitApp::showExceptionDialog(e);
{% if add_extension -%}
delete ext;
{%- endif %}
}
if (parentApp->getLoadedModels()->size() != 0) {
RBDLModelWrapper* rbdl_model = nullptr;
if (parentApp->getLoadedModels()->size() == 1) {
rbdl_model = parentApp->getLoadedModels()->at(0);
} else {
rbdl_model = parentApp->selectModel(nullptr);
}
if (rbdl_model != nullptr) {
rbdl_model->addExtension(ext);
{% if reload -%}
model_file_map[rbdl_model] = file_dialog.selectedFiles().at(0);
{%- endif %}
} else {
delete ext;
}
}
{%- endif %}
}
} else {
//should never happen
throw RigidBodyDynamics::Errors::RBDLError("{{ plugin_name }}Plugin was not initialized correctly!");
}
}
{%- endif %}
{% if filereader and add_extension -%}
{{ plugin_name }}ModelExtension* {{ plugin_name }}Plugin::load{{ plugin_name }}File(QString path) {
{{ plugin_name }}ModelExtension* extension = new {{ plugin_name }}ModelExtension();
//implement open of file + read data + put in extension here
return extension;
}
{%- endif %}
{% if reload -%}
void {{ plugin_name }}Plugin::reload(RBDLModelWrapper *model) {
{% if add_extension -%}
for (auto it = model_file_map.begin(); it != model_file_map.end(); it++) {
if ( it->first == model ) {
auto ext = load{{ plugin_name }}File(it->second);
model->addExtension(ext);
}
}
{%- endif %}
}
{%- endif %}