00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "FunctionParametersDialog.h"
00025 #include "CustomDelegate.h"
00026 #include <QtGui>
00027
00028 #include <FunctionParametersDialog.moc>
00029
00030 namespace Aseba
00031 {
00034
00035 FunctionParametersDialog::FunctionParametersDialog(const QString &name, const std::vector<unsigned> ¶meters)
00036 {
00037 QVBoxLayout *mainLayout = new QVBoxLayout;
00038
00039
00040 QHBoxLayout *titleLayout = new QHBoxLayout;
00041 titleLayout->addWidget(new QLabel(QString("<b>%0</b>").arg(name)));
00042 titleLayout->addStretch();
00043
00044 addParameterButton = new QPushButton(QIcon(":/images/add.png"), "");
00045 addParameterButton->setMinimumSize(20, 20);
00046 titleLayout->addWidget(addParameterButton);
00047
00048 delParameterButton = new QPushButton(QIcon(":/images/remove.png"), "");
00049 delParameterButton->setMinimumSize(20, 20);
00050 delParameterButton->setDisabled(true);
00051 titleLayout->addWidget(delParameterButton);
00052
00053
00054 parametersTable = new QTableWidget(0, 3);
00055 parametersTable->setShowGrid(false);
00056 parametersTable->verticalHeader()->hide();
00057 parametersTable->horizontalHeader()->hide();
00058
00059 parametersTable->setSelectionMode(QAbstractItemView::SingleSelection);
00060 parametersTable->setSelectionBehavior(QAbstractItemView::SelectRows);
00061 parametersTable->setItemDelegateForColumn(0, &spinBoxDelegate);
00062
00063
00064 parametersTable->resizeColumnToContents(1);
00065 parametersTable->resizeColumnToContents(2);
00066 for (size_t i = 0; i < parameters.size(); i++)
00067 addEntry(parameters[i]);
00068 parametersTable->resizeRowsToContents();
00069
00070
00071 QPushButton *okButton = new QPushButton(tr("OK"));
00072 okButton->setDefault(true);
00073
00074
00075 connect(addParameterButton, SIGNAL(clicked()), SLOT(addParameter()));
00076 connect(delParameterButton, SIGNAL(clicked()), SLOT(deleteParameter()));
00077 connect(parametersTable, SIGNAL(itemSelectionChanged()), SLOT(argumentSelectionChanged()));
00078 connect(parametersTable, SIGNAL(cellClicked ( int , int )), SLOT(cellClicked ( int , int )));
00079 connect(okButton, SIGNAL(clicked()), SLOT(accept()));
00080
00081
00082 mainLayout->addLayout(titleLayout);
00083 mainLayout->addWidget(parametersTable);
00084 mainLayout->addWidget(okButton);
00085
00086
00087 setLayout(mainLayout);
00088 resize(240, 240);
00089 setWindowTitle(tr("Native function"));
00090 }
00091
00092 std::vector<unsigned> FunctionParametersDialog::getParameters() const
00093 {
00094 std::vector<unsigned> parameters;
00095 for (int i = 0; i < parametersTable->rowCount(); i++)
00096 parameters.push_back(parametersTable->item(i, 0)->data(Qt::DisplayRole).toUInt());
00097 return parameters;
00098 }
00099
00100 void FunctionParametersDialog::addParameter()
00101 {
00102 addEntry(1);
00103 }
00104
00105 void FunctionParametersDialog::deleteParameter()
00106 {
00107 parametersTable->removeRow(parametersTable->currentRow());
00108 }
00109
00110 void FunctionParametersDialog::argumentSelectionChanged()
00111 {
00112 delParameterButton->setEnabled(!parametersTable->selectedItems().isEmpty());
00113 }
00114
00115 void FunctionParametersDialog::cellClicked(int row, int column)
00116 {
00117 if (column == 1)
00118 {
00119 if (row > 0)
00120 {
00121 QString text = parametersTable->item(row, 0)->text();
00122 parametersTable->item(row, 0)->setText(parametersTable->item(row - 1, 0)->text());
00123 parametersTable->item(row - 1, 0)->setText(text);
00124 parametersTable->setCurrentCell(row - 1, 0);
00125 }
00126 }
00127 else if (column == 2)
00128 {
00129 if (row + 1 < parametersTable->rowCount())
00130 {
00131 QString text = parametersTable->item(row, 0)->text();
00132 parametersTable->item(row, 0)->setText(parametersTable->item(row + 1, 0)->text());
00133 parametersTable->item(row + 1, 0)->setText(text);
00134 parametersTable->setCurrentCell(row + 1, 0);
00135 }
00136 }
00137 }
00138
00139 void FunctionParametersDialog::addEntry(int value)
00140 {
00141 int rowCount = parametersTable->rowCount();
00142 parametersTable->insertRow(rowCount);
00143 QTableWidgetItem *item;
00144
00145 item = new QTableWidgetItem(QString("%0").arg(value));
00146 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
00147 parametersTable->setItem(rowCount, 0, item);
00148
00149 item = new QTableWidgetItem(QIcon(":/images/up.png"), "");
00150 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00151 parametersTable->setItem(rowCount, 1, item);
00152
00153 item = new QTableWidgetItem(QIcon(":/images/down.png"), "");
00154 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
00155 parametersTable->setItem(rowCount, 2, item);
00156
00157 parametersTable->resizeColumnToContents(1);
00158 parametersTable->resizeColumnToContents(2);
00159 }
00160
00162 }
00163