My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
id_dialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  id_dialog.cpp
3  ---------------
4  begin : Sat Oct 16 2004
5  copyright : (C) 2004 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 "id_dialog.h"
19 #include "id_text.h"
20 
21 #include <qhbox.h>
22 #include <qvbox.h>
23 #include <qlabel.h>
24 #include <qlayout.h>
25 #include <qlistview.h>
26 #include <qcheckbox.h>
27 #include <qlineedit.h>
28 #include <qvgroupbox.h>
29 #include <qvalidator.h>
30 #include <qpushbutton.h>
31 #include <qmessagebox.h>
32 
33 
35 {
36  idText = idText_;
37  setCaption(tr("Edit Subcircuit Properties"));
38 
39  v = new QVBoxLayout(this);
40  v->setSpacing(5);
41  v->setMargin(5);
42 
43  QHBox *h0 = new QHBox(this);
44  h0->setSpacing(5);
45  v->addWidget(h0);
46 
47  Expr.setPattern("[A-Za-z][A-Za-z0-9_]*");
48  SubVal = new QRegExpValidator(Expr, this);
49  new QLabel(tr("Prefix:"), h0);
50  Prefix = new QLineEdit(h0);
51  Prefix->setValidator(SubVal);
52  Prefix->setText(idText->Prefix);
53 
54 
55  QVGroupBox *ParamBox = new QVGroupBox(tr("Parameters"), this);
56  v->addWidget(ParamBox);
57  ParamList = new QListView(ParamBox);
58  ParamList->addColumn(tr("display"));
59  ParamList->addColumn(tr("Name"));
60  ParamList->addColumn(tr("Default"));
61  ParamList->addColumn(tr("Description"));
62  ParamList->addColumn(tr("Type"));
63  ParamList->setSorting(-1); // no sorting
64 
65  SubParameter *pp;
66  for(pp = idText->Parameter.last(); pp!=0; pp = idText->Parameter.prev())
67  new QListViewItem(ParamList,
68  pp->display ? tr("yes") : tr("no"), pp->Name.section('=', 0,0),
69  pp->Name.section('=', 1,1), pp->Description, pp->Type);
70 
71  connect(ParamList, SIGNAL(selectionChanged(QListViewItem*)),
72  SLOT(slotEditParameter(QListViewItem*)));
73 
74  showCheck = new QCheckBox(tr("display in schematic"), ParamBox);
75  showCheck->setChecked(true);
76  connect(showCheck, SIGNAL(toggled(bool)), SLOT(slotToggleShow(bool)));
77 
78  QHBox *h1 = new QHBox(ParamBox);
79  QVBox *v1 = new QVBox(h1);
80  new QLabel(tr("Name:"), v1);
81  new QLabel(tr("Default Value:"), v1);
82  new QLabel(tr("Description:"), v1);
83  new QLabel(tr("Type:"), v1);
84 
85  QVBox *v2 = new QVBox(h1);
86 
87  Expr.setPattern("[\\w_]+");
88  NameVal = new QRegExpValidator(Expr, this);
89  ParamNameEdit = new QLineEdit(v2);
90  ParamNameEdit->setValidator(NameVal);
91  connect(ParamNameEdit, SIGNAL(textChanged(const QString&)),
92  SLOT(slotNameChanged(const QString&)));
93 
94  Expr.setPattern("[^\"=]*");
95  ValueVal = new QRegExpValidator(Expr, this);
96  ValueEdit = new QLineEdit(v2);
97  ValueEdit->setValidator(ValueVal);
98  connect(ValueEdit, SIGNAL(textChanged(const QString&)),
99  SLOT(slotValueChanged(const QString&)));
100 
101  Expr.setPattern("[^\"=\\x005B\\x005D]*");
102  DescrVal = new QRegExpValidator(Expr, this);
103  DescriptionEdit = new QLineEdit(v2);
104  DescriptionEdit->setValidator(DescrVal);
105  connect(DescriptionEdit, SIGNAL(textChanged(const QString&)),
106  SLOT(slotDescrChanged(const QString&)));
107 
108  Expr.setPattern("[\\w_]+");
109  TypeVal = new QRegExpValidator(Expr, this);
110  TypeEdit = new QLineEdit(v2);
111  TypeEdit->setValidator(TypeVal);
112  connect(TypeEdit, SIGNAL(textChanged(const QString&)),
113  SLOT(slotTypeChanged(const QString&)));
114 
115  QHBox *h2 = new QHBox(ParamBox);
116  h2->setStretchFactor(new QWidget(h2), 10);
117  QPushButton *ButtAdd = new QPushButton(tr("Add"), h2);
118  connect(ButtAdd, SIGNAL(clicked()), SLOT(slotAddParameter()));
119  QPushButton *ButtRemove = new QPushButton(tr("Remove"), h2);
120  connect(ButtRemove, SIGNAL(clicked()), SLOT(slotRemoveParameter()));
121 
122  QHBox *h3 = new QHBox(this);
123  h3->setSpacing(5);
124  v->addWidget(h3);
125 
126  QPushButton *ButtOK = new QPushButton(tr("OK"),h3);
127  connect(ButtOK, SIGNAL(clicked()), SLOT(slotOk()));
128  QPushButton *ButtCancel = new QPushButton(tr("Cancel"),h3);
129  connect(ButtCancel, SIGNAL(clicked()), SLOT(reject()));
130 
131  resize(320, 350);
132 }
133 
135 {
136  delete v;
137  delete SubVal;
138  delete NameVal;
139  delete ValueVal;
140  delete DescrVal;
141  delete TypeVal;
142 }
143 
144 // -----------------------------------------------------------
145 void ID_Dialog::slotEditParameter(QListViewItem *Item)
146 {
147  if(Item == 0) {
148  ParamList->clearSelection();
149  ParamNameEdit->clear();
150  showCheck->setChecked(true);
151  ValueEdit->clear();
152  DescriptionEdit->clear();
153  TypeEdit->clear();
154  return;
155  }
156 
157  showCheck->setChecked(Item->text(0) == tr("yes"));
158  ParamNameEdit->setText(Item->text(1));
159  ValueEdit->setText(Item->text(2));
160  DescriptionEdit->setText(Item->text(3));
161  TypeEdit->setText(Item->text(4));
162 }
163 
164 // -----------------------------------------------------------
165 void ID_Dialog::slotAddParameter()
166 {
167  if(ParamNameEdit->text().isEmpty())
168  return;
169 
170  if(ParamNameEdit->text() == "File") {
171  QMessageBox::critical(this, tr("Error"),
172  tr("Parameter must not be named \"File\"!"));
173  return;
174  }
175 
176  QListViewItem *item, *lastItem=0;
177  for(item = ParamList->firstChild(); item!=0; item = item->itemBelow()) {
178  if(item->text(1) == ParamNameEdit->text()) {
179  QMessageBox::critical(this, tr("Error"),
180  tr("Parameter \"%1\" already in list!").arg(ParamNameEdit->text()));
181  return;
182  }
183  lastItem = item;
184  }
185 
186 
187  new QListViewItem(ParamList, lastItem,
188  showCheck->isChecked() ? tr("yes") : tr("no"), ParamNameEdit->text(),
189  ValueEdit->text(), DescriptionEdit->text(), TypeEdit->text());
190 
191  slotEditParameter(0); // clear entry fields
192  ParamList->clearSelection();
193 }
194 
195 // -----------------------------------------------------------
196 void ID_Dialog::slotRemoveParameter()
197 {
198  QListViewItem *nextItem = 0;
199 
200  QListViewItem *Item = ParamList->selectedItem();
201  if(Item) {
202  nextItem = Item->itemBelow();
203  if(nextItem == 0) nextItem = Item->itemAbove();
204  ParamList->takeItem(Item); // remove from ListView
205  delete Item; // delete item
206  }
207 
208  slotEditParameter(nextItem);
209 }
210 
211 // -----------------------------------------------------------
212 void ID_Dialog::slotToggleShow(bool On)
213 {
214  QListViewItem *Item = ParamList->selectedItem();
215  if(Item == 0) return;
216 
217  Item->setText(0, On ? tr("yes") : tr("no"));
218 }
219 
220 // -----------------------------------------------------------
221 void ID_Dialog::slotNameChanged(const QString& text)
222 {
223  QListViewItem *Item = ParamList->selectedItem();
224  if(Item == 0) return;
225 
226  Item->setText(1, text);
227 }
228 
229 // -----------------------------------------------------------
230 void ID_Dialog::slotValueChanged(const QString& text)
231 {
232  QListViewItem *Item = ParamList->selectedItem();
233  if(Item == 0) return;
234 
235  Item->setText(2, text);
236 }
237 
238 // -----------------------------------------------------------
239 void ID_Dialog::slotDescrChanged(const QString& text)
240 {
241  QListViewItem *Item = ParamList->selectedItem();
242  if(Item == 0) return;
243 
244  Item->setText(3, text);
245 }
246 
247 // -----------------------------------------------------------
248 void ID_Dialog::slotTypeChanged(const QString& text)
249 {
250  QListViewItem *Item = ParamList->selectedItem();
251  if(Item == 0) return;
252 
253  Item->setText(4, text);
254 }
255 
256 // -----------------------------------------------------------
257 void ID_Dialog::slotOk()
258 {
259  bool changed = false;
260 
261  if(!Prefix->text().isEmpty())
262  if(idText->Prefix != Prefix->text()) {
263  idText->Prefix = Prefix->text();
264  changed = true;
265  }
266 
267  QString s;
268  QListViewItem *item;
269  SubParameter *pp = idText->Parameter.first();
270  for(item = ParamList->firstChild(); item != 0; item = item->itemBelow()) {
271  s = item->text(1) + "=" + item->text(2);
272 
273  if(pp) {
274  if(pp->display != (item->text(0) == tr("yes"))) {
275  pp->display = (item->text(0) == tr("yes"));
276  changed = true;
277  }
278  if(pp->Name != s) {
279  pp->Name = s;
280  changed = true;
281  }
282  if(pp->Description != item->text(3)) {
283  pp->Description = item->text(3);
284  changed = true;
285  }
286  if(pp->Type != item->text(4)) {
287  pp->Type = item->text(4);
288  changed = true;
289  }
290  }
291  else {
292  idText->Parameter.append(new SubParameter(
293  (item->text(0) == tr("yes")) ? true : false, s, item->text(3),
294  item->text(4)));
295  changed = true;
296  }
297 
298  pp = idText->Parameter.next();
299  }
300 
301  // if more properties than in ListView -> delete the rest
302  if(pp) {
303  pp = idText->Parameter.prev();
304  idText->Parameter.last();
305  while(pp != idText->Parameter.current())
306  idText->Parameter.remove();
307  changed = true;
308  }
309 
310  if(changed) accept();
311  else reject();
312 }