displays_panel.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
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 are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <QTimer>
00031 #include <QHBoxLayout>
00032 #include <QVBoxLayout>
00033 #include <QPushButton>
00034 #include <QInputDialog>
00035 #include <QApplication>
00036 
00037 #include <boost/bind.hpp>
00038 
00039 #include "rviz/display_factory.h"
00040 #include "rviz/display.h"
00041 #include "rviz/add_display_dialog.h"
00042 #include "rviz/properties/property.h"
00043 #include "rviz/properties/property_tree_widget.h"
00044 #include "rviz/properties/property_tree_with_help.h"
00045 #include "rviz/visualization_manager.h"
00046 
00047 #include "rviz/displays_panel.h"
00048 
00049 namespace rviz
00050 {
00051 
00052 DisplaysPanel::DisplaysPanel( QWidget* parent )
00053   : Panel( parent )
00054 {
00055   tree_with_help_ = new PropertyTreeWithHelp;
00056   property_grid_ = tree_with_help_->getTree();
00057 
00058   QPushButton* add_button = new QPushButton( "Add" );
00059   add_button->setShortcut( QKeySequence( QString( "Ctrl+N" )));
00060   add_button->setToolTip( "Add a new display, Ctrl+N" );
00061   remove_button_ = new QPushButton( "Remove" );
00062   remove_button_->setShortcut( QKeySequence( QString( "Ctrl+X" )));
00063   remove_button_->setToolTip( "Remove displays, Ctrl+X" );
00064   remove_button_->setEnabled( false );
00065   rename_button_ = new QPushButton( "Rename" );
00066   rename_button_->setShortcut( QKeySequence( QString( "Ctrl+R" )));
00067   rename_button_->setToolTip( "Rename a display, Ctrl+R" );
00068   rename_button_->setEnabled( false );
00069 
00070   QHBoxLayout* button_layout = new QHBoxLayout;
00071   button_layout->addWidget( add_button );
00072   button_layout->addWidget( remove_button_ );
00073   button_layout->addWidget( rename_button_ );
00074   button_layout->setContentsMargins( 2, 0, 2, 2 );
00075 
00076   QVBoxLayout* layout = new QVBoxLayout;
00077   layout->setContentsMargins( 0, 0, 0, 2 );
00078   layout->addWidget( tree_with_help_ );
00079   layout->addLayout( button_layout );
00080 
00081   setLayout( layout );
00082 
00083   connect( add_button, SIGNAL( clicked( bool )), this, SLOT( onNewDisplay() ));
00084   connect( remove_button_, SIGNAL( clicked( bool )), this, SLOT( onDeleteDisplay() ));
00085   connect( rename_button_, SIGNAL( clicked( bool )), this, SLOT( onRenameDisplay() ));
00086   connect( property_grid_, SIGNAL( selectionHasChanged() ), this, SLOT( onSelectionChanged() ));
00087 }
00088 
00089 DisplaysPanel::~DisplaysPanel()
00090 {
00091 }
00092 
00093 void DisplaysPanel::onInitialize()
00094 {
00095   property_grid_->setModel( vis_manager_->getDisplayTreeModel() );
00096 }
00097 
00098 void DisplaysPanel::onNewDisplay()
00099 {
00100   QString lookup_name;
00101   QString display_name;
00102   QString topic;
00103   QString datatype;
00104 
00105   QStringList empty;
00106 
00107   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00108   AddDisplayDialog* dialog = new AddDisplayDialog( vis_manager_->getDisplayFactory(),
00109                                                    "Display",
00110                                                    empty, empty,
00111                                                    &lookup_name,
00112                                                    &display_name,
00113                                                    &topic,
00114                                                    &datatype );
00115   QApplication::restoreOverrideCursor();
00116 
00117   vis_manager_->stopUpdate();
00118   if( dialog->exec() == QDialog::Accepted )
00119   {
00120     Display *disp = vis_manager_->createDisplay( lookup_name, display_name, true );
00121     if ( !topic.isEmpty() && !datatype.isEmpty() )
00122     {
00123       disp->setTopic( topic, datatype );
00124     }
00125   }
00126   vis_manager_->startUpdate();
00127   activateWindow(); // Force keyboard focus back on main window.
00128 }
00129 
00130 void DisplaysPanel::onDeleteDisplay()
00131 {
00132   QList<Display*> displays_to_delete = property_grid_->getSelectedObjects<Display>();
00133 
00134   for( int i = 0; i < displays_to_delete.size(); i++ )
00135   {
00136     // Displays can emit signals from other threads with self pointers.  We're
00137     // freeing the display now, so ensure no one is listening to those signals.
00138     displays_to_delete[ i ]->disconnect();
00139     // Delete display later in case there are pending signals to it.
00140     displays_to_delete[ i ]->deleteLater();
00141   }
00142   vis_manager_->notifyConfigChanged();
00143 }
00144 
00145 void DisplaysPanel::onSelectionChanged()
00146 {
00147   QList<Display*> displays = property_grid_->getSelectedObjects<Display>();
00148 
00149   int num_displays_selected = displays.size();
00150 
00151   remove_button_->setEnabled( num_displays_selected > 0 );
00152   rename_button_->setEnabled( num_displays_selected == 1 );
00153 }
00154 
00155 void DisplaysPanel::onRenameDisplay()
00156 {
00157   QList<Display*> displays = property_grid_->getSelectedObjects<Display>();
00158   if( displays.size() == 0 )
00159   {
00160     return;
00161   }
00162   Display* display_to_rename = displays[ 0 ];
00163 
00164   if( !display_to_rename )
00165   {
00166     return;
00167   }
00168 
00169   QString old_name = display_to_rename->getName();
00170   QString new_name = QInputDialog::getText( this, "Rename Display", "New Name?", QLineEdit::Normal, old_name );
00171 
00172   if( new_name.isEmpty() || new_name == old_name )
00173   {
00174     return;
00175   }
00176 
00177   display_to_rename->setName( new_name );
00178 }
00179 
00180 void DisplaysPanel::save( Config config ) const
00181 {
00182   Panel::save( config );
00183   tree_with_help_->save( config );
00184 }
00185 
00186 void DisplaysPanel::load( const Config& config )
00187 {
00188   Panel::load( config );
00189   tree_with_help_->load( config );
00190 }
00191 
00192 } // namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:27