35 #include <QMouseEvent> 36 #include <QTextStream> 39 #if QT_VERSION >= 0x050000 40 #include <QGuiApplication> 42 #include <QApplication> 58 config_widget_(new QWidget()),
60 is_mouse_down_(false),
61 max_ms_(Q_INT64_C(500)),
66 ui_.main_color->setColor(Qt::black);
67 ui_.bkgnd_color->setColor(Qt::white);
69 QObject::connect(
ui_.clear, SIGNAL(clicked()),
this,
71 QObject::connect(
ui_.show_measurements, SIGNAL(toggled(
bool)),
this,
73 QObject::connect(
ui_.show_bkgnd_color, SIGNAL(toggled(
bool)),
this,
75 QObject::connect(
ui_.font_size, SIGNAL(valueChanged(
int)),
this,
77 QObject::connect(
ui_.alpha, SIGNAL(valueChanged(
double)),
this,
79 connect(
ui_.main_color, SIGNAL(colorEdited(
const QColor &)),
this, SLOT(
DrawIcon()));
80 connect(
ui_.bkgnd_color, SIGNAL(colorEdited(
const QColor &)),
this, SLOT(
DrawIcon()));
82 #if QT_VERSION >= 0x050000 83 ui_.measurement->setText(tr(
"Click on the map. Distance between clicks will appear here"));
84 ui_.totaldistance->setText(tr(
"Click on the map. Total distance between clicks will appear here"));
100 ui_.measurement->setText(tr(
"Click on the map. Distance between clicks will appear here"));
101 ui_.totaldistance->setText(tr(
"Click on the map. Total distance between clicks will appear here"));
126 ROS_DEBUG(
"Ignoring mouse event, since measuring plugin is hidden");
130 switch (event->type())
132 case QEvent::MouseButtonPress:
134 case QEvent::MouseButtonRelease:
136 case QEvent::MouseMove:
146 int closest_point = 0;
147 double closest_distance = std::numeric_limits<double>::max();
148 #if QT_VERSION >= 0x050000 149 QPointF point =
event->localPos();
151 QPointF point =
event->posF();
153 ROS_DEBUG(
"Map point: %f %f", point.x(), point.y());
154 for (
size_t i = 0; i <
vertices_.size(); i++)
159 double distance = QLineF(transformed, point).length();
161 if (distance < closest_distance)
163 closest_distance = distance;
164 closest_point =
static_cast<int>(i);
167 if (event->button() == Qt::LeftButton)
169 if (closest_distance < 15)
177 #if QT_VERSION >= 0x050000 186 else if (event->button() == Qt::RightButton)
188 if (closest_distance < 15)
204 #if QT_VERSION >= 0x050000 205 QPointF point =
event->localPos();
207 QPointF point =
event->posF();
210 tf::Vector3 position(transformed.x(), transformed.y(), 0.0);
222 #if QT_VERSION >= 0x050000 235 #if QT_VERSION >= 0x050000 236 QPointF point =
event->localPos();
238 QPointF point =
event->posF();
242 tf::Vector3 position(transformed.x(), transformed.y(), 0.0);
254 double distance_instant = -1;
255 double distance_sum = 0;
259 for (
size_t i = 0; i <
vertices_.size(); i++)
262 if (last_position_ != tf::Vector3(0,0,0))
264 distance_instant = last_position_.distance(vertex);
265 distance_sum = distance_sum + distance_instant;
268 last_position_ = vertex;
273 QTextStream stream(&new_point);
274 stream.setRealNumberPrecision(4);
276 if (distance_instant > 0.0)
278 stream << distance_instant <<
" meters";
281 ui_.measurement->setText(new_point);
284 QTextStream stream2(&new_point2);
285 stream2.setRealNumberPrecision(4);
287 if (distance_sum > 0.0)
289 stream2 << distance_sum <<
" meters";
292 ui_.totaldistance->setText(new_point2);
299 #if QT_VERSION >= 0x050000 300 QPointF point =
event->localPos();
302 QPointF point =
event->posF();
306 tf::Vector3 position(transformed.x(), transformed.y(), 0.0);
318 const QColor color =
ui_.main_color->color();
319 glColor4d(color.redF(), color.greenF(), color.blueF(),
ui_.alpha->value()/2.0);
320 glBegin(GL_LINE_STRIP);
324 glVertex2d(vertex.x(), vertex.y());
331 glColor4d(color.redF(), color.greenF(), color.blueF(),
ui_.alpha->value()/2.0);
339 for (
const auto& vertex: vertices_)
341 glVertex2d(vertex.x(), vertex.y());
350 bool show_measurements =
ui_.show_measurements->isChecked();
351 if (!show_measurements ||
vertices_.empty())
356 QTransform
tf = painter->worldTransform();
357 QFont font(
"Helvetica",
ui_.font_size->value());
358 painter->setFont(font);
360 painter->resetTransform();
363 QColor color =
ui_.main_color->color();
364 double alpha =
ui_.alpha->value()*2.0 < 1.0 ?
ui_.alpha->value()*2.0 : 1.0;
365 color.setAlphaF(alpha);
366 QPen pen(QBrush(color), 1);
367 painter->setPen(pen);
369 const QRectF qrect = QRectF(0, 0, 0, 0);
371 std::vector<MeasurementBox> tags;
383 painter->drawText(qrect, 0, mb.
string, &mb.
rect);
384 mb.
rect.moveTopLeft(tf.map(QPointF((v1.x()+v2.x())/2, (v1.y()+v2.y())/2)));
389 mb.
string.prepend(
" Total: ");
391 painter->drawText(qrect, 0, mb.
string, &mb.
rect);
396 for (
int i=0; i<tags.size(); i++)
398 for (
int j=0; j<tags.size(); j++)
400 if (i != j && tags[i].rect.intersects(tags[j].rect))
402 QRectF overlap = tags[i].rect.intersected(tags[j].rect);
403 if (tags[i].rect.y() > tags[j].rect.y())
405 tags[i].rect.moveTop(tags[i].rect.y() + overlap.height());
409 tags[i].rect.moveTop(tags[i].rect.y() - overlap.height());
416 for (
const auto& tag: tags)
418 if (
ui_.show_bkgnd_color->isChecked())
420 color =
ui_.bkgnd_color->color();
421 color.setAlphaF(
ui_.alpha->value());
422 painter->fillRect(tag.rect, color);
423 painter->drawRect(tag.rect);
425 painter->drawText(tag.rect, tag.string);
432 if (node[
"main_color"])
435 node[
"main_color"] >> color;
436 ui_.main_color->setColor(QColor(color.c_str()));
439 if (node[
"bkgnd_color"])
442 node[
"bkgnd_color"] >> color;
443 ui_.bkgnd_color->setColor(QColor(color.c_str()));
446 if (node[
"show_bkgnd_color"])
448 bool show_bkgnd_color =
false;
449 node[
"show_bkgnd_color"] >> show_bkgnd_color;
450 ui_.show_bkgnd_color->setChecked(show_bkgnd_color);
454 if (node[
"show_measurements"])
456 bool show_measurements =
false;
457 node[
"show_measurements"] >> show_measurements;
458 ui_.show_measurements->setChecked(show_measurements);
462 if (node[
"font_size"])
465 node[
"font_size"] >> font_size;
466 ui_.font_size->setValue(font_size);
473 node[
"alpha"] >> alpha;
474 ui_.alpha->setValue(alpha);
481 emitter << YAML::Key <<
"main_color" << YAML::Value <<
ui_.main_color->color().name().toStdString();
482 emitter << YAML::Key <<
"bkgnd_color" << YAML::Value <<
ui_.bkgnd_color->color().name().toStdString();
483 emitter << YAML::Key <<
"show_bkgnd_color" << YAML::Value <<
ui_.show_bkgnd_color->isChecked();
484 emitter << YAML::Key <<
"show_measurements" << YAML::Value <<
ui_.show_measurements->isChecked();
485 emitter << YAML::Key <<
"font_size" << YAML::Value <<
ui_.font_size->value();
486 emitter << YAML::Key <<
"alpha" << YAML::Value <<
ui_.alpha->value();
bool eventFilter(QObject *object, QEvent *event)
void MeasurementsToggled(bool)
bool Initialize(QGLWidget *canvas)
QPointF MapGlCoordToFixedFrame(const QPointF &point)
void PrintWarning(const std::string &message)
void Paint(QPainter *painter, double x, double y, double scale)
static void PrintWarningHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
static void PrintErrorHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
bool handleMouseRelease(QMouseEvent *)
void FontSizeChanged(int)
mapviz::MapCanvas * map_canvas_
std::string target_frame_
static void PrintInfoHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
std::vector< double > measurements_
bool handleMousePress(QMouseEvent *)
QWidget * GetConfigWidget(QWidget *parent)
void BkgndColorToggled(bool)
void DistanceCalculation()
QPointF FixedFrameToMapGlCoord(const QPointF &point)
std::vector< tf::Vector3 > vertices_
tf::Vector3 last_position_
void AlphaChanged(double)
void PrintError(const std::string &message)
virtual ~MeasuringPlugin()
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
bool handleMouseMove(QMouseEvent *)
void Draw(double x, double y, double scale)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
void LoadConfig(const YAML::Node &node, const std::string &path)
void PrintInfo(const std::string &message)
void SaveConfig(YAML::Emitter &emitter, const std::string &path)