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 #include <QLineEdit>
00034
00035 #include "rviz/properties/edit_enum_item.h"
00036 #include "rviz/properties/property.h"
00037 #include "rviz/properties/property_tree_widget.h"
00038
00039 namespace rviz
00040 {
00041
00042 EditEnumItem::EditEnumItem( EditEnumProperty* property )
00043 : PropertyWidgetItem( property, property->getName(), property->hasSetter() )
00044 , signal_changes_( true )
00045 {
00046 }
00047
00048 QWidget* EditEnumItem::createEditor( QWidget* parent, const QStyleOptionViewItem & option )
00049 {
00050 QComboBox* editor = new QComboBox( parent );
00051 editor->setEditable( true );
00052 editor->setFrame( false );
00053 QObject::connect( editor, SIGNAL( currentIndexChanged( int )), this, SLOT( setChoiceIndex( int )));
00054 return editor;
00055 }
00056
00057 bool EditEnumItem::setEditorData( QWidget* editor )
00058 {
00059 if( QComboBox* enum_editor = qobject_cast<QComboBox*>( editor ))
00060 {
00061 signal_changes_ = false;
00062 enum_editor->clear();
00063 if( option_cb_ )
00064 {
00065 choices_.clear();
00066 option_cb_( choices_ );
00067 }
00068 int index = 0;
00069 int chosen_index = -1;
00070 for( V_string::iterator ci = choices_.begin(); ci != choices_.end(); ci++ )
00071 {
00072 enum_editor->addItem( QString::fromStdString( *ci ));
00073 if( *ci == choice_ )
00074 {
00075 chosen_index = index;
00076 }
00077 index++;
00078 }
00079 if( chosen_index == -1 )
00080 {
00081 enum_editor->setEditText( QString::fromStdString( choice_ ));
00082 }
00083 else
00084 {
00085 enum_editor->setCurrentIndex( chosen_index );
00086 }
00087 signal_changes_ = true;
00088 return true;
00089 }
00090 return false;
00091 }
00092
00093 bool EditEnumItem::setModelData( QWidget* editor )
00094 {
00095 if( QComboBox* enum_editor = qobject_cast<QComboBox*>( editor ))
00096 {
00097 std::string new_choice = enum_editor->currentText().toStdString();
00098 if( choice_ != new_choice )
00099 {
00100 choice_ = new_choice;
00101
00102 setText( 1, QString::fromStdString( choice_ ));
00103 }
00104 return true;
00105 }
00106 return false;
00107 }
00108
00109 void EditEnumItem::setChoice( const std::string& new_choice )
00110 {
00111 if( new_choice != choice_ )
00112 {
00113 choice_ = new_choice;
00114 setRightText( choice_ );
00115 }
00116 }
00117
00118 void EditEnumItem::setChoices( V_string& choices )
00119 {
00120 if( choices_ != choices )
00121 {
00122 choices_ = choices;
00123 bool ign = getProperty()->getPropertyTreeWidget()->setIgnoreChanges( true );
00124 emitDataChanged();
00125 getProperty()->getPropertyTreeWidget()->setIgnoreChanges( ign );
00126 }
00127 }
00128
00129 void EditEnumItem::setChoiceIndex( int index )
00130 {
00131 if( signal_changes_ && index >= 0 && index < (int) choices_.size() )
00132 {
00133 std::string new_choice = choices_[ index ];
00134 if( new_choice != choice_ )
00135 {
00136 choice_ = new_choice;
00137
00138 setText( 1, QString::fromStdString( new_choice ));
00139 }
00140 }
00141 }
00142
00143 }