FunctionParametersDialog.cpp
Go to the documentation of this file.
00001 /*
00002         Aseba - an event-based framework for distributed robot control
00003         Copyright (C) 2007--2012:
00004                 Stephane Magnenat <stephane at magnenat dot net>
00005                 (http://stephane.magnenat.net)
00006                 and other contributors, see authors.txt for details
00007         
00008         This program is free software: you can redistribute it and/or modify
00009         it under the terms of the GNU Lesser General Public License as published
00010         by the Free Software Foundation, version 3 of the License.
00011         
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU Lesser General Public License for more details.
00016         
00017         You should have received a copy of the GNU Lesser General Public License
00018         along with this program. If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "FunctionParametersDialog.h"
00022 #include "CustomDelegate.h"
00023 #include <QtGui>
00024 
00025 #include <FunctionParametersDialog.moc>
00026 
00027 namespace Aseba
00028 {
00031         
00032         FunctionParametersDialog::FunctionParametersDialog(const QString &name, const std::vector<unsigned> &parameters)
00033         {
00034                 QVBoxLayout *mainLayout = new QVBoxLayout;
00035                 
00036                 // title
00037                 QHBoxLayout *titleLayout = new QHBoxLayout;
00038                 titleLayout->addWidget(new QLabel(QString("<b>%0</b>").arg(name)));
00039                 titleLayout->addStretch();
00040                 
00041                 addParameterButton = new QPushButton(QIcon(":/images/add.png"), "");
00042                 addParameterButton->setMinimumSize(20, 20);
00043                 titleLayout->addWidget(addParameterButton);
00044                 
00045                 delParameterButton = new QPushButton(QIcon(":/images/remove.png"), "");
00046                 delParameterButton->setMinimumSize(20, 20);
00047                 delParameterButton->setDisabled(true);
00048                 titleLayout->addWidget(delParameterButton);
00049                 
00050                 // parameter table
00051                 parametersTable = new QTableWidget(0, 3);
00052                 parametersTable->setShowGrid(false);
00053                 parametersTable->verticalHeader()->hide();
00054                 parametersTable->horizontalHeader()->hide();
00055                 //parametersTable->setSelectionMode(QAbstractItemView::NoSelection);
00056                 parametersTable->setSelectionMode(QAbstractItemView::SingleSelection);
00057                 parametersTable->setSelectionBehavior(QAbstractItemView::SelectRows);
00058                 parametersTable->setItemDelegateForColumn(0, &spinBoxDelegate);
00059                 
00060                 // fill table
00061                 parametersTable->resizeColumnToContents(1);
00062                 parametersTable->resizeColumnToContents(2);
00063                 for (size_t i = 0; i < parameters.size(); i++)
00064                         addEntry(parameters[i]);
00065                 parametersTable->resizeRowsToContents();
00066                 
00067                 // ok button
00068                 QPushButton *okButton = new QPushButton(tr("OK"));
00069                 okButton->setDefault(true);
00070                 
00071                 // connections
00072                 connect(addParameterButton, SIGNAL(clicked()), SLOT(addParameter()));
00073                 connect(delParameterButton, SIGNAL(clicked()), SLOT(deleteParameter()));
00074                 connect(parametersTable, SIGNAL(itemSelectionChanged()), SLOT(argumentSelectionChanged()));
00075                 connect(parametersTable, SIGNAL(cellClicked ( int , int )), SLOT(cellClicked ( int , int )));
00076                 connect(okButton, SIGNAL(clicked()), SLOT(accept()));
00077                 
00078                 // layout
00079                 mainLayout->addLayout(titleLayout);
00080                 mainLayout->addWidget(parametersTable);
00081                 mainLayout->addWidget(okButton);
00082                 
00083                 // main
00084                 setLayout(mainLayout);
00085                 resize(240, 240);
00086                 setWindowTitle(tr("Native function"));
00087         }
00088         
00089         std::vector<unsigned> FunctionParametersDialog::getParameters() const
00090         {
00091                 std::vector<unsigned> parameters;
00092                 for (int i = 0; i < parametersTable->rowCount(); i++)
00093                         parameters.push_back(parametersTable->item(i, 0)->data(Qt::DisplayRole).toUInt());
00094                 return parameters;
00095         }
00096         
00097         void FunctionParametersDialog::addParameter()
00098         {
00099                 addEntry(1);
00100         }
00101         
00102         void FunctionParametersDialog::deleteParameter()
00103         {
00104                 parametersTable->removeRow(parametersTable->currentRow());
00105         }
00106         
00107         void FunctionParametersDialog::argumentSelectionChanged()
00108         {
00109                 delParameterButton->setEnabled(!parametersTable->selectedItems().isEmpty());
00110         }
00111         
00112         void FunctionParametersDialog::cellClicked(int row, int column)
00113         {
00114                 if (column == 1)
00115                 {
00116                         if (row > 0)
00117                         {
00118                                 QString text = parametersTable->item(row, 0)->text();
00119                                 parametersTable->item(row, 0)->setText(parametersTable->item(row - 1, 0)->text());
00120                                 parametersTable->item(row - 1, 0)->setText(text);
00121                                 parametersTable->setCurrentCell(row - 1, 0);
00122                         }
00123                 }
00124                 else if (column == 2)
00125                 {
00126                         if (row + 1 < parametersTable->rowCount())
00127                         {
00128                                 QString text = parametersTable->item(row, 0)->text();
00129                                 parametersTable->item(row, 0)->setText(parametersTable->item(row + 1, 0)->text());
00130                                 parametersTable->item(row + 1, 0)->setText(text);
00131                                 parametersTable->setCurrentCell(row + 1, 0);
00132                         }
00133                 }
00134         }
00135         
00136         void FunctionParametersDialog::addEntry(int value)
00137         {
00138                 int rowCount = parametersTable->rowCount();
00139                 parametersTable->insertRow(rowCount);
00140                 QTableWidgetItem *item;
00141                 
00142                 item = new QTableWidgetItem(QString("%0").arg(value));
00143                 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
00144                 parametersTable->setItem(rowCount, 0, item);
00145                 
00146                 item = new QTableWidgetItem(QIcon(":/images/up.png"), "");
00147                 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00148                 parametersTable->setItem(rowCount, 1, item);
00149                 
00150                 item = new QTableWidgetItem(QIcon(":/images/down.png"), "");
00151                 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00152                 parametersTable->setItem(rowCount, 2, item);
00153                 
00154                 parametersTable->resizeColumnToContents(1);
00155                 parametersTable->resizeColumnToContents(2);
00156         }
00157         
00159 }
00160 


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:38