Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
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
00093 QMenu* menu = 0;
00094 {
00095
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
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
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
00136
00137
00138 std::vector<QByteArray> argv_array;
00139 const char *argv[argc+1];
00140 argv[0] = "";
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
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
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)