summaryrefslogtreecommitdiff
path: root/src/data
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2018-04-10 16:07:44 +0200
committerAleks Kissinger <aleks0@gmail.com>2018-04-10 16:07:44 +0200
commiteac7dee2d8ba86001afbc61c4e9d7baae7341cb8 (patch)
tree4214eb557fbeee94144c298bd2ec5e5a0e2df27f /src/data
parentfacfa3e8aa7e3c278b0016c02c6a9f1f019903e2 (diff)
added edgestyles, but cant apply to nodes yet
Diffstat (limited to 'src/data')
-rw-r--r--src/data/edgestyle.cpp109
-rw-r--r--src/data/edgestyle.h38
-rw-r--r--src/data/nodestyle.cpp80
-rw-r--r--src/data/nodestyle.h21
-rw-r--r--src/data/style.cpp60
-rw-r--r--src/data/style.h36
-rw-r--r--src/data/tikzlexer.l9
-rw-r--r--src/data/tikzstyles.cpp27
-rw-r--r--src/data/tikzstyles.h4
9 files changed, 296 insertions, 88 deletions
diff --git a/src/data/edgestyle.cpp b/src/data/edgestyle.cpp
new file mode 100644
index 0000000..d366946
--- /dev/null
+++ b/src/data/edgestyle.cpp
@@ -0,0 +1,109 @@
+#include "edgestyle.h"
+
+#include <QPainter>
+#include <QPixmap>
+
+EdgeStyle *noneEdgeStyle = new EdgeStyle();
+
+EdgeStyle::EdgeStyle() : Style()
+{
+}
+
+EdgeStyle::EdgeStyle(QString name, GraphElementData *data) : Style(name, data)
+{
+}
+
+EdgeStyle::ArrowTipStyle EdgeStyle::arrowHead() const
+{
+ if (_data == 0) return NoTip;
+
+ if (_data->atom("->") || _data->atom("<->") || _data->atom("|->")) return Pointer;
+ if (_data->atom("-|") || _data->atom("<-|") || _data->atom("|-|")) return Flat;
+ return NoTip;
+}
+
+EdgeStyle::ArrowTipStyle EdgeStyle::arrowTail() const
+{
+ if (_data == 0) return NoTip;
+ if (_data->atom("<-") || _data->atom("<->") || _data->atom("<-|")) return Pointer;
+ if (_data->atom("|-") || _data->atom("|->") || _data->atom("|-|")) return Flat;
+ return NoTip;
+}
+
+EdgeStyle::DrawStyle EdgeStyle::drawStyle() const
+{
+ if (_data == 0) return Solid;
+ if (_data->atom("dashed")) return Dashed;
+ if (_data->atom("dotted")) return Dotted;
+ return Solid;
+}
+
+QPen EdgeStyle::pen() const
+{
+ QPen p(strokeColor());
+ p.setWidthF((float)strokeThickness() * 3.0f);
+
+ QVector<qreal> pat;
+ switch (drawStyle()) {
+ case Dashed:
+ pat << 3.0 << 3.0;
+ p.setDashPattern(pat);
+ break;
+ case Dotted:
+ pat << 1.0 << 1.0;
+ p.setDashPattern(pat);
+ break;
+ }
+
+ return p;
+}
+
+QPainterPath EdgeStyle::path() const
+{
+ return QPainterPath();
+}
+
+QPainterPath EdgeStyle::palettePath() const
+{
+ return QPainterPath();
+}
+
+QIcon EdgeStyle::icon() const
+{
+ // draw an icon matching the style
+ QPixmap px(100,100);
+ px.fill(Qt::transparent);
+ QPainter painter(&px);
+
+ if (_data == 0) {
+ QPen pen(Qt::black);
+ pen.setWidth(3);
+ } else {
+ painter.setPen(pen());
+ }
+
+ painter.drawLine(10, 50, 90, 50);
+
+ switch (arrowHead()) {
+ case Pointer:
+ painter.drawLine(90,50,80,40);
+ painter.drawLine(90,50,80,60);
+ break;
+ case Flat:
+ painter.drawLine(90,40,90,60);
+ break;
+ }
+
+ switch (arrowTail()) {
+ case Pointer:
+ painter.drawLine(10,50,20,40);
+ painter.drawLine(10,50,20,60);
+ break;
+ case Flat:
+ painter.drawLine(10,40,10,60);
+ break;
+ }
+
+
+ return QIcon(px);
+}
diff --git a/src/data/edgestyle.h b/src/data/edgestyle.h
new file mode 100644
index 0000000..6b0c3bb
--- /dev/null
+++ b/src/data/edgestyle.h
@@ -0,0 +1,38 @@
+#ifndef EDGESTYLE_H
+#define EDGESTYLE_H
+
+#include "style.h"
+
+#include <QColor>
+#include <QPen>
+#include <QBrush>
+#include <QPainterPath>
+#include <QIcon>
+
+class EdgeStyle : public Style
+{
+public:
+ EdgeStyle();
+ EdgeStyle(QString name, GraphElementData *data);
+
+ enum ArrowTipStyle {
+ Flat, Pointer, NoTip
+ };
+
+ enum DrawStyle {
+ Solid, Dotted, Dashed
+ };
+
+ ArrowTipStyle arrowHead() const;
+ ArrowTipStyle arrowTail() const;
+ DrawStyle drawStyle() const;
+
+ QPen pen() const;
+ QPainterPath path() const override;
+ QPainterPath palettePath() const override;
+ QIcon icon() const override;
+};
+
+extern EdgeStyle *noneEdgeStyle;
+
+#endif // EDGESTYLE_H
diff --git a/src/data/nodestyle.cpp b/src/data/nodestyle.cpp
index b3d72fb..3f2c921 100644
--- a/src/data/nodestyle.cpp
+++ b/src/data/nodestyle.cpp
@@ -3,91 +3,43 @@
NodeStyle *noneStyle = new NodeStyle();
-NodeStyle::NodeStyle() : _name("none"), _data(0)
+NodeStyle::NodeStyle() : Style()
{
}
-NodeStyle::NodeStyle(QString name, GraphElementData *data): _name(name), _data(data)
+NodeStyle::NodeStyle(QString name, GraphElementData *data): Style(name, data)
{
}
-bool NodeStyle::isNone() { return _data == 0; }
-
-GraphElementData *NodeStyle::data() const
-{
- return _data;
-}
-
-QString NodeStyle::name() const
-{
- return _name;
-}
-
-NodeStyle::Shape NodeStyle::shape() const
-{
- if (_data == 0) return NodeStyle::Circle;
-
- QString sh = _data->property("shape");
- if (sh.isNull()) return NodeStyle::Circle;
- else if (sh == "circle") return NodeStyle::Circle;
- else if (sh == "rectangle") return NodeStyle::Rectangle;
- else return NodeStyle::Circle;
-}
-
QColor NodeStyle::fillColor() const
{
if (_data == 0) return Qt::white;
- QString col = _data->property("fill");
-
- if (col.isNull()) {
- return QColor(Qt::white);
- } else {
- QColor namedColor(col);
- if (namedColor.isValid()) {
- return namedColor;
- } else {
- // TODO: read RGB colors
- return QColor(Qt::white);
- }
- }
-}
-
-QColor NodeStyle::strokeColor() const
-{
- if (_data == 0) return Qt::black;
+ QString col = propertyWithDefault("fill", "white");
- QString col = _data->property("draw");
-
- if (col.isNull()) {
- return QColor(Qt::black);
+ QColor namedColor(col);
+ if (namedColor.isValid()) {
+ return namedColor;
} else {
- QColor namedColor(col);
- if (namedColor.isValid()) {
- return namedColor;
- } else {
- // TODO: read RGB colors
- return QColor(Qt::white);
- }
+ // TODO: read RGB colors
+ return QColor(Qt::white);
}
}
-int NodeStyle::strokeThickness() const
+QBrush NodeStyle::brush() const
{
- return 1;
+ return QBrush(fillColor());
}
-QPen NodeStyle::pen() const
+NodeStyle::Shape NodeStyle::shape() const
{
- QPen p(strokeColor());
- p.setWidthF((float)strokeThickness() * 3.0f);
- return p;
-}
+ if (_data == 0) return NodeStyle::Circle;
-QBrush NodeStyle::brush() const
-{
- return QBrush(fillColor());
+ QString sh = propertyWithDefault("shape", "circle");
+ if (sh == "circle") return NodeStyle::Circle;
+ else if (sh == "rectangle") return NodeStyle::Rectangle;
+ else return NodeStyle::Circle;
}
QPainterPath NodeStyle::path() const
diff --git a/src/data/nodestyle.h b/src/data/nodestyle.h
index 0b9f282..4b48bb3 100644
--- a/src/data/nodestyle.h
+++ b/src/data/nodestyle.h
@@ -1,7 +1,7 @@
#ifndef NODESTYLE_H
#define NODESTYLE_H
-#include "graphelementdata.h"
+#include "style.h"
#include <QColor>
#include <QPen>
@@ -9,7 +9,7 @@
#include <QPainterPath>
#include <QIcon>
-class NodeStyle
+class NodeStyle : public Style
{
public:
enum Shape {
@@ -18,23 +18,14 @@ public:
NodeStyle();
NodeStyle(QString name, GraphElementData *data);
- bool isNone();
- GraphElementData *data() const;
- QString name() const;
- Shape shape() const;
QColor fillColor() const;
- QColor strokeColor() const;
- int strokeThickness() const;
-
- QPen pen() const;
QBrush brush() const;
QPainterPath path() const;
- QPainterPath palettePath() const;
- QIcon icon() const;
-private:
- QString _name;
- GraphElementData *_data;
+ Shape shape() const;
+
+ QPainterPath palettePath() const override;
+ QIcon icon() const override;
};
extern NodeStyle *noneStyle;
diff --git a/src/data/style.cpp b/src/data/style.cpp
new file mode 100644
index 0000000..927271c
--- /dev/null
+++ b/src/data/style.cpp
@@ -0,0 +1,60 @@
+#include "style.h"
+
+Style::Style() : _name("none"), _data(0)
+{
+}
+
+Style::Style(QString name, GraphElementData *data) : _name(name), _data(data)
+{
+}
+
+bool Style::isNone()
+{
+ return _data == 0;
+}
+
+GraphElementData *Style::data() const
+{
+ return _data;
+}
+
+QString Style::name() const
+{
+ return _name;
+}
+
+QColor Style::strokeColor() const
+{
+ if (_data == 0) return Qt::black;
+
+ QString col = propertyWithDefault("draw", "black");
+
+ QColor namedColor(col);
+ if (namedColor.isValid()) {
+ return namedColor;
+ } else {
+ // TODO: read RGB colors
+ return QColor(Qt::black);
+ }
+}
+
+// TODO
+int Style::strokeThickness() const
+{
+ return 1;
+}
+
+QPen Style::pen() const
+{
+ QPen p(strokeColor());
+ p.setWidthF((float)strokeThickness() * 3.0f);
+ return p;
+}
+
+QString Style::propertyWithDefault(QString prop, QString def) const
+{
+ QString val = _data->property("tikzit " + prop);
+ if (val.isNull()) val = _data->property(prop);
+ if (val.isNull()) val = def;
+ return val;
+}
diff --git a/src/data/style.h b/src/data/style.h
new file mode 100644
index 0000000..9d58ebe
--- /dev/null
+++ b/src/data/style.h
@@ -0,0 +1,36 @@
+#ifndef STYLE_H
+#define STYLE_H
+
+
+#include "graphelementdata.h"
+
+#include <QColor>
+#include <QPen>
+#include <QBrush>
+#include <QPainterPath>
+#include <QIcon>
+
+class Style
+{
+public:
+ Style();
+ Style(QString name, GraphElementData *data);
+ bool isNone();
+
+ // properties that both edges and nodes have
+ GraphElementData *data() const;
+ QString name() const;
+ QColor strokeColor() const;
+ int strokeThickness() const;
+
+ // methods that are implemented differently for edges and nodes
+ virtual QPen pen() const;
+ virtual QPainterPath path() const = 0;
+ virtual QPainterPath palettePath() const = 0;
+ virtual QIcon icon() const = 0;
+protected:
+ QString propertyWithDefault(QString prop, QString def) const;
+ QString _name;
+ GraphElementData *_data;
+};
+#endif // STYLE_H
diff --git a/src/data/tikzlexer.l b/src/data/tikzlexer.l
index d90ad4b..45494d2 100644
--- a/src/data/tikzlexer.l
+++ b/src/data/tikzlexer.l
@@ -65,6 +65,7 @@ FLOAT \-?[0-9]*(\.[0-9]+)?
yylloc->first_column = yylloc->last_column = 0;
}
<INITIAL,xcoord,ycoord,props,noderef>[\t ]+ { }
+<INITIAL,xcoord,ycoord,props,noderef>%.*$ { }
\\begin\{tikzpicture\} { return BEGIN_TIKZPICTURE_CMD; }
\\end\{tikzpicture\} { return END_TIKZPICTURE_CMD; }
@@ -74,12 +75,12 @@ FLOAT \-?[0-9]*(\.[0-9]+)?
\\draw { return DRAW_CMD; }
\\node { return NODE_CMD; }
\\path { return PATH_CMD; }
-rectangle { return RECTANGLE; }
-node { return NODE; }
-at { return AT; }
-to { return TO; }
; { return SEMICOLON; }
= { return EQUALS; }
+<INITIAL>rectangle { return RECTANGLE; }
+<INITIAL>node { return NODE; }
+<INITIAL>at { return AT; }
+<INITIAL>to { return TO; }
\([ ]*{FLOAT}[ ]*,[ ]*{FLOAT}[ ]*\) {
yylloc->last_column = yylloc->first_column + 1;
diff --git a/src/data/tikzstyles.cpp b/src/data/tikzstyles.cpp
index 186e19b..c198412 100644
--- a/src/data/tikzstyles.cpp
+++ b/src/data/tikzstyles.cpp
@@ -12,7 +12,14 @@ NodeStyle *TikzStyles::nodeStyle(QString name) const
{
foreach (NodeStyle *s , _nodeStyles)
if (s->name() == name) return s;
- return noneStyle; //NodeStyle(name, NodeShape::Circle, Qt::white);
+ return noneStyle;
+}
+
+EdgeStyle *TikzStyles::edgeStyle(QString name) const
+{
+ foreach (EdgeStyle *s , _edgeStyles)
+ if (s->name() == name) return s;
+ return noneEdgeStyle;
}
QVector<NodeStyle *> TikzStyles::nodeStyles() const
@@ -23,14 +30,24 @@ QVector<NodeStyle *> TikzStyles::nodeStyles() const
void TikzStyles::clear()
{
_nodeStyles.clear();
+ _edgeStyles.clear();
+}
+
+QVector<EdgeStyle *> TikzStyles::edgeStyles() const
+{
+ return _edgeStyles;
}
void TikzStyles::addStyle(QString name, GraphElementData *data)
{
- //qDebug() << "got style {" << name << "} = [" << data << "]";
- if (!data->property("fill").isNull()) { // node style
+ if (data->atom("-") || data->atom("->") || data->atom("-|") ||
+ data->atom("<-") || data->atom("<->") || data->atom("<-|") ||
+ data->atom("|-") || data->atom("|->") || data->atom("|-|"))
+ { // edge style
+ qDebug() << "got edge style" << name;
+ _edgeStyles << new EdgeStyle(name, data);
+ } else { // node style
+ qDebug() << "got node style" << name;
_nodeStyles << new NodeStyle(name, data);
- } else { // edge style
- // TODO: edge styles
}
}
diff --git a/src/data/tikzstyles.h b/src/data/tikzstyles.h
index eaf7e64..4cd7d6e 100644
--- a/src/data/tikzstyles.h
+++ b/src/data/tikzstyles.h
@@ -3,6 +3,7 @@
#include "graphelementdata.h"
#include "nodestyle.h"
+#include "edgestyle.h"
#include <QObject>
#include <QString>
@@ -15,7 +16,9 @@ public:
void addStyle(QString name, GraphElementData *data);
NodeStyle *nodeStyle(QString name) const;
+ EdgeStyle *edgeStyle(QString name) const;
QVector<NodeStyle *> nodeStyles() const;
+ QVector<EdgeStyle *> edgeStyles() const;
void clear();
signals:
@@ -24,6 +27,7 @@ public slots:
private:
QVector<NodeStyle*> _nodeStyles;
+ QVector<EdgeStyle*> _edgeStyles;
};
#endif // PROJECT_H