my_rviz_panel.cpp
Go to the documentation of this file.
2 
3 namespace simple_rviz_plugin
4 {
5 MyRvizPanel::MyRvizPanel(QWidget* parent) :
6  rviz::Panel(parent)
7 {
8  qRegisterMetaType<QMessageBox::Icon>();
9  connect(this, &MyRvizPanel::displayMessageBox, this, [=]
10  (const QString title,
11  const QString text,
12  const QString info,
13  const QMessageBox::Icon icon)
14  {
15  const bool old_state(isEnabled());
16  setEnabled(false);
17  QMessageBox msg_box;
18  msg_box.setWindowTitle(title);
19  msg_box.setText(text);
20  msg_box.setInformativeText(info);
21  msg_box.setIcon(icon);
22  msg_box.setStandardButtons(QMessageBox::Ok);
23  msg_box.exec();
24  setEnabled(old_state);
25  });
26 
27  connect(this, &MyRvizPanel::enable, this, &MyRvizPanel::setEnabled);
28  Q_EMIT enable(false);
29 
30  setObjectName("MyRvizPanel");
31  setName(objectName());
32 
33  QVBoxLayout* layout = new QVBoxLayout();
34  setLayout(layout);
35 
36  value_ = new QSpinBox;
37  button_ = new QPushButton("Click me!");
38 
39  layout->addWidget(new QLabel("Value:"));
40  layout->addWidget(value_);
41  layout->addStretch();
42  layout->addWidget(button_);
43  layout->addStretch();
44 
45  connect(value_, qOverload<int>(&QSpinBox::valueChanged), this, &MyRvizPanel::configChanged);
46  connect(button_, &QPushButton::released, this, [=]
47  {
48  srv_.request.value = value_->value();
49  QtConcurrent::run(this, &MyRvizPanel::callService);
50  }
51  );
52 
53  service_ = nh_.serviceClient<simple_node::GetMessage>("get_message");
54  QtConcurrent::run(this, &MyRvizPanel::connectToServices);
55 }
56 
58 {
59 }
60 
62 {
63  Q_EMIT enable(false);
64 
65  const bool rvalue (service_.call(srv_));
66  Q_EMIT enable(true);
67 
68  if (!rvalue)
69  {
70  Q_EMIT displayMessageBox("Failure",
71  "Call to service failed!",
72  "",
73  QMessageBox::Icon::Critical);
74  return;
75  }
76  else if (!srv_.response.return_status)
77  {
78  Q_EMIT displayMessageBox("Failure",
79  "Service responded with an error:",
80  QString::fromStdString(srv_.response.return_message),
81  QMessageBox::Icon::Warning);
82  return;
83  }
84 
85  Q_EMIT displayMessageBox("Success",
86  "Service responded:",
87  QString::fromStdString(srv_.response.return_message),
88  QMessageBox::Icon::Information);
89 }
90 
91 void MyRvizPanel::load(const rviz::Config& config)
92 {
93  rviz::Panel::load(config);
94  int tmp_int;
95 
96  if (config.mapGetInt("value", &tmp_int))
97  value_->setValue(tmp_int);
98 }
99 
100 void MyRvizPanel::save(rviz::Config config) const
101  {
102  rviz::Panel::save(config);
103  config.mapSetValue("value", value_->value());
104 }
105 
107 {
108  while (nh_.ok())
109  {
110  if (client.waitForExistence(ros::Duration(5)))
111  {
112  ROS_INFO_STREAM("RViz panel " << getName().toStdString()
113  << " connected to the service " << client.getService());
114  break;
115  }
116  }
117 }
118 
120 {
122  ROS_INFO_STREAM("RViz panel \'" << getName().toStdString() << "\' services connections have been made");
123  Q_EMIT enable(true);
124 }
125 
126 }
127 
virtual void load(const rviz::Config &config)
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
virtual QString getName() const
void connectToService(ros::ServiceClient &client)
bool call(MReq &req, MRes &res)
virtual void save(Config config) const
void configChanged()
void mapSetValue(const QString &key, QVariant value)
void displayMessageBox(const QString, const QString, const QString, const QMessageBox::Icon)
virtual void setName(const QString &name)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
virtual void save(rviz::Config config) const
MyRvizPanel(QWidget *parent=0)
simple_node::GetMessage srv_
#define ROS_INFO_STREAM(args)
std::string getService()
virtual void load(const Config &config)
bool mapGetInt(const QString &key, int *value_out) const
bool ok() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


simple_rviz_plugin
Author(s): Victor Lamoine - Institut Maupertuis
autogenerated on Mon Feb 28 2022 23:45:59