double_list_widget.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2012, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Dave Coleman */
00036 
00037 #include <QVBoxLayout>
00038 #include <QHBoxLayout>
00039 #include <QGroupBox>
00040 #include <QPushButton>
00041 #include <QFormLayout>
00042 #include <QLineEdit>
00043 #include <QMessageBox>
00044 #include <QString>
00045 #include <QHeaderView>
00046 #include "double_list_widget.h"
00047 
00048 namespace moveit_setup_assistant
00049 {
00050 
00051 // ******************************************************************************************
00052 //
00053 // ******************************************************************************************
00054 DoubleListWidget::DoubleListWidget( QWidget *parent, moveit_setup_assistant::MoveItConfigDataPtr config_data,
00055                                     QString long_name, QString short_name , bool add_ok_cancel)
00056   :  QWidget( parent ), long_name_( long_name ), short_name_( short_name ), config_data_( config_data )
00057 {
00058   // Basic widget container
00059   QVBoxLayout *layout = new QVBoxLayout( );
00060 
00061   // Label ------------------------------------------------
00062   title_ = new QLabel( "", this ); // specify the title from the parent widget
00063   QFont group_title_font( "Arial", 12, QFont::Bold );
00064   title_->setFont(group_title_font);
00065   layout->addWidget( title_ );
00066 
00067   // Double selection lists -------------------------------
00068   QHBoxLayout *hlayout = new QHBoxLayout();
00069 
00070   // Left column -------------------------------------------
00071   QVBoxLayout *column1 = new QVBoxLayout();
00072 
00073   // Label
00074   column1_label_ = new QLabel( QString("Available ").append( short_name_ ).append( 's' ), this );
00075   column1->addWidget( column1_label_ );
00076 
00077   // Table
00078   data_table_ = new QTableWidget( this );
00079   data_table_->setColumnCount(1);
00080   data_table_->setSortingEnabled(true);
00081   column1->addWidget( data_table_ );
00082   connect( data_table_->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), this,
00083            SLOT( previewSelectedLeft( QItemSelection, QItemSelection ) ) );
00084 
00085   // Table headers
00086   QStringList data_header_list;
00087   data_header_list.append( QString( " Names" ).prepend( short_name_ ));
00088   data_table_->setHorizontalHeaderLabels( data_header_list );
00089   data_table_->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);
00090 
00091   // Add layouts
00092   hlayout->addLayout( column1 );
00093 
00094   // Center column ------------------------------------------
00095   QVBoxLayout *column2 = new QVBoxLayout();
00096   column2->setSizeConstraint( QLayout::SetFixedSize ); // constraint it
00097 
00098   // Right Arrow Button
00099   QPushButton *btn_right = new QPushButton( ">", this);
00100   btn_right->setMaximumSize(25, 80);
00101   connect( btn_right, SIGNAL( clicked() ), this, SLOT( selectDataButtonClicked() ) );
00102   column2->addWidget( btn_right );
00103 
00104   // Left Arrow Button
00105   QPushButton *btn_left = new QPushButton( "<", this);
00106   btn_left->setMaximumSize(25, 80);
00107   connect( btn_left, SIGNAL( clicked() ), this, SLOT( deselectDataButtonClicked() ) );
00108   column2->addWidget( btn_left );
00109 
00110   // Add layouts
00111   hlayout->addLayout( column2 );
00112 
00113   // Right column -------------------------------------------
00114   QVBoxLayout *column3 = new QVBoxLayout();
00115 
00116   // Label
00117   column2_label_ = new QLabel( QString("Selected ").append( short_name_ ).append( "s" ), this );
00118   column3->addWidget( column2_label_ );
00119 
00120   // Table
00121   selected_data_table_ = new QTableWidget( this );
00122   selected_data_table_->setColumnCount(1);
00123   selected_data_table_->setSortingEnabled(true);
00124   column3->addWidget( selected_data_table_ );
00125   connect( selected_data_table_->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), this,
00126            SLOT( previewSelectedRight( QItemSelection, QItemSelection ) ) );
00127 
00128   // Table Headers (use same)
00129   selected_data_table_->setHorizontalHeaderLabels( data_header_list );
00130 
00131   // Add layouts
00132   hlayout->addLayout( column3 );
00133 
00134   // End Double Selection List ---------------------------------
00135   layout->addLayout( hlayout );
00136 
00137   if (add_ok_cancel)
00138   {
00139     // Button controls -------------------------------------------
00140     QHBoxLayout *controls_layout = new QHBoxLayout();
00141     controls_layout->setContentsMargins( 0, 25, 0, 15 );
00142 
00143     // Spacer
00144     QWidget *spacer = new QWidget( this );
00145     spacer->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
00146     controls_layout->addWidget( spacer );
00147 
00148     // Save
00149     QPushButton *btn_save = new QPushButton( "&Save", this );
00150     //btn_save->setMaximumWidth( 200 );
00151     connect( btn_save, SIGNAL(clicked()), this, SIGNAL( doneEditing() ) );
00152     controls_layout->addWidget( btn_save );
00153     controls_layout->setAlignment(btn_save, Qt::AlignRight);
00154 
00155     // Cancel
00156     QPushButton *btn_cancel = new QPushButton( "&Cancel", this );
00157     //btn_cancel->setMaximumWidth( 200 );
00158     connect( btn_cancel, SIGNAL(clicked()), this, SIGNAL( cancelEditing() ) );
00159     controls_layout->addWidget( btn_cancel );
00160     controls_layout->setAlignment(btn_cancel, Qt::AlignRight);
00161 
00162     // Add layout
00163     layout->addLayout( controls_layout );
00164   }
00165 
00166   // Finish Layout --------------------------------------------------
00167   this->setLayout(layout);
00168 }
00169 
00170 // ******************************************************************************************
00171 // Set the left box
00172 // ******************************************************************************************
00173 void DoubleListWidget::setAvailable( const std::vector<std::string> &items )
00174 {
00175   setTable( items, data_table_ );
00176 
00177   // Resize both tables
00178   data_table_->resizeColumnToContents(0);
00179   selected_data_table_->setColumnWidth( 0, data_table_->columnWidth( 0 ) );
00180 }
00181 
00182 // ******************************************************************************************
00183 // Set the right box
00184 // ******************************************************************************************
00185 void DoubleListWidget::setSelected( const std::vector<std::string> &items )
00186 {
00187   setTable( items, selected_data_table_ );
00188 }
00189 
00190 void DoubleListWidget::clearContents()
00191 {
00192   selected_data_table_->clearContents();
00193   data_table_->clearContents();
00194 }
00195 
00196 void DoubleListWidget::setColumnNames( const QString &col1, const QString &col2)
00197 {
00198   column1_label_->setText(col1);
00199   column2_label_->setText(col2);
00200 }
00201 
00202 // ******************************************************************************************
00203 // Convenience function for reusing set table code
00204 // ******************************************************************************************
00205 void DoubleListWidget::setTable( const std::vector<std::string> &items, QTableWidget *table )
00206 {
00207   // Disable Table
00208   table->setUpdatesEnabled(false); // prevent table from updating until we are completely done
00209   table->setDisabled(true); // make sure we disable it so that the cellChanged event is not called
00210   table->clearContents();
00211 
00212   // Set size of datatable
00213   table->setRowCount( items.size() );
00214 
00215   // Loop through every item
00216   int row = 0;
00217   for( std::vector<std::string>::const_iterator data_it = items.begin(); data_it != items.end(); ++data_it )
00218   {
00219     // This is a hack to prevent a dummy joint from being added. Not really the best place to place this but
00220     // here is computationally smart
00221     if( *data_it == "ASSUMED_FIXED_ROOT_JOINT" )
00222       continue;
00223 
00224     // Create row elements
00225     QTableWidgetItem* data_name = new QTableWidgetItem( data_it->c_str() );
00226     data_name->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00227 
00228     // Add to table
00229     table->setItem( row, 0, data_name );
00230 
00231     // Increment counter
00232     ++row;
00233   }
00234 
00235   table->setRowCount( row );
00236 
00237   // Reenable
00238   table->setUpdatesEnabled(true); // prevent table from updating until we are completely done
00239   table->setDisabled(false); // make sure we disable it so that the cellChanged event is not called
00240 }
00241 
00242 // ******************************************************************************************
00243 // Move selected data right
00244 // ******************************************************************************************
00245 void DoubleListWidget::selectDataButtonClicked()
00246 {
00247   // Get list of all selected items
00248   QList<QTableWidgetItem*> selected = data_table_->selectedItems();
00249 
00250   // Loop through all selected items
00251   for(int i = 0; i < selected.size(); i++)
00252   {
00253     std::string name = selected[i]->text().toStdString();
00254     bool alreadyExists = false;
00255     int rowToAdd = 0;
00256 
00257     // Check if this selected joint is already in the selected joint table
00258     for(int r = 0; r < selected_data_table_->rowCount(); r++)
00259     {
00260       QTableWidgetItem* item = selected_data_table_->item(r, 0);
00261 
00262       if(item->text().toStdString() == name)
00263       {
00264         alreadyExists = true;
00265         break;
00266       }
00267       rowToAdd = r + 1;
00268     }
00269 
00270     // This joint needs to be added to the selected joint table
00271     if(!alreadyExists)
00272     {
00273       selected_data_table_->setRowCount(selected_data_table_->rowCount() + 1);
00274       QTableWidgetItem* newItem = new QTableWidgetItem(name.c_str());
00275       newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00276       selected_data_table_->setItem(rowToAdd, 0, newItem);
00277     }
00278   }
00279 
00280   Q_EMIT( selectionUpdated() );
00281 }
00282 
00283 // ******************************************************************************************
00284 // Move selected data left
00285 // ******************************************************************************************
00286 void DoubleListWidget::deselectDataButtonClicked()
00287 {
00288   // Get list of joints to be removed from selected list
00289   QList<QTableWidgetItem*> deselected = selected_data_table_->selectedItems();
00290 
00291   // loop through deselect list and remove
00292   for(int i = 0; i < deselected.size(); i++)
00293   {
00294     selected_data_table_->removeRow(deselected[i]->row());
00295   }
00296 
00297   Q_EMIT( selectionUpdated() );
00298 }
00299 
00300 // ******************************************************************************************
00301 // Highlight links of robot for left list
00302 // ******************************************************************************************
00303 void DoubleListWidget::previewSelectedLeft( const QItemSelection& selected, const QItemSelection& deselected )
00304 {
00305   const QList<QTableWidgetItem*> selected_items = data_table_->selectedItems();
00306   previewSelected(selected_items);
00307 }
00308 
00309 // ******************************************************************************************
00310 // Highlight links of robot for right list
00311 // ******************************************************************************************
00312 void DoubleListWidget::previewSelectedRight( const QItemSelection& selected, const QItemSelection& deselected )
00313 {
00314   const QList<QTableWidgetItem*> selected_items = selected_data_table_->selectedItems();
00315   previewSelected(selected_items);
00316 }
00317 
00318 // ******************************************************************************************
00319 // Highlight links of robot
00320 // ******************************************************************************************
00321 void DoubleListWidget::previewSelected( const QList<QTableWidgetItem*>& selected )
00322 {
00323   // Check that an element was selected
00324   if( !selected.size() )
00325     return;
00326 
00327   std::vector<std::string> selected_vector;
00328 
00329   // Convert QList to std vector
00330   for(int i = 0; i < selected.size(); ++i)
00331   {
00332     selected_vector.push_back( selected[i]->text().toStdString() );
00333     //std::cout << "  " << selected[i]->text().toStdString() << std::endl;
00334   }
00335 
00336   // Send to shared function
00337   Q_EMIT( previewSelected( selected_vector ) );
00338 }
00339 
00340 } //namespace moveit_setup_assistant


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Mon Oct 6 2014 02:32:27