Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/components/optimizedialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                             optimizedialog.cpp
00003                            --------------------
00004     begin                : Sun Jul 30 2006
00005     copyright            : (C) 2006 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 
00018 #ifdef HAVE_CONFIG_H
00019 # include <config.h>
00020 #endif
00021 
00022 #include "main.h"
00023 #include "qucs.h"
00024 #include "optimizedialog.h"
00025 #include "opt_sim.h"
00026 #include "schematic.h"
00027 
00028 #include <QLabel>
00029 #include <QCheckBox>
00030 #include <QLineEdit>
00031 #include <QComboBox>
00032 #include <QTableWidget>
00033 #include <QTabWidget>
00034 #include <QValidator>
00035 #include <QPushButton>
00036 #include <QMessageBox>
00037 #include <QGridLayout>
00038 #include <QVBoxLayout>
00039 #include <QHBoxLayout>
00040 #include <QHeaderView>
00041 #include <QApplication>
00042 #include <QClipboard>
00043 #include <QAction>
00044 #include <QMenu>
00045 #include <QInputDialog>
00046 
00047 
00048 OptimizeDialog::OptimizeDialog(Optimize_Sim *c_, Schematic *d_)
00049       : QDialog(d_)
00050 {
00051   Comp = c_;
00052   Doc  = d_;
00053   changed = false;
00054   numPrec = 3;
00055   setWindowTitle(tr("Edit Optimization Properties"));
00056 
00057   Expr.setPattern("[\\w_]+");
00058   Validator = new QRegExpValidator(Expr, this);
00059   numVal = new QDoubleValidator(this);
00060   intVal = new QIntValidator(this);
00061 
00062   all = new QVBoxLayout(); // to provide the neccessary size
00063   QTabWidget *t = new QTabWidget();
00064   all->addWidget(t);
00065   this->setLayout(all);
00066 
00067   // ...........................................................
00068   QWidget *Tab1 = new QWidget(t);
00069   QGridLayout *gp1 = new QGridLayout();
00070   Tab1->setLayout(gp1);
00071 
00072   NameEdit = new QLineEdit();
00073   NameEdit->setValidator(Validator);
00074   SimEdit = new QComboBox(Tab1);
00075   SimEdit->setEditable(true);
00076 
00077   gp1->addWidget(new QLabel(tr("Name:"), Tab1), 0, 0);
00078   gp1->addWidget(NameEdit, 0, 1);
00079   gp1->addWidget(new QLabel(tr("Simulation:"), Tab1), 1, 0);
00080   gp1->addWidget(SimEdit, 1, 1);
00081 
00082   setTabOrder(NameEdit, SimEdit);
00083 
00084   t->addTab(Tab1, tr("General"));
00085 
00086   // ...........................................................
00087   QWidget *Tab2 = new QWidget(t);
00088   QGridLayout *gp2 = new QGridLayout();
00089   Tab2->setLayout(gp2);
00090 
00091   MethodCombo = new QComboBox();
00092   MethodCombo->insertItems(-1, QString("DE/best/1/exp;"
00093                "DE/rand/1/exp;"
00094                "DE/rand-to-best/1/exp;"
00095                "DE/best/2/exp;"
00096                "DE/rand/1/exp;"
00097                "DE/best/1/bin;"
00098                "DE/rand/1/bin;"
00099                "DE/rand-to-best/1/bin;"
00100                "DE/best/2/bin;"
00101                "DE/rand/2/bin").split(";"));
00102 
00103   gp2->addWidget(new QLabel(tr("Method:")), 0,0);
00104   gp2->addWidget(MethodCombo,0,1);
00105 
00106   IterEdit = new QLineEdit();
00107   IterEdit->setValidator(intVal);
00108 
00109   gp2->addWidget(new QLabel(tr("Maximum number of iterations:")), 1,0);
00110   gp2->addWidget(IterEdit,1,1);
00111 
00112   RefreshEdit = new QLineEdit();
00113   RefreshEdit->setValidator(intVal);
00114 
00115   gp2->addWidget(new QLabel(tr("Output refresh cycle:")), 2,0);
00116   gp2->addWidget(RefreshEdit,2,1);
00117 
00118   ParentsEdit = new QLineEdit();
00119   ParentsEdit->setValidator(intVal);
00120 
00121   gp2->addWidget(new QLabel(tr("Number of parents:")), 3,0);
00122   gp2->addWidget(ParentsEdit,3,1);
00123 
00124   ConstEdit = new QLineEdit();
00125   ConstEdit->setValidator(new QDoubleValidator(0.0,2.0,20,ConstEdit));
00126 
00127   gp2->addWidget(new QLabel(tr("Constant F:")), 4,0);
00128   gp2->addWidget(ConstEdit,4,1);
00129 
00130   CrossEdit = new QLineEdit();
00131   CrossEdit->setValidator(new QDoubleValidator(0.0,1.0,20,CrossEdit));
00132 
00133   gp2->addWidget(new QLabel(tr("Crossing over factor:")), 5,0);
00134   gp2->addWidget(CrossEdit,5,1);
00135 
00136   SeedEdit = new QLineEdit();
00137   SeedEdit->setValidator(numVal);
00138 
00139   gp2->addWidget(new QLabel(tr("Pseudo random number seed:")), 6,0);
00140   gp2->addWidget(SeedEdit,6,1);
00141 
00142   CostVarEdit = new QLineEdit();
00143   CostVarEdit->setValidator(numVal);
00144 
00145   gp2->addWidget(new QLabel(tr("Minimum cost variance:")), 7,0);
00146   gp2->addWidget(CostVarEdit,7,1);
00147 
00148   CostObjEdit = new QLineEdit();
00149   CostObjEdit->setValidator(numVal);
00150 
00151   gp2->addWidget(new QLabel(tr("Cost objectives:")), 8,0);
00152   gp2->addWidget(CostObjEdit,8,1);
00153 
00154   CostConEdit = new QLineEdit();
00155   CostConEdit->setValidator(numVal);
00156 
00157   gp2->addWidget(new QLabel(tr("Cost constraints:")), 9,0);
00158   gp2->addWidget(CostConEdit,9,1);
00159 
00160   t->addTab(Tab2, tr("Algorithm"));
00161 
00162   // ...........................................................
00163   QWidget *Tab3 = new QWidget(t);
00164   QGridLayout *gp3 = new QGridLayout();
00165   Tab3->setLayout(gp3);
00166 
00167   VarTable = new QTableWidget();
00168   VarTable->horizontalHeader()->setStretchLastSection(true);
00169   VarTable->verticalHeader()->hide();
00170   VarTable->setColumnCount(6);
00171   VarTable->setHorizontalHeaderLabels(
00172       QStringList() << tr("Name") << tr("active") << tr("initial") << tr("min") << tr("max") << tr("Type"));
00173   VarTable->setSortingEnabled(false);
00174   VarTable->setSelectionBehavior(QAbstractItemView::SelectRows);
00175   VarTable->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
00176   VarTable->horizontalHeader()->setClickable(false); // no action when clicking on the header 
00177 
00178   // right-click on the table header to open the context menu
00179   VarTable->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
00180   connect(VarTable->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(slotSetPrecision(const QPoint&)));
00181 
00182   connect(VarTable, SIGNAL(currentCellChanged(int, int, int, int)),
00183       SLOT(slotEditVariable()));
00184   gp3->addWidget(VarTable, 0, 0, 1, 3);
00185 
00186   VarNameEdit = new QLineEdit();
00187   VarNameEdit->setValidator(Validator);
00188   connect(VarNameEdit, SIGNAL(textChanged(const QString&)),
00189           SLOT(slotChangeVarName(const QString&)));
00190   VarActiveCheck = new QCheckBox(tr("active"));
00191   VarActiveCheck->setChecked(true);
00192   connect(VarActiveCheck, SIGNAL(toggled(bool)),
00193           SLOT(slotChangeVarActive(bool)));
00194 
00195   QHBoxLayout *VarLine = new QHBoxLayout();
00196   VarLine->setSpacing(3);
00197   VarLine->addWidget(new QLabel(tr("Name:")));
00198   VarLine->addWidget(VarNameEdit);
00199   VarLine->addWidget(VarActiveCheck);
00200   gp3->addLayout(VarLine, 1, 0, 1, 3);
00201 
00202   VarInitEdit = new QLineEdit();
00203   VarInitEdit->setValidator(numVal);
00204   connect(VarInitEdit, SIGNAL(textChanged(const QString&)),
00205           SLOT(slotChangeVarInit(const QString&)));
00206   VarMinEdit = new QLineEdit();
00207   VarMinEdit->setValidator(numVal);
00208   connect(VarMinEdit, SIGNAL(textChanged(const QString&)),
00209           SLOT(slotChangeVarMin(const QString&)));
00210   VarMaxEdit = new QLineEdit(Tab3);
00211   VarMaxEdit->setValidator(numVal);
00212   connect(VarMaxEdit, SIGNAL(textChanged(const QString&)),
00213           SLOT(slotChangeVarMax(const QString&)));
00214 
00215   gp3->addWidget(new QLabel(tr("initial:")), 2,0);
00216   gp3->addWidget(new QLabel(tr("min:")), 2,1);
00217   gp3->addWidget(new QLabel(tr("max:")), 2,2);
00218   gp3->addWidget(VarInitEdit,3,0);
00219   gp3->addWidget(VarMinEdit,3,1);
00220   gp3->addWidget(VarMaxEdit,3,2);
00221 
00222   setTabOrder(VarActiveCheck, VarInitEdit);
00223   setTabOrder(VarInitEdit, VarMinEdit);
00224   setTabOrder(VarMinEdit, VarMaxEdit);
00225 
00226   VarTypeCombo = new QComboBox();
00227   VarTypeCombo->insertItem(0, tr("linear double"));
00228   VarTypeCombo->insertItem(1, tr("logarithmic double"));
00229   VarTypeCombo->insertItem(2, tr("linear integer"));
00230   VarTypeCombo->insertItem(3, tr("logarithmic integer"));
00231   connect(VarTypeCombo, SIGNAL(activated(const QString&)),
00232           SLOT(slotChangeVarType(const QString&)));
00233 
00234   QPushButton *AddVar_Butt = new QPushButton(tr("Add"));
00235   connect(AddVar_Butt, SIGNAL(clicked()), SLOT(slotAddVariable()));
00236   QPushButton *DelVar_Butt = new QPushButton(tr("Delete"));
00237   connect(DelVar_Butt, SIGNAL(clicked()), SLOT(slotDeleteVariable()));
00238 
00239   QHBoxLayout *VarButtons = new QHBoxLayout();
00240   VarButtons->setSpacing(3);
00241   VarButtons->addWidget(new QLabel(tr("Type:")));
00242   VarButtons->addWidget(VarTypeCombo);
00243   VarButtons->addStretch();
00244   VarButtons->addWidget(AddVar_Butt);
00245   VarButtons->addWidget(DelVar_Butt);
00246   gp3->addLayout(VarButtons, 4, 0, 1, 3);
00247 
00248   // add horizontal line
00249   QFrame *line = new QFrame(this);
00250   line->setFrameShape(QFrame::HLine);
00251   line->setFrameShadow(QFrame::Sunken); 
00252   gp3->addWidget(line, 5, 0, 1, -1); // fill the entire width
00253   QPushButton *CreateEqn_Butt = new QPushButton(tr("Copy current values to equation"));
00254   connect(CreateEqn_Butt, SIGNAL(clicked()), SLOT(slotCreateEqn()));
00255   gp3->addWidget(CreateEqn_Butt,6,0);
00256 
00257   t->addTab(Tab3, tr("Variables"));
00258 
00259   // ...........................................................
00260   QWidget *Tab4 = new QWidget(t);
00261   QGridLayout *gp4 = new QGridLayout();
00262   Tab4->setLayout(gp4);
00263 
00264   GoalTable = new QTableWidget();
00265   GoalTable->horizontalHeader()->setStretchLastSection(true);
00266   GoalTable->verticalHeader()->hide();
00267   GoalTable->setColumnCount(3);
00268   GoalTable->setHorizontalHeaderLabels(
00269       QStringList() << tr("Name") << tr("Type") << tr("Value"));
00270   GoalTable->setSortingEnabled(false);
00271   GoalTable->setSelectionBehavior(QAbstractItemView::SelectRows);
00272   connect(GoalTable, SIGNAL(currentCellChanged(int, int, int, int)),
00273                     SLOT(slotEditGoal()));
00274   gp4->addWidget(GoalTable, 0, 0, 1, 3);
00275 
00276   GoalNameEdit = new QLineEdit();
00277   GoalNameEdit->setValidator(Validator);
00278   connect(GoalNameEdit, SIGNAL(textChanged(const QString&)),
00279           SLOT(slotChangeGoalName(const QString&)));
00280 
00281   gp4->addWidget(new QLabel(tr("Name:")), 1,0);
00282   gp4->addWidget(GoalNameEdit,1,1);
00283 
00284   GoalNumEdit = new QLineEdit();
00285   GoalNumEdit->setValidator(numVal);
00286   connect(GoalNumEdit, SIGNAL(textChanged(const QString&)),
00287           SLOT(slotChangeGoalNum(const QString&)));
00288 
00289   gp4->addWidget(new QLabel(tr("Value:")), 2,0);
00290   gp4->addWidget(GoalNumEdit,2,1);
00291 
00292   GoalTypeCombo = new QComboBox();
00293   GoalTypeCombo->insertItem(0, tr("minimize"));
00294   GoalTypeCombo->insertItem(1, tr("maximize"));
00295   GoalTypeCombo->insertItem(2, tr("less"));
00296   GoalTypeCombo->insertItem(3, tr("greater"));
00297   GoalTypeCombo->insertItem(4, tr("equal"));
00298   GoalTypeCombo->insertItem(5, tr("monitor"));
00299   connect(GoalTypeCombo, SIGNAL(activated(const QString&)),
00300           SLOT(slotChangeGoalType(const QString&)));
00301   gp4->addWidget(GoalTypeCombo,2,2);
00302 
00303   QPushButton *AddGoal_Butt = new QPushButton(tr("Add"));
00304   connect(AddGoal_Butt, SIGNAL(clicked()), SLOT(slotAddGoal()));
00305   QPushButton *DelGoal_Butt = new QPushButton(tr("Delete"));
00306   connect(DelGoal_Butt, SIGNAL(clicked()), SLOT(slotDeleteGoal()));
00307 
00308   QHBoxLayout *GoalButtons = new QHBoxLayout();
00309   GoalButtons->setSpacing(3);
00310   GoalButtons->addStretch();
00311   GoalButtons->addWidget(AddGoal_Butt);
00312   GoalButtons->addWidget(DelGoal_Butt);
00313   gp4->addLayout(GoalButtons, 3, 0, 1, 3);
00314 
00315   t->addTab(Tab4, tr("Goals"));
00316 
00317   // ...........................................................
00318   // buttons on the bottom of the dialog (independent of the TabWidget)
00319   QHBoxLayout *Butts = new QHBoxLayout();
00320   Butts->setSpacing(3);
00321   Butts->setMargin(3);
00322 
00323   QPushButton *OkButt = new QPushButton(tr("OK"));
00324   connect(OkButt, SIGNAL(clicked()), SLOT(slotOK()));
00325   QPushButton *ApplyButt = new QPushButton(tr("Apply"));
00326   connect(ApplyButt, SIGNAL(clicked()), SLOT(slotApply()));
00327   QPushButton *CancelButt = new QPushButton(tr("Cancel"));
00328   connect(CancelButt, SIGNAL(clicked()), SLOT(slotCancel()));
00329 
00330   Butts->addStretch();
00331   Butts->addWidget(OkButt);
00332   Butts->addWidget(ApplyButt);
00333   Butts->addWidget(CancelButt);
00334   all->addLayout(Butts);
00335 
00336   OkButt->setFocus();
00337 
00338   // ...........................................................
00339 
00340   Component *pc;
00341   for(pc=Doc->Components->first(); pc!=0; pc=Doc->Components->next())
00342     if(pc != Comp)
00343       if(pc->Model[0] == '.' && pc->Model != ".Opt")
00344         SimEdit->insertItem(SimEdit->count(), pc->Name);
00345 
00346   Property *pp;
00347   pp = Comp->Props.at(0);
00348   if(!pp->Value.isEmpty()) {
00349     // set selected simulation in combo box to the currently used one
00350     int i = SimEdit->findText(pp->Value);
00351     if (i != -1) // current simulation is in the available simulations list (normal case)
00352       SimEdit->setCurrentIndex(i);
00353     else  // current simulation not in the available simulations list
00354       SimEdit->setEditText(pp->Value);
00355   }
00356 
00357   pp = Comp->Props.at(1);
00358   if(!pp->Value.isEmpty()) {
00359     MethodCombo->setCurrentIndex(pp->Value.section('|',0,0).toInt()-1);
00360     IterEdit->setText(pp->Value.section('|',1,1));
00361     RefreshEdit->setText(pp->Value.section('|',2,2));
00362     ParentsEdit->setText(pp->Value.section('|',3,3));
00363     ConstEdit->setText(pp->Value.section('|',4,4));
00364     CrossEdit->setText(pp->Value.section('|',5,5));
00365     SeedEdit->setText(pp->Value.section('|',6,6));
00366     CostVarEdit->setText(pp->Value.section('|',7,7));
00367     CostObjEdit->setText(pp->Value.section('|',8,8));
00368     CostConEdit->setText(pp->Value.section('|',9,9));
00369   }
00370 
00371   NameEdit->setText(Comp->Name);
00372 
00373   QTableWidgetItem *item;
00374   for(pp = Comp->Props.at(2); pp != 0; pp = Comp->Props.next()) {
00375     if(pp->Name == "Var") {
00376       QStringList ValueSplit = pp->Value.split("|");
00377       int row = VarTable->rowCount();
00378       VarTable->insertRow(row);
00379       // Name
00380       item = new QTableWidgetItem(ValueSplit.at(0));
00381       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00382       VarTable->setItem(row, 0, item);
00383       // active
00384       item = new QTableWidgetItem((ValueSplit.at(1) == "yes")? tr("yes") : tr("no"));
00385       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00386       VarTable->setItem(row, 1, item);
00387       // initial
00388       item = new QTableWidgetItem(QString::number(ValueSplit.at(2).toDouble(), 'g', numPrec));
00389       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00390       VarTable->setItem(row, 2, item);
00391       // min
00392       item = new QTableWidgetItem(ValueSplit.at(3));
00393       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00394       VarTable->setItem(row, 3, item);
00395       // max
00396       item = new QTableWidgetItem(ValueSplit.at(4));
00397       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00398       VarTable->setItem(row, 4, new QTableWidgetItem(ValueSplit.at(4)));
00399       // Type
00400       QString typeStr;
00401       if (ValueSplit.at(5) == "LIN_DOUBLE") {
00402         typeStr = tr("linear double");
00403       } else if (ValueSplit.at(5) == "LOG_DOUBLE") {
00404         typeStr = tr("logarithmic double");
00405       } else if (ValueSplit.at(5) == "LIN_INT") {
00406         typeStr = tr("linear integer");
00407       } else {
00408         typeStr = tr("logarithmic integer");
00409       }
00410       item = new QTableWidgetItem(typeStr);
00411       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00412       VarTable->setItem(row, 5, item);
00413     }
00414     if(pp->Name == "Goal") {
00415       QStringList GoalSplit = pp->Value.split("|");
00416       int row = GoalTable->rowCount();
00417       GoalTable->insertRow(row);
00418 
00419       item = new QTableWidgetItem(GoalSplit.at(0));
00420       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00421       GoalTable->setItem(row, 0, item);
00422       QString typeStr;
00423       if (GoalSplit.at(1) == "MIN") {
00424         typeStr = tr("minimize");
00425       } else if (GoalSplit.at(1) == "MAX") {
00426         typeStr = tr("maximize");
00427       } else if (GoalSplit.at(1) == "LE") {
00428         typeStr = tr("less");
00429       } else if (GoalSplit.at(1) == "GE") {
00430         typeStr = tr("greater");
00431       } else if (GoalSplit.at(1) == "EQ") {
00432         typeStr = tr("equal");
00433       } else {
00434         typeStr = tr("monitor");
00435       }
00436       item = new QTableWidgetItem(typeStr);
00437       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00438       GoalTable->setItem(row, 1, item);
00439       item = new QTableWidgetItem(GoalSplit.at(2));
00440       item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00441       GoalTable->setItem(row, 2, item);
00442     }
00443   }
00444 
00445   resize(300, 250);
00446 }
00447 
00448 OptimizeDialog::~OptimizeDialog()
00449 {
00450   delete all;
00451   delete numVal;
00452   delete intVal;
00453   delete Validator;
00454 }
00455 
00456 // -----------------------------------------------------------
00457 void OptimizeDialog::slotEditVariable()
00458 {
00459   VarNameEdit->blockSignals(true);
00460 
00461   int row = VarTable->currentRow();
00462   if (row < 0 || row >= VarTable->rowCount()) {
00463     VarNameEdit->clear();
00464     VarActiveCheck->setChecked(true);
00465     VarInitEdit->clear();
00466     VarMinEdit->clear();
00467     VarMaxEdit->clear();
00468     VarTypeCombo->setCurrentIndex(0);
00469 
00470     VarNameEdit->blockSignals(false);
00471     return;
00472   }
00473 
00474   VarNameEdit->setText(VarTable->item(row, 0)->text());
00475   VarActiveCheck->setChecked(VarTable->item(row, 1)->text() == tr("yes"));
00476   VarInitEdit->setText(VarTable->item(row, 2)->text());
00477   VarMinEdit->setText(VarTable->item(row, 3)->text());
00478   VarMaxEdit->setText(VarTable->item(row, 4)->text());
00479   VarTypeCombo->setCurrentIndex(VarTypeCombo->findText(VarTable->item(row, 5)->text()));
00480 
00481   VarNameEdit->blockSignals(false);
00482 }
00483 
00484 // -----------------------------------------------------------
00485 void OptimizeDialog::slotAddVariable()
00486 {
00487   if(VarNameEdit->text().isEmpty() || VarInitEdit->text().isEmpty() ||
00488       VarMinEdit->text().isEmpty() || VarMaxEdit->text().isEmpty()) {
00489     QMessageBox::critical(this, tr("Error"),
00490        tr("Every text field must be non-empty!"));
00491     return;
00492   }
00493 
00494   int row;
00495   for (row = 0; row < VarTable->rowCount(); ++row) {
00496     if (VarNameEdit->text() == VarTable->item(row, 0)->text()) {
00497       QMessageBox::critical(this, tr("Error"), 
00498           tr("Variable \"%1\" aleardy in list!").arg(VarNameEdit->text()));
00499       return;
00500     }
00501   }
00502 
00503   QTableWidgetItem *item;
00504   row = VarTable->rowCount();
00505   VarTable->insertRow(row);
00506   item = new QTableWidgetItem(VarNameEdit->text());
00507   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00508   VarTable->setItem(row, 0, item);
00509   item = new QTableWidgetItem(VarActiveCheck->isChecked() ? tr("yes") : tr("no"));
00510   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00511   VarTable->setItem(row, 1, item);
00512   item = new QTableWidgetItem(VarInitEdit->text());
00513   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00514   VarTable->setItem(row, 2, item);
00515   item = new QTableWidgetItem(VarMinEdit->text());
00516   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00517   VarTable->setItem(row, 3, item);
00518   item = new QTableWidgetItem(VarMaxEdit->text());
00519   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00520   VarTable->setItem(row, 4, item);
00521   item = new QTableWidgetItem(VarTypeCombo->currentText());
00522   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00523   VarTable->setItem(row, 5, item);
00524 
00525   VarTable->setCurrentCell(row, 0);
00526   slotEditVariable();   // clear entry fields
00527   VarTable->clearSelection();
00528 }
00529 
00530 // -----------------------------------------------------------
00531 void OptimizeDialog::slotDeleteVariable()
00532 {
00533   int selectedrow = VarTable->currentRow();
00534   VarTable->removeRow(selectedrow);
00535   int nextRow = (selectedrow == VarTable->rowCount())? selectedrow-1 : selectedrow;
00536   VarTable->setCurrentCell(nextRow, 0);
00537   slotEditVariable();
00538 }
00539 
00540 // -----------------------------------------------------------
00541 void OptimizeDialog::slotChangeVarActive(bool On)
00542 {
00543   int selectedrow = VarTable->currentRow();
00544   QTableWidgetItem *item = VarTable->item(selectedrow, 1);
00545   if (item) {
00546     item->setText( On ? tr("yes") : tr("no") );
00547   }
00548 }
00549 
00550 // -----------------------------------------------------------
00551 void OptimizeDialog::slotChangeVarName(const QString&)
00552 {
00553   VarTable->clearSelection();
00554 }
00555 
00556 // -----------------------------------------------------------
00557 void OptimizeDialog::slotChangeVarInit(const QString& Text)
00558 {
00559   int selectedrow = VarTable->currentRow();
00560   QTableWidgetItem *item = VarTable->item(selectedrow, 2);
00561   if (item) {
00562     item->setText(Text);
00563   }
00564 }
00565 
00566 // -----------------------------------------------------------
00567 void OptimizeDialog::slotChangeVarMin(const QString& Text)
00568 {
00569   int selectedrow = VarTable->currentRow();
00570   QTableWidgetItem *item = VarTable->item(selectedrow, 3);
00571   if (item) {
00572     item->setText(Text);
00573   }
00574 }
00575 
00576 // -----------------------------------------------------------
00577 void OptimizeDialog::slotChangeVarMax(const QString& Text)
00578 {
00579   int selectedrow = VarTable->currentRow();
00580   QTableWidgetItem *item = VarTable->item(selectedrow, 4);
00581   if (item) {
00582     item->setText(Text);
00583   }
00584 }
00585 
00586 // -----------------------------------------------------------
00587 void OptimizeDialog::slotChangeVarType(const QString& Text)
00588 {
00589   int selectedrow = VarTable->currentRow();
00590   QTableWidgetItem *item = VarTable->item(selectedrow, 5);
00591   if (item) {
00592     item->setText(Text);
00593   }
00594 }
00595 
00596 // -----------------------------------------------------------
00597 void OptimizeDialog::slotEditGoal()
00598 {
00599   GoalNameEdit->blockSignals(true);
00600 
00601   int row = GoalTable->currentRow();
00602   if(row < 0 || row >= GoalTable->rowCount()) {
00603     GoalNameEdit->clear();
00604     GoalTypeCombo->setCurrentIndex(0);
00605     GoalNumEdit->clear();
00606     GoalNameEdit->blockSignals(false);
00607     return;
00608   }
00609 
00610   GoalNameEdit->setText(GoalTable->item(row, 0)->text());
00611   GoalTypeCombo->setCurrentIndex(GoalTypeCombo->findText(GoalTable->item(row, 1)->text()));
00612   GoalNumEdit->setText(GoalTable->item(row, 2)->text());
00613 
00614   GoalNameEdit->blockSignals(false);
00615 }
00616 
00617 // -----------------------------------------------------------
00618 void OptimizeDialog::slotAddGoal()
00619 {
00620   if(GoalNameEdit->text().isEmpty() || GoalNumEdit->text().isEmpty()) {
00621     QMessageBox::critical(this, tr("Error"),
00622        tr("Every text field must be non-empty!"));
00623     return;
00624   }
00625 
00626   int row;
00627   for (row = 0; row < GoalTable->rowCount(); ++row) {
00628     if (GoalNameEdit->text() == GoalTable->item(row, 0)->text()) {
00629       QMessageBox::critical(this, tr("Error"),
00630          tr("Goal \"%1\" already in list!").arg(GoalNameEdit->text()));
00631       return;
00632     }
00633   }
00634 
00635   QTableWidgetItem *item;
00636   row = GoalTable->rowCount();
00637   GoalTable->insertRow(row);
00638   item = new QTableWidgetItem(GoalNameEdit->text());
00639   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00640   GoalTable->setItem(row, 0, item);
00641   item = new QTableWidgetItem(GoalTypeCombo->currentText());
00642   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00643   GoalTable->setItem(row, 1, item);
00644   item = new QTableWidgetItem(GoalNumEdit->text());
00645   item->setFlags(item->flags() & ~Qt::ItemIsEditable);
00646   GoalTable->setItem(row, 2, item);
00647 
00648   GoalTable->setCurrentCell(row, 0);
00649   slotEditGoal();    // clear entry fields
00650   GoalTable->clearSelection();
00651 }
00652 
00653 // -----------------------------------------------------------
00654 void OptimizeDialog::slotDeleteGoal()
00655 {
00656   int selectedrow = GoalTable->currentRow();
00657   GoalTable->removeRow(selectedrow);
00658   int nextRow = (selectedrow == GoalTable->rowCount())? selectedrow-1 : selectedrow;
00659   GoalTable->setCurrentCell(nextRow, 0);
00660   slotEditVariable();
00661 }
00662 
00663 // -----------------------------------------------------------
00664 void OptimizeDialog::slotChangeGoalName(const QString&)
00665 {
00666   GoalTable->clearSelection();
00667 }
00668 
00669 // -----------------------------------------------------------
00670 void OptimizeDialog::slotChangeGoalType(const QString& Text)
00671 {
00672   int selectedrow = GoalTable->currentRow();
00673   QTableWidgetItem *item = GoalTable->item(selectedrow, 1);
00674   if (item) {
00675     item->setText(Text);
00676   }
00677 }
00678 
00679 // -----------------------------------------------------------
00680 void OptimizeDialog::slotChangeGoalNum(const QString& Text)
00681 {
00682   int selectedrow = GoalTable->currentRow();
00683   QTableWidgetItem *item = GoalTable->item(selectedrow, 2);
00684   if (item) {
00685     item->setText(Text);
00686   }
00687 }
00688 
00689 // -----------------------------------------------------------
00690 void OptimizeDialog::slotOK()
00691 {
00692   slotApply();
00693   slotCancel();
00694 }
00695 
00696 // -----------------------------------------------------------
00697 void OptimizeDialog::slotApply()
00698 {
00699   Component *pc;
00700   if(NameEdit->text().isEmpty())
00701     NameEdit->setText(Comp->Name);
00702   else
00703   if(NameEdit->text() != Comp->Name) {
00704     for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next())
00705       if(pc->Name == NameEdit->text())
00706         break;  // found component with the same name ?
00707     if(pc)
00708       NameEdit->setText(Comp->Name);
00709     else {
00710       Comp->Name = NameEdit->text();
00711       changed = true;
00712     }
00713   }
00714 
00715   QString Prop;
00716   if(SimEdit->currentText() != Comp->Props.at(0)->Value) {
00717     Comp->Props.at(0)->Value = SimEdit->currentText();
00718     changed = true;
00719   }
00720   Prop = QString::number(MethodCombo->currentIndex()+1) + "|" +
00721     IterEdit->text() + "|" +
00722     RefreshEdit->text() + "|" +
00723     ParentsEdit->text() + "|" +
00724     ConstEdit->text() + "|" +
00725     CrossEdit->text() + "|" +
00726     SeedEdit->text() + "|" +
00727     CostVarEdit->text() + "|" +
00728     CostObjEdit->text() + "|" +
00729     CostConEdit->text();
00730   if(Prop != Comp->Props.at(1)->Value) {
00731     Comp->Props.at(1)->Value = Prop;
00732     changed = true;
00733   }
00734 
00735   Property *pp = Comp->Props.at(2);
00736   int row;
00737   // apply all the new property values in the TableWidget
00738   for (row = 0; row < VarTable->rowCount(); ++row) {
00739     QStringList propList;
00740     propList << VarTable->item(row, 0)->text();
00741     propList << ((VarTable->item(row, 1)->text() == tr("yes"))? "yes" : "no");
00742     propList << VarTable->item(row, 2)->text();
00743     propList << VarTable->item(row, 3)->text();
00744     propList << VarTable->item(row, 4)->text();
00745     QString typeStr = VarTable->item(row, 5)->text();
00746     if (typeStr == tr("linear double")) {
00747       propList << "LIN_DOUBLE";
00748     } else if (typeStr == tr("logarithmic double")) {
00749       propList << "LOG_DOUBLE";
00750     } else if (typeStr == tr("linear integer")) {
00751       propList << "LIN_INT";
00752     } else {
00753       propList << "LOG_INT";
00754     }
00755     Prop = propList.join("|");
00756 
00757     if(pp) {
00758       if(pp->Name != "Var") {
00759         pp->Name = "Var";
00760         changed = true;
00761       }
00762       if(pp->Value != Prop) {
00763         pp->Value = Prop;
00764         changed = true;
00765       }
00766     }
00767     else {
00768       Comp->Props.append(new Property("Var", Prop, false, ""));
00769       changed = true;
00770     }
00771     pp = Comp->Props.next();
00772   }
00773 
00774   for (row = 0; row < GoalTable->rowCount(); ++row) {
00775     QStringList propList;
00776     propList << GoalTable->item(row, 0)->text();
00777     QString typeStr = GoalTable->item(row, 1)->text();
00778     if (typeStr == tr("minimize")) {
00779       propList << "MIN";
00780     } else if (typeStr == tr("maximize")) {
00781       propList << "MAX";
00782     } else if (typeStr == tr("less")) {
00783       propList << "LE";
00784     } else if (typeStr == tr("greater")) {
00785       propList << "GE";
00786     } else if (typeStr == tr("equal")) {
00787       propList << "EQ";
00788     } else {
00789       propList << "MON";
00790     }
00791     propList << GoalTable->item(row, 2)->text();
00792     Prop = propList.join("|");
00793 
00794     if(pp) {
00795       if(pp->Name != "Goal") {
00796         pp->Name = "Goal";
00797         changed = true;
00798       }
00799       if(pp->Value != Prop) {
00800         pp->Value = Prop;
00801         changed = true;
00802       }
00803     }
00804     else {
00805       Comp->Props.append(new Property("Goal", Prop, false, ""));
00806       changed = true;
00807     }
00808     pp = Comp->Props.next();
00809   }
00810 
00811   // if more properties than in ListView -> delete the rest
00812   if(pp) {
00813     pp = Comp->Props.prev();
00814     Comp->Props.last();
00815     while(pp != Comp->Props.current())
00816       Comp->Props.remove();
00817     changed = true;
00818   }
00819 
00820   if(changed)
00821     Doc->viewport()->repaint();
00822 }
00823 
00824 // -------------------------------------------------------------------------
00825 // Is called if the "Cancel"-button is pressed.
00826 void OptimizeDialog::slotCancel()
00827 {
00828   if(changed) done(QDialog::Accepted); // changed could have been done before
00829   else done(QDialog::Rejected);        // (by "Apply"-button)
00830 }
00831 
00832 
00833 void OptimizeDialog::slotCreateEqn()
00834 {
00835   QString s = "<Qucs Schematic " PACKAGE_VERSION ">\n"
00836               "<Components>\n"
00837               //<Model Name ShowName cx cy tx ty mirroredX rotate
00838               "<Eqn OptValues 1 0 0 -28 15 0 0 ";
00839 
00840  Property *pp;
00841  for(pp = Comp->Props.at(2); pp != 0; pp = Comp->Props.next()) {
00842    if(pp->Name == "Var") { // property is an optimization variable
00843       QStringList ValueSplit = pp->Value.split("|");
00844       // "Name" = "initial (current) value"
00845       s += "\"" + ValueSplit.at(0) + "=" + ValueSplit.at(2) + "\" 1 ";
00846    }
00847  }
00848 
00849  s += QString("\"yes\" 0>\n" // Export yes, no display
00850               "</Components>\n"
00851         "<Wires>\n"
00852         "</Wires>\n"
00853         "<Diagrams>\n"
00854         "</Diagrams>\n"
00855         "<Paintings>\n"
00856         "</Paintings>\n");
00857 
00858  QApplication::clipboard()->setText(s, QClipboard::Clipboard);
00859  // uncomment to have the dialog close and the Equation pasted...
00860  //QucsMain->slotEditPaste(true);
00861  //accept();
00862 }
00863 
00864 void OptimizeDialog::slotSetPrecision(const QPoint& pos)
00865 {
00866   QPoint globalPos = VarTable->horizontalHeader()->mapToGlobal(pos);
00867   int column = VarTable->horizontalHeader()->logicalIndexAt(pos);
00868 
00869   // change precision is supported only for the 'initial' column
00870   if (column != 2) return;
00871 
00872   QMenu cMenu;
00873   cMenu.addAction("Set precision");
00874 
00875   QAction* selectedItem = cMenu.exec(globalPos);
00876   if (selectedItem) {
00877     bool ok;
00878     int i = QInputDialog::getInt(this, tr("Set precision"),
00879                                  tr("Precision:"), 2, 1, 16, 1, &ok);
00880     if (!ok) return;
00881     numPrec = i;
00882     // update the shown values according to the new precision
00883     int row = 0;
00884     Property *pp;
00885     QTableWidgetItem *item;
00886     for(pp = Comp->Props.at(2); pp != 0; pp = Comp->Props.next()) {
00887       if(pp->Name == "Var") {
00888   QStringList ValueSplit = pp->Value.split("|");
00889   // 'initial' column
00890   item = VarTable->item(row++, 2);
00891   item->setText(QString::number(ValueSplit.at(2).toDouble(), 'g', numPrec));
00892       }
00893     }
00894   }
00895 }
00896 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines