diff options
Diffstat (limited to 'src/data')
-rw-r--r-- | src/data/graphelementdata.cpp | 2 | ||||
-rw-r--r-- | src/data/node.cpp | 2 | ||||
-rw-r--r-- | src/data/nodestyle.cpp | 119 | ||||
-rw-r--r-- | src/data/nodestyle.h | 32 | ||||
-rw-r--r-- | src/data/project.cpp | 13 | ||||
-rw-r--r-- | src/data/project.h | 21 | ||||
-rw-r--r-- | src/data/tikzassembler.cpp | 14 | ||||
-rw-r--r-- | src/data/tikzassembler.h | 10 | ||||
-rw-r--r-- | src/data/tikzlexer.l | 2 | ||||
-rw-r--r-- | src/data/tikzparser.y | 7 | ||||
-rw-r--r-- | src/data/tikzstyles.cpp | 36 | ||||
-rw-r--r-- | src/data/tikzstyles.h | 29 |
12 files changed, 209 insertions, 78 deletions
diff --git a/src/data/graphelementdata.cpp b/src/data/graphelementdata.cpp index 41fcbf0..43f7516 100644 --- a/src/data/graphelementdata.cpp +++ b/src/data/graphelementdata.cpp @@ -66,7 +66,7 @@ QString GraphElementData::property(QString key) if (i != -1) { return _properties[i].value(); } else { - return 0; + return QString(); // null QString } } diff --git a/src/data/node.cpp b/src/data/node.cpp index f94a3df..c78f49c 100644 --- a/src/data/node.cpp +++ b/src/data/node.cpp @@ -70,7 +70,7 @@ void Node::setStyleName(const QString &styleName) void Node::attachStyle() { if (_styleName == "none") _style = noneStyle; - else _style = tikzit->nodeStyle(_styleName); + else _style = tikzit->styles()->nodeStyle(_styleName); } NodeStyle *Node::style() const diff --git a/src/data/nodestyle.cpp b/src/data/nodestyle.cpp index 7eca791..e38d3a3 100644 --- a/src/data/nodestyle.cpp +++ b/src/data/nodestyle.cpp @@ -1,32 +1,113 @@ #include "nodestyle.h" +#include <QPainter> NodeStyle *noneStyle = new NodeStyle(); -NodeStyle::NodeStyle() +NodeStyle::NodeStyle() : _name("none"), _data(0) { - name = "none"; - shape = NodeShape::Circle; - fillColor = Qt::white; - strokeColor = Qt::black; - strokeThickness = 1; } -NodeStyle::NodeStyle(QString nm, NodeShape sh, QColor fillCol) + +NodeStyle::NodeStyle(QString name, GraphElementData *data): _name(name), _data(data) +{ +} + +bool NodeStyle::isNone() { return _data == 0; } + +GraphElementData *NodeStyle::data() const +{ + return _data; +} + +QString NodeStyle::name() const +{ + return _name; +} + +NodeShape NodeStyle::shape() const { - name = nm; - shape = sh; - fillColor = fillCol; - strokeColor = Qt::black; - strokeThickness = 1; + QString sh = _data->property("shape"); + if (sh.isNull()) return NodeShape::Circle; + else if (sh == "circle") return NodeShape::Circle; + else if (sh == "rectangle") return NodeShape::Rectangle; + else return NodeShape::Circle; } -NodeStyle::NodeStyle(QString nm, NodeShape sh, QColor fillCol, QColor strokeCol, int strokeThick) +QColor NodeStyle::fillColor() const { - name = nm; - shape = sh; - fillColor = fillCol; - strokeColor = strokeCol; - strokeThickness = strokeThick; + 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 +{ + QString col = _data->property("draw"); + + if (col.isNull()) { + return QColor(Qt::black); + } else { + QColor namedColor(col); + if (namedColor.isValid()) { + return namedColor; + } else { + // TODO: read RGB colors + return QColor(Qt::white); + } + } +} + +int NodeStyle::strokeThickness() const +{ + return 1; +} + +QPen NodeStyle::pen() const +{ + QPen p(strokeColor()); + p.setWidthF((float)strokeThickness() * 3.0f); + return p; +} + +QBrush NodeStyle::brush() const +{ + return QBrush(fillColor()); +} + +QPainterPath NodeStyle::path() const +{ + QPainterPath pth; + pth.addEllipse(QPointF(0.0f,0.0f), 30.0f, 30.0f); + return pth; +} + +QPainterPath NodeStyle::palettePath() const +{ + return path(); +} + +QIcon NodeStyle::icon() const +{ + // draw an icon matching the style + QPixmap px(100,100); + px.fill(Qt::transparent); + QPainter painter(&px); + QPainterPath pth = path(); + painter.setPen(pen()); + painter.setBrush(brush()); + + pth.translate(50.0f, 50.0f); + painter.drawPath(pth); + return QIcon(px); } -bool NodeStyle::isNone() { return name == "none"; } diff --git a/src/data/nodestyle.h b/src/data/nodestyle.h index 00d1b20..58c0c12 100644 --- a/src/data/nodestyle.h +++ b/src/data/nodestyle.h @@ -1,24 +1,40 @@ #ifndef NODESTYLE_H #define NODESTYLE_H +#include "graphelementdata.h" + #include <QColor> +#include <QPen> +#include <QBrush> +#include <QPainterPath> +#include <QIcon> enum NodeShape { - Square, UpTriangle, DownTriangle, Circle + Rectangle, UpTriangle, DownTriangle, Circle }; class NodeStyle { public: NodeStyle(); - NodeStyle(QString nm, NodeShape sh, QColor fillCol); - NodeStyle(QString nm, NodeShape sh, QColor fillCol, QColor strokeCol, int strokeThick); + NodeStyle(QString name, GraphElementData *data); bool isNone(); - QString name; - NodeShape shape; - QColor fillColor; - QColor strokeColor; - int strokeThickness; + + GraphElementData *data() const; + QString name() const; + NodeShape 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; }; extern NodeStyle *noneStyle; diff --git a/src/data/project.cpp b/src/data/project.cpp deleted file mode 100644 index b129dc0..0000000 --- a/src/data/project.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "project.h" - -#include "QDebug" - -Project::Project(QObject *parent) : QObject(parent) -{ - -} - -void Project::addStyle(QString name, GraphElementData *properties) -{ - qDebug() << "got style {" << name << "} = [" << properties << "]"; -} diff --git a/src/data/project.h b/src/data/project.h deleted file mode 100644 index cbc2cb9..0000000 --- a/src/data/project.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef PROJECT_H -#define PROJECT_H - -#include "graphelementdata.h" - -#include <QObject> -#include <QString> - -class Project : public QObject -{ - Q_OBJECT -public: - explicit Project(QObject *parent = 0); - void addStyle(QString name, GraphElementData *properties); - -signals: - -public slots: -}; - -#endif // PROJECT_H diff --git a/src/data/tikzassembler.cpp b/src/data/tikzassembler.cpp index 456464a..e0197da 100644 --- a/src/data/tikzassembler.cpp +++ b/src/data/tikzassembler.cpp @@ -7,14 +7,14 @@ int yyparse(void *scanner); TikzAssembler::TikzAssembler(Graph *graph, QObject *parent) : - QObject(parent), _graph(graph), _project(0) + QObject(parent), _graph(graph), _tikzStyles(0) { yylex_init(&scanner); yyset_extra(this, scanner); } -TikzAssembler::TikzAssembler(Project *project, QObject *parent) : - QObject(parent), _graph(0), _project(project) +TikzAssembler::TikzAssembler(TikzStyles *tikzStyles, QObject *parent) : + QObject(parent), _graph(0), _tikzStyles(tikzStyles) { yylex_init(&scanner); yyset_extra(this, scanner); @@ -37,9 +37,9 @@ Graph *TikzAssembler::graph() const return _graph; } -Project *TikzAssembler::project() const +TikzStyles *TikzAssembler::tikzStyles() const { - return _project; + return _tikzStyles; } bool TikzAssembler::isGraph() const @@ -47,8 +47,8 @@ bool TikzAssembler::isGraph() const return _graph != 0; } -bool TikzAssembler::isProject() const +bool TikzAssembler::isTikzStyles() const { - return _project != 0; + return _tikzStyles != 0; } diff --git a/src/data/tikzassembler.h b/src/data/tikzassembler.h index 8dbbc9b..38d67a7 100644 --- a/src/data/tikzassembler.h +++ b/src/data/tikzassembler.h @@ -7,7 +7,7 @@ #include "node.h" #include "graph.h" -#include "project.h" +#include "tikzstyles.h" #include <QObject> #include <QHash> @@ -17,15 +17,15 @@ class TikzAssembler : public QObject Q_OBJECT public: explicit TikzAssembler(Graph *graph, QObject *parent = 0); - explicit TikzAssembler(Project *project, QObject *parent = 0); + explicit TikzAssembler(TikzStyles *tikzStyles, QObject *parent = 0); void addNodeToMap(Node *n); Node *nodeWithName(QString name); bool parse(const QString &tikz); Graph *graph() const; - Project *project() const; + TikzStyles *tikzStyles() const; bool isGraph() const; - bool isProject() const; + bool isTikzStyles() const; signals: @@ -35,7 +35,7 @@ public slots: private: QHash<QString,Node*> _nodeMap; Graph *_graph; - Project *_project; + TikzStyles *_tikzStyles; void *scanner; }; diff --git a/src/data/tikzlexer.l b/src/data/tikzlexer.l index faf0d43..0a7ff39 100644 --- a/src/data/tikzlexer.l +++ b/src/data/tikzlexer.l @@ -68,6 +68,7 @@ FLOAT \-?[0-9]*(\.[0-9]+)? \\begin\{tikzpicture\} { return BEGIN_TIKZPICTURE_CMD; } \\end\{tikzpicture\} { return END_TIKZPICTURE_CMD; } +\\tikzstyle { return TIKZSTYLE_CMD; } \\begin\{pgfonlayer\} { return BEGIN_PGFONLAYER_CMD; } \\end\{pgfonlayer\} { return END_PGFONLAYER_CMD; } \\draw { return DRAW_CMD; } @@ -78,6 +79,7 @@ node { return NODE; } at { return AT; } to { return TO; } ; { return SEMICOLON; } += { return EQUALS; } \([ ]*{FLOAT}[ ]*,[ ]*{FLOAT}[ ]*\) { yylloc->last_column = yylloc->first_column + 1; diff --git a/src/data/tikzparser.y b/src/data/tikzparser.y index a4db3dd..76674f1 100644 --- a/src/data/tikzparser.y +++ b/src/data/tikzparser.y @@ -7,7 +7,7 @@ /* * Copyright 2010 Chris Heunen - * Copyright 2010-2013 Aleks Kissinger + * Copyright 2010-2017 Aleks Kissinger * Copyright 2013 K. Johan Paulsson * Copyright 2013 Alex Merry <dev@randomguy3.me.uk> * @@ -85,6 +85,7 @@ void yyerror(YYLTYPE *yylloc, void *scanner, const char *str) { %token BEGIN_TIKZPICTURE_CMD "\\begin{tikzpicture}" %token END_TIKZPICTURE_CMD "\\end{tikzpicture}" +%token TIKZSTYLE_CMD "\\tikzstyle" %token BEGIN_PGFONLAYER_CMD "\\begin{pgfonlayer}" %token END_PGFONLAYER_CMD "\\end{pgfonlayer}" %token DRAW_CMD "\\draw" @@ -133,8 +134,8 @@ tikz: tikzstyles | tikzpicture; tikzstyles: tikzstyles tikzstyle | ; tikzstyle: "\\tikzstyle" DELIMITEDSTRING "=" "[" properties "]" { - if (assembler->isProject()) { - assembler->project()->addStyle(QString($2), $5); + if (assembler->isTikzStyles()) { + assembler->tikzStyles()->addStyle(QString($2), $5); } } diff --git a/src/data/tikzstyles.cpp b/src/data/tikzstyles.cpp new file mode 100644 index 0000000..186e19b --- /dev/null +++ b/src/data/tikzstyles.cpp @@ -0,0 +1,36 @@ +#include "tikzstyles.h" +#include "nodestyle.h" + +#include <QDebug> + +TikzStyles::TikzStyles(QObject *parent) : QObject(parent) +{ + +} + +NodeStyle *TikzStyles::nodeStyle(QString name) const +{ + foreach (NodeStyle *s , _nodeStyles) + if (s->name() == name) return s; + return noneStyle; //NodeStyle(name, NodeShape::Circle, Qt::white); +} + +QVector<NodeStyle *> TikzStyles::nodeStyles() const +{ + return _nodeStyles; +} + +void TikzStyles::clear() +{ + _nodeStyles.clear(); +} + +void TikzStyles::addStyle(QString name, GraphElementData *data) +{ + //qDebug() << "got style {" << name << "} = [" << data << "]"; + if (!data->property("fill").isNull()) { // node style + _nodeStyles << new NodeStyle(name, data); + } else { // edge style + // TODO: edge styles + } +} diff --git a/src/data/tikzstyles.h b/src/data/tikzstyles.h new file mode 100644 index 0000000..eaf7e64 --- /dev/null +++ b/src/data/tikzstyles.h @@ -0,0 +1,29 @@ +#ifndef PROJECT_H +#define PROJECT_H + +#include "graphelementdata.h" +#include "nodestyle.h" + +#include <QObject> +#include <QString> + +class TikzStyles : public QObject +{ + Q_OBJECT +public: + explicit TikzStyles(QObject *parent = 0); + void addStyle(QString name, GraphElementData *data); + + NodeStyle *nodeStyle(QString name) const; + QVector<NodeStyle *> nodeStyles() const; + void clear(); + +signals: + +public slots: + +private: + QVector<NodeStyle*> _nodeStyles; +}; + +#endif // PROJECT_H |