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 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 DoubleListWidget::DoubleListWidget(QWidget* parent, moveit_setup_assistant::MoveItConfigDataPtr config_data,
00054                                    QString long_name, QString short_name, bool add_ok_cancel)
00055   : QWidget(parent), long_name_(long_name), short_name_(short_name), config_data_(config_data)
00056 {
00057   // Basic widget container
00058   QVBoxLayout* layout = new QVBoxLayout();
00059 
00060   // Label ------------------------------------------------
00061   title_ = new QLabel("", this);  // specify the title from the parent widget
00062   QFont group_title_font("Arial", 12, QFont::Bold);
00063   title_->setFont(group_title_font);
00064   layout->addWidget(title_);
00065 
00066   // Double selection lists -------------------------------
00067   QHBoxLayout* hlayout = new QHBoxLayout();
00068 
00069   // Left column -------------------------------------------
00070   QVBoxLayout* column1 = new QVBoxLayout();
00071 
00072   // Label
00073   column1_label_ = new QLabel(QString("Available ").append(short_name_).append('s'), this);
00074   column1->addWidget(column1_label_);
00075 
00076   // Table
00077   data_table_ = new QTableWidget(this);
00078   data_table_->setColumnCount(1);
00079   data_table_->setSortingEnabled(true);
00080   column1->addWidget(data_table_);
00081   connect(data_table_->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this,
00082           SLOT(previewSelectedLeft(QItemSelection, QItemSelection)));
00083 
00084   // Table headers
00085   QStringList data_header_list;
00086   data_header_list.append(QString(" Names").prepend(short_name_));
00087   data_table_->setHorizontalHeaderLabels(data_header_list);
00088   data_table_->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);
00089 
00090   // Add layouts
00091   hlayout->addLayout(column1);
00092 
00093   // Center column ------------------------------------------
00094   QVBoxLayout* column2 = new QVBoxLayout();
00095   column2->setSizeConstraint(QLayout::SetFixedSize);  // constraint it
00096 
00097   // Right Arrow Button
00098   QPushButton* btn_right = new QPushButton(">", this);
00099   btn_right->setMaximumSize(25, 80);
00100   connect(btn_right, SIGNAL(clicked()), this, SLOT(selectDataButtonClicked()));
00101   column2->addWidget(btn_right);
00102 
00103   // Left Arrow Button
00104   QPushButton* btn_left = new QPushButton("<", this);
00105   btn_left->setMaximumSize(25, 80);
00106   connect(btn_left, SIGNAL(clicked()), this, SLOT(deselectDataButtonClicked()));
00107   column2->addWidget(btn_left);
00108 
00109   // Add layouts
00110   hlayout->addLayout(column2);
00111 
00112   // Right column -------------------------------------------
00113   QVBoxLayout* column3 = new QVBoxLayout();
00114 
00115   // Label
00116   column2_label_ = new QLabel(QString("Selected ").append(short_name_).append("s"), this);
00117   column3->addWidget(column2_label_);
00118 
00119   // Table
00120   selected_data_table_ = new QTableWidget(this);
00121   selected_data_table_->setColumnCount(1);
00122   selected_data_table_->setSortingEnabled(true);
00123   column3->addWidget(selected_data_table_);
00124   connect(selected_data_table_->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this,
00125           SLOT(previewSelectedRight(QItemSelection, QItemSelection)));
00126 
00127   // Table Headers (use same)
00128   selected_data_table_->setHorizontalHeaderLabels(data_header_list);
00129 
00130   // Add layouts
00131   hlayout->addLayout(column3);
00132 
00133   // End Double Selection List ---------------------------------
00134   layout->addLayout(hlayout);
00135 
00136   if (add_ok_cancel)
00137   {
00138     // Button controls -------------------------------------------
00139     QHBoxLayout* controls_layout = new QHBoxLayout();
00140     controls_layout->setContentsMargins(0, 25, 0, 15);
00141 
00142     // Spacer
00143     QWidget* spacer = new QWidget(this);
00144     spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
00145     controls_layout->addWidget(spacer);
00146 
00147     // Save
00148     QPushButton* btn_save = new QPushButton("&Save", this);
00149     // btn_save->setMaximumWidth( 200 );
00150     connect(btn_save, SIGNAL(clicked()), this, SIGNAL(doneEditing()));
00151     controls_layout->addWidget(btn_save);
00152     controls_layout->setAlignment(btn_save, Qt::AlignRight);
00153 
00154     // Cancel
00155     QPushButton* btn_cancel = new QPushButton("&Cancel", this);
00156     // btn_cancel->setMaximumWidth( 200 );
00157     connect(btn_cancel, SIGNAL(clicked()), this, SIGNAL(cancelEditing()));
00158     controls_layout->addWidget(btn_cancel);
00159     controls_layout->setAlignment(btn_cancel, Qt::AlignRight);
00160 
00161     // Add layout
00162     layout->addLayout(controls_layout);
00163   }
00164 
00165   // Finish Layout --------------------------------------------------
00166   this->setLayout(layout);
00167 }
00168 
00169 // ******************************************************************************************
00170 // Set the left box
00171 // ******************************************************************************************
00172 void DoubleListWidget::setAvailable(const std::vector<std::string>& items)
00173 {
00174   setTable(items, data_table_);
00175 
00176   // Resize both tables
00177   data_table_->resizeColumnToContents(0);
00178   selected_data_table_->setColumnWidth(0, data_table_->columnWidth(0));
00179 }
00180 
00181 // ******************************************************************************************
00182 // Set the right box
00183 // ******************************************************************************************
00184 void DoubleListWidget::setSelected(const std::vector<std::string>& items)
00185 {
00186   setTable(items, selected_data_table_);
00187 }
00188 
00189 void DoubleListWidget::clearContents()
00190 {
00191   selected_data_table_->clearContents();
00192   data_table_->clearContents();
00193 }
00194 
00195 void DoubleListWidget::setColumnNames(const QString& col1, const QString& col2)
00196 {
00197   column1_label_->setText(col1);
00198   column2_label_->setText(col2);
00199 }
00200 
00201 // ******************************************************************************************
00202 // Convenience function for reusing set table code
00203 // ******************************************************************************************
00204 void DoubleListWidget::setTable(const std::vector<std::string>& items, QTableWidget* table)
00205 {
00206   // Disable Table
00207   table->setUpdatesEnabled(false);  // prevent table from updating until we are completely done
00208   table->setDisabled(true);         // make sure we disable it so that the cellChanged event is not called
00209   table->clearContents();
00210 
00211   // Set size of datatable
00212   table->setRowCount(items.size());
00213 
00214   // Loop through every item
00215   int row = 0;
00216   for (std::vector<std::string>::const_iterator data_it = items.begin(); data_it != items.end(); ++data_it)
00217   {
00218     // This is a hack to prevent a dummy joint from being added. Not really the best place to place this but
00219     // here is computationally smart
00220     if (*data_it == "ASSUMED_FIXED_ROOT_JOINT")
00221       continue;
00222 
00223     // Create row elements
00224     QTableWidgetItem* data_name = new QTableWidgetItem(data_it->c_str());
00225     data_name->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00226 
00227     // Add to table
00228     table->setItem(row, 0, data_name);
00229 
00230     // Increment counter
00231     ++row;
00232   }
00233 
00234   table->setRowCount(row);
00235 
00236   // Reenable
00237   table->setUpdatesEnabled(true);  // prevent table from updating until we are completely done
00238   table->setDisabled(false);       // make sure we disable it so that the cellChanged event is not called
00239 }
00240 
00241 // ******************************************************************************************
00242 // Move selected data right
00243 // ******************************************************************************************
00244 void DoubleListWidget::selectDataButtonClicked()
00245 {
00246   // Get list of all selected items
00247   QList<QTableWidgetItem*> selected = data_table_->selectedItems();
00248 
00249   // Loop through all selected items
00250   for (int i = 0; i < selected.size(); i++)
00251   {
00252     std::string name = selected[i]->text().toStdString();
00253     bool alreadyExists = false;
00254     int rowToAdd = 0;
00255 
00256     // Check if this selected joint is already in the selected joint table
00257     for (int r = 0; r < selected_data_table_->rowCount(); r++)
00258     {
00259       QTableWidgetItem* item = selected_data_table_->item(r, 0);
00260 
00261       if (item->text().toStdString() == name)
00262       {
00263         alreadyExists = true;
00264         break;
00265       }
00266       rowToAdd = r + 1;
00267     }
00268 
00269     // This joint needs to be added to the selected joint table
00270     if (!alreadyExists)
00271     {
00272       selected_data_table_->setRowCount(selected_data_table_->rowCount() + 1);
00273       QTableWidgetItem* newItem = new QTableWidgetItem(name.c_str());
00274       newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00275       selected_data_table_->setItem(rowToAdd, 0, newItem);
00276     }
00277   }
00278 
00279   Q_EMIT(selectionUpdated());
00280 }
00281 
00282 // ******************************************************************************************
00283 // Move selected data left
00284 // ******************************************************************************************
00285 void DoubleListWidget::deselectDataButtonClicked()
00286 {
00287   // Get list of joints to be removed from selected list
00288   QList<QTableWidgetItem*> deselected = selected_data_table_->selectedItems();
00289 
00290   // loop through deselect list and remove
00291   for (int i = 0; i < deselected.size(); i++)
00292   {
00293     selected_data_table_->removeRow(deselected[i]->row());
00294   }
00295 
00296   Q_EMIT(selectionUpdated());
00297 }
00298 
00299 // ******************************************************************************************
00300 // Highlight links of robot for left list
00301 // ******************************************************************************************
00302 void DoubleListWidget::previewSelectedLeft(const QItemSelection& selected, const QItemSelection& deselected)
00303 {
00304   const QList<QTableWidgetItem*> selected_items = data_table_->selectedItems();
00305   previewSelected(selected_items);
00306 }
00307 
00308 // ******************************************************************************************
00309 // Highlight links of robot for right list
00310 // ******************************************************************************************
00311 void DoubleListWidget::previewSelectedRight(const QItemSelection& selected, const QItemSelection& deselected)
00312 {
00313   const QList<QTableWidgetItem*> selected_items = selected_data_table_->selectedItems();
00314   previewSelected(selected_items);
00315 }
00316 
00317 // ******************************************************************************************
00318 // Highlight links of robot
00319 // ******************************************************************************************
00320 void DoubleListWidget::previewSelected(const QList<QTableWidgetItem*>& selected)
00321 {
00322   // Check that an element was selected
00323   if (!selected.size())
00324     return;
00325 
00326   std::vector<std::string> selected_vector;
00327 
00328   // Convert QList to std vector
00329   for (int i = 0; i < selected.size(); ++i)
00330   {
00331     selected_vector.push_back(selected[i]->text().toStdString());
00332   }
00333 
00334   // Send to shared function
00335   Q_EMIT(previewSelected(selected_vector));
00336 }
00337 
00338 }  // namespace moveit_setup_assistant


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Mon Jul 24 2017 02:22:29