Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/paintings/id_dialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                                id_dialog.cpp
00003                               ---------------
00004     begin                : Sat Oct 16 2004
00005     copyright            : (C) 2004 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include "id_dialog.h"
00018 #include "id_text.h"
00019 
00020 #include <QHeaderView>
00021 #include <QHBoxLayout>
00022 #include <QVBoxLayout>
00023 #include <QGridLayout>
00024 #include <QLabel>
00025 #include <QTableWidget>
00026 #include <QTableWidgetItem>
00027 #include <QCheckBox>
00028 #include <QLineEdit>
00029 #include <QGroupBox>
00030 #include <QValidator>
00031 #include <QPushButton>
00032 #include <QMessageBox>
00033 
00034 
00035 ID_Dialog::ID_Dialog(ID_Text *idText_)
00036 {
00037   idText = idText_;
00038   setWindowTitle(tr("Edit Subcircuit Properties"));
00039 
00040   all = new QVBoxLayout;
00041   all->setSpacing(5);
00042   all->setMargin(5);
00043 
00044   QHBoxLayout *htop = new QHBoxLayout;
00045   htop->setSpacing(5);
00046   all->addLayout(htop);
00047 
00048   Expr.setPattern("[A-Za-z][A-Za-z0-9_]*");
00049   SubVal = new QRegExpValidator(Expr, this);
00050   Prefix = new QLineEdit(idText->Prefix);
00051   Prefix->setValidator(SubVal);
00052 
00053   htop->addWidget(new QLabel(tr("Prefix:")));
00054   htop->addWidget(Prefix);
00055 
00056   QGroupBox *ParamBox = new QGroupBox(tr("Parameters"));
00057   all->addWidget(ParamBox);
00058   QVBoxLayout *vbox_param = new QVBoxLayout;
00059   ParamBox->setLayout(vbox_param);
00060 
00061   ParamTable = new QTableWidget();
00062   ParamTable->horizontalHeader()->setStretchLastSection(true);
00063   // set automatic resize so all content will be visible, 
00064   //  horizontal scrollbar will appear if table becomes too large
00065   ParamTable->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
00066   ParamTable->horizontalHeader()->setClickable(false); // no action when clicking on the header 
00067   ParamTable->verticalHeader()->hide();
00068   ParamTable->setColumnCount(5);
00069   ParamTable->setHorizontalHeaderLabels(
00070       QStringList() << tr("display") << tr("Name") << tr("Default") << tr("Description") << tr("Type"));
00071   ParamTable->setSortingEnabled(false); // no sorting
00072   ParamTable->setSelectionBehavior(QAbstractItemView::SelectRows);
00073   vbox_param->addWidget(ParamTable);
00074 
00075   QTableWidgetItem *item;
00076   QList<SubParameter *>::const_iterator it;
00077   for(it = idText->Parameter.constBegin(); it != idText->Parameter.constEnd(); it++) {
00078     int row = ParamTable->rowCount();
00079     ParamTable->insertRow(row);
00080     item = new QTableWidgetItem(((*it)->display)? tr("yes") : tr("no"));
00081     item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00082     ParamTable->setItem(row, 0, item);
00083     item = new QTableWidgetItem((*it)->Name.section('=', 0, 0));
00084     item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00085     ParamTable->setItem(row, 1, item);
00086     item = new QTableWidgetItem((*it)->Name.section('=', 1, 1));
00087     item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00088     ParamTable->setItem(row, 2, item);
00089     item = new QTableWidgetItem((*it)->Description);
00090     item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00091     ParamTable->setItem(row, 3, item);
00092     item = new QTableWidgetItem((*it)->Type);
00093     item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00094     ParamTable->setItem(row, 4, item);
00095   }
00096   connect(ParamTable, SIGNAL(currentCellChanged(int, int, int, int)), SLOT(slotEditParameter()));
00097 
00098   showCheck = new QCheckBox(tr("display in schematic"));
00099   showCheck->setChecked(true);
00100 
00101   vbox_param->addWidget(showCheck);
00102 
00103   QGridLayout *paramEditLayout = new QGridLayout;
00104   vbox_param->addLayout(paramEditLayout);
00105 
00106   paramEditLayout->addWidget(new QLabel(tr("Name:")), 0, 0);
00107   paramEditLayout->addWidget(new QLabel(tr("Default Value:")), 1, 0);
00108   paramEditLayout->addWidget(new QLabel(tr("Description:")), 2, 0);
00109   paramEditLayout->addWidget(new QLabel(tr("Type:")), 3, 0);
00110 
00111   Expr.setPattern("[\\w_]+");
00112   NameVal = new QRegExpValidator(Expr, this);
00113   ParamNameEdit = new QLineEdit;
00114   ParamNameEdit->setValidator(NameVal);
00115 
00116   Expr.setPattern("[^\"=]*");
00117   ValueVal = new QRegExpValidator(Expr, this);
00118   ValueEdit = new QLineEdit;
00119   ValueEdit->setValidator(ValueVal);
00120 
00121   Expr.setPattern("[^\"=\\x005B\\x005D]*");
00122   DescrVal = new QRegExpValidator(Expr, this);
00123   DescriptionEdit = new QLineEdit;
00124   DescriptionEdit->setValidator(DescrVal);
00125 
00126   Expr.setPattern("[\\w_]+");
00127   TypeVal = new QRegExpValidator(Expr, this);
00128   TypeEdit = new QLineEdit;
00129   TypeEdit->setValidator(TypeVal);
00130 
00131   paramEditLayout->addWidget(ParamNameEdit, 0, 1);
00132   paramEditLayout->addWidget(ValueEdit, 1, 1);
00133   paramEditLayout->addWidget(DescriptionEdit, 2, 1);
00134   paramEditLayout->addWidget(TypeEdit, 3, 1);
00135 
00136   QPushButton *ButtAdd = new QPushButton(tr("Add"));
00137   connect(ButtAdd, SIGNAL(clicked()), SLOT(slotAddParameter()));
00138   QPushButton *ButtRemove = new QPushButton(tr("Remove"));
00139   connect(ButtRemove, SIGNAL(clicked()), SLOT(slotRemoveParameter()));
00140 
00141   QHBoxLayout *hbox_paramedit = new QHBoxLayout;
00142   vbox_param->addLayout(hbox_paramedit);
00143   hbox_paramedit->addStretch();
00144   hbox_paramedit->addWidget(ButtAdd);
00145   hbox_paramedit->addWidget(ButtRemove);
00146 
00147   QPushButton *ButtOK = new QPushButton(tr("OK"));
00148   connect(ButtOK, SIGNAL(clicked()), SLOT(slotOk()));
00149   QPushButton *ButtApply = new QPushButton(tr("Apply"));
00150   connect(ButtApply, SIGNAL(clicked()), SLOT(slotApply()));
00151   QPushButton *ButtCancel = new QPushButton(tr("Cancel"));
00152   connect(ButtCancel, SIGNAL(clicked()), SLOT(reject()));
00153 
00154   QHBoxLayout *hbox_bottom = new QHBoxLayout;
00155   hbox_bottom->setSpacing(5);
00156   all->addLayout(hbox_bottom);
00157   hbox_bottom->addWidget(ButtOK);
00158   hbox_bottom->addWidget(ButtApply);
00159   hbox_bottom->addWidget(ButtCancel);
00160 
00161   this->setLayout(all);
00162 }
00163 
00164 ID_Dialog::~ID_Dialog()
00165 {
00166   delete all;
00167   delete SubVal;
00168   delete NameVal;
00169   delete ValueVal;
00170   delete DescrVal;
00171   delete TypeVal;
00172 }
00173 
00174 
00179 void ID_Dialog::slotEditParameter()
00180 {
00181   int row = ParamTable->currentRow();
00182   if (row < 0 || row >= ParamTable->rowCount()) {
00183     return;
00184   }
00185 
00186   showCheck->setChecked(ParamTable->item(row, 0)->text() == tr("yes"));
00187   ParamNameEdit->setText(ParamTable->item(row, 1)->text());
00188   ValueEdit->setText(ParamTable->item(row, 2)->text());
00189   DescriptionEdit->setText(ParamTable->item(row, 3)->text());
00190   TypeEdit->setText(ParamTable->item(row, 4)->text());
00191 }
00192 
00193 
00199 void ID_Dialog::slotAddParameter()
00200 {
00201   if(ParamNameEdit->text().isEmpty())
00202     return;
00203 
00204   if(ParamNameEdit->text() == "File") {
00205     QMessageBox::critical(this, tr("Error"),
00206        tr("Parameter must not be named \"File\"!"));
00207     return;
00208   }
00209   
00210   int row;
00211   for (row = 0; row < ParamTable->rowCount(); ++row) {
00212     if(ParamTable->item(row, 1)->text() == ParamNameEdit->text()) {
00213       QMessageBox::critical(this, tr("Error"),
00214          tr("Parameter \"%1\" already in list!").arg(ParamNameEdit->text()));
00215       return;
00216     }
00217   }
00218 
00219   row = ParamTable->rowCount();
00220   ParamTable->insertRow(row);
00221 
00222   QTableWidgetItem *item;
00223   item = new QTableWidgetItem((showCheck->isChecked())? tr("yes") : tr("no"));
00224   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00225   ParamTable->setItem(row, 0, item);
00226   item = new QTableWidgetItem(ParamNameEdit->text());
00227   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00228   ParamTable->setItem(row, 1, item);
00229   item = new QTableWidgetItem(ValueEdit->text());
00230   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00231   ParamTable->setItem(row, 2, item);
00232   item = new QTableWidgetItem(DescriptionEdit->text());
00233   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00234   ParamTable->setItem(row, 3, item);
00235   item = new QTableWidgetItem(TypeEdit->text());
00236   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00237   ParamTable->setItem(row, 4, item);
00238 
00239   ParamTable->setCurrentCell(row, 0);
00240 }
00241 
00242 
00247 void ID_Dialog::slotRemoveParameter()
00248 {
00249   int selectedrow = ParamTable->currentRow();
00250   ParamTable->removeRow(selectedrow);
00251   int nextRow = (selectedrow == ParamTable->rowCount())? selectedrow-1 : selectedrow;
00252   ParamTable->setCurrentCell(nextRow, 0);
00253 }
00254 
00255 
00260 void ID_Dialog::slotOk()
00261 {
00262   bool changed = false;
00263 
00264   if(!Prefix->text().isEmpty())
00265     if(idText->Prefix != Prefix->text()) {
00266       idText->Prefix = Prefix->text();
00267       changed = true;
00268     }
00269 
00270   QList<SubParameter *> scratch;
00271   for (int row = 0; row < ParamTable->rowCount(); ++row) {
00272       bool display = ParamTable->item(row, 0)->text() == tr("yes");
00273       // s = "name=defaultval"
00274       QString s(ParamTable->item(row, 1)->text() + "=" + ParamTable->item(row, 2)->text()),
00275               desc(ParamTable->item(row, 3)->text()),
00276               type(ParamTable->item(row, 4)->text());
00277 
00278       scratch.append(new SubParameter(display, s, desc, type));
00279   }
00280 
00281   if (scratch.size()!=idText->Parameter.size()) {
00282       changed = true;
00283   } else {
00284       QList<SubParameter *>::const_iterator A(scratch.begin()),
00285                                             end(scratch.end()),
00286                                             B(idText->Parameter.begin());
00287       for(;A!=end; ++A, ++B) {
00288           if((*A)->display!=(*B)->display || (*A)->Name!=(*B)->Name ||
00289              (*A)->Description!=(*B)->Description || (*A)->Type!=(*B)->Type) {
00290               changed = true;
00291               break;
00292           }
00293       }
00294   }
00295 
00296   if(changed)
00297       idText->Parameter.swap(scratch);
00298 
00299   foreach(SubParameter *p, scratch) {
00300       delete p;
00301   }
00302 
00303   if(changed)  accept();
00304   else  reject();
00305 }
00306 
00307 
00312 void ID_Dialog::slotApply()
00313 {
00314   int selectedrow = ParamTable->currentRow();
00315   if (selectedrow<0) return; // Nothing selected
00316 
00317   QTableWidgetItem *item;
00318   item = ParamTable->item(selectedrow, 0);
00319   item->setText(showCheck->isChecked() ? tr("yes") : tr("no"));
00320   item = ParamTable->item(selectedrow, 1);
00321   item->setText(ParamNameEdit->text());
00322   item = ParamTable->item(selectedrow, 2);
00323   item->setText(ValueEdit->text());
00324   item = ParamTable->item(selectedrow, 3);
00325   item->setText(DescriptionEdit->text());
00326   item = ParamTable->item(selectedrow, 4);
00327   item->setText(TypeEdit->text());
00328 
00329   ParamTable->setCurrentCell(selectedrow,0);
00330   ParamNameEdit->clear();
00331   ValueEdit->clear();
00332   DescriptionEdit->clear();
00333   TypeEdit->clear();
00334 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines