File indexing completed on 2024-12-01 05:13:04

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef ITEMDOCUMENTDATA_H
0012 #define ITEMDOCUMENTDATA_H
0013 
0014 #include "item.h"
0015 #include "microsettings.h"
0016 
0017 #include <QDomDocument>
0018 #include <QDomElement>
0019 
0020 class Connector;
0021 class ECSubcircuit;
0022 class QUrl;
0023 class Node;
0024 class PinMapping;
0025 
0026 typedef QList<QPointer<Connector>> ConnectorList;
0027 typedef QList<QPointer<Item>> ItemList;
0028 typedef QList<QPointer<Node>> NodeList;
0029 typedef QMap<QString, PinMapping> PinMappingMap;
0030 
0031 typedef QList<QPoint> QPointList;
0032 typedef QMap<QString, bool> BoolMap;
0033 typedef QMap<QString, double> DoubleMap;
0034 typedef QMap<QString, int> IntMap;
0035 typedef QMap<QString, QColor> QColorMap;
0036 typedef QMap<QString, QString> QStringMap;
0037 typedef QMap<QString, QBitArray> QBitArrayMap;
0038 
0039 class ItemData
0040 {
0041 public:
0042     ItemData();
0043 
0044     QString type;
0045     double x;
0046     double y;
0047     int z;
0048     QRect size;
0049     bool setSize;
0050     int orientation; // used for flowparts, should be set to -1 if not used.
0051     double angleDegrees;
0052     bool flipped;
0053     BoolMap buttonMap;
0054     IntMap sliderMap;
0055     QString parentId;
0056     BoolMap dataBool;
0057     DoubleMap dataNumber;
0058     QColorMap dataColor;
0059     QStringMap dataString;
0060     QBitArrayMap dataRaw;
0061 };
0062 typedef QMap<QString, ItemData> ItemDataMap;
0063 
0064 class ConnectorData
0065 {
0066 public:
0067     ConnectorData();
0068 
0069     QPointList route;
0070     bool manualRoute;
0071 
0072     bool startNodeIsChild;
0073     bool endNodeIsChild;
0074 
0075     QString startNodeCId;
0076     QString endNodeCId;
0077 
0078     QString startNodeParent;
0079     QString endNodeParent;
0080 
0081     QString startNodeId;
0082     QString endNodeId;
0083 };
0084 typedef QMap<QString, ConnectorData> ConnectorDataMap;
0085 
0086 class NodeData
0087 {
0088 public:
0089     NodeData();
0090 
0091     double x;
0092     double y;
0093 };
0094 typedef QMap<QString, NodeData> NodeDataMap;
0095 
0096 class PinData
0097 {
0098 public:
0099     PinData();
0100 
0101     PinSettings::pin_type type;
0102     PinSettings::pin_state state;
0103 };
0104 typedef QMap<QString, PinData> PinDataMap;
0105 
0106 class MicroData
0107 {
0108 public:
0109     MicroData();
0110     void reset();
0111 
0112     QString id;
0113     PinDataMap pinMap;
0114     QStringMap variableMap;
0115     PinMappingMap pinMappings;
0116 };
0117 
0118 /**
0119 This class encapsulates all or part of an ItemDocument. It is used for writing
0120 the document to file / reading from file, as well as for the clipboard and
0121 undo/redo system.
0122 @author David Saxton
0123 */
0124 class ItemDocumentData
0125 {
0126 public:
0127     ItemDocumentData(uint documentType);
0128     ~ItemDocumentData();
0129     /**
0130      * Erases / resets all data to defaults
0131      */
0132     void reset();
0133     /**
0134      * Read in data from a saved file. Any existing data in this class will
0135      * be deleted first.
0136      * @returns true iff successful
0137      */
0138     bool loadData(const QUrl &url);
0139     /**
0140      * Write the data to the given file.
0141      * @returns true iff successful
0142      */
0143     bool saveData(const QUrl &url);
0144     /**
0145      * Returns the xml used for describing the data
0146      */
0147     QString toXML();
0148     /**
0149      * Restore the document from the given xml
0150      * @return true if successful
0151      */
0152     bool fromXML(const QString &xml);
0153     /**
0154      * Saves the document to the data
0155      */
0156     void saveDocumentState(ItemDocument *itemDocument);
0157     /**
0158      * Restores a document to the state stored in this class
0159      */
0160     void restoreDocument(ItemDocument *itemDocument);
0161     /**
0162      * Merges the stuff stored here with the given document. If this is
0163      * being used for e.g. pasting, you should call generateUniqueIDs()
0164      * @param selectNew if true then the newly created items & connectors will be selected
0165      */
0166     void mergeWithDocument(ItemDocument *itemDocument, bool selectNew);
0167     /**
0168      * Replaces the IDs of everything with unique ones for the document.
0169      * Used in pasting.
0170      */
0171     void generateUniqueIDs(ItemDocument *itemDocument);
0172     /**
0173      * Move all the items, connectors, nodes, etc by the given amount
0174      */
0175     void translateContents(int dx, int dy);
0176     /**
0177      * Returns the document type.
0178      * @see Document::DocumentType
0179      */
0180     uint documentType() const
0181     {
0182         return m_documentType;
0183     }
0184 
0185     // BEGIN functions for adding data
0186     void setMicroData(const MicroData &data);
0187     void addItems(const ItemList &itemList);
0188     void addConnectors(const ConnectorList &connectorList);
0189     void addNodes(const NodeList &nodeList);
0190 
0191     /**
0192      * Add the given ItemData to the stored data
0193      */
0194     void addItemData(ItemData itemData, QString id);
0195     /**
0196      * Add the given ConnectorData to the stored data
0197      */
0198     void addConnectorData(ConnectorData connectorData, QString id);
0199     /**
0200      * Add the given NodeData to the stored data
0201      */
0202     void addNodeData(NodeData nodeData, QString id);
0203     // END functions for adding data
0204 
0205     // BEGIN functions for returning strings for saving to xml
0206     QString documentTypeString() const;
0207     QString revisionString() const;
0208     // END functions for returning strings for saving to xml
0209 
0210 protected:
0211     // BEGIN functions for generating QDomElements
0212     QDomElement microDataToElement(QDomDocument &doc);
0213     QDomElement itemDataToElement(QDomDocument &doc, const ItemData &itemData);
0214     QDomElement nodeDataToElement(QDomDocument &doc, const NodeData &nodeData);
0215     QDomElement connectorDataToElement(QDomDocument &doc, const ConnectorData &connectorData);
0216     // END functions for generating QDomElements
0217 
0218     // BEGIN functions for reading QDomElements to stored data
0219     void elementToMicroData(QDomElement element);
0220     void elementToItemData(QDomElement element);
0221     void elementToNodeData(QDomElement element);
0222     void elementToConnectorData(QDomElement element);
0223     // END functions for reading QDomElements to stored data
0224 
0225     ItemDataMap m_itemDataMap;
0226     ConnectorDataMap m_connectorDataMap;
0227     NodeDataMap m_nodeDataMap;
0228     MicroData m_microData;
0229     uint m_documentType; // See Document::DocumentType
0230 };
0231 
0232 class SubcircuitData : public ItemDocumentData
0233 {
0234 public:
0235     SubcircuitData();
0236     void initECSubcircuit(ECSubcircuit *ecSubcircuit);
0237 };
0238 
0239 #endif