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 <map>
00031
00032 #include <QGroupBox>
00033 #include <QTreeWidget>
00034 #include <QLabel>
00035 #include <QLineEdit>
00036 #include <QTextBrowser>
00037 #include <QVBoxLayout>
00038 #include <QDialogButtonBox>
00039 #include <QPushButton>
00040
00041 #include "new_object_dialog.h"
00042
00043 namespace rviz
00044 {
00045
00046 NewObjectDialog::NewObjectDialog( Factory* factory,
00047 const QString& object_type,
00048 const QStringList& disallowed_display_names,
00049 const QStringList& disallowed_class_lookup_names,
00050 QString* lookup_name_output,
00051 QString* display_name_output,
00052 QWidget* parent )
00053 : QDialog( parent )
00054 , factory_( factory )
00055 , disallowed_display_names_( disallowed_display_names )
00056 , disallowed_class_lookup_names_( disallowed_class_lookup_names )
00057 , lookup_name_output_( lookup_name_output )
00058 , display_name_output_( display_name_output )
00059 {
00060
00061
00062
00063 QGroupBox* type_box = new QGroupBox( object_type + " Type" );
00064
00065 QTreeWidget* tree = new QTreeWidget;
00066 tree->setHeaderHidden( true );
00067 fillTree( tree );
00068
00069 QLabel* description_label = new QLabel( "Description:" );
00070 description_ = new QTextBrowser;
00071 description_->setMaximumHeight( 100 );
00072 description_->setOpenExternalLinks( true );
00073
00074 QVBoxLayout* type_layout = new QVBoxLayout;
00075 type_layout->addWidget( tree );
00076 type_layout->addWidget( description_label );
00077 type_layout->addWidget( description_ );
00078
00079 type_box->setLayout( type_layout );
00080
00081
00082 QGroupBox* name_box;
00083 if( display_name_output_ )
00084 {
00085 name_box = new QGroupBox( object_type + " Name" );
00086 name_editor_ = new QLineEdit;
00087 QVBoxLayout* name_layout = new QVBoxLayout;
00088 name_layout->addWidget( name_editor_ );
00089 name_box->setLayout( name_layout );
00090 }
00091
00092
00093 button_box_ = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
00094 Qt::Horizontal );
00095
00096 QVBoxLayout* main_layout = new QVBoxLayout;
00097 main_layout->addWidget( type_box );
00098 if( display_name_output_ )
00099 {
00100 main_layout->addWidget( name_box );
00101 }
00102 main_layout->addWidget( button_box_ );
00103 setLayout( main_layout );
00104
00105
00106 connect( tree, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* )),
00107 this, SLOT( onDisplaySelected( QTreeWidgetItem* )));
00108 connect( tree, SIGNAL( itemActivated( QTreeWidgetItem*, int )),
00109 this, SLOT( accept() ));
00110 connect( button_box_, SIGNAL( accepted() ), this, SLOT( accept() ));
00111 connect( button_box_, SIGNAL( rejected() ), this, SLOT( reject() ));
00112
00113 if( display_name_output_ )
00114 {
00115 connect( name_editor_, SIGNAL( textEdited( const QString& )),
00116 this, SLOT( onNameChanged() ));
00117 }
00118 }
00119
00120 void NewObjectDialog::fillTree( QTreeWidget* tree )
00121 {
00122 QStringList classes = factory_->getDeclaredClassIds();
00123
00124
00125 std::map<QString, QTreeWidgetItem*> package_items;
00126
00127 for( int i = 0; i < classes.size(); i++ )
00128 {
00129 QString lookup_name = classes[ i ];
00130 QString package = factory_->getClassPackage( lookup_name );
00131 QString description = factory_->getClassDescription( lookup_name );
00132 QString name = factory_->getClassName( lookup_name );
00133
00134 QTreeWidgetItem* package_item;
00135
00136 std::map<QString, QTreeWidgetItem*>::iterator mi;
00137 mi = package_items.find( package );
00138 if( mi == package_items.end() )
00139 {
00140 package_item = new QTreeWidgetItem( tree );
00141 package_item->setText( 0, package );
00142 package_item->setExpanded( true );
00143 package_items[ package ] = package_item;
00144 }
00145 else
00146 {
00147 package_item = (*mi).second;
00148 }
00149 QTreeWidgetItem* class_item = new QTreeWidgetItem( package_item );
00150 class_item->setText( 0, name );
00151 class_item->setWhatsThis( 0, description );
00152
00153 class_item->setData( 0, Qt::UserRole, lookup_name );
00154 class_item->setDisabled( disallowed_class_lookup_names_.contains( lookup_name ));
00155 }
00156 }
00157
00158 void NewObjectDialog::onDisplaySelected( QTreeWidgetItem* selected_item )
00159 {
00160 QString html = "<html><body>" + selected_item->whatsThis( 0 ) + "</body></html>";
00161 description_->setHtml( html );
00162
00163
00164 QVariant user_data = selected_item->data( 0, Qt::UserRole );
00165 bool selection_is_valid = user_data.isValid();
00166 if( selection_is_valid )
00167 {
00168 lookup_name_ = user_data.toString();
00169 if( display_name_output_ )
00170 {
00171 QString display_name = selected_item->text( 0 );
00172
00173 int counter = 1;
00174 QString name;
00175 do
00176 {
00177 name = display_name;
00178 if( counter > 1 )
00179 {
00180 name += QString::number( counter );
00181 }
00182 ++counter;
00183
00184 } while( disallowed_display_names_.contains( name ));
00185
00186 name_editor_->setText( name );
00187 }
00188 }
00189 else
00190 {
00191 lookup_name_ = "";
00192 if( display_name_output_ )
00193 {
00194 name_editor_->setText( "" );
00195 }
00196 }
00197 button_box_->button( QDialogButtonBox::Ok )->setEnabled( isValid() );
00198 }
00199
00200 bool NewObjectDialog::isValid()
00201 {
00202 if( lookup_name_.size() == 0 )
00203 {
00204 setError( "Select a Display type." );
00205 return false;
00206 }
00207 if( display_name_output_ )
00208 {
00209 QString display_name = name_editor_->text();
00210 if( display_name.size() == 0 )
00211 {
00212 setError( "Enter a name for the display." );
00213 return false;
00214 }
00215 if( disallowed_display_names_.contains( display_name ))
00216 {
00217 setError( "Name in use. Display names must be unique." );
00218 return false;
00219 }
00220 }
00221 setError( "" );
00222 return true;
00223 }
00224
00225 void NewObjectDialog::setError( const QString& error_text )
00226 {
00227 button_box_->button( QDialogButtonBox::Ok )->setToolTip( error_text );
00228 }
00229
00230 void NewObjectDialog::onNameChanged()
00231 {
00232 button_box_->button( QDialogButtonBox::Ok )->setEnabled( isValid() );
00233 }
00234
00235 void NewObjectDialog::accept()
00236 {
00237 if( isValid() )
00238 {
00239 *lookup_name_output_ = lookup_name_;
00240 if( display_name_output_ )
00241 {
00242 *display_name_output_ = name_editor_->text();
00243 }
00244 QDialog::accept();
00245 }
00246 }
00247
00248 }
00249