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  widget_->setSplashPath(QString());
91 
93 
94  // disable quit action in menu bar
95  QAction* action = menu_bar_->findChild<QAction*>("actQuit");
96  if (action)
97  action->setVisible(false);
98 
99  context.addWidget(widget_);
100 
101  // trigger deleteLater for plugin when widget or frame is closed
102  widget_->installEventFilter(this);
103 }
104 
105 void RViz::onDisplayConfigChanged(const QString& fullpath) {
106  display_config_ = fullpath.toStdString();
107 
108  if (context_->serialNumber() != 1 && !widget_->windowTitle().endsWith(")"))
109  widget_->setWindowTitle(widget_->windowTitle() + " (" + QString::number(context_->serialNumber()) + ")");
110 }
111 
113 {
114  namespace po = boost::program_options;
115 
116  const QStringList& qargv = context_->argv();
117 
118  const int argc = qargv.count();
119 
120  // temporary storage for args obtained from qargv - since each QByteArray
121  // owns its storage, we need to keep these around until we're done parsing
122  // args using boost::program_options
123  std::vector<QByteArray> argv_array;
124  std::vector<const char *> argv(argc+1);
125  argv[0] = ""; // dummy program name
126 
127  for (int i = 0; i < argc; ++i)
128  {
129  argv_array.push_back(qargv.at(i).toLocal8Bit());
130  argv[i+1] = argv_array[i].constData();
131  }
132 
133  po::variables_map vm;
134  po::options_description options;
135  options.add_options()
136  ("display-config,d", po::value<std::string>(), "")
137  ("hide-menu,m", "")
138  ("ogre-log,l", "");
139 
140  try
141  {
142  po::store(po::parse_command_line(argc+1, argv.data(), options), vm);
143  po::notify(vm);
144 
145  if (vm.count("hide-menu"))
146  {
147  hide_menu_ = true;
149  }
150 
151  if (vm.count("display-config"))
152  {
153  display_config_ = vm["display-config"].as<std::string>();
155  }
156 
157  if (vm.count("ogre-log"))
158  {
159  ogre_log_ = true;
161  }
162  }
163  catch (std::exception& e)
164  {
165  ROS_ERROR("Error parsing command line: %s", e.what());
166  }
167 }
168 
170  qt_gui_cpp::Settings& instance_settings) const
171 {
172  instance_settings.setValue("rviz_config_file", display_config_.c_str());
173  instance_settings.setValue("hide_menu", hide_menu_);
174 }
175 
176 void RViz::restoreSettings(const qt_gui_cpp::Settings& plugin_settings,
177  const qt_gui_cpp::Settings& instance_settings)
178 {
180  instance_settings.contains("rviz_config_file"))
181  {
182  display_config_ = instance_settings.value("rviz_config_file").toString().toLocal8Bit().constData();;
184  }
185 
186  if (!(set_from_cmdline_ & HIDE_MENU) &&
187  instance_settings.contains("hide_menu"))
188  {
189  bool hide_menu_ = instance_settings.value("hide_menu").toBool();
190  menu_bar_->setVisible(!hide_menu_);
191  }
192 }
193 
195 {
196  return true;
197 }
198 
200 {
201  // Dialog
202  ConfigDialog *dialog = new ConfigDialog();
203  dialog->SetFile(display_config_);
204  dialog->SetHide(hide_menu_);
205 
206  if (dialog->exec() != QDialog::Accepted)
207  return;
208 
209  // Store and apply
210  display_config_ = dialog->GetFile();
211  hide_menu_ = dialog->GetHide();
212 
214  menu_bar_->setVisible(!hide_menu_);
215  set_from_cmdline_ = 0u;
216 }
217 
218 bool RViz::eventFilter(QObject* watched, QEvent* event)
219 {
220  if (watched == widget_ && event->type() == QEvent::Close)
221  {
222  event->ignore();
224  return true;
225  }
226 
227  return QObject::eventFilter(watched, event);
228 }
229 
230 }
231 
rqt_rviz::RViz::triggerConfiguration
void triggerConfiguration()
Definition: rviz.cpp:199
rqt_rviz::RViz::initPlugin
virtual void initPlugin(qt_gui_cpp::PluginContext &context)
Definition: rviz.cpp:67
rqt_rviz::ConfigDialog::SetHide
void SetHide(const bool hide)
Set the hide menu option.
Definition: config_dialog.cpp:121
qt_gui_cpp::Settings::setValue
void setValue(const QString &key, const QVariant &value)
rqt_rviz::RViz::saveSettings
virtual void saveSettings(qt_gui_cpp::Settings &plugin_settings, qt_gui_cpp::Settings &instance_settings) const
Definition: rviz.cpp:169
qt_gui_cpp::PluginContext::closePlugin
void closePlugin()
qt_gui_cpp::PluginContext
rqt_rviz::RViz::set_from_cmdline_
uint8_t set_from_cmdline_
Definition: rviz.h:93
qt_gui_cpp::Settings::contains
bool contains(const QString &key) const
rqt_rviz::RViz::parseArguments
void parseArguments()
Definition: rviz.cpp:112
rqt_rviz::RViz::widget_
rviz::VisualizationFrame * widget_
Definition: rviz.h:77
rqt_rviz
Definition: config_dialog.h:40
qt_gui_cpp::PluginContext::addWidget
void addWidget(QWidget *widget)
rqt_rviz::ConfigDialog::GetHide
bool GetHide() const
Get the hide menu option.
Definition: config_dialog.cpp:116
rqt_rviz::RViz::HIDE_MENU
@ HIDE_MENU
Definition: rviz.h:90
qt_gui_cpp::Settings
rqt_rviz::ConfigDialog
Definition: config_dialog.h:43
class_list_macros.h
rqt_rviz::ConfigDialog::GetFile
std::string GetFile() const
Get the file path entered by the user.
Definition: config_dialog.cpp:106
rqt_rviz::RViz::display_config_
std::string display_config_
Definition: rviz.h:82
rqt_rviz::RViz::hasConfiguration
bool hasConfiguration() const
Definition: rviz.cpp:194
rqt_rviz::RViz::DISPLAY_CONFIG
@ DISPLAY_CONFIG
Definition: rviz.h:91
rqt_gui_cpp::Plugin
rqt_gui_cpp
rqt_rviz::RViz::hide_menu_
bool hide_menu_
Definition: rviz.h:81
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rqt_rviz::RViz::ogre_log_
bool ogre_log_
Definition: rviz.h:83
rqt_rviz::RViz::~RViz
~RViz()
Definition: rviz.cpp:58
rviz::VisualizationFrame::displayConfigFileChanged
void displayConfigFileChanged(const QString &fullpath)
rqt_rviz::RViz::menu_bar_
QMenuBar * menu_bar_
Pointer to menu bar.
Definition: rviz.h:86
qt_gui_cpp::PluginContext::argv
const QStringList & argv() const
rviz.h
qt_gui_cpp::PluginContext::serialNumber
int serialNumber() const
rqt_rviz::RViz::RViz
RViz()
Definition: rviz.cpp:47
rqt_rviz::RViz::context_
qt_gui_cpp::PluginContext * context_
Definition: rviz.h:75
rqt_rviz::RViz
Definition: rviz.h:45
ROS_ERROR
#define ROS_ERROR(...)
rviz::VisualizationFrame
rviz::VisualizationFrame::setSplashPath
void setSplashPath(const QString &splash_path)
rqt_rviz::RViz::OGRE_LOG
@ OGRE_LOG
Definition: rviz.h:89
rqt_rviz::RViz::log_
Ogre::Log * log_
Definition: rviz.h:79
rqt_rviz::RViz::onDisplayConfigChanged
void onDisplayConfigChanged(const QString &fullpath)
Definition: rviz.cpp:105
rviz::VisualizationFrame::loadDisplayConfig
void loadDisplayConfig(const QString &path)
qt_gui_cpp::Settings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
config_dialog.h
rqt_rviz::RViz::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
Definition: rviz.cpp:218
rqt_rviz::ConfigDialog::SetFile
void SetFile(const std::string &file)
Populate the file path line edit.
Definition: config_dialog.cpp:111
rviz::VisualizationFrame::initialize
void initialize(const QString &display_config_file="")
rqt_rviz::RViz::restoreSettings
virtual void restoreSettings(const qt_gui_cpp::Settings &plugin_settings, const qt_gui_cpp::Settings &instance_settings)
Definition: rviz.cpp:176


rqt_rviz
Author(s): Dorian Scholz
autogenerated on Wed Mar 2 2022 00:59:27