Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/dialogs/changedialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                                changedialog.cpp
00003                               ------------------
00004     begin                : Fri Jul 22 2005
00005     copyright            : (C) 2005 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 "changedialog.h"
00018 #include "node.h"
00019 #include "schematic.h"
00020 #include "components/component.h"
00021 
00022 #include <QLabel>
00023 #include <QLineEdit>
00024 #include <QComboBox>
00025 #include <QValidator>
00026 #include <QPushButton>
00027 #include <QScrollArea>
00028 #include <QCheckBox>
00029 #include <QMessageBox>
00030 #include <QGridLayout>
00031 #include <QList>
00032 #include <QListIterator>
00033 #include <QVBoxLayout>
00034 #include <QDebug>
00035 
00036 
00037 ChangeDialog::ChangeDialog(Schematic *Doc_)
00038       : QDialog(Doc_) 
00039 {
00040   Doc = Doc_;
00041   setWindowTitle(tr("Change Component Properties"));
00042 
00043   Expr.setPattern("[^\"=]+");  // valid expression for property value
00044   Validator = new QRegExpValidator(Expr, this);
00045   Expr.setPattern("[\\w_]+");  // valid expression for property name
00046   ValRestrict = new QRegExpValidator(Expr, this);
00047 
00048 
00049   // ...........................................................
00050   all = new QGridLayout(this);//, 6,2,3,3);
00051   all->setMargin(5);
00052 
00053   all->addWidget(new QLabel(tr("Components:"), this), 0,0);
00054   CompTypeEdit = new QComboBox(this);
00055   CompTypeEdit->insertItem(tr("all components"));
00056   CompTypeEdit->insertItem(tr("resistors"));
00057   CompTypeEdit->insertItem(tr("capacitors"));
00058   CompTypeEdit->insertItem(tr("inductors"));
00059   CompTypeEdit->insertItem(tr("transistors"));
00060   all->addWidget(CompTypeEdit, 0,1);
00061 
00062   all->addWidget(new QLabel(tr("Component Names:"), this), 1,0);
00063   CompNameEdit = new QLineEdit(this);
00064   CompNameEdit->setValidator(Validator);
00065   CompNameEdit->setText("*");
00066   all->addWidget(CompNameEdit, 1,1);
00067   connect(CompNameEdit, SIGNAL(returnPressed()), SLOT(slotButtReplace()));
00068 
00069   all->addWidget(new QLabel(tr("Property Name:"), this), 2,0);
00070   PropNameEdit = new QComboBox(this);
00071   PropNameEdit->setEditable(true);
00072   PropNameEdit->setValidator(ValRestrict);
00073   PropNameEdit->insertItem("Temp");
00074   PropNameEdit->insertItem("Subst");
00075   PropNameEdit->insertItem("Model");
00076   all->addWidget(PropNameEdit, 2,1);
00077   connect(PropNameEdit, SIGNAL(activated(int)), SLOT(slotButtReplace()));
00078 
00079   all->addWidget(new QLabel(tr("New Value:"), this), 3,0);
00080   NewValueEdit = new QLineEdit(this);
00081   NewValueEdit->setValidator(Validator);
00082   NewValueEdit->setText("-273.15");
00083   all->addWidget(NewValueEdit, 3,1);
00084   connect(NewValueEdit, SIGNAL(returnPressed()), SLOT(slotButtReplace()));
00085 
00086   // ...........................................................
00087   QPushButton *pushReplace = new QPushButton(tr("Replace"));
00088   QPushButton *pushCancel = new QPushButton(tr("Cancel"));
00089   all->addWidget(pushReplace, 4,0);
00090   all->addWidget(pushCancel, 4,1);
00091   connect(pushReplace, SIGNAL(clicked()), SLOT(slotButtReplace()));
00092   connect(pushCancel, SIGNAL(clicked()), SLOT(reject()));
00093 }
00094 
00095 ChangeDialog::~ChangeDialog()
00096 {
00097   delete all;
00098   delete Validator;
00099   delete ValRestrict;
00100 }
00101 
00102 // -----------------------------------------------------------------------
00103 // Returns "true" if the component model matches the user selection
00104 // in "CompTypeEdit".
00105 bool ChangeDialog::matches(const QString& CompModel)
00106 {
00107   switch(CompTypeEdit->currentItem()) {
00108     case 0: return true;
00109     case 1: if(CompModel == "R") return true;
00110             return false;
00111     case 2: if(CompModel == "C") return true;
00112             return false;
00113     case 3: if(CompModel == "L") return true;
00114             return false;
00115     case 4: if(CompModel == "BJT") return true;
00116             if(CompModel == "_BJT") return true;
00117             if(CompModel == "JFET") return true;
00118             if(CompModel == "MOSFET") return true;
00119             if(CompModel == "_MOSFET") return true;
00120             return false;
00121   }
00122 
00123   return false;
00124 }
00125 
00126 // -----------------------------------------------------------------------
00127 // Is called if the "Replace"-button is pressed.
00128 void ChangeDialog::slotButtReplace()
00129 {
00130   Expr.setWildcard(true);  // switch into wildcard mode
00131   Expr.setPattern(CompNameEdit->text());
00132   if(!Expr.isValid()) {
00133     QMessageBox::critical(this, tr("Error"),
00134     tr("Regular expression for component name is invalid."));
00135     return;
00136   }
00137 
00138   // create dialog showing all found components
00139   QDialog *Dia = new QDialog(this);
00140   Dia->setWindowTitle(tr("Found Components"));
00141   QVBoxLayout *Dia_All = new QVBoxLayout(Dia);
00142   Dia_All->setSpacing(3);
00143   Dia_All->setMargin(5);
00144   
00145   QScrollArea *Dia_Scroll = new QScrollArea(Dia);
00146   //Dia_Scroll->setMargin(5);
00147   Dia_All->addWidget(Dia_Scroll);
00148   
00149   QVBoxLayout *Dia_Box = new QVBoxLayout(Dia_Scroll->viewport());
00150   Dia_Scroll->insertChild(Dia_Box);
00151   QLabel *Dia_Label = new QLabel(tr("Change properties of\n")
00152                                + tr("these components ?"), Dia);
00153   Dia_All->addWidget(Dia_Label);
00154   
00155   QHBoxLayout *Dia_h = new QHBoxLayout(Dia);
00156   Dia_h->setSpacing(5);
00157   QPushButton *YesButton = new QPushButton(tr("Yes"));
00158   QPushButton *CancelButton = new QPushButton(tr("Cancel"));
00159   Dia_h->addWidget(YesButton);
00160   Dia_h->addWidget(CancelButton);
00161   connect(YesButton, SIGNAL(clicked()), Dia, SLOT(accept()));
00162   connect(CancelButton, SIGNAL(clicked()), Dia, SLOT(reject()));
00163   
00164   Dia_All->addLayout(Dia_h);
00165 
00166   QList<QCheckBox *> pList;
00167   QCheckBox *pb;
00168   Component *pc;
00169   QStringList List;
00170   QString str;
00171   int i1, i2;
00172   // search through all components
00173   for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next()) {
00174     if(matches(pc->Model)) {
00175       if(Expr.search(pc->Name) >= 0)
00176         for(Property *pp = pc->Props.first(); pp!=0; pp = pc->Props.next())
00177           if(pp->Name == PropNameEdit->currentText()) {
00178             pb = new QCheckBox(pc->Name);
00179             Dia_Box->addWidget(pb);   
00180             pList.append(pb);
00181             pb->setChecked(true);
00182             i1 = pp->Description.indexOf('[');
00183             if(i1 < 0)  break;  // no multiple-choice property
00184 
00185             i2 = pp->Description.findRev(']');
00186             if(i2-i1 < 2)  break;
00187             str = pp->Description.mid(i1+1, i2-i1-1);
00188             str.replace( QRegExp("[^a-zA-Z0-9_,]"), "" );
00189             List = List.split(',',str);
00190             if(List.findIndex(NewValueEdit->text()) >= 0)
00191               break;    // property value is okay
00192 
00193             pb->setChecked(false);
00194             pb->setEnabled(false);
00195             break;
00196           }
00197     }
00198   }
00199 /*
00200   QColor theColor;
00201   if(pList.isEmpty()) {
00202     YesButton->setEnabled(false);
00203     theColor =
00204        (new QLabel(tr("No match found!"), Dia_Box))->paletteBackgroundColor();
00205   }
00206   else  theColor = pList.current()->paletteBackgroundColor();
00207 */
00208   //Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
00209   Dia->resize(50, 300);
00210 
00211 
00212   // show user all components found
00213   int Result = Dia->exec();
00214   if(Result != QDialog::Accepted) return;
00215 
00216 
00217   bool changed = false;
00218   // change property values
00219   
00220   QListIterator<QCheckBox *> i(pList);
00221   while(i.hasNext()){
00222     pb = i.next();
00223     if(!pb->isChecked())  continue;
00224 
00225     for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next()) {
00226       if(pb->text() != pc->Name)  continue;
00227 
00228       for(Property *pp = pc->Props.first(); pp!=0; pp = pc->Props.next()) {
00229         if(pp->Name != PropNameEdit->currentText())  continue;
00230 
00231         int tx_Dist, ty_Dist, tmp;
00232         pc->textSize(tx_Dist, ty_Dist);
00233         tmp = pc->tx+tx_Dist - pc->x1;
00234         if((tmp > 0) || (tmp < -6))  tx_Dist = 0; // remember text position
00235         tmp = pc->ty+ty_Dist - pc->y1;
00236         if((tmp > 0) || (tmp < -6))  ty_Dist = 0;
00237 
00238         pp->Value = NewValueEdit->text();
00239 
00240         int dx, dy;
00241         pc->textSize(dx, dy);   // correct text position
00242         if(tx_Dist != 0) {
00243           pc->tx += tx_Dist-dx;
00244           tx_Dist = dx;
00245         }
00246         if(ty_Dist != 0) {
00247           pc->ty += ty_Dist-dy;
00248           ty_Dist = dy;
00249         }
00250 
00251         // apply changes to schematic symbol
00252         Doc->recreateComponent(pc);
00253         changed = true;
00254         break;
00255       }
00256       break;
00257     }
00258   }
00259 
00260   delete Dia_All;
00261   delete Dia;
00262   if(changed) accept();
00263   else reject();
00264 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines