summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2018-03-12 21:44:49 +0100
committerAleks Kissinger <aleks0@gmail.com>2018-03-12 21:44:49 +0100
commit39c2c74c664a6c770639ead8f45322352cacb997 (patch)
treee93bf563eef3c3cd9762b36d06205b91e35a33db /src
parent09c331761648541de907c866c56fb6084c6f7a9b (diff)
style loading works
Diffstat (limited to 'src')
-rw-r--r--src/data/graphelementdata.cpp2
-rw-r--r--src/data/node.cpp2
-rw-r--r--src/data/nodestyle.cpp119
-rw-r--r--src/data/nodestyle.h32
-rw-r--r--src/data/project.cpp13
-rw-r--r--src/data/project.h21
-rw-r--r--src/data/tikzassembler.cpp14
-rw-r--r--src/data/tikzassembler.h10
-rw-r--r--src/data/tikzlexer.l2
-rw-r--r--src/data/tikzparser.y7
-rw-r--r--src/data/tikzstyles.cpp36
-rw-r--r--src/data/tikzstyles.h29
-rw-r--r--src/gui/nodeitem.cpp6
-rw-r--r--src/gui/toolpalette.cpp2
-rw-r--r--src/main.cpp1
-rw-r--r--src/tikzit.cpp91
-rw-r--r--src/tikzit.h14
17 files changed, 293 insertions, 108 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
diff --git a/src/gui/nodeitem.cpp b/src/gui/nodeitem.cpp
index 71226f3..21cdf79 100644
--- a/src/gui/nodeitem.cpp
+++ b/src/gui/nodeitem.cpp
@@ -57,10 +57,10 @@ void NodeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
painter->setBrush(Qt::NoBrush);
painter->drawPath(shape());
} else {
- QPen pen(_node->style()->strokeColor);
- pen.setWidth(_node->style()->strokeThickness);
+ QPen pen(_node->style()->strokeColor());
+ pen.setWidth(_node->style()->strokeThickness());
painter->setPen(pen);
- painter->setBrush(QBrush(_node->style()->fillColor));
+ painter->setBrush(QBrush(_node->style()->fillColor()));
painter->drawPath(shape());
}
diff --git a/src/gui/toolpalette.cpp b/src/gui/toolpalette.cpp
index 430df3f..0a832a6 100644
--- a/src/gui/toolpalette.cpp
+++ b/src/gui/toolpalette.cpp
@@ -20,7 +20,7 @@ ToolPalette::ToolPalette(QWidget *parent) :
select = new QAction(QIcon(":/images/Inkscape_icons_edit_select_all.svg"), "Select");
vertex = new QAction(QIcon(":/images/Inkscape_icons_draw_ellipse.svg"), "Add Vertex");
edge = new QAction(QIcon(":/images/Inkscape_icons_draw_path.svg"), "Add Edge");
- crop = new QAction(QIcon(":/images/Inkscape_icons_draw_rectangle.svg"), "Bounding Box");
+ crop = new QAction(QIcon(":/images/crop.svg"), "Bounding Box");
tools->addAction(select);
tools->addAction(vertex);
diff --git a/src/main.cpp b/src/main.cpp
index 96069ef..49b064d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -20,6 +20,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
tikzit = new Tikzit();
+ tikzit->init();
return a.exec();
}
diff --git a/src/tikzit.cpp b/src/tikzit.cpp
index 746268c..6ef86dd 100644
--- a/src/tikzit.cpp
+++ b/src/tikzit.cpp
@@ -1,7 +1,12 @@
#include "tikzit.h"
+#include "tikzassembler.h"
+#include "tikzstyles.h"
+#include <QFile>
#include <QFileDialog>
#include <QSettings>
+#include <QDebug>
+#include <QMessageBox>
// application-level instance of Tikzit
Tikzit *tikzit;
@@ -9,25 +14,27 @@ Tikzit *tikzit;
// font to use for node labels
QFont Tikzit::LABEL_FONT("Courrier", 9);
-Tikzit::Tikzit()
+Tikzit::Tikzit() : _styleFile("[default]"), _activeWindow(0)
{
- _mainMenu = new MainMenu();
+}
- _activeWindow = 0;
+void Tikzit::init()
+{
+ QSettings settings("tikzit", "tikzit");
+ _mainMenu = new MainMenu();
QMainWindow *dummy = new QMainWindow();
_toolPalette = new ToolPalette(dummy);
_propertyPalette = new PropertyPalette(dummy);
_stylePalette = new StylePalette(dummy);
+ _styles = new TikzStyles(this);
- loadStyles();
-
- //_toolPalette->show();
- //_propertyPalette->show();
_stylePalette->show();
-
_windows << new MainWindow();
_windows[0]->show();
+
+ QString styleFile = settings.value("previous-tikzstyles-file").toString();
+ if (!styleFile.isEmpty()) loadStyles(styleFile);
}
//QMenuBar *Tikzit::mainMenu() const
@@ -45,13 +52,6 @@ PropertyPalette *Tikzit::propertyPalette() const
return _propertyPalette;
}
-void Tikzit::loadStyles()
-{
- _nodeStyles << new NodeStyle("black dot", NodeShape::Circle, Qt::black, Qt::black, 1);
- _nodeStyles << new NodeStyle("white dot", NodeShape::Circle, Qt::white, Qt::black, 1);
- _nodeStyles << new NodeStyle("gray dot", NodeShape::Circle, Qt::gray, Qt::black, 1);
-}
-
void Tikzit::newDoc()
{
MainWindow *w = new MainWindow();
@@ -81,13 +81,6 @@ void Tikzit::removeWindow(MainWindow *w)
}
}
-NodeStyle *Tikzit::nodeStyle(QString name)
-{
- foreach (NodeStyle *s , _nodeStyles)
- if (s->name == name) return s;
- return noneStyle; //NodeStyle(name, NodeShape::Circle, Qt::white);
-}
-
void Tikzit::open()
{
QSettings settings("tikzit", "tikzit");
@@ -109,8 +102,62 @@ void Tikzit::open()
}
}
+void Tikzit::openTikzStyles() {
+ QSettings settings("tikzit", "tikzit");
+ QString fileName = QFileDialog::getOpenFileName(0,
+ tr("Open File"),
+ settings.value("previous-tikzstyles-path").toString(),
+ tr("TiKZ Style Files (*.tikzstyles)"));
+
+ if (!fileName.isEmpty()) {
+ loadStyles(fileName);
+ }
+}
+
+void Tikzit::loadStyles(QString fileName)
+{
+ QSettings settings("tikzit", "tikzit");
+ QFile file(fileName);
+ if (file.open(QIODevice::ReadOnly)) {
+ QFileInfo fi(file);
+ settings.setValue("previous-tikzstyles-path", fi.absolutePath());
+ settings.setValue("previous-tikzstyles-file", fileName);
+ _styleFile = fi.fileName();
+ QTextStream in(&file);
+ QString styleTikz = in.readAll();
+ file.close();
+
+ _styles->clear();
+ TikzAssembler ass(_styles);
+ bool parseSuccess = ass.parse(styleTikz);
+ if (parseSuccess) {
+ qDebug() << "parse successful";
+ } else {
+ qDebug() << "parse failed";
+ }
+ _stylePalette->reloadStyles();
+
+ } else {
+ settings.setValue("previous-tikzstyles-file", "");
+ QMessageBox::warning(0, "Style file not found.", "Could not open style file, reverting to default.");
+ }
+}
+
+QString Tikzit::styleFile() const
+{
+ return _styleFile;
+}
+
+
+TikzStyles *Tikzit::styles() const
+{
+ return _styles;
+}
+
void Tikzit::quit()
{
_stylePalette->close();
QApplication::quit();
}
+
+
diff --git a/src/tikzit.h b/src/tikzit.h
index 07878aa..802b3ab 100644
--- a/src/tikzit.h
+++ b/src/tikzit.h
@@ -38,6 +38,7 @@
#include "propertypalette.h"
#include "stylepalette.h"
#include "nodestyle.h"
+#include "tikzstyles.h"
#include <QObject>
#include <QVector>
@@ -68,7 +69,6 @@ public:
MainWindow *activeWindow() const;
void setActiveWindow(MainWindow *activeWindow);
void removeWindow(MainWindow *w);
- NodeStyle *nodeStyle(QString name);
static QFont LABEL_FONT;
// Ui::MainMenu *_mainMenuUi;
@@ -77,10 +77,15 @@ public:
void newDoc();
void open();
void quit();
+ void init();
+
+ void openTikzStyles();
+ TikzStyles *styles() const;
+ QString styleFile() const;
private:
-// void createMenu();
- void loadStyles();
+ // void createMenu();
+ void loadStyles(QString fileName);
MainMenu *_mainMenu;
ToolPalette *_toolPalette;
@@ -88,7 +93,8 @@ private:
StylePalette *_stylePalette;
QVector<MainWindow*> _windows;
MainWindow *_activeWindow;
- QVector<NodeStyle*> _nodeStyles;
+ TikzStyles *_styles;
+ QString _styleFile;
};