Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
changedialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  changedialog.cpp
3  ------------------
4  begin : Fri Jul 22 2005
5  copyright : (C) 2005 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <QtGui>
18 #include "changedialog.h"
19 #include "node.h"
20 #include "schematic.h"
21 #include "components/component.h"
22 
23 #include <QLabel>
24 #include <QLineEdit>
25 #include <QComboBox>
26 #include <QValidator>
27 #include <QPushButton>
28 #include <QScrollArea>
29 #include <QCheckBox>
30 #include <QMessageBox>
31 #include <QGridLayout>
32 #include <QList>
33 #include <QListIterator>
34 #include <QVBoxLayout>
35 #include <QDebug>
36 
37 
39  : QDialog(Doc_) //, 0, TRUE, Qt::WDestructiveClose)
40 {
41  Doc = Doc_;
42  setWindowTitle(tr("Change Component Properties"));
43 
44  Expr.setPattern("[^\"=]+"); // valid expression for property value
45  Validator = new QRegExpValidator(Expr, this);
46  Expr.setPattern("[\\w_]+"); // valid expression for property name
47  ValRestrict = new QRegExpValidator(Expr, this);
48 
49 
50  // ...........................................................
51  all = new QGridLayout(this);//, 6,2,3,3);
52  all->setMargin(5);
53 
54  all->addWidget(new QLabel(tr("Components:"), this), 0,0);
55  CompTypeEdit = new QComboBox(this);
56  CompTypeEdit->insertItem(tr("all components"));
57  CompTypeEdit->insertItem(tr("resistors"));
58  CompTypeEdit->insertItem(tr("capacitors"));
59  CompTypeEdit->insertItem(tr("inductors"));
60  CompTypeEdit->insertItem(tr("transistors"));
61  all->addWidget(CompTypeEdit, 0,1);
62 
63  all->addWidget(new QLabel(tr("Component Names:"), this), 1,0);
64  CompNameEdit = new QLineEdit(this);
65  CompNameEdit->setValidator(Validator);
66  CompNameEdit->setText("*");
67  all->addWidget(CompNameEdit, 1,1);
68  connect(CompNameEdit, SIGNAL(returnPressed()), SLOT(slotButtReplace()));
69 
70  all->addWidget(new QLabel(tr("Property Name:"), this), 2,0);
71  PropNameEdit = new QComboBox(this);
72  PropNameEdit->setEditable(true);
73  PropNameEdit->setValidator(ValRestrict);
74  PropNameEdit->insertItem("Temp");
75  PropNameEdit->insertItem("Subst");
76  PropNameEdit->insertItem("Model");
77  all->addWidget(PropNameEdit, 2,1);
78  connect(PropNameEdit, SIGNAL(activated(int)), SLOT(slotButtReplace()));
79 
80  all->addWidget(new QLabel(tr("New Value:"), this), 3,0);
81  NewValueEdit = new QLineEdit(this);
82  NewValueEdit->setValidator(Validator);
83  NewValueEdit->setText("-273.15");
84  all->addWidget(NewValueEdit, 3,1);
85  connect(NewValueEdit, SIGNAL(returnPressed()), SLOT(slotButtReplace()));
86 
87  // ...........................................................
88  QPushButton *pushReplace = new QPushButton(tr("Replace"));
89  QPushButton *pushCancel = new QPushButton(tr("Cancel"));
90  all->addWidget(pushReplace, 4,0);
91  all->addWidget(pushCancel, 4,1);
92  connect(pushReplace, SIGNAL(clicked()), SLOT(slotButtReplace()));
93  connect(pushCancel, SIGNAL(clicked()), SLOT(reject()));
94 }
95 
97 {
98  delete all;
99  delete Validator;
100  delete ValRestrict;
101 }
102 
103 // -----------------------------------------------------------------------
104 // Returns "true" if the component model matches the user selection
105 // in "CompTypeEdit".
106 bool ChangeDialog::matches(const QString& CompModel)
107 {
108  switch(CompTypeEdit->currentItem()) {
109  case 0: return true;
110  case 1: if(CompModel == "R") return true;
111  return false;
112  case 2: if(CompModel == "C") return true;
113  return false;
114  case 3: if(CompModel == "L") return true;
115  return false;
116  case 4: if(CompModel == "BJT") return true;
117  if(CompModel == "_BJT") return true;
118  if(CompModel == "JFET") return true;
119  if(CompModel == "MOSFET") return true;
120  if(CompModel == "_MOSFET") return true;
121  return false;
122  }
123 
124  return false;
125 }
126 
127 // -----------------------------------------------------------------------
128 // Is called if the "Replace"-button is pressed.
130 {
131  Expr.setWildcard(true); // switch into wildcard mode
132  Expr.setPattern(CompNameEdit->text());
133  if(!Expr.isValid()) {
134  QMessageBox::critical(this, tr("Error"),
135  tr("Regular expression for component name is invalid."));
136  return;
137  }
138 
139  // create dialog showing all found components
140  QDialog *Dia = new QDialog(this);
141  Dia->setCaption(tr("Found Components"));
142  QVBoxLayout *Dia_All = new QVBoxLayout(Dia);
143  Dia_All->setSpacing(3);
144  Dia_All->setMargin(5);
145 
146  QScrollArea *Dia_Scroll = new QScrollArea(Dia);
147  //Dia_Scroll->setMargin(5);
148  Dia_All->addWidget(Dia_Scroll);
149 
150  QVBoxLayout *Dia_Box = new QVBoxLayout(Dia_Scroll->viewport());
151  Dia_Scroll->insertChild(Dia_Box);
152  QLabel *Dia_Label = new QLabel(tr("Change properties of\n")
153  + tr("these components ?"), Dia);
154  Dia_All->addWidget(Dia_Label);
155 
156  QHBoxLayout *Dia_h = new QHBoxLayout(Dia);
157  Dia_h->setSpacing(5);
158  QPushButton *YesButton = new QPushButton(tr("Yes"));
159  QPushButton *CancelButton = new QPushButton(tr("Cancel"));
160  Dia_h->addWidget(YesButton);
161  Dia_h->addWidget(CancelButton);
162  connect(YesButton, SIGNAL(clicked()), Dia, SLOT(accept()));
163  connect(CancelButton, SIGNAL(clicked()), Dia, SLOT(reject()));
164 
165  Dia_All->addLayout(Dia_h);
166 
167  QList<QCheckBox *> pList;
168  QCheckBox *pb;
169  Component *pc;
170  QStringList List;
171  QString str;
172  int i1, i2;
173  // search through all components
174  for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next()) {
175  if(matches(pc->Model)) {
176  if(Expr.search(pc->Name) >= 0)
177  for(Property *pp = pc->Props.first(); pp!=0; pp = pc->Props.next())
178  if(pp->Name == PropNameEdit->currentText()) {
179  pb = new QCheckBox(pc->Name);
180  Dia_Box->addWidget(pb);
181  pList.append(pb);
182  pb->setChecked(true);
183  i1 = pp->Description.find('[');
184  if(i1 < 0) break; // no multiple-choice property
185 
186  i2 = pp->Description.findRev(']');
187  if(i2-i1 < 2) break;
188  str = pp->Description.mid(i1+1, i2-i1-1);
189  str.replace( QRegExp("[^a-zA-Z0-9_,]"), "" );
190  List = List.split(',',str);
191  if(List.findIndex(NewValueEdit->text()) >= 0)
192  break; // property value is okay
193 
194  pb->setChecked(false);
195  pb->setEnabled(false);
196  break;
197  }
198  }
199  }
200 /*
201  QColor theColor;
202  if(pList.isEmpty()) {
203  YesButton->setEnabled(false);
204  theColor =
205  (new QLabel(tr("No match found!"), Dia_Box))->paletteBackgroundColor();
206  }
207  else theColor = pList.current()->paletteBackgroundColor();
208 */
209  //Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
210  Dia->resize(50, 300);
211 
212 
213  // show user all components found
214  int Result = Dia->exec();
215  if(Result != QDialog::Accepted) return;
216 
217 
218  bool changed = false;
219  // change property values
220 
221  QListIterator<QCheckBox *> i(pList);
222  while(i.hasNext()){
223  pb = i.next();
224  if(!pb->isChecked()) continue;
225 
226  for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next()) {
227  if(pb->text() != pc->Name) continue;
228 
229  for(Property *pp = pc->Props.first(); pp!=0; pp = pc->Props.next()) {
230  if(pp->Name != PropNameEdit->currentText()) continue;
231 
232  int tx_Dist, ty_Dist, tmp;
233  pc->textSize(tx_Dist, ty_Dist);
234  tmp = pc->tx+tx_Dist - pc->x1;
235  if((tmp > 0) || (tmp < -6)) tx_Dist = 0; // remember text position
236  tmp = pc->ty+ty_Dist - pc->y1;
237  if((tmp > 0) || (tmp < -6)) ty_Dist = 0;
238 
239  pp->Value = NewValueEdit->text();
240 
241  int dx, dy;
242  pc->textSize(dx, dy); // correct text position
243  if(tx_Dist != 0) {
244  pc->tx += tx_Dist-dx;
245  tx_Dist = dx;
246  }
247  if(ty_Dist != 0) {
248  pc->ty += ty_Dist-dy;
249  ty_Dist = dy;
250  }
251 
252  // apply changes to schematic symbol
253  Doc->recreateComponent(pc);
254  changed = true;
255  break;
256  }
257  break;
258  }
259  }
260 
261  delete Dia_All;
262  delete Dia;
263  if(changed) accept();
264  else reject();
265 }
void slotButtReplace()
QRegExpValidator * ValRestrict
Definition: changedialog.h:46
QLineEdit * CompNameEdit
Definition: changedialog.h:48
int y1
Definition: element.h:153
Schematic * Doc
Definition: changedialog.h:44
QRegExp Expr
Definition: changedialog.h:47
int tx
Definition: component.h:78
int x1
Definition: element.h:153
ChangeDialog(Schematic *)
int textSize(int &, int &)
Definition: component.cpp:86
int ty
Definition: component.h:78
Q3PtrList< Component > * Components
Definition: schematic.h:123
Q3PtrList< Property > Props
Definition: component.h:72
bool matches(const QString &)
QRegExpValidator * Validator
Definition: changedialog.h:46
QString Name
Definition: component.h:80
QString Model
Definition: component.h:80
QComboBox * PropNameEdit
Definition: changedialog.h:49
QComboBox * CompTypeEdit
Definition: changedialog.h:49
QGridLayout * all
Definition: changedialog.h:45
void recreateComponent(Component *)
QLineEdit * NewValueEdit
Definition: changedialog.h:48