Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/components/vacomponent.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                             vacomponent.cpp
00003                             ---------------
00004     begin                : Thur Feb 21 2014
00005     copyright            : (C) 2014 by Guilherme Brondani Torri
00006     email                : guitorri AT gmail DOT com
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 
00018 
00019 #include "vacomponent.h"
00020 
00021 #include <QString>
00022 #include <QScriptEngine>
00023 #include <QScriptValue>
00024 #include <QScriptValueIterator>
00025 #include <QFile>
00026 #include <QTextStream>
00027 #include <QMessageBox>
00028 
00029 
00040 vacomponent::vacomponent(QString filename)
00041 {
00042   QString data = getData(filename);
00043 
00046   QScriptEngine engine;
00047   QScriptValue vadata = engine.evaluate("(" + data + ")");
00048 
00049   Description = getString(vadata, "description");
00050 
00051   QScriptValue entries = vadata.property("property");
00052 
00053   QScriptValueIterator it(entries);
00054 
00055   while (it.hasNext()) {
00056     it.next();
00057 
00058     QScriptValue entry = it.value();
00059 
00060     // skip length named iterate
00061     if (it.name().compare("length")) {
00062       QString name = getString(entry, "name");
00063       QString value = getString(entry, "value");
00064       QString display = getString(entry, "display");
00065       QString desc = getString(entry, "desc");
00066       // QString unit = getString(entry, "unit");
00067 
00069 
00070       bool show;
00071       if (!display.compare("false"))
00072         show = false;
00073       else
00074         show = true;
00075 
00077 
00078       Props.append (new Property (name, value, show, desc));
00079     }
00080   }
00081 
00082   createSymbol(filename);
00083 
00084   Model = getString(vadata, "Model");
00085   Name  = getString(vadata, "SymName");
00086 
00088   tx = x1+100;
00089   ty = y1+20;
00090 }
00091 
00098 Component *vacomponent::newOne(QString filename)
00099 {
00100   vacomponent * p = new vacomponent(filename);
00101   if (Props.count())
00102       p->Props.getFirst()->Value = Props.getFirst()->Value;
00103   p->recreate(0);
00104   return p;
00105 
00106 }
00107 
00118 Element *vacomponent::info(QString &Name, QString &BitmapFile,
00119                            bool getNewOne, QString filename)
00120 {
00121   // get variables out of file
00122   QString data = getData(filename);
00123 
00124   QScriptEngine engine;
00125   QScriptValue vadata = engine.evaluate("(" + data + ")");
00126 
00127   Name  = getString(vadata, "Model");
00128 
00131   BitmapFile  = getString(vadata, "BitmapFile");
00132 
00133   if(getNewOne) return new vacomponent(filename);
00134   return 0;
00135 }
00136 
00143 void vacomponent::createSymbol(QString filename)
00144 {
00145   // map string to Qt Pen/Brush
00146   // any better to convert QString into Qt::BrushStyle ?
00147   QMap<QString, Qt::BrushStyle> brushMap;
00148   QMap<QString, Qt::PenStyle> penMap;
00149 
00150   brushMap.insert("Qt::NoBrush",          Qt::NoBrush);
00151   brushMap.insert("Qt::SolidPattern",     Qt::SolidPattern);
00152   brushMap.insert("Qt::Dense1Pattern",    Qt::Dense1Pattern);
00153   brushMap.insert("Qt::Dense2Pattern",    Qt::Dense2Pattern);
00154   brushMap.insert("Qt::Dense3Pattern",    Qt::Dense3Pattern);
00155   brushMap.insert("Qt::Dense4Pattern",    Qt::Dense4Pattern);
00156   brushMap.insert("Qt::Dense5Pattern",    Qt::Dense5Pattern);
00157   brushMap.insert("Qt::Dense6Pattern",    Qt::Dense6Pattern);
00158   brushMap.insert("Qt::Dense7Pattern",    Qt::Dense7Pattern);
00159   brushMap.insert("Qt::HorPattern",       Qt::HorPattern);
00160   brushMap.insert("Qt::VerPattern",       Qt::VerPattern);
00161   brushMap.insert("Qt::CrossPattern",     Qt::CrossPattern);
00162   brushMap.insert("Qt::BDiagPattern",     Qt::BDiagPattern);
00163   brushMap.insert("Qt::FDiagPattern",     Qt::FDiagPattern);
00164   brushMap.insert("Qt::DiagCrossPattern", Qt::DiagCrossPattern);
00165 
00166   penMap.insert("Qt::NoPen", Qt::NoPen);
00167   penMap.insert("Qt::SolidLine", Qt::SolidLine);
00168   penMap.insert("Qt::DashLine", Qt::DashLine);
00169   penMap.insert("Qt::DotLine", Qt::DotLine);
00170   penMap.insert("Qt::DashDotLine", Qt::DashDotLine);
00171   penMap.insert("Qt::DashDotDotLine", Qt::DashDotDotLine);
00172   penMap.insert("Qt::CustomDashLine", Qt::CustomDashLine);
00173 
00174 
00175   QString data = getData(filename);
00176 
00177   QScriptEngine engine;
00178   QScriptValue vadata = engine.evaluate("(" + data + ")");
00179 
00180   // get array of symbol paintigs
00181   QScriptValue entries = vadata.property("paintings");
00182 
00183   QScriptValueIterator it(entries);
00184   while (it.hasNext()) {
00185     it.next();
00186 
00187     qreal x, x1, x2, y, y1, y2, w, h, thick, angle, arclen;
00188     qreal size, cos, sin;
00189     QString color, style    , colorfill, stylefill, s;
00190 
00191     QScriptValue entry = it.value();
00192     QString type = getString(entry, "type");
00193 
00194     if (!type.compare("line")) {
00195       x1 = getDouble(entry, "x1");
00196       y1 = getDouble(entry, "y1");
00197       x2 = getDouble(entry, "x2");
00198       y2 = getDouble(entry, "y2");
00199       color = getString(entry, "color");
00200       thick = getDouble(entry, "thick");
00201       style = getString(entry, "style");
00202 
00203       Lines.append (new Line (x1, y1, x2, y2,
00204                         QPen (QColor (color), thick, penMap.value(style))));
00205     }
00206 
00207     if (!type.compare("rectangle")) {
00208       x = getDouble(entry, "x");
00209       y = getDouble(entry, "y");
00210       w = getDouble(entry, "w");
00211       h = getDouble(entry, "h");
00212       color = getString(entry, "color");
00213       thick = getDouble(entry, "thick");
00214       style = getString(entry, "style");
00215       colorfill = getString(entry, "colorfill");
00216       stylefill = getString(entry, "stylefill");
00217 
00218       Rects.append (new Area (x, y, w, h,
00219                         QPen (QColor (color), thick, penMap.value(style)),
00220                         QBrush(QColor (colorfill), brushMap.value(stylefill))
00221                         ));
00222     }
00223 
00224     if (!type.compare("ellipse")) {
00225       x = getDouble(entry, "x");
00226       y = getDouble(entry, "y");
00227       w = getDouble(entry, "w");
00228       h = getDouble(entry, "h");
00229       color = getString(entry, "color");
00230       thick = getDouble(entry, "thick");
00231       style = getString(entry, "style");
00232       colorfill = getString(entry, "colorfill");
00233       stylefill = getString(entry, "stylefill");
00234 
00235       Ellips.append (new Area (x, y, w, h,
00236                          QPen (QColor (color), thick, penMap.value(style)),
00237                          QBrush(QColor (colorfill), brushMap.value(stylefill))
00238                          ));
00239     }
00240 
00241     if (!type.compare("ellipsearc")) {
00242       x = getDouble(entry, "x");
00243       y = getDouble(entry, "y");
00244       w = getDouble(entry, "w");
00245       h = getDouble(entry, "h");
00246       angle = getDouble(entry, "angle");
00247       arclen = getDouble(entry, "arclen");
00248       color = getString(entry, "color");
00249       thick = getDouble(entry, "thick");
00250       style = getString(entry, "style");
00251 
00252       Arcs.append (new Arc (x, y, w, h, angle, arclen,
00253                        QPen (QColor (color), thick, penMap.value(style))));
00254     }
00255 
00256     if (!type.compare("portsymbol")) {
00257       x = getDouble(entry, "x");
00258       y = getDouble(entry, "y");
00259       Ports.append (new Port (x, y));
00260     }
00261 
00262     if (!type.compare("graphictext")) {
00263       x = getDouble(entry, "x");
00264       y = getDouble(entry, "y");
00265       s = getString(entry, "s");
00266       color = getString(entry, "color");
00267       size = getDouble(entry, "size");
00268       cos = getDouble(entry, "cos");
00269       sin = getDouble(entry, "sin");
00270       Texts.append (new Text (x, y, s,
00271                               QColor (color), size, cos, sin));
00272     }
00273 
00274     if (!type.compare("arrow")) {
00275       x1 = getDouble(entry, "x1");
00276       y1 = getDouble(entry, "y1");
00277       x2 = getDouble(entry, "x2");
00278       y2 = getDouble(entry, "y2");
00279       color = getString(entry, "color");
00280       thick = getDouble(entry, "thick");
00281       style = getString(entry, "style");
00282       Lines.append (new Line (x1, y1, x2, y2,
00283                         QPen (QColor (color), thick, penMap.value(style))));
00284      }
00285   }
00286 
00287   // bounding box, painted gray if component selected
00288   x1 = getDouble(vadata, "x1");
00289   y1 = getDouble(vadata, "y1");
00290   x2 = getDouble(vadata, "x2");
00291   y2 = getDouble(vadata, "y2");
00292 }
00293 
00294 
00295 // Move this elsewhere?
00301 QString getData(QString filename)
00302 {
00303   // Try to open the JSON file
00304   QFile file(filename);
00305   if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
00306     QMessageBox::critical(0, QObject::tr("Error"),
00307                           QObject::tr("Symbol file not found: %1").arg(filename));
00308     return "";
00309   }
00310 
00311   // Stream-in the file
00312   QTextStream in(&file);
00313 
00314   // Put into a string
00315   QString data = (QString) in.readAll();
00316   // close
00317   file.close();
00318 
00319   return data;
00320 }
00321 
00328 double getDouble(QScriptValue data, QString prop){
00329   return data.property(prop).toString().toDouble();
00330 }
00331 
00338 QString getString(QScriptValue data, QString prop){
00339   return data.property(prop).toString();
00340 }
00341 
00342 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines