Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vacomponent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  vacomponent.cpp
3  ---------------
4  begin : Thur Feb 21 2014
5  copyright : (C) 2014 by Guilherme Brondani Torri
6  email : guitorri AT gmail DOT com
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 <QtGui>
19 
20 #include "vacomponent.h"
21 
32 vacomponent::vacomponent(QString filename)
33 {
34  QString data = getData(filename);
35 
38  QScriptEngine engine;
39  QScriptValue vadata = engine.evaluate("(" + data + ")");
40 
41  Description = getString(vadata, "description");
42 
43  QScriptValue entries = vadata.property("property");
44 
45  QScriptValueIterator it(entries);
46 
47  while (it.hasNext()) {
48  it.next();
49 
50  QScriptValue entry = it.value();
51 
52  // skip length named iterate
53  if (it.name().compare("length")) {
54  QString name = getString(entry, "name");
55  QString value = getString(entry, "value");
56  QString display = getString(entry, "display");
57  QString desc = getString(entry, "desc");
58  // QString unit = getString(entry, "unit");
59 
61 
62  bool show;
63  if (!display.compare("false"))
64  show = false;
65  else
66  show = true;
67 
69 
70  Props.append (new Property (name, value, show, desc));
71  }
72  }
73 
74  createSymbol(filename);
75 
76  Model = getString(vadata, "Model");
77  Name = getString(vadata, "SymName");
78 
80  tx = x1+100;
81  ty = y1+20;
82 }
83 
90 Component *vacomponent::newOne(QString filename)
91 {
92  vacomponent * p = new vacomponent(filename);
93  if (Props.count())
94  p->Props.getFirst()->Value = Props.getFirst()->Value;
95  p->recreate(0);
96  return p;
97 
98 }
99 
110 Element *vacomponent::info(QString &Name, QString &BitmapFile,
111  bool getNewOne, QString filename)
112 {
113  // get variables out of file
114  QString data = getData(filename);
115 
116  QScriptEngine engine;
117  QScriptValue vadata = engine.evaluate("(" + data + ")");
118 
119  Name = getString(vadata, "Model");
120 
123  BitmapFile = getString(vadata, "BitmapFile");
124 
125  if(getNewOne) return new vacomponent(filename);
126  return 0;
127 }
128 
135 void vacomponent::createSymbol(QString filename)
136 {
137  // map string to Qt Pen/Brush
138  // any better to convert QString into Qt::BrushStyle ?
139  QMap<QString, Qt::BrushStyle> brushMap;
140  QMap<QString, Qt::PenStyle> penMap;
141 
142  brushMap.insert("Qt::NoBrush", Qt::NoBrush);
143  brushMap.insert("Qt::SolidPattern", Qt::SolidPattern);
144  brushMap.insert("Qt::Dense1Pattern", Qt::Dense1Pattern);
145  brushMap.insert("Qt::Dense2Pattern", Qt::Dense2Pattern);
146  brushMap.insert("Qt::Dense3Pattern", Qt::Dense3Pattern);
147  brushMap.insert("Qt::Dense4Pattern", Qt::Dense4Pattern);
148  brushMap.insert("Qt::Dense5Pattern", Qt::Dense5Pattern);
149  brushMap.insert("Qt::Dense6Pattern", Qt::Dense6Pattern);
150  brushMap.insert("Qt::Dense7Pattern", Qt::Dense7Pattern);
151  brushMap.insert("Qt::HorPattern", Qt::HorPattern);
152  brushMap.insert("Qt::VerPattern", Qt::VerPattern);
153  brushMap.insert("Qt::CrossPattern", Qt::CrossPattern);
154  brushMap.insert("Qt::BDiagPattern", Qt::BDiagPattern);
155  brushMap.insert("Qt::FDiagPattern", Qt::FDiagPattern);
156  brushMap.insert("Qt::DiagCrossPattern", Qt::DiagCrossPattern);
157 
158  penMap.insert("Qt::NoPen", Qt::NoPen);
159  penMap.insert("Qt::SolidLine", Qt::SolidLine);
160  penMap.insert("Qt::DashLine", Qt::DashLine);
161  penMap.insert("Qt::DotLine", Qt::DotLine);
162  penMap.insert("Qt::DashDotLine", Qt::DashDotLine);
163  penMap.insert("Qt::DashDotDotLine", Qt::DashDotDotLine);
164  penMap.insert("Qt::CustomDashLine", Qt::CustomDashLine);
165 
166 
167  QString data = getData(filename);
168 
169  QScriptEngine engine;
170  QScriptValue vadata = engine.evaluate("(" + data + ")");
171 
172  // get array of symbol paintigs
173  QScriptValue entries = vadata.property("paintings");
174 
175  QScriptValueIterator it(entries);
176  while (it.hasNext()) {
177  it.next();
178 
179  qreal x, x1, x2, y, y1, y2, w, h, thick, angle, arclen;
180  qreal size, cos, sin;
181  QString color, style , colorfill, stylefill, s;
182 
183  QScriptValue entry = it.value();
184  QString type = getString(entry, "type");
185 
186  if (!type.compare("line")) {
187  x1 = getDouble(entry, "x1");
188  y1 = getDouble(entry, "y1");
189  x2 = getDouble(entry, "x2");
190  y2 = getDouble(entry, "y2");
191  color = getString(entry, "color");
192  thick = getDouble(entry, "thick");
193  style = getString(entry, "style");
194 
195  Lines.append (new Line (x1, y1, x2, y2,
196  QPen (QColor (color), thick, penMap.value(style))));
197  }
198 
199  if (!type.compare("rectangle")) {
200  x = getDouble(entry, "x");
201  y = getDouble(entry, "y");
202  w = getDouble(entry, "w");
203  h = getDouble(entry, "h");
204  color = getString(entry, "color");
205  thick = getDouble(entry, "thick");
206  style = getString(entry, "style");
207  colorfill = getString(entry, "colorfill");
208  stylefill = getString(entry, "stylefill");
209 
210  Rects.append (new Area (x, y, w, h,
211  QPen (QColor (color), thick, penMap.value(style)),
212  QBrush(QColor (colorfill), brushMap.value(stylefill))
213  ));
214  }
215 
216  if (!type.compare("ellipse")) {
217  x = getDouble(entry, "x");
218  y = getDouble(entry, "y");
219  w = getDouble(entry, "w");
220  h = getDouble(entry, "h");
221  color = getString(entry, "color");
222  thick = getDouble(entry, "thick");
223  style = getString(entry, "style");
224  colorfill = getString(entry, "colorfill");
225  stylefill = getString(entry, "stylefill");
226 
227  Ellips.append (new Area (x, y, w, h,
228  QPen (QColor (color), thick, penMap.value(style)),
229  QBrush(QColor (colorfill), brushMap.value(stylefill))
230  ));
231  }
232 
233  if (!type.compare("ellipsearc")) {
234  x = getDouble(entry, "x");
235  y = getDouble(entry, "y");
236  w = getDouble(entry, "w");
237  h = getDouble(entry, "h");
238  angle = getDouble(entry, "angle");
239  arclen = getDouble(entry, "arclen");
240  color = getString(entry, "color");
241  thick = getDouble(entry, "thick");
242  style = getString(entry, "style");
243 
244  Arcs.append (new Arc (x, y, w, h, angle, arclen,
245  QPen (QColor (color), thick, penMap.value(style))));
246  }
247 
248  if (!type.compare("portsymbol")) {
249  x = getDouble(entry, "x");
250  y = getDouble(entry, "y");
251  Ports.append (new Port (x, y));
252  }
253 
254  if (!type.compare("graphictext")) {
255  x = getDouble(entry, "x");
256  y = getDouble(entry, "y");
257  s = getString(entry, "s");
258  color = getString(entry, "color");
259  size = getDouble(entry, "size");
260  cos = getDouble(entry, "cos");
261  sin = getDouble(entry, "sin");
262  Texts.append (new Text (x, y, s,
263  QColor (color), size, cos, sin));
264  }
265 
266  if (!type.compare("arrow")) {
267  x1 = getDouble(entry, "x1");
268  y1 = getDouble(entry, "y1");
269  x2 = getDouble(entry, "x2");
270  y2 = getDouble(entry, "y2");
271  color = getString(entry, "color");
272  thick = getDouble(entry, "thick");
273  style = getString(entry, "style");
274  Lines.append (new Line (x1, y1, x2, y2,
275  QPen (QColor (color), thick, penMap.value(style))));
276  }
277  }
278 
279  // bounding box, painted gray if component selected
280  x1 = getDouble(vadata, "x1");
281  y1 = getDouble(vadata, "y1");
282  x2 = getDouble(vadata, "x2");
283  y2 = getDouble(vadata, "y2");
284 }
285 
286 
287 // Move this elsewhere?
293 QString getData(QString filename)
294 {
295  // Try to open the JSON file
296  QFile file(filename);
297  if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
298  QMessageBox::critical(0, QObject::tr("Error"),
299  QObject::tr("Symbol file not found: %1").arg(filename));
300  return "";
301  }
302 
303  // Stream-in the file
304  QTextStream in(&file);
305 
306  // Put into a string
307  QString data = (QString) in.readAll();
308  // close
309  file.close();
310 
311  return data;
312 }
313 
320 double getDouble(QScriptValue data, QString prop){
321  return data.property(prop).toString().toDouble();
322 }
323 
330 QString getString(QScriptValue data, QString prop){
331  return data.property(prop).toString();
332 }
333 
334 
Q3PtrList< Line > Lines
Definition: component.h:67
virtual Component * newOne()
Definition: component.cpp:70
Definition: element.h:55
int y1
Definition: element.h:153
Q3PtrList< struct Arc > Arcs
Definition: component.h:68
Q3PtrList< Area > Rects
Definition: component.h:69
double getDouble(QScriptValue data, QString prop)
getDouble Helper to get a property out of a JSON script
void createSymbol(QString filename)
vacomponent::createSymbol Constructor call this to create the symbol.
int tx
Definition: component.h:78
int y2
Definition: element.h:153
Definition: element.h:72
int x1
Definition: element.h:153
int ty
Definition: component.h:78
Q3PtrList< Property > Props
Definition: component.h:72
QString getData(QString filename)
getData Reads the JSON file
Definition: element.h:63
Definition: element.h:82
static Element * info(QString &, QString &, bool getNewOne=false, QString filename="")
vacomponent::info is used to either get information or create objects.
Definition: element.h:48
Superclass of all schematic drawing elements.
Definition: element.h:142
Q3PtrList< Port > Ports
Definition: component.h:70
vacomponent(QString filename)
vacomponent::vacomponent
Definition: vacomponent.cpp:32
QString Name
Definition: component.h:80
Q3PtrList< Text > Texts
Definition: component.h:71
QString Model
Definition: component.h:80
QString getString(QScriptValue data, QString prop)
getString Helper to get a property out of a JSON script
QString Description
Definition: component.h:81
Definition of the vacomponent class.
Q3PtrList< Area > Ellips
Definition: component.h:69
int x2
Definition: element.h:153
virtual void recreate(Schematic *)
Definition: component.h:39