-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathalgorithmwidget.cpp
293 lines (245 loc) · 9.99 KB
/
algorithmwidget.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "algorithmwidget.h"
#include "boost/foreach.hpp"
#include <QHBoxLayout>
#include <QGridLayout>
#include <QtConcurrent>
namespace {
void copyStringVector(const std::vector<std::string> &input, QVector<QString> &output)
{
output.clear();
std::transform(input.begin(), input.end(), std::back_inserter(output), &QString::fromStdString);
}
} // namespace
namespace tvseg_ui {
AlgorithmWidget::AlgorithmWidget(QWidget *parent) :
QWidget(parent),
display_(new CvImageDisplayWidget),
sidePane_(new QVBoxLayout),
actionsGroup_(new QGroupBox),
groupLayout_(new QGridLayout),
settingsEditor_(new SettingsEditor),
computeButtonMapper_(new QSignalMapper(this)),
clearButtonMapper_(new QSignalMapper(this)),
showButtonMapper_(new QSignalMapper(this)),
watcher_(new QFutureWatcher<void>(this)),
runningAction_(-1),
runAllButton_(new QPushButton),
saveResultButton_(new QPushButton),
computeMetricsButton_(new QPushButton),
commandsGroup_(new QGroupBox),
presetsGroup_(new QGroupBox),
presetsComboBox_(new QComboBox),
presetsSetButton_(new QPushButton)
{
QHBoxLayout *topLayout = new QHBoxLayout;
setLayout(topLayout);
topLayout->addWidget(display_, 1);
topLayout->addLayout(sidePane_, 0);
sidePane_->addWidget(commandsGroup_, 0);
sidePane_->addWidget(actionsGroup_, 0);
sidePane_->addWidget(presetsGroup_, 0);
sidePane_->addWidget(settingsEditor_, 1);
// FIXME: find out why margins generated by designer/uic tool does not correspond to defaults show in designer or used if nothing is set.
topLayout->setContentsMargins(11,11,11,11);
topLayout->setSpacing(6);
actionsGroup_->setTitle("Actions");
actionsGroup_->setLayout(groupLayout_);
groupLayout_->setSpacing(6);
commandsGroup_->setTitle("Commands");
QVBoxLayout *commandsLayout = new QVBoxLayout;
commandsLayout->setContentsMargins(11,11,11,11);
commandsLayout->setSpacing(6);
commandsGroup_->setLayout(commandsLayout);
runAllButton_->setText("run all");
commandsLayout->addWidget(runAllButton_);
connect(runAllButton_, SIGNAL(clicked()), this, SIGNAL(runAll()));
saveResultButton_->setText("save result");
commandsLayout->addWidget(saveResultButton_);
connect(saveResultButton_, SIGNAL(clicked()), this, SIGNAL(saveResult()));
computeMetricsButton_->setText("compute metrics");
commandsLayout->addWidget(computeMetricsButton_);
connect(computeMetricsButton_, SIGNAL(clicked()), this, SIGNAL(computeMetrics()));
connect(computeButtonMapper_, SIGNAL(mapped(int)), this, SLOT(computeButtonClicked(int)));
connect(clearButtonMapper_, SIGNAL(mapped(int)), this, SLOT(clearButtonClicked(int)));
connect(showButtonMapper_, SIGNAL(mapped(int)), this, SLOT(showButtonClicked(int)));
connect(watcher_, SIGNAL(started()), this, SIGNAL(algorithmStarted()));
connect(watcher_, SIGNAL(finished()), this, SIGNAL(algorithmStopped()));
connect(watcher_, SIGNAL(finished()), this, SLOT(algorithmCompleted()));
connect(settingsEditor_, SIGNAL(valueChanged(QtProperty*,QVariant)), this, SLOT(settingsValueChanged(QtProperty*,QVariant)));
presetsGroup_->setTitle("Setting presets");
QHBoxLayout *presetsLayout = new QHBoxLayout;
presetsLayout->setContentsMargins(11,11,11,11);
presetsLayout->setMargin(6);
presetsGroup_->setLayout(presetsLayout);
presetsLayout->addWidget(presetsComboBox_, 1);
presetsLayout->addWidget(presetsSetButton_, 0);
presetsComboBox_->addItem("<select>");
presetsSetButton_->setText("set");
connect(presetsSetButton_, SIGNAL(clicked()), this, SLOT(setPresetClicked()));
}
void AlgorithmWidget::addAlgorithm(const tvseg::CVAlgorithmInterface *algorithmInterface)
{
// save a copy of the algorithm interface
const int algoId = algorithms_.size();
algorithms_.push_back(tvseg::CVAlgorithmInterfaceBase(algorithmInterface));
// first id of actions
int actionId = actions_.size();
// setup settings editor
tvseg::settings::SettingsPtr settings = algorithmInterface->settings();
if (settings) {
// TODO: remove `settingsInSetup_` and make sure settingseditor only send value changed when settings actually
// changed, not when edit was attempted.
copyStringVector(settings->names(), settingsInSetup_);
QVector<QtProperty*> addedProperties = settingsEditor_->addEntries(*settings);
foreach (QtProperty* p, addedProperties) {
if (p) {
propertyMap_[p] = QPair<int,int>(algoId, actionId);
}
}
settingsInSetup_.clear();
}
// setup action buttons
BOOST_FOREACH(const tvseg::CVAlgorithmInterface::Action &action, algorithmInterface->actions()) {
actions_.push_back(action);
QLabel *label = new QLabel;
label->setText(QString::fromStdString(action.name));
QPushButton *computeButton = new QPushButton;
computeButton->setText("c");
connect(computeButton, SIGNAL(clicked()), computeButtonMapper_, SLOT(map()));
computeButtonMapper_->setMapping(computeButton, actionId);
QPushButton *showButton = new QPushButton;
showButton->setText("s");
connect(showButton, SIGNAL(clicked()), showButtonMapper_, SLOT(map()));
showButtonMapper_->setMapping(showButton, actionId);
showButtons_.push_back(showButton);
showButton->setEnabled(action.resultAvailable());
QPushButton *clearButton = new QPushButton;
clearButton->setText("x");
connect(clearButton, SIGNAL(clicked()), clearButtonMapper_, SLOT(map()));
clearButtonMapper_->setMapping(clearButton, actionId);
clearButtons_.push_back(clearButton);
clearButton->setEnabled(action.resultAvailable());
groupLayout_->addWidget(label, actionId, 0);
groupLayout_->addWidget(computeButton, actionId, 1);
groupLayout_->addWidget(showButton, actionId, 2);
groupLayout_->addWidget(clearButton, actionId, 3);
++actionId;
}
}
void AlgorithmWidget::clear()
{
propertyMap_.clear();
algorithms_.clear();
settingsEditor_->clear();
clearActions();
}
void AlgorithmWidget::updateFromSettings()
{
settingsEditor_->updateFromSettings();
}
void AlgorithmWidget::addPreset(QString name)
{
presetsComboBox_->addItem(name);
}
void AlgorithmWidget::computeButtonClicked(int id)
{
if (id < 0 || id >= actions_.size()) {
LWARNING << "Invalid action id " << id << ". Ignore button click.";
return;
}
runningAction_ = id;
QFuture<void> future = QtConcurrent::run(actions_[id].computeFn);
watcher_->setFuture(future);
}
void AlgorithmWidget::clearButtonClicked(int id)
{
if (id < 0 || id >= actions_.size()) {
LWARNING << "Invalid action id " << id << ". Ignore button click.";
return;
}
actions_[id].clear();
display_->clearImage();
clearButtons_[id]->setEnabled(false);
showButtons_[id]->setEnabled(false);
}
void AlgorithmWidget::showButtonClicked(int id)
{
if (id < 0 || id >= actions_.size()) {
LWARNING << "Invalid action id " << id << ". Ignore button click.";
return;
}
const tvseg::CVAlgorithmInterface::Action &action = actions_[id];
if (action.resultAvailable()) {
display_->setImage(action.result());
clearButtons_[id]->setEnabled(true);
showButtons_[id]->setEnabled(true);
} else {
clearButtons_[id]->setEnabled(false);
showButtons_[id]->setEnabled(false);
LWARNING << "Result for action '" << action.name << "' not available. Cannot display.";
}
}
void AlgorithmWidget::settingsValueChanged(QtProperty *property, QVariant /*value*/)
{
if (property) {
if (!propertyMap_.contains(property)) {
if (!settingsInSetup_.contains(property->propertyName())) {
LWARNING << "AlgorithmWidget: Got valueChanged signal for unregistered property '" << property->propertyName() << "' with value '" << property->displayText() << "'";
}
return;
}
const int algoId = propertyMap_[property].first;
if (0 > algoId || algoId >= algorithms_.size()) {
LWARNING << "AlgorithmWidget: Invalid algorithm id '" << algoId << "' registered for property '" << property->propertyName() << "' with value '" << property->displayText() << "'";
return;
}
tvseg::settings::SettingsConstPtr s = algorithms_[algoId].settings();
std::string name = "autoRecompute";
tvseg::settings::EntryConstPtr e = s->getEntry(name);
if (e) {
boost::any value = e->value();
const bool* autoCompute = boost::any_cast<bool>(&value);
if (!autoCompute) {
LWARNING << "AlgorithmWidget: autoRecompute setting not boolean.";
return;
}
if (*autoCompute) {
const int actionId = propertyMap_[property].second;
if (actionId < 0 || actionId >= actions_.size()) {
LWARNING << "AlgorithmWidget: Invalid action id " << actionId << ".";
return;
}
// if result was computed before, recompute it.
if (actions_[actionId].resultAvailable()) {
computeButtonClicked(actionId);
}
}
}
}
}
void AlgorithmWidget::algorithmCompleted()
{
showButtonClicked(runningAction_);
}
void AlgorithmWidget::setPresetClicked()
{
if (presetsComboBox_->currentIndex() >= 1) {
QString name = presetsComboBox_->currentText();
presetsComboBox_->setCurrentIndex(0);
emit setPreset(name);
}
}
void AlgorithmWidget::clearActions()
{
showButtons_.clear();
clearButtons_.clear();
actions_.clear();
for (QLayoutItem* item = actionsGroup_->layout()->itemAt(0); item != NULL; item = actionsGroup_->layout()->itemAt(0)) {
QWidget *w = item->widget();
if (w) {
delete w;
}
delete item;
}
}
} // namespace tvseg_ui