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 #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 )
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 }