qnodedialog.cpp
Go to the documentation of this file.
1 #include "qnodedialog.h"
2 #include "ui_qnodedialog.h"
3 #include <boost/make_shared.hpp>
4 #include <QSettings>
5 #include <QMessageBox>
6 
7 std::string getDefaultMasterURI()
8 {
9  if( qgetenv("ROS_MASTER_URI").isEmpty() )
10  {
11  QMessageBox msgBox;
12  msgBox.setText("WARNINGS: the ROS_MASTER_URI is not defined in your environment\n"
13  "Using the default value [http://localhost:11311]\n");
14  msgBox.exec();
15  return "http://localhost:11311";
16  }
17  else{
18  auto master_uri = ( qgetenv("ROS_MASTER_URI"));
19  return std::string( master_uri.data() );
20  }
21 }
22 
23 QNodeDialog::QNodeDialog( QWidget *parent) :
24  QDialog(parent),
25  ui(new Ui::QNodeDialog)
26 {
27  ui->setupUi(this);
28 
29  QSettings settings;
30 
31  auto master_ui = settings.value("QNode.master_uri", tr("http://localhost:11311")).toString();
32  auto host_ip = settings.value("QNode.host_ip", tr("localhost")).toString();
33 
34  ui->lineEditMaster->setText( master_ui );
35  ui->lineEditHost->setText( host_ip );
36 }
37 
38 namespace ros {
39  namespace master {
40  void init(const M_string& remappings);
41  }
42 }
43 
44 bool QNodeDialog::Connect(const std::string& ros_master_uri,
45  const std::string& hostname)
46 {
47  std::map<std::string,std::string> remappings;
48  remappings["__master"] = ros_master_uri;
49  remappings["__hostname"] = hostname;
50 
51  static bool first_time = true;
52  if( first_time)
53  {
54  ros::init(remappings,"PlotJugglerListener");
55  first_time = false;
56  }
57  else{
58  ros::master::init(remappings);
59  }
60 
61  bool connected = ros::master::check();
62  if ( ! connected ) {
63  QMessageBox msgBox;
64  msgBox.setText( QString("Could not connect to the ros master [%1]").
65  arg(QString::fromStdString(ros_master_uri)));
66  msgBox.exec();
67  }
68  return connected;
69 }
70 
71 
73 {
74  QSettings settings;
75  settings.setValue ("QNode.master_uri", ui->lineEditMaster->text() );
76  settings.setValue("QNode.host_ip", ui->lineEditHost->text() );
77  delete ui;
78 }
79 
81 {
82  bool connected = false;
83  if( ui->checkBoxUseEnvironment->isChecked() )
84  {
85  const std::string master_uri = getDefaultMasterURI();
86  connected = QNodeDialog::Connect(master_uri, "localhost" );
87  }
88  else{
89  std::string ros_master_uri = ui->lineEditMaster->text().toStdString();
90  std::string hostname = ui->lineEditHost->text().toStdString();
91  connected = QNodeDialog::Connect(ros_master_uri, hostname);
92  }
93  if( connected ) {
94  this->close();
95  }
96  else{
97 
98  }
99 }
100 
101 
103 {
104  ui->lineEditMaster->setEnabled( !checked );
105  ui->lineEditHost->setEnabled( !checked );
106 }
107 
108 
110 {
111  this->close();
112 }
113 
115 {
116  static RosManager manager;
117  return manager;
118 }
119 
121 {
122  if(ros::isStarted() )
123  {
124  ros::shutdown(); // explicitly needed since we use ros::start();;
126  }
127 }
128 
130 {
131 
132 }
133 
135 {
136  RosManager& manager = RosManager::get();
137 
139  {
140  bool connected = QNodeDialog::Connect(getDefaultMasterURI(), "localhost" );
141  if ( ! connected )
142  {
143  //as a fallback strategy, launch the QNodeDialog
144  QNodeDialog dialog;
145  dialog.exec();
146  }
147  }
149  {
150  if( !manager._node ){
152  {
154  } );
155  }
156  ros::start();
157  }
158  return manager._node;
159 }
ROSCPP_DECL bool check()
ROSCPP_DECL void start()
void stopROS()
ROSCPP_DECL bool isInitialized()
void init(const M_string &remappings)
std::string getDefaultMasterURI()
Definition: qnodedialog.cpp:7
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static bool Connect(const std::string &ros_master_uri, const std::string &hostname="localhost")
Definition: qnodedialog.cpp:44
void on_checkBoxUseEnvironment_toggled(bool checked)
boost::shared_ptr< NodeHandle > NodeHandlePtr
std::map< std::string, std::string > M_string
void on_pushButtonConnect_pressed()
Definition: qnodedialog.cpp:80
Ui::QNodeDialog * ui
Definition: qnodedialog.h:30
void on_pushButtonCancel_pressed()
static ros::NodeHandlePtr getNode()
static RosManager & get()
QNodeDialog(QWidget *parent=0)
Definition: qnodedialog.cpp:23
ROSCPP_DECL bool isStarted()
void * arg
ROSCPP_DECL void shutdown()
ros::NodeHandlePtr _node
Definition: qnodedialog.h:38
ROSCPP_DECL void waitForShutdown()


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17