views_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 <QLabel>
00031 #include <QListWidget>
00032 #include <QComboBox>
00033 #include <QPushButton>
00034 #include <QVBoxLayout>
00035 #include <QHBoxLayout>
00036 #include <QInputDialog>
00037 
00038 #include "rviz/properties/property_tree_widget.h"
00039 #include "rviz/view_controller.h"
00040 #include "rviz/view_manager.h"
00041 #include "rviz/visualization_manager.h"
00042 
00043 #include "views_panel.h"
00044 
00045 namespace rviz
00046 {
00047 
00048 ViewsPanel::ViewsPanel( QWidget* parent )
00049   : Panel( parent )
00050   , view_man_( NULL )
00051 {
00052   camera_type_selector_ = new QComboBox;
00053   properties_view_ = new PropertyTreeWidget();
00054 
00055   save_button_ = new QPushButton( "Save" );
00056   QPushButton* remove_button = new QPushButton( "Remove" );
00057   QPushButton* rename_button = new QPushButton( "Rename" );
00058   QPushButton* zero_button = new QPushButton( "Zero" );
00059   zero_button->setToolTip( "Jump to 0,0,0 with the current view controller. Shortcut: Z" );
00060 
00061   QHBoxLayout* top_layout = new QHBoxLayout;
00062   top_layout->addWidget( new QLabel( "Type:" ));
00063   top_layout->addWidget( camera_type_selector_ );
00064   top_layout->addStretch();
00065   top_layout->addWidget( zero_button );
00066   top_layout->setContentsMargins( 2, 6, 2, 2 );
00067 
00068   QHBoxLayout* button_layout = new QHBoxLayout;
00069   button_layout->addWidget( save_button_ );
00070   button_layout->addWidget( remove_button );
00071   button_layout->addWidget( rename_button );
00072   button_layout->setContentsMargins( 2, 0, 2, 2 );
00073 
00074   QVBoxLayout* main_layout = new QVBoxLayout;
00075   main_layout->setContentsMargins( 0,0,0,0 );
00076   main_layout->addLayout( top_layout );
00077   main_layout->addWidget( properties_view_ );
00078   main_layout->addLayout( button_layout );
00079   setLayout( main_layout );
00080 
00081   connect( remove_button, SIGNAL( clicked() ), this, SLOT( onDeleteClicked() ));
00082   connect( rename_button, SIGNAL( clicked() ), this, SLOT( renameSelected() ));
00083   connect( zero_button, SIGNAL( clicked() ), this, SLOT( onZeroClicked() ));
00084   connect( properties_view_, SIGNAL( clicked( const QModelIndex& )), this, SLOT( setCurrentViewFromIndex( const QModelIndex& )));
00085   connect( properties_view_, SIGNAL( activated( const QModelIndex& )), this, SLOT( setCurrentViewFromIndex( const QModelIndex& )));
00086 }
00087 
00088 void ViewsPanel::onInitialize()
00089 {
00090   setViewManager( vis_manager_->getViewManager() );
00091 }
00092 
00093 void ViewsPanel::setViewManager( ViewManager* view_man )
00094 {
00095   if( view_man_ )
00096   {
00097     disconnect( save_button_, SIGNAL( clicked() ), view_man_, SLOT( copyCurrentToList() ));
00098     disconnect( camera_type_selector_, SIGNAL( activated( int )), this, SLOT( onTypeSelectorChanged( int )));
00099     disconnect( view_man_, SIGNAL( currentChanged() ), this, SLOT( onCurrentChanged() ));
00100   }
00101   view_man_ = view_man;
00102   camera_type_selector_->clear();
00103   if( view_man_ )
00104   {
00105     properties_view_->setModel( view_man_->getPropertyModel() );
00106 
00107     QStringList ids = view_man_->getFactory()->getDeclaredClassIds();
00108     for( int i = 0; i < ids.size(); i++ )
00109     {
00110       const QString& id = ids[ i ];
00111       camera_type_selector_->addItem( ViewController::formatClassId( id ), id ); // send the regular-formatted id as userData.
00112     }
00113 
00114     connect( save_button_, SIGNAL( clicked() ), view_man_, SLOT( copyCurrentToList() ));
00115     connect( camera_type_selector_, SIGNAL( activated( int )), this, SLOT( onTypeSelectorChanged( int )));
00116     connect( view_man_, SIGNAL( currentChanged() ), this, SLOT( onCurrentChanged() ));
00117   }
00118   else
00119   {
00120     properties_view_->setModel( NULL );
00121   }
00122   onCurrentChanged();
00123 }
00124 
00125 void ViewsPanel::onTypeSelectorChanged( int selected_index )
00126 {
00127   QString class_id = camera_type_selector_->itemData( selected_index ).toString();
00128   view_man_->setCurrentViewControllerType( class_id );
00129 }
00130 
00131 void ViewsPanel::onZeroClicked()
00132 {
00133   if( view_man_->getCurrent() )
00134   {
00135     view_man_->getCurrent()->reset();
00136   }
00137 }
00138 
00139 void ViewsPanel::setCurrentViewFromIndex( const QModelIndex& index )
00140 {
00141   Property* prop = view_man_->getPropertyModel()->getProp( index );
00142   if( ViewController* view = qobject_cast<ViewController*>( prop ))
00143   {
00144     view_man_->setCurrentFrom( view );
00145   }
00146 }
00147 
00148 void ViewsPanel::onDeleteClicked()
00149 {
00150   QList<ViewController*> views_to_delete = properties_view_->getSelectedObjects<ViewController>();
00151 
00152   for( int i = 0; i < views_to_delete.size(); i++ )
00153   {
00154     // TODO: should eventually move to a scheme where the CURRENT view
00155     // is not in the same list as the saved views, at which point this
00156     // check can go away.
00157     if( views_to_delete[ i ] != view_man_->getCurrent() )
00158     {
00159       delete views_to_delete[ i ];
00160     }
00161   }
00162 }
00163 
00164 void ViewsPanel::renameSelected()
00165 {
00166   QList<ViewController*> views_to_rename = properties_view_->getSelectedObjects<ViewController>();
00167   if( views_to_rename.size() == 1 )
00168   {
00169     ViewController* view = views_to_rename[ 0 ];
00170 
00171     // TODO: should eventually move to a scheme where the CURRENT view
00172     // is not in the same list as the saved views, at which point this
00173     // check can go away.
00174     if( view == view_man_->getCurrent() )
00175     {
00176       return;
00177     }
00178 
00179     QString old_name = view->getName();
00180     QString new_name = QInputDialog::getText( this, "Rename View", "New Name?", QLineEdit::Normal, old_name );
00181 
00182     if( new_name.isEmpty() || new_name == old_name )
00183     {
00184       return;
00185     }
00186 
00187     view->setName( new_name );
00188   }
00189 }
00190 
00191 void ViewsPanel::onCurrentChanged()
00192 {
00193   QString formatted_class_id = ViewController::formatClassId( view_man_->getCurrent()->getClassId() );
00194 
00195   // Make sure the type selector shows the type of the current view.
00196   // This is only here in case the type is changed programmatically,
00197   // instead of via the camera_type_selector_ being used.
00198   camera_type_selector_->setCurrentIndex( camera_type_selector_->findText( formatted_class_id ));
00199 
00200   properties_view_->setAnimated( false );
00201   view_man_->getCurrent()->expand();
00202   properties_view_->setAnimated( true );
00203 }
00204 
00205 void ViewsPanel::save( Config config ) const
00206 {
00207   Panel::save( config );
00208   properties_view_->save( config );
00209 }
00210 
00211 void ViewsPanel::load( const Config& config )
00212 {
00213   Panel::load( config );
00214   properties_view_->load( config );
00215 }
00216 
00217 } // namespace rviz
00218 


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:16