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 <QMenuBar>
00037 
00038 #include <pluginlib/class_list_macros.h>
00039 #include <boost/program_options.hpp>
00040 
00041 #include <rqt_rviz/rviz.h>
00042 
00043 
00044 namespace rqt_rviz {
00045 
00046 RViz::RViz()
00047   : rqt_gui_cpp::Plugin()
00048   , context_(0)
00049   , widget_(0)
00050   , log_(0)
00051   , hide_menu_(false)
00052 {
00053   setObjectName("RViz");
00054 }
00055 
00056 RViz::~RViz()
00057 {
00058   Ogre::LogManager* log_manager = Ogre::LogManager::getSingletonPtr();
00059   if (log_manager && log_)
00060   {
00061     log_manager->destroyLog(log_);
00062   }
00063 }
00064 
00065 void RViz::initPlugin(qt_gui_cpp::PluginContext& context)
00066 {
00067   context_ = &context;
00068 
00069   parseArguments();
00070 
00071   // prevent output of Ogre stuff to console
00072   Ogre::LogManager* log_manager = Ogre::LogManager::getSingletonPtr();
00073   if (!log_manager)
00074   {
00075     log_manager = new Ogre::LogManager();
00076   }
00077   QString filename = QString("rqt_rviz_ogre") + (context.serialNumber() > 1 ? QString::number(context.serialNumber()) : QString("")) + QString(".log");
00078   log_ = log_manager->createLog(filename.toStdString().c_str(), false, false);
00079 
00080   widget_ = new rviz::VisualizationFrame();
00081 
00082   // create own menu bar to disable native menu bars on Unity and Mac
00083   QMenuBar* menu_bar = new QMenuBar();
00084   menu_bar->setNativeMenuBar(false);
00085   menu_bar->setVisible(!hide_menu_);
00086   widget_->setMenuBar(menu_bar);
00087 
00088   widget_->initialize(display_config_.c_str());
00089 
00090   // disable quit action in menu bar
00091   QMenu* menu = 0;
00092   {
00093     // find first menu in menu bar
00094     const QObjectList& children = menu_bar->children();
00095     for (QObjectList::const_iterator it = children.begin(); !menu && it != children.end(); it++)
00096     {
00097       menu = dynamic_cast<QMenu*>(*it);
00098     }
00099   }
00100   if (menu)
00101   {
00102     // hide last action in menu
00103     const QObjectList& children = menu->children();
00104     if (!children.empty())
00105     {
00106       QAction* action = dynamic_cast<QAction*>(children.last());
00107       if (action)
00108       {
00109         action->setVisible(false);
00110       }
00111     }
00112   }
00113 
00114   widget_->setWindowTitle("RViz[*]");
00115   if (context.serialNumber() != 1)
00116   {
00117     widget_->setWindowTitle(widget_->windowTitle() + " (" + QString::number(context.serialNumber()) + ")");
00118   }
00119   context.addWidget(widget_);
00120 
00121   // trigger deleteLater for plugin when widget or frame is closed
00122   widget_->installEventFilter(this);
00123 }
00124 
00125 void RViz::parseArguments()
00126 {
00127   namespace po = boost::program_options;
00128 
00129   const QStringList& qargv = context_->argv();
00130 
00131   const int argc = qargv.count();
00132   
00133   // temporary storage for args obtained from qargv - since each QByteArray
00134   // owns its storage, we need to keep these around until we're done parsing
00135   // args using boost::program_options
00136   std::vector<QByteArray> argv_array;
00137   const char *argv[argc+1];
00138   argv[0] = ""; // dummy program name
00139 
00140   for (int i = 0; i < argc; ++i)
00141   {
00142     argv_array.push_back(qargv.at(i).toLocal8Bit());
00143     argv[i+1] = argv_array[i].constData();
00144   }
00145 
00146   po::variables_map vm;
00147   po::options_description options;
00148   options.add_options()
00149     ("display-config,d", po::value<std::string>(), "")
00150     ("hide-menu,m", "");
00151 
00152   try
00153   {
00154     po::store(po::parse_command_line(argc+1, argv, options), vm);
00155     po::notify(vm);
00156 
00157     if (vm.count("hide-menu"))
00158     {
00159       hide_menu_ = true;
00160     }
00161 
00162     if (vm.count("display-config"))
00163     {
00164       display_config_ = vm["display-config"].as<std::string>();
00165     }
00166   }
00167   catch (std::exception& e)
00168   {
00169     ROS_ERROR("Error parsing command line: %s", e.what());
00170   }
00171 }
00172 
00173 bool RViz::eventFilter(QObject* watched, QEvent* event)
00174 {
00175   if (watched == widget_ && event->type() == QEvent::Close)
00176   {
00177     event->ignore();
00178     context_->closePlugin();
00179     return true;
00180   }
00181 
00182   return QObject::eventFilter(watched, event);
00183 }
00184 
00185 }
00186 
00187 PLUGINLIB_EXPORT_CLASS(rqt_rviz::RViz, rqt_gui_cpp::Plugin)


rqt_rviz
Author(s): Dorian Scholz
autogenerated on Fri Aug 28 2015 12:50:57