enum_item.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, 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 <QPainter>
00031 #include <QStyleOptionViewItem>
00032 #include <QComboBox>
00033 
00034 #include "rviz/properties/enum_item.h"
00035 #include "rviz/properties/property.h"
00036 #include "rviz/properties/property_tree_widget.h"
00037 
00038 namespace rviz
00039 {
00040 
00041 EnumItem::EnumItem( EnumProperty* property )
00042   : PropertyWidgetItem( property, property->getName(), property->hasSetter() )
00043   , choice_value_( -93993328 ) // some very unlikely value
00044   , signal_changes_( true )
00045 {
00046 }
00047 
00048 QWidget* EnumItem::createEditor( QWidget* parent, const QStyleOptionViewItem & option )
00049 {
00050   QComboBox* editor = new QComboBox( parent );
00051   editor->setFrame( false );
00052   QObject::connect( editor, SIGNAL( currentIndexChanged( int )), this, SLOT( setChoiceIndex( int )));
00053   return editor;
00054 }
00055 
00056 bool EnumItem::setEditorData( QWidget* editor )
00057 {
00058   if( QComboBox* enum_editor = qobject_cast<QComboBox*>( editor ))
00059   {
00060     signal_changes_ = false;
00061     enum_editor->clear();
00062     int index = 0;
00063     int chosen_index = -1;
00064     for( Choices::iterator ci = choices_.begin(); ci != choices_.end(); ci++ )
00065     {
00066       enum_editor->addItem( QString::fromStdString( (*ci).first ), (*ci).second );
00067       if( (*ci).second == choice_value_ )
00068       {
00069         chosen_index = index;
00070       }
00071       index++;
00072     }
00073     enum_editor->setCurrentIndex( chosen_index );
00074     signal_changes_ = true;
00075     return true;
00076   }
00077   return false;
00078 }
00079 
00080 bool EnumItem::setModelData( QWidget* editor )
00081 {
00082   if( QComboBox* enum_editor = qobject_cast<QComboBox*>( editor ))
00083   {
00084     setChoiceIndex( enum_editor->currentIndex() );
00085     return true;
00086   }
00087   return false;
00088 }
00089 
00090 void EnumItem::setChoiceIndex( int index )
00091 {
00092   if( signal_changes_ && index >= 0 && index < (int) choices_.size() )
00093   {
00094     int new_choice = choices_[ index ].second;
00095     if( new_choice != choice_value_ )
00096     {
00097       setChoiceValue( new_choice );
00098       emitDataChanged();
00099     }
00100   }
00101 }
00102 
00103 std::string EnumItem::currentChoiceName()
00104 {
00105   for( Choices::iterator ci = choices_.begin(); ci != choices_.end(); ci++ )
00106   {
00107     if( (*ci).second == choice_value_ )
00108     {
00109       return (*ci).first;
00110     }
00111   }
00112   return "";
00113 }
00114 
00115 void EnumItem::setChoices( Choices choices )
00116 {
00117   if( choices_ != choices )
00118   {
00119     choices_ = choices;
00120     setRightText( currentChoiceName() );
00121   }
00122 }
00123 
00124 void EnumItem::setChoiceValue( int choice_value )
00125 {
00126   if( choice_value_ != choice_value )
00127   {
00128     choice_value_ = choice_value;
00129     setRightText( currentChoiceName() );
00130   }
00131 }
00132 
00133 } // end namespace rviz


rviz
Author(s): Dave Hershberger, Josh Faust
autogenerated on Mon Jan 6 2014 11:54:32