From c15a7a101d4de67759c5c302bd227472cfea6dd0 Mon Sep 17 00:00:00 2001 From: Felix Salfelder Date: Thu, 18 Aug 2016 19:36:34 +0100 Subject: [PATCH] introduce symbol class. (start to) use it in qucslib --- qucs/qucs-lib/libcomp.h | 89 ++++++++++++++++++++++++++++++++ qucs/qucs-lib/qucslib.cpp | 25 ++++----- qucs/qucs-lib/qucslib.h | 2 +- qucs/qucs-lib/symbolwidget.cpp | 80 ---------------------------- qucs/qucs-lib/symbolwidget.h | 2 - qucs/qucs/components/component.h | 3 +- qucs/qucs/symbol.h | 45 ++++++++++++++++ 7 files changed, 150 insertions(+), 96 deletions(-) create mode 100644 qucs/qucs-lib/libcomp.h create mode 100644 qucs/qucs/symbol.h diff --git a/qucs/qucs-lib/libcomp.h b/qucs/qucs-lib/libcomp.h new file mode 100644 index 0000000000..e59bb372b3 --- /dev/null +++ b/qucs/qucs-lib/libcomp.h @@ -0,0 +1,89 @@ +#include "symbol.h" + +/*! + * \brief this library provides symbols. these. + */ +class QucsLibComponent : public Symbol{ + public: + QucsLibComponent( QString& , const QString&, const QString& ); + +}; + +QucsLibComponent::QucsLibComponent( QString& SymbolString, + const QString& Lib_, const QString& Comp_) + : Symbol(), LibraryName(Lib_), ComponentName(Comp_) +{ + if (SymbolString.isEmpty())//Whenever SymbolString is empty, it tries to load the default symbol + { + //Load the default symbol for the current Qucs library + ComponentLibrary parsedlib; + QString libpath = QucsSettings.LibDir + Lib_ + ".lib"; + int result = parseComponentLibrary (libpath, parsedlib); + + // BUG: throw if there is an error. don't randomly spawn Messageboxes + // (cleanup later) + switch (result)//Check if the library was properly loaded + { + case QUCS_COMP_LIB_IO_ERROR: + QMessageBox::critical(0, tr ("Error"), tr("Cannot open \"%1\".").arg (libpath)); + return -1; + case QUCS_COMP_LIB_CORRUPT: + QMessageBox::critical(0, tr("Error"), tr("Library is corrupt.")); + return -1; + default: + break; + } + + // copy the contents of default symbol section to a string + SymbolString = parsedlib.defaultSymbol; + } +} + +QucsLibComponent::draw(Qwidget& w) +{ + w.Arcs.clear(); + w.Lines.clear(); + w.Rects.clear(); + w.Ellips.clear(); + w.Texts.clear(); + + QString Line; + ///QString foo = SymbolString; + QTextStream stream(&SymbolString, QIODevice::ReadOnly); + + x1 = y1 = INT_MAX; + x2 = y2 = INT_MIN; + + int z=0, Result; + while(!stream.atEnd()) { + Line = stream.readLine(); + Line = Line.trimmed(); + if(Line.isEmpty()) continue; + + if(Line.at(0) != '<') return -1; // check for start char + if(Line.at(Line.length()-1) != '>') return -1; // check for end char + Line = Line.mid(1, Line.length()-2); // cut off start and end character + Result = analyseLine(Line); + if(Result < 0) return -6; // line format error + z += Result; + } + + x1 -= 4; // enlarge component boundings a little + x2 += 4; + y1 -= 4; + y2 += 4; + cx = -x1 + TextWidth; + cy = -y1; + + int dx = x2-x1 + TextWidth; + if((x2-x1) < DragNDropWidth) + dx = (x2-x1 + DragNDropWidth)/2 + TextWidth; + if(dx < DragNDropWidth) + dx = DragNDropWidth; + setMinimumSize(dx, y2-y1 + TextHeight+4); + if(width() > dx) dx = width(); + resize(dx, y2-y1 + TextHeight+4); + update(); + return z; // return number of ports +} + diff --git a/qucs/qucs-lib/qucslib.cpp b/qucs/qucs-lib/qucslib.cpp index f254560f15..8bf94039bb 100644 --- a/qucs/qucs-lib/qucslib.cpp +++ b/qucs/qucs-lib/qucslib.cpp @@ -136,7 +136,7 @@ QucsLib::QucsLib() CompDescr->setWordWrapMode(QTextOption::NoWrap); - Symbol = new SymbolWidget(); + symWidget = new SymbolWidget(); QGroupBox *ButtonGroup = new QGroupBox(); QHBoxLayout *ButtonGroupLayout = new QHBoxLayout(); @@ -152,7 +152,7 @@ QucsLib::QucsLib() CompGroupLayout->addWidget(CompDescr); - CompGroupLayout->addWidget(Symbol); + CompGroupLayout->addWidget(symWidget); CompGroupLayout->addWidget(ButtonGroup); CompGroup->setLayout(CompGroupLayout); @@ -283,7 +283,7 @@ void QucsLib::slotCopyToClipBoard() { QString s = "\n"; s += "\n " + - Symbol->theModel() + + symWidget->theModel() + "\n\n"; // put resulting schematic into clipboard @@ -294,7 +294,7 @@ void QucsLib::slotCopyToClipBoard() // ---------------------------------------------------- void QucsLib::slotShowModel() { - DisplayDialog *d = new DisplayDialog(this, Symbol->ModelString, Symbol->VHDLModelString, Symbol->VerilogModelString); + DisplayDialog *d = new DisplayDialog(this, symWidget->ModelString, symWidget->VHDLModelString, symWidget->VerilogModelString); d->setWindowTitle(tr("Model")); d->resize(500, 150); d->show(); @@ -487,23 +487,23 @@ void QucsLib::slotShowComponent(QListWidgetItem *Item) QMessageBox::critical(this, tr("Error"), tr("Library is corrupt.")); return; } - Symbol->ModelString = content; - if(Symbol->ModelString.count('\n') < 2) - Symbol->createSymbol(LibName, Item->text()); + symWidget->ModelString = content; + if(symWidget->ModelString.count('\n') < 2) + symWidget->createSymbol(LibName, Item->text()); if(!getSection("VHDLModel", CompString, content)) { QMessageBox::critical(this, tr("Error"), tr("Library is corrupt.")); return; } - Symbol->VHDLModelString = content; + symWidget->VHDLModelString = content; if(!getSection("VerilogModel", CompString, content)) { QMessageBox::critical(this, tr("Error"), tr("Library is corrupt.")); return; } - Symbol->VerilogModelString = content; + symWidget->VerilogModelString = content; if(!getSection("Symbol", CompString, content)) { @@ -511,10 +511,11 @@ void QucsLib::slotShowComponent(QListWidgetItem *Item) return; } + // it's a bit late, but without a symbol we cannot draw a symbol to the + // widget... lets create a symbol! + Symbol compSym(content, LibName, Item->text()); + compSym.draw(symWidget); - Symbol->setSymbol(content, LibName, Item->text()); - - // change currently selected category, so the user will // learn where the component comes from Library->setCurrentIndex(i); diff --git a/qucs/qucs-lib/qucslib.h b/qucs/qucs-lib/qucslib.h index 1c3f828a44..61b819a259 100644 --- a/qucs/qucs-lib/qucslib.h +++ b/qucs/qucs-lib/qucslib.h @@ -78,7 +78,7 @@ private slots: int UserLibCount; int libCurIdx; - SymbolWidget *Symbol; + SymbolWidget *symWidget; QTextEdit *CompDescr; QVBoxLayout *all; QLineEdit *CompSearch; diff --git a/qucs/qucs-lib/symbolwidget.cpp b/qucs/qucs-lib/symbolwidget.cpp index 4e842c1e4a..c54cb367f3 100644 --- a/qucs/qucs-lib/symbolwidget.cpp +++ b/qucs/qucs-lib/symbolwidget.cpp @@ -350,86 +350,6 @@ int SymbolWidget::createSymbol(const QString& Lib_, const QString& Comp_) return PortNo; } -/*! - * \brief Loads the symbol for the component from the symbol field - * \param SymbolString - * \param Lib_ - * \param Comp_ - * \return the number of painting elements or a negative nuber if error - */ -int SymbolWidget::setSymbol( QString& SymbolString, - const QString& Lib_, const QString& Comp_) -{ - if (SymbolString.isEmpty())//Whenever SymbolString is empty, it tries to load the default symbol - { - //Load the default symbol for the current Qucs library - ComponentLibrary parsedlib; - QString libpath = QucsSettings.LibDir + Lib_ + ".lib"; - int result = parseComponentLibrary (libpath, parsedlib); - - switch (result)//Check if the library was properly loaded - { - case QUCS_COMP_LIB_IO_ERROR: - QMessageBox::critical(0, tr ("Error"), tr("Cannot open \"%1\".").arg (libpath)); - return -1; - case QUCS_COMP_LIB_CORRUPT: - QMessageBox::critical(0, tr("Error"), tr("Library is corrupt.")); - return -1; - default: - break; - } - - // copy the contents of default symbol section to a string - SymbolString = parsedlib.defaultSymbol; - } - - Arcs.clear(); - Lines.clear(); - Rects.clear(); - Ellips.clear(); - Texts.clear(); - LibraryName = Lib_; - ComponentName = Comp_; - - QString Line; - ///QString foo = SymbolString; - QTextStream stream(&SymbolString, QIODevice::ReadOnly); - - x1 = y1 = INT_MAX; - x2 = y2 = INT_MIN; - - int z=0, Result; - while(!stream.atEnd()) { - Line = stream.readLine(); - Line = Line.trimmed(); - if(Line.isEmpty()) continue; - - if(Line.at(0) != '<') return -1; // check for start char - if(Line.at(Line.length()-1) != '>') return -1; // check for end char - Line = Line.mid(1, Line.length()-2); // cut off start and end character - Result = analyseLine(Line); - if(Result < 0) return -6; // line format error - z += Result; - } - - x1 -= 4; // enlarge component boundings a little - x2 += 4; - y1 -= 4; - y2 += 4; - cx = -x1 + TextWidth; - cy = -y1; - - int dx = x2-x1 + TextWidth; - if((x2-x1) < DragNDropWidth) - dx = (x2-x1 + DragNDropWidth)/2 + TextWidth; - if(dx < DragNDropWidth) - dx = DragNDropWidth; - setMinimumSize(dx, y2-y1 + TextHeight+4); - if(width() > dx) dx = width(); - resize(dx, y2-y1 + TextHeight+4); - update(); - return z; // return number of ports -} // --------------------------------------------------------------------- int SymbolWidget::analyseLine(const QString& Row) diff --git a/qucs/qucs-lib/symbolwidget.h b/qucs/qucs-lib/symbolwidget.h index f1479c0cf5..23e2c1340b 100644 --- a/qucs/qucs-lib/symbolwidget.h +++ b/qucs/qucs-lib/symbolwidget.h @@ -29,8 +29,6 @@ #include #include -#include "element.h" - /*! * \file symbolwidget.h * \brief Definition of the SymbolWidget class. diff --git a/qucs/qucs/components/component.h b/qucs/qucs/components/component.h index d18757d6fc..d4a5c1289b 100644 --- a/qucs/qucs/components/component.h +++ b/qucs/qucs/components/component.h @@ -21,6 +21,7 @@ #include #include "element.h" +// #include "symbol.h" not yet. class Schematic; class ViewPainter; @@ -28,7 +29,7 @@ class QString; class QPen; -class Component : public Element { +class Component : public Element /* public Symbol */ { public: Component(); virtual ~Component() {}; diff --git a/qucs/qucs/symbol.h b/qucs/qucs/symbol.h new file mode 100644 index 0000000000..3d7d41b847 --- /dev/null +++ b/qucs/qucs/symbol.h @@ -0,0 +1,45 @@ +/*************************************************************************** + element.h + ----------- + begin : 2016 + copyright : (C) 2016 Felix Salfelder + email : felix@salfelder.org + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +/** \file symbol.h + * \brief Defines symbols for components, to be used in schematics. + * + * a symbol mostly takes care of ports, parameters and connectivity. + * (this is under construction) + * + */ + +#ifndef SYMBOL_H +#define SYMBOL_H + +/** \class Symbol + * \brief Superclass of all circuit components (except wires). + * + * + */ +class Symbol { +public: // construct + Symbol(); + virtual ~Symbol(){} + +public: // interface + virtual void draw(Widget&) = 0; + //... more to come +}; + + +#endif