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


rqt_rviz
Author(s): Dorian Scholz
autogenerated on Mon May 1 2017 02:35:37