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 #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 // Set the text and emit data-changed. 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 // Set the text and emit data-changed. 00138 setText( 1, QString::fromStdString( new_choice )); 00139 } 00140 } 00141 } 00142 00143 } // end namespace rviz