QSapecNG
 All Classes Functions Enumerations Properties
schematicsceneparser.h
00001 /*
00002     QSapecNG - Qt based SapecNG GUI front-end
00003     Copyright (C) 2009, Michele Caini
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 
00020 #ifndef SCHEMATICSCENEPARSER_H
00021 #define SCHEMATICSCENEPARSER_H
00022 
00023 
00024 #include "parser/parser.h"
00025 #include "gui/editor/schematicscene.h"
00026 #include "gui/editor/item.h"
00027 
00028 #include <QtCore/QPair>
00029 #include <QtCore/QStack>
00030 #include <QtCore/QPointF>
00031 #include <QtCore/QList>
00032 
00033 #include <vector>
00034 #include <map>
00035 
00036 
00037 class QGraphicsItem;
00038 
00039 
00040 namespace qsapecng
00041 {
00042 
00043 
00044 class SchematicSceneParser: public sapecng::abstract_parser
00045 {
00046 
00047 public:
00048   SchematicSceneParser(const SchematicScene& scene);
00049   SchematicSceneParser(
00050       const SchematicScene& scene,
00051       const QList<QGraphicsItem*>& items
00052     );
00053   SchematicSceneParser(
00054       const SchematicScene& scene,
00055       QGraphicsItem* gItem
00056     );
00057 
00058   void parse(sapecng::abstract_builder& builder);
00059 
00060 private:
00061   void storeItemData(
00062       std::map<std::string,std::string>& props,
00063       SchematicScene::SupportedItemType type,
00064       Item* item
00065     );
00066   void storeLabel(
00067       std::map<std::string,std::string>& props,
00068       Component* component
00069     );
00070   void setupMap();
00071   void parse_item(
00072     sapecng::abstract_builder& builder,
00073     Item* item
00074   );
00075 
00076 private:
00077   QMap<SchematicScene::SupportedItemType,
00078     sapecng::abstract_builder::dual_component_type> dualMap_;
00079   QMap<SchematicScene::SupportedItemType,
00080     sapecng::abstract_builder::quad_component_type> quadMap_;
00081 
00082   QList<Item*> items_;
00083   int discard_;
00084 
00085 };
00086 
00087 
00088 
00089 class SchematicSceneBuilder: public sapecng::abstract_builder
00090 {
00091 
00092 public:
00093   SchematicSceneBuilder(SchematicScene& scene)
00094     : scene_(&scene), offset_(QPointF(0, 0)) { }
00095 
00096   SchematicSceneBuilder(SchematicScene& scene, const QPointF& offset)
00097     : scene_(&scene), offset_(offset) { }
00098 
00099   void add_circuit_properties(std::map<std::string,std::string> map);
00100   void add_circuit_property(std::string name, std::string value);
00101 
00102   void add_wire_component(
00103       std::map<std::string,std::string> props =
00104         std::map<std::string,std::string>()
00105     );
00106 
00107   void add_out_component(
00108       unsigned int v,
00109       std::map<std::string,std::string> props =
00110         std::map<std::string,std::string>()
00111     );
00112 
00113   void add_dual_component(
00114       sapecng::abstract_builder::dual_component_type c_type,
00115       std::string name,
00116       double value,
00117       bool symbolic,
00118       unsigned int va,
00119       unsigned int vb,
00120       std::map<std::string,std::string> props =
00121         std::map<std::string,std::string>()
00122     );
00123 
00124   void add_quad_component(
00125       sapecng::abstract_builder::quad_component_type c_type,
00126       std::string name,
00127       double value,
00128       bool symbolic,
00129       unsigned int va,
00130       unsigned int vb,
00131       unsigned int vac,
00132       unsigned int vbc,
00133       std::map<std::string,std::string> props =
00134         std::map<std::string,std::string>()
00135     );
00136 
00137   void add_unknow_component(
00138       std::map<std::string,std::string> props =
00139         std::map<std::string,std::string>()
00140     );
00141 
00142   void begin_userdef_component(
00143       std::string name,
00144       std::map<std::string,std::string> props =
00145         std::map<std::string,std::string>()
00146     );
00147 
00148   void end_userdef_component(
00149       std::string name,
00150       std::map<std::string,std::string> props =
00151         std::map<std::string,std::string>()
00152     );
00153 
00154   void flush();
00155 
00156   inline QList<QGraphicsItem*> items() const { return items_; }
00157 
00158 private:
00159   bool discard(std::map<std::string,std::string> props);
00160   void grid_coordinate(Item* item, std::map<std::string,std::string> props);
00161   void mirror_and_rotate(Item* item, std::map<std::string,std::string> props);
00162   void adjust_label(Component* cmp, std::map<std::string,std::string> props);
00163   void setup_properties(
00164       Item* item,
00165       std::string name,
00166       double value,
00167       bool symbolic,
00168       std::map<std::string,std::string> props
00169     );
00170 
00171   QStack< QPair< SchematicScene*, QList<QGraphicsItem*> > > stack_;
00172   QList<QGraphicsItem*> items_;
00173   SchematicScene* scene_;
00174   QPointF offset_;
00175 
00176 };
00177 
00178 
00179 
00180 }
00181 
00182 
00183 #endif // SCHEMATICSCENEPARSER_H
 All Classes Functions Enumerations Properties