rviz.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Dorian Scholz, TU Darmstadt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  * * Neither the name of the TU Darmstadt nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <OGRE/OgreLogManager.h>
34 
35 #include <QCloseEvent>
36 #include <QFileDialog>
37 
39 #include <boost/program_options.hpp>
40 #include <fstream>
41 
42 #include <rqt_rviz/config_dialog.h>
43 #include <rqt_rviz/rviz.h>
44 
45 namespace rqt_rviz {
46 
48  : rqt_gui_cpp::Plugin()
49  , context_(0)
50  , widget_(0)
51  , log_(0)
52  , hide_menu_(false)
53  , ogre_log_(false)
54 {
55  setObjectName("RViz");
56 }
57 
59 {
60  Ogre::LogManager* log_manager = Ogre::LogManager::getSingletonPtr();
61  if (log_manager && log_)
62  {
63  log_manager->destroyLog(log_);
64  }
65 }
66 
68 {
69  context_ = &context;
70 
72 
73  // prevent output of Ogre stuff to console
74  Ogre::LogManager* log_manager = Ogre::LogManager::getSingletonPtr();
75  if (!log_manager)
76  {
77  log_manager = new Ogre::LogManager();
78  }
79  QString filename = QString("rqt_rviz_ogre") + (context.serialNumber() > 1 ? QString::number(context.serialNumber()) : QString("")) + QString(".log");
80  log_ = log_manager->createLog(filename.toStdString().c_str(), false, false, !ogre_log_);
81 
83 
84  // create own menu bar to disable native menu bars on Unity and Mac
85  menu_bar_ = new QMenuBar();
86  menu_bar_->setNativeMenuBar(false);
87  menu_bar_->setVisible(!hide_menu_);
88  widget_->setMenuBar(menu_bar_);
89 
91 
92  // disable quit action in menu bar
93  QMenu* menu = 0;
94  {
95  // find first menu in menu bar
96  const QObjectList& children = menu_bar_->children();
97  for (QObjectList::const_iterator it = children.begin(); !menu && it != children.end(); it++)
98  {
99  menu = dynamic_cast<QMenu*>(*it);
100  }
101  }
102  if (menu)
103  {
104  // hide last action in menu
105  const QObjectList& children = menu->children();
106  if (!children.empty())
107  {
108  QAction* action = dynamic_cast<QAction*>(children.last());
109  if (action)
110  {
111  action->setVisible(false);
112  }
113  }
114  }
115 
116  widget_->setWindowTitle("RViz[*]");
117  if (context.serialNumber() != 1)
118  {
119  widget_->setWindowTitle(widget_->windowTitle() + " (" + QString::number(context.serialNumber()) + ")");
120  }
121  context.addWidget(widget_);
122 
123  // trigger deleteLater for plugin when widget or frame is closed
124  widget_->installEventFilter(this);
125 }
126 
128 {
129  namespace po = boost::program_options;
130 
131  const QStringList& qargv = context_->argv();
132 
133  const int argc = qargv.count();
134 
135  // temporary storage for args obtained from qargv - since each QByteArray
136  // owns its storage, we need to keep these around until we're done parsing
137  // args using boost::program_options
138  std::vector<QByteArray> argv_array;
139  const char *argv[argc+1];
140  argv[0] = ""; // dummy program name
141 
142  for (int i = 0; i < argc; ++i)
143  {
144  argv_array.push_back(qargv.at(i).toLocal8Bit());
145  argv[i+1] = argv_array[i].constData();
146  }
147 
148  po::variables_map vm;
149  po::options_description options;
150  options.add_options()
151  ("display-config,d", po::value<std::string>(), "")
152  ("hide-menu,m", "")
153  ("ogre-log,l", "");
154 
155  try
156  {
157  po::store(po::parse_command_line(argc+1, argv, options), vm);
158  po::notify(vm);
159 
160  if (vm.count("hide-menu"))
161  {
162  hide_menu_ = true;
163  }
164 
165  if (vm.count("display-config"))
166  {
167  display_config_ = vm["display-config"].as<std::string>();
168  }
169 
170  if (vm.count("ogre-log"))
171  {
172  ogre_log_ = true;
173  }
174  }
175  catch (std::exception& e)
176  {
177  ROS_ERROR("Error parsing command line: %s", e.what());
178  }
179 }
180 
182  qt_gui_cpp::Settings& instance_settings) const
183 {
184  instance_settings.setValue("rviz_config_file", display_config_.c_str());
185  instance_settings.setValue("hide_menu", hide_menu_);
186 }
187 
188 void RViz::restoreSettings(const qt_gui_cpp::Settings& plugin_settings,
189  const qt_gui_cpp::Settings& instance_settings)
190 {
191  if (instance_settings.contains("rviz_config_file"))
192  {
193  display_config_ = instance_settings.value("rviz_config_file").toString().toLocal8Bit().constData();;
195  }
196 
197  if (instance_settings.contains("hide_menu"))
198  {
199  bool hide_menu_ = instance_settings.value("hide_menu").toBool();
200  menu_bar_->setVisible(!hide_menu_);
201  }
202 }
203 
205 {
206  return true;
207 }
208 
210 {
211  // Dialog
212  ConfigDialog *dialog = new ConfigDialog();
213  dialog->SetFile(display_config_);
214  dialog->SetHide(hide_menu_);
215 
216  if (dialog->exec() != QDialog::Accepted)
217  return;
218 
219  // Store and apply
220  display_config_ = dialog->GetFile();
221  hide_menu_ = dialog->GetHide();
222 
224  menu_bar_->setVisible(!hide_menu_);
225 }
226 
227 bool RViz::eventFilter(QObject* watched, QEvent* event)
228 {
229  if (watched == widget_ && event->type() == QEvent::Close)
230  {
231  event->ignore();
233  return true;
234  }
235 
236  return QObject::eventFilter(watched, event);
237 }
238 
239 }
240 
Ogre::Log * log_
Definition: rviz.h:76
filename
virtual void initPlugin(qt_gui_cpp::PluginContext &context)
Definition: rviz.cpp:67
virtual void restoreSettings(const qt_gui_cpp::Settings &plugin_settings, const qt_gui_cpp::Settings &instance_settings)
Definition: rviz.cpp:188
void parseArguments()
Definition: rviz.cpp:127
bool GetHide() const
Get the hide menu option.
bool hasConfiguration() const
Definition: rviz.cpp:204
rviz::VisualizationFrame * widget_
Definition: rviz.h:74
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
bool hide_menu_
Definition: rviz.h:78
void addWidget(QWidget *widget)
bool ogre_log_
Definition: rviz.h:80
void initialize(const QString &display_config_file="")
std::string GetFile() const
Get the file path entered by the user.
bool contains(const QString &key) const
const QStringList & argv() const
void SetHide(const bool hide)
Set the hide menu option.
options
std::string display_config_
Definition: rviz.h:79
void setValue(const QString &key, const QVariant &value)
QMenuBar * menu_bar_
Pointer to menu bar.
Definition: rviz.h:83
action
void loadDisplayConfig(const QString &path)
qt_gui_cpp::PluginContext * context_
Definition: rviz.h:72
void triggerConfiguration()
Definition: rviz.cpp:209
virtual bool eventFilter(QObject *watched, QEvent *event)
Definition: rviz.cpp:227
void SetFile(const std::string &file)
Populate the file path line edit.
virtual void saveSettings(qt_gui_cpp::Settings &plugin_settings, qt_gui_cpp::Settings &instance_settings) const
Definition: rviz.cpp:181
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)


rqt_rviz
Author(s): Dorian Scholz
autogenerated on Mon Jun 10 2019 14:59:57