File indexing completed on 2024-05-19 15:27:49

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 #ifndef GRAPH_ELEMENT_H
0020 #define GRAPH_ELEMENT_H
0021 
0022 #include "dotrenderop.h"
0023 
0024 #include <QList>
0025 #include <QMap>
0026 #include <QTextStream>
0027 #include <QVector>
0028 #include <QObject>
0029 
0030 namespace KGraphViewer
0031 {
0032 class CanvasElement;
0033 
0034 /**
0035  * The base of all Graphviz DOT graph elements (nodes, edges, subgraphs,
0036  * graphs). It is used to store the element attributes
0037  */
0038 class GraphElement : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     GraphElement();
0043     GraphElement(const GraphElement &element);
0044 
0045     ~GraphElement() override
0046     {
0047     }
0048 
0049     inline void setId(const QString &id)
0050     {
0051         m_attributes[KEY_ID] = id;
0052     }
0053     inline void setStyle(const QString &ls)
0054     {
0055         m_attributes[KEY_STYLE] = ls;
0056     }
0057     inline void setShape(const QString &lc)
0058     {
0059         m_attributes[KEY_SHAPE] = lc;
0060     }
0061     inline void setColor(const QString &nt)
0062     {
0063         m_attributes[KEY_COLOR] = nt;
0064     }
0065     inline void setLineColor(const QString &nt)
0066     {
0067         m_attributes[KEY_COLOR] = nt;
0068     }
0069     inline void setBackColor(const QString &nc)
0070     {
0071         m_attributes[KEY_BGCOLOR] = nc;
0072     }
0073 
0074     inline QString id() const
0075     {
0076         return m_attributes[KEY_ID];
0077     }
0078     inline QString style() const
0079     {
0080         return m_attributes[KEY_STYLE];
0081     }
0082     inline QString shape() const
0083     {
0084         return m_attributes[KEY_SHAPE];
0085     }
0086     inline QString color() const
0087     {
0088         return m_attributes[KEY_COLOR];
0089     }
0090     inline QString lineColor() const
0091     {
0092         return m_attributes[KEY_COLOR];
0093     }
0094     virtual QString backColor() const;
0095 
0096     inline void setLabel(const QString &label)
0097     {
0098         m_attributes[KEY_LABEL] = label;
0099     }
0100     inline const QString label() const
0101     {
0102         return m_attributes[KEY_LABEL];
0103     }
0104 
0105     inline unsigned int fontSize() const
0106     {
0107         return m_attributes[KEY_FONTSIZE].toUInt();
0108     }
0109     inline void setFontSize(unsigned int fs)
0110     {
0111         m_attributes[KEY_FONTSIZE] = QString::number(fs);
0112     }
0113     inline QString fontName() const
0114     {
0115         return m_attributes[KEY_FONTNAME];
0116     }
0117     inline void setFontName(const QString &fn)
0118     {
0119         m_attributes[KEY_FONTNAME] = fn;
0120     }
0121     inline QString fontColor() const
0122     {
0123         return m_attributes[KEY_FONTCOLOR];
0124     }
0125     inline void setFontColor(const QString &fc)
0126     {
0127         m_attributes[KEY_FONTCOLOR] = fc;
0128     }
0129 
0130     inline const DotRenderOpVec &renderOperations() const
0131     {
0132         return m_renderOperations;
0133     };
0134     void setRenderOperations(const DotRenderOpVec &drov);
0135     /**
0136      * indicates the version of the render operations, gets increased every time
0137      * @c setRenderOperations gets called.
0138      */
0139     inline quint32 renderOperationsRevision() const
0140     {
0141         return m_renderOperationsRevision;
0142     };
0143 
0144     inline double z() const
0145     {
0146         return m_z;
0147     }
0148     inline void setZ(double thez)
0149     {
0150         m_z = thez;
0151     }
0152 
0153     inline QString shapeFile() const
0154     {
0155         return m_attributes[KEY_SHAPEFILE];
0156     }
0157     inline void setShapeFile(const QString &sf)
0158     {
0159         m_attributes[KEY_SHAPEFILE] = sf;
0160     }
0161 
0162     inline QString url() const
0163     {
0164         return m_attributes[KEY_URL];
0165     }
0166     inline void setUrl(const QString &theUrl)
0167     {
0168         m_attributes[KEY_URL] = theUrl;
0169     }
0170 
0171     virtual void updateWithElement(const GraphElement &element);
0172 
0173     inline QMap<QString, QString> &attributes()
0174     {
0175         return m_attributes;
0176     }
0177     inline const QMap<QString, QString> &attributes() const
0178     {
0179         return m_attributes;
0180     }
0181 
0182     inline QList<QString> &originalAttributes()
0183     {
0184         return m_originalAttributes;
0185     }
0186     inline const QList<QString> &originalAttributes() const
0187     {
0188         return m_originalAttributes;
0189     }
0190 
0191     virtual inline void storeOriginalAttributes()
0192     {
0193         m_originalAttributes = m_attributes.keys();
0194     }
0195 
0196     virtual void removeAttribute(const QString &attribName);
0197 
0198     inline CanvasElement *canvasElement()
0199     {
0200         return m_ce;
0201     }
0202     inline const CanvasElement *canvasElement() const
0203     {
0204         return m_ce;
0205     }
0206     inline void setCanvasElement(CanvasElement *ce)
0207     {
0208         m_ce = ce;
0209     }
0210 
0211     inline void setSelected(bool s)
0212     {
0213         m_selected = s;
0214     }
0215     inline bool isSelected()
0216     {
0217         return m_selected;
0218     }
0219 
0220     bool isVisible() const
0221     {
0222         return m_visible;
0223     }
0224     void setVisible(bool v)
0225     {
0226         m_visible = v;
0227     }
0228 
0229     void exportToGraphviz(void *element) const;
0230 
0231 Q_SIGNALS:
0232     void changed();
0233 
0234 protected:
0235     QMap<QString, QString> m_attributes;
0236     QList<QString> m_originalAttributes;
0237 
0238     CanvasElement *m_ce;
0239 
0240     static const QString KEY_ID;
0241     static const QString KEY_STYLE;
0242     static const QString KEY_LABEL;
0243     static const QString KEY_SHAPE;
0244     static const QString KEY_SHAPEFILE;
0245     static const QString KEY_COLOR;
0246     static const QString KEY_BGCOLOR;
0247     static const QString KEY_URL;
0248     static const QString KEY_FONTSIZE;
0249     static const QString KEY_FONTNAME;
0250     static const QString KEY_FONTCOLOR;
0251     static const QString KEY_FILLCOLOR;
0252 
0253 private:
0254     double m_z;
0255     bool m_visible;
0256 
0257     DotRenderOpVec m_renderOperations;
0258     quint32 m_renderOperationsRevision;
0259 
0260     bool m_selected;
0261 };
0262 
0263 QTextStream &operator<<(QTextStream &s, const GraphElement &n);
0264 
0265 }
0266 
0267 #endif