rviz.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Dorian Scholz, TU Darmstadt
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   * Redistributions of source code must retain the above copyright
00010  *     notice, this list of conditions and the following disclaimer.
00011  *   * Redistributions in binary form must reproduce the above
00012  *     copyright notice, this list of conditions and the following
00013  *     disclaimer in the documentation and/or other materials provided
00014  *     with the distribution.
00015  *   * Neither the name of the TU Darmstadt nor the names of its
00016  *     contributors may be used to endorse or promote products derived
00017  *     from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00027  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00029  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #include <OGRE/OgreLogManager.h>
00034 
00035 #include <QCloseEvent>
00036 #include <QFileDialog>
00037 
00038 #include <pluginlib/class_list_macros.h>
00039 #include <boost/program_options.hpp>
00040 #include <fstream>
00041 
00042 #include <rqt_rviz/config_dialog.h>
00043 #include <rqt_rviz/rviz.h>
00044 
00045 namespace rqt_rviz {
00046 
00047 RViz::RViz()
00048   : rqt_gui_cpp::Plugin()
00049   , context_(0)
00050   , widget_(0)
00051   , log_(0)
00052   , hide_menu_(false)
00053   , ogre_log_(false)
00054 {
00055   setObjectName("RViz");
00056 }
00057 
00058 RViz::~RViz()
00059 {
00060   Ogre::LogManager* log_manager = Ogre::LogManager::getSingletonPtr();
00061   if (log_manager && log_)
00062   {
00063     log_manager->destroyLog(log_);
00064   }
00065 }
00066 
00067 void RViz::initPlugin(qt_gui_cpp::PluginContext& context)
00068 {
00069   context_ = &context;
00070 
00071   parseArguments();
00072 
00073   // prevent output of Ogre stuff to console
00074   Ogre::LogManager* log_manager = Ogre::LogManager::getSingletonPtr();
00075   if (!log_manager)
00076   {
00077     log_manager = new Ogre::LogManager();
00078   }
00079   QString filename = QString("rqt_rviz_ogre") + (context.serialNumber() > 1 ? QString::number(context.serialNumber()) : QString("")) + QString(".log");
00080   log_ = log_manager->createLog(filename.toStdString().c_str(), false, false, !ogre_log_);
00081 
00082   widget_ = new rviz::VisualizationFrame();
00083 
00084   // create own menu bar to disable native menu bars on Unity and Mac
00085   menu_bar_ = new QMenuBar();
00086   menu_bar_->setNativeMenuBar(false);
00087   menu_bar_->setVisible(!hide_menu_);
00088   widget_->setMenuBar(menu_bar_);
00089 
00090   widget_->initialize(display_config_.c_str());
00091 
00092   // disable quit action in menu bar
00093   QMenu* menu = 0;
00094   {
00095     // find first menu in menu bar
00096     const QObjectList& children = menu_bar_->children();
00097     for (QObjectList::const_iterator it = children.begin(); !menu && it != children.end(); it++)
00098     {
00099       menu = dynamic_cast<QMenu*>(*it);
00100     }
00101   }
00102   if (menu)
00103   {
00104     // hide last action in menu
00105     const QObjectList& children = menu->children();
00106     if (!children.empty())
00107     {
00108       QAction* action = dynamic_cast<QAction*>(children.last());
00109       if (action)
00110       {
00111         action->setVisible(false);
00112       }
00113     }
00114   }
00115 
00116   widget_->setWindowTitle("RViz[*]");
00117   if (context.serialNumber() != 1)
00118   {
00119     widget_->setWindowTitle(widget_->windowTitle() + " (" + QString::number(context.serialNumber()) + ")");
00120   }
00121   context.addWidget(widget_);
00122 
00123   // trigger deleteLater for plugin when widget or frame is closed
00124   widget_->installEventFilter(this);
00125 }
00126 
00127 void RViz::parseArguments()
00128 {
00129   namespace po = boost::program_options;
00130 
00131   const QStringList& qargv = context_->argv();
00132 
00133   const int argc = qargv.count();
00134 
00135   // temporary storage for args obtained from qargv - since each QByteArray
00136   // owns its storage, we need to keep these around until we're done parsing
00137   // args using boost::program_options
00138   std::vector<QByteArray> argv_array;
00139   const char *argv[argc+1];
00140   argv[0] = ""; // dummy program name
00141 
00142   for (int i = 0; i < argc; ++i)
00143   {
00144     argv_array.push_back(qargv.at(i).toLocal8Bit());
00145     argv[i+1] = argv_array[i].constData();
00146   }
00147 
00148   po::variables_map vm;
00149   po::options_description options;
00150   options.add_options()
00151     ("display-config,d", po::value<std::string>(), "")
00152     ("hide-menu,m", "")
00153     ("ogre-log,l", "");
00154 
00155   try
00156   {
00157     po::store(po::parse_command_line(argc+1, argv, options), vm);
00158     po::notify(vm);
00159 
00160     if (vm.count("hide-menu"))
00161     {
00162       hide_menu_ = true;
00163     }
00164 
00165     if (vm.count("display-config"))
00166     {
00167       display_config_ = vm["display-config"].as<std::string>();
00168     }
00169 
00170     if (vm.count("ogre-log"))
00171     {
00172       ogre_log_ = true;
00173     }
00174   }
00175   catch (std::exception& e)
00176   {
00177     ROS_ERROR("Error parsing command line: %s", e.what());
00178   }
00179 }
00180 
00181 void RViz::saveSettings(qt_gui_cpp::Settings& plugin_settings,
00182                         qt_gui_cpp::Settings& instance_settings) const
00183 {
00184   instance_settings.setValue("rviz_config_file", display_config_.c_str());
00185   instance_settings.setValue("hide_menu", hide_menu_);
00186 }
00187 
00188 void RViz::restoreSettings(const qt_gui_cpp::Settings& plugin_settings,
00189                            const qt_gui_cpp::Settings& instance_settings)
00190 {
00191   if (instance_settings.contains("rviz_config_file"))
00192   {
00193     display_config_ = instance_settings.value("rviz_config_file").toString().toLocal8Bit().constData();;
00194     widget_->loadDisplayConfig(display_config_.c_str());
00195   }
00196 
00197   if (instance_settings.contains("hide_menu"))
00198   {
00199     bool hide_menu_ = instance_settings.value("hide_menu").toBool();
00200     menu_bar_->setVisible(!hide_menu_);
00201   }
00202 }
00203 
00204 bool RViz::hasConfiguration() const
00205 {
00206   return true;
00207 }
00208 
00209 void RViz::triggerConfiguration()
00210 {
00211   // Dialog
00212   ConfigDialog *dialog = new ConfigDialog();
00213   dialog->SetFile(display_config_);
00214   dialog->SetHide(hide_menu_);
00215 
00216   if (dialog->exec() != QDialog::Accepted)
00217     return;
00218 
00219   // Store and apply
00220   display_config_ = dialog->GetFile();
00221   hide_menu_ = dialog->GetHide();
00222 
00223   widget_->loadDisplayConfig(display_config_.c_str());
00224   menu_bar_->setVisible(!hide_menu_);
00225 }
00226 
00227 bool RViz::eventFilter(QObject* watched, QEvent* event)
00228 {
00229   if (watched == widget_ && event->type() == QEvent::Close)
00230   {
00231     event->ignore();
00232     context_->closePlugin();
00233     return true;
00234   }
00235 
00236   return QObject::eventFilter(watched, event);
00237 }
00238 
00239 }
00240 
00241 PLUGINLIB_EXPORT_CLASS(rqt_rviz::RViz, rqt_gui_cpp::Plugin)


rqt_rviz
Author(s): Dorian Scholz
autogenerated on Sat Jun 8 2019 20:26:00