My Project  0.0.16
QUCS Mapping
 All Classes Namespaces 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 
18 #include "changedialog.h"
19 #include "node.h"
20 #include "schematic.h"
21 #include "components/component.h"
22 
23 #include <qlabel.h>
24 #include <qlayout.h>
25 #include <qhbox.h>
26 #include <qlineedit.h>
27 #include <qcombobox.h>
28 #include <qvalidator.h>
29 #include <qpushbutton.h>
30 #include <qscrollview.h>
31 #include <qvbox.h>
32 #include <qcheckbox.h>
33 #include <qmessagebox.h>
34 
35 
37  : QDialog(Doc_, 0, TRUE, Qt::WDestructiveClose)
38 {
39  Doc = Doc_;
40  setCaption(tr("Change Component Properties"));
41 
42  Expr.setPattern("[^\"=]+"); // valid expression for property value
43  Validator = new QRegExpValidator(Expr, this);
44  Expr.setPattern("[\\w_]+"); // valid expression for property name
45  ValRestrict = new QRegExpValidator(Expr, this);
46 
47 
48  // ...........................................................
49  all = new QGridLayout(this, 6,2,3,3);
50  all->setMargin(5);
51 
52  all->addWidget(new QLabel(tr("Components:"), this), 0,0);
53  CompTypeEdit = new QComboBox(this);
54  CompTypeEdit->insertItem(tr("all components"));
55  CompTypeEdit->insertItem(tr("resistors"));
56  CompTypeEdit->insertItem(tr("capacitors"));
57  CompTypeEdit->insertItem(tr("inductors"));
58  CompTypeEdit->insertItem(tr("transistors"));
59  all->addWidget(CompTypeEdit, 0,1);
60 
61  all->addWidget(new QLabel(tr("Component Names:"), this), 1,0);
62  CompNameEdit = new QLineEdit(this);
63  CompNameEdit->setValidator(Validator);
64  CompNameEdit->setText("*");
65  all->addWidget(CompNameEdit, 1,1);
66 // connect(CompNameEdit, SIGNAL(returnPressed()), SLOT(slotButtReplace()));
67 
68  all->addWidget(new QLabel(tr("Property Name:"), this), 2,0);
69  PropNameEdit = new QComboBox(this);
70  PropNameEdit->setEditable(true);
71  PropNameEdit->setValidator(ValRestrict);
72  PropNameEdit->insertItem("Temp");
73  PropNameEdit->insertItem("Subst");
74  PropNameEdit->insertItem("Model");
75  all->addWidget(PropNameEdit, 2,1);
76 // connect(PropNameEdit, SIGNAL(activated(int)), SLOT(slotButtReplace()));
77 
78  all->addWidget(new QLabel(tr("New Value:"), this), 3,0);
79  NewValueEdit = new QLineEdit(this);
80  NewValueEdit->setValidator(Validator);
81  NewValueEdit->setText("-273.15");
82  all->addWidget(NewValueEdit, 3,1);
83 // connect(NewValueEdit, SIGNAL(returnPressed()), SLOT(slotButtReplace()));
84 
85  // ...........................................................
86  QHBox *h0 = new QHBox(this);
87  h0->setSpacing(5);
88  all->setRowStretch(4,5);
89  all->addMultiCellWidget(h0, 5,5, 0,1);
90  connect(new QPushButton(tr("Replace"),h0), SIGNAL(clicked()),
91  SLOT(slotButtReplace()));
92  connect(new QPushButton(tr("Cancel"),h0), SIGNAL(clicked()),
93  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.
129 void ChangeDialog::slotButtReplace()
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 
140  // create dialog showing all found components
141  QDialog *Dia = new QDialog(this);
142  Dia->setCaption(tr("Found Components"));
143  QVBoxLayout *Dia_All = new QVBoxLayout(Dia);
144  Dia_All->setSpacing(3);
145  Dia_All->setMargin(5);
146  QScrollView *Dia_Scroll = new QScrollView(Dia);
147  Dia_Scroll->setMargin(5);
148  Dia_All->addWidget(Dia_Scroll);
149  QVBox *Dia_Box = new QVBox(Dia_Scroll->viewport());
150  Dia_Scroll->addChild(Dia_Box);
151  QLabel *Dia_Label = new QLabel(tr("Change properties of\n")
152  + tr("these components ?"), Dia);
153  Dia_All->addWidget(Dia_Label);
154 
155  QHBox *Dia_h = new QHBox(Dia);
156  Dia_h->setSpacing(5);
157  Dia_All->addWidget(Dia_h);
158  QPushButton *YesButton = new QPushButton(tr("Yes"), Dia_h);
159  connect(YesButton, SIGNAL(clicked()),
160  Dia, SLOT(accept()));
161  connect(new QPushButton(tr("Cancel"), Dia_h), SIGNAL(clicked()),
162  Dia, SLOT(reject()));
163 
164 
165  QPtrList<QCheckBox> pList;
166  QCheckBox *pb;
167  Component *pc;
168  QStringList List;
169  QString str;
170  int i1, i2;
171  // search through all components
172  for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next()) {
173  if(matches(pc->Model)) {
174  if(Expr.search(pc->Name) >= 0)
175  for(Property *pp = pc->Props.first(); pp!=0; pp = pc->Props.next())
176  if(pp->Name == PropNameEdit->currentText()) {
177  pb = new QCheckBox(pc->Name, Dia_Box);
178  pList.append(pb);
179  pb->setChecked(true);
180  i1 = pp->Description.find('[');
181  if(i1 < 0) break; // no multiple-choice property
182 
183  i2 = pp->Description.findRev(']');
184  if(i2-i1 < 2) break;
185  str = pp->Description.mid(i1+1, i2-i1-1);
186  str.replace( QRegExp("[^a-zA-Z0-9_,]"), "" );
187  List = List.split(',',str);
188  if(List.findIndex(NewValueEdit->text()) >= 0)
189  break; // property value is okay
190 
191  pb->setChecked(false);
192  pb->setEnabled(false);
193  break;
194  }
195  }
196  }
197 
198  QColor theColor;
199  if(pList.isEmpty()) {
200  YesButton->setEnabled(false);
201  theColor =
202  (new QLabel(tr("No match found!"), Dia_Box))->paletteBackgroundColor();
203  }
204  else theColor = pList.current()->paletteBackgroundColor();
205 
206  Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
207  Dia->resize(50, 300);
208 
209 
210  // show user all components found
211  int Result = Dia->exec();
212  if(Result != QDialog::Accepted) return;
213 
214 
215  bool changed = false;
216  // change property values
217  for(pb = pList.first(); pb!=0; pb = pList.next()) {
218  if(!pb->isChecked()) continue;
219 
220  for(pc = Doc->Components->first(); pc!=0; pc = Doc->Components->next()) {
221  if(pb->text() != pc->Name) continue;
222 
223  for(Property *pp = pc->Props.first(); pp!=0; pp = pc->Props.next()) {
224  if(pp->Name != PropNameEdit->currentText()) continue;
225 
226  int tx_Dist, ty_Dist, tmp;
227  pc->textSize(tx_Dist, ty_Dist);
228  tmp = pc->tx+tx_Dist - pc->x1;
229  if((tmp > 0) || (tmp < -6)) tx_Dist = 0; // remember text position
230  tmp = pc->ty+ty_Dist - pc->y1;
231  if((tmp > 0) || (tmp < -6)) ty_Dist = 0;
232 
233  pp->Value = NewValueEdit->text();
234 
235  int dx, dy;
236  pc->textSize(dx, dy); // correct text position
237  if(tx_Dist != 0) {
238  pc->tx += tx_Dist-dx;
239  tx_Dist = dx;
240  }
241  if(ty_Dist != 0) {
242  pc->ty += ty_Dist-dy;
243  ty_Dist = dy;
244  }
245 
246  // apply changes to schematic symbol
247  Doc->recreateComponent(pc);
248  changed = true;
249  break;
250  }
251  break;
252  }
253  }
254 
255  delete Dia_All;
256  delete Dia;
257  if(changed) accept();
258  else reject();
259 }