forked from swri-robotics/mapviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_plugin.cpp
435 lines (383 loc) · 13.1 KB
/
string_plugin.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// *****************************************************************************
//
// Copyright (c) 2015, Southwest Research Institute® (SwRI®)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Southwest Research Institute® (SwRI®) nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// *****************************************************************************
#include <mapviz_plugins/string_plugin.h>
#include <mapviz/select_topic_dialog.h>
#include <QFontDialog>
#include <pluginlib/class_list_macros.hpp>
#include <string>
PLUGINLIB_EXPORT_CLASS(mapviz_plugins::StringPlugin, mapviz::MapvizPlugin)
namespace mapviz_plugins
{
const char* StringPlugin::COLOR_KEY = "color";
const char* StringPlugin::FONT_KEY = "font";
const char* StringPlugin::TOPIC_KEY = "topic";
const char* StringPlugin::ANCHOR_KEY = "anchor";
const char* StringPlugin::UNITS_KEY = "units";
const char* StringPlugin::OFFSET_X_KEY = "offset_x";
const char* StringPlugin::OFFSET_Y_KEY = "offset_y";
StringPlugin::StringPlugin()
: MapvizPlugin()
, ui_()
, config_widget_(new QWidget())
, anchor_(TOP_LEFT)
, units_(PIXELS)
, offset_x_(0)
, offset_y_(0)
, has_message_(false)
, has_painted_(false)
, color_(Qt::black)
{
ui_.setupUi(config_widget_);
// Set background white
QPalette p(config_widget_->palette());
p.setColor(QPalette::Window, Qt::white);
config_widget_->setPalette(p);
// Set status text red
QPalette p3(ui_.status->palette());
p3.setColor(QPalette::Text, Qt::red);
ui_.status->setPalette(p3);
QObject::connect(ui_.selecttopic, SIGNAL(clicked()), this, SLOT(SelectTopic()));
QObject::connect(ui_.topic, SIGNAL(editingFinished()), this, SLOT(TopicEdited()));
QObject::connect(ui_.anchor, SIGNAL(activated(QString)), this, SLOT(SetAnchor(QString)));
QObject::connect(ui_.units, SIGNAL(activated(QString)), this, SLOT(SetUnits(QString)));
QObject::connect(ui_.offsetx, SIGNAL(valueChanged(int)), this, SLOT(SetOffsetX(int)));
QObject::connect(ui_.offsety, SIGNAL(valueChanged(int)), this, SLOT(SetOffsetY(int)));
QObject::connect(ui_.font_button, SIGNAL(clicked()), this, SLOT(SelectFont()));
QObject::connect(ui_.color, SIGNAL(colorEdited(const QColor &)), this, SLOT(SelectColor()));
font_.setFamily(tr("Helvetica"));
ui_.font_button->setFont(font_);
ui_.font_button->setText(font_.family());
ui_.color->setColor(color_);
}
bool StringPlugin::Initialize(QGLWidget* canvas)
{
canvas_ = canvas;
return true;
}
void StringPlugin::Draw(double, double, double)
{
// This plugin doesn't do any OpenGL drawing.
}
void StringPlugin::Paint(QPainter* painter, double, double, double)
{
if (has_message_)
{
painter->save();
painter->resetTransform();
painter->setFont(font_);
if (!has_painted_)
{
// After the first time we get a new message, we do not know how wide it's
// going to be when rendered, so we can't accurately calculate the top left
// coordinate if it's offset from the right or bottom borders.
// The easiest workaround I've found for this is to draw it once using
// a completely transparent pen, which will cause the QStaticText class to
// know how wide it is; then we can recalculate the offsets and draw it
// again with a visible pen.
QPen invisPen(QBrush(Qt::transparent), 1);
painter->setPen(invisPen);
PaintText(painter);
has_painted_ = true;
}
QPen pen(QBrush(color_), 1);
painter->setPen(pen);
PaintText(painter);
painter->restore();
PrintInfo("OK");
} else {
PrintWarning("No messages received.");
}
}
void StringPlugin::PaintText(QPainter* painter)
{
// Calculate the correct offsets and dimensions
int x_offset = offset_x_;
int y_offset = offset_y_;
if (units_ == PERCENT)
{
x_offset = static_cast<int>(static_cast<float>(offset_x_ * canvas_->width()) / 100.0);
y_offset = static_cast<int>(static_cast<float>(offset_y_ * canvas_->height()) / 100.0);
}
int right = static_cast<int>(
static_cast<float>(canvas_->width()) - message_.size().width()) - x_offset;
int bottom = static_cast<int>(
static_cast<float>(canvas_->height()) - message_.size().height()) - y_offset;
int yCenter = static_cast<int>(
static_cast<float>(canvas_->height()) / 2.0 - message_.size().height()/2.0);
int xCenter = static_cast<int>(
static_cast<float>(canvas_->width()) / 2.0 - message_.size().width()/2.0);
QPoint ulPoint;
switch (anchor_)
{
case TOP_LEFT:
ulPoint.setX(x_offset);
ulPoint.setY(y_offset);
break;
case TOP_CENTER:
ulPoint.setX(xCenter);
ulPoint.setY(y_offset);
break;
case TOP_RIGHT:
ulPoint.setX(right);
ulPoint.setY(y_offset);
break;
case CENTER_LEFT:
ulPoint.setX(x_offset);
ulPoint.setY(yCenter);
break;
case CENTER:
ulPoint.setX(xCenter);
ulPoint.setY(yCenter);
break;
case CENTER_RIGHT:
ulPoint.setX(right);
ulPoint.setY(yCenter);
break;
case BOTTOM_LEFT:
ulPoint.setX(x_offset);
ulPoint.setY(bottom);
break;
case BOTTOM_CENTER:
ulPoint.setX(xCenter);
ulPoint.setY(bottom);
break;
case BOTTOM_RIGHT:
ulPoint.setX(right);
ulPoint.setY(bottom);
break;
}
painter->drawStaticText(ulPoint, message_);
}
void StringPlugin::LoadConfig(const YAML::Node& node, const std::string& path)
{
if (node[TOPIC_KEY])
{
ui_.topic->setText(QString(node[TOPIC_KEY].as<std::string>().c_str()));
TopicEdited();
}
if (node[FONT_KEY])
{
font_.fromString(QString(node[FONT_KEY].as<std::string>().c_str()));
ui_.font_button->setFont(font_);
ui_.font_button->setText(font_.family());
}
if (node[COLOR_KEY])
{
color_ = QColor(node[COLOR_KEY].as<std::string>().c_str());
ui_.color->setColor(QColor(color_.name().toStdString().c_str()));
}
if (node[ANCHOR_KEY])
{
std::string anchor = node[ANCHOR_KEY].as<std::string>();
ui_.anchor->setCurrentIndex(ui_.anchor->findText(anchor.c_str()));
SetAnchor(anchor.c_str());
}
if (node[UNITS_KEY])
{
std::string units = node[UNITS_KEY].as<std::string>();
ui_.units->setCurrentIndex(ui_.units->findText(units.c_str()));
SetUnits(units.c_str());
}
if (node[OFFSET_X_KEY])
{
offset_x_ = node[OFFSET_X_KEY].as<int>();
ui_.offsetx->setValue(offset_x_);
}
if (node[OFFSET_Y_KEY])
{
offset_y_ = node[OFFSET_Y_KEY].as<int>();
ui_.offsety->setValue(offset_y_);
}
}
void StringPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path)
{
emitter << YAML::Key << FONT_KEY << YAML::Value << font_.toString().toStdString();
emitter << YAML::Key << COLOR_KEY << YAML::Value << color_.name().toStdString();
emitter << YAML::Key << TOPIC_KEY << YAML::Value << ui_.topic->text().toStdString();
emitter << YAML::Key << ANCHOR_KEY << YAML::Value << AnchorToString(anchor_);
emitter << YAML::Key << UNITS_KEY << YAML::Value << UnitsToString(units_);
emitter << YAML::Key << OFFSET_X_KEY << YAML::Value << offset_x_;
emitter << YAML::Key << OFFSET_Y_KEY << YAML::Value << offset_y_;
}
QWidget* StringPlugin::GetConfigWidget(QWidget* parent)
{
config_widget_->setParent(parent);
return config_widget_;
}
void StringPlugin::PrintError(const std::string& message)
{
PrintErrorHelper(ui_.status, message);
}
void StringPlugin::PrintInfo(const std::string& message)
{
PrintInfoHelper(ui_.status, message);
}
void StringPlugin::PrintWarning(const std::string& message)
{
PrintWarningHelper(ui_.status, message);
}
void StringPlugin::SelectColor()
{
color_ = ui_.color->color();
}
void StringPlugin::SelectFont()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, font_, canvas_);
if (ok)
{
font_ = font;
message_.prepare(QTransform(), font_);
ui_.font_button->setFont(font_);
ui_.font_button->setText(font_.family());
}
}
void StringPlugin::SelectTopic()
{
std::string topic = mapviz::SelectTopicDialog::selectTopic(
node_, "std_msgs/msg/String"
);
if (!topic.empty())
{
ui_.topic->setText(QString::fromStdString(topic));
TopicEdited();
}
}
void StringPlugin::TopicEdited()
{
std::string topic = ui_.topic->text().trimmed().toStdString();
if (topic != topic_)
{
initialized_ = false;
has_message_ = false;
PrintWarning("No messages received.");
string_sub_.reset();
topic_ = topic;
if (!topic.empty())
{
string_sub_ = node_->create_subscription<std_msgs::msg::String>(topic_,
rclcpp::QoS(1),
[this](const std_msgs::msg::String::ConstSharedPtr str) {
SetText(QString(str->data.c_str()));
});
string_stamped_sub_ = node_->create_subscription<marti_common_msgs::msg::StringStamped>(topic_,
rclcpp::QoS(1),
[this](const marti_common_msgs::msg::StringStamped::ConstSharedPtr str) {
SetText(QString(str->value.c_str()));
});
RCLCPP_INFO(node_->get_logger(), "Subscribing to %s", topic_.c_str());
}
}
}
void StringPlugin::SetText(const QString& text)
{
message_.setText(text);
message_.prepare(QTransform(), font_);
has_message_ = true;
has_painted_ = false;
initialized_ = true;
}
void StringPlugin::SetAnchor(QString anchor)
{
if (anchor == "top left")
{
anchor_ = TOP_LEFT;
} else if (anchor == "top center") {
anchor_ = TOP_CENTER;
} else if (anchor == "top right") {
anchor_ = TOP_RIGHT;
} else if (anchor == "center left") {
anchor_ = CENTER_LEFT;
} else if (anchor == "center") {
anchor_ = CENTER;
} else if (anchor == "center right") {
anchor_ = CENTER_RIGHT;
} else if (anchor == "bottom left") {
anchor_ = BOTTOM_LEFT;
} else if (anchor == "bottom center") {
anchor_ = BOTTOM_CENTER;
} else if (anchor == "bottom right") {
anchor_ = BOTTOM_RIGHT;
}
}
void StringPlugin::SetUnits(QString units)
{
if (units == "pixels")
{
units_ = PIXELS;
} else if (units == "percent") {
units_ = PERCENT;
}
}
void StringPlugin::SetOffsetX(int offset)
{
offset_x_ = offset;
}
void StringPlugin::SetOffsetY(int offset)
{
offset_y_ = offset;
}
std::string StringPlugin::AnchorToString(StringPlugin::Anchor anchor)
{
std::string anchor_string = "top left";
if (anchor == TOP_LEFT)
{
anchor_string = "top left";
} else if (anchor == TOP_CENTER) {
anchor_string = "top center";
} else if (anchor == TOP_RIGHT) {
anchor_string = "top right";
} else if (anchor == CENTER_LEFT) {
anchor_string = "center left";
} else if (anchor == CENTER) {
anchor_string = "center";
} else if (anchor == CENTER_RIGHT) {
anchor_string = "center right";
} else if (anchor == BOTTOM_LEFT) {
anchor_string = "bottom left";
} else if (anchor == BOTTOM_CENTER) {
anchor_string = "bottom center";
} else if (anchor == BOTTOM_RIGHT) {
anchor_string = "bottom right";
}
return anchor_string;
}
std::string StringPlugin::UnitsToString(StringPlugin::Units units)
{
std::string units_string = "pixels";
if (units == PIXELS)
{
units_string = "pixels";
} else if (units == PERCENT) {
units_string = "percent";
}
return units_string;
}
} // namespace mapviz_plugins