diff options
-rw-r--r-- | src/data/nodestyle.cpp | 11 | ||||
-rw-r--r-- | src/data/style.cpp | 17 | ||||
-rw-r--r-- | src/data/style.h | 2 | ||||
-rw-r--r-- | src/data/tikzstyles.cpp | 67 | ||||
-rw-r--r-- | src/data/tikzstyles.h | 5 | ||||
-rw-r--r-- | src/gui/styleeditor.cpp | 85 | ||||
-rw-r--r-- | src/gui/styleeditor.h | 12 | ||||
-rw-r--r-- | src/gui/stylepalette.ui | 7 | ||||
-rw-r--r-- | src/tikzit.cpp | 134 | ||||
-rw-r--r-- | src/tikzit.h | 14 |
10 files changed, 239 insertions, 115 deletions
diff --git a/src/data/nodestyle.cpp b/src/data/nodestyle.cpp index 928712d..aec899f 100644 --- a/src/data/nodestyle.cpp +++ b/src/data/nodestyle.cpp @@ -17,6 +17,8 @@ */
#include "nodestyle.h"
+#include "tikzit.h"
+
#include <QPainter>
NodeStyle *noneStyle = new NodeStyle();
@@ -35,14 +37,7 @@ QColor NodeStyle::fillColor(bool tikzitOverride) const if (_data == 0) return Qt::white;
QString col = propertyWithDefault("fill", "white", tikzitOverride);
-
- QColor namedColor(col);
- if (namedColor.isValid()) {
- return namedColor;
- } else {
- // TODO: read RGB colors
- return QColor(Qt::white);
- }
+ return tikzit->colorByName(col);
}
QBrush NodeStyle::brush() const
diff --git a/src/data/style.cpp b/src/data/style.cpp index 61c86d6..2e19c4f 100644 --- a/src/data/style.cpp +++ b/src/data/style.cpp @@ -17,6 +17,7 @@ */
#include "style.h"
+#include "tikzit.h"
Style::Style() : _name("none"), _data(0)
{
@@ -43,17 +44,10 @@ QString Style::name() const QColor Style::strokeColor(bool tikzitOverride) const
{
- if (_data == 0) return Qt::black;
+ if (_data == 0) return QColor(Qt::black);
QString col = propertyWithDefault("draw", "black", tikzitOverride);
-
- QColor namedColor(col);
- if (namedColor.isValid()) {
- return namedColor;
- } else {
- // TODO: read RGB colors
- return QColor(Qt::black);
- }
+ return tikzit->colorByName(col);
}
// TODO
@@ -81,3 +75,8 @@ QString Style::propertyWithDefault(QString prop, QString def, bool tikzitOverrid if (val.isNull()) val = def;
return val;
}
+
+void Style::setName(const QString &name)
+{
+ _name = name;
+}
diff --git a/src/data/style.h b/src/data/style.h index 0be8b87..7a6ff18 100644 --- a/src/data/style.h +++ b/src/data/style.h @@ -46,6 +46,8 @@ public: virtual QPainterPath path() const = 0;
virtual QPainterPath palettePath() const = 0;
virtual QIcon icon() const = 0;
+ void setName(const QString &name);
+
protected:
QString propertyWithDefault(QString prop, QString def, bool tikzitOverride=true) const;
QString _name;
diff --git a/src/data/tikzstyles.cpp b/src/data/tikzstyles.cpp index c96c55b..4a92d6d 100644 --- a/src/data/tikzstyles.cpp +++ b/src/data/tikzstyles.cpp @@ -18,40 +18,15 @@ #include "tikzstyles.h"
#include "nodestyle.h"
+#include "tikzassembler.h"
#include <QDebug>
#include <QColorDialog>
+#include <QFile>
+#include <QFileInfo>
TikzStyles::TikzStyles(QObject *parent) : QObject(parent)
{
- // 19 standard xcolor colours
- _colNames <<
- "black" <<
- "gray" <<
- "darkgray" <<
- "lightgray" <<
- "white" <<
-
- "red" <<
- "orange" <<
- "yellow" <<
- "lime" <<
- "blue" <<
- "purple" <<
-
- "brown" <<
- "olive" <<
- "green" <<
- "teal" <<
- "cyan" <<
-
- "magenta" <<
- "violet" <<
- "pink";
-
- for (int i = 0; i < _colNames.length(); ++i) {
- _cols << QColor(_colNames[i]);
- }
}
NodeStyle *TikzStyles::nodeStyle(QString name) const
@@ -79,31 +54,28 @@ void TikzStyles::clear() _edgeStyles.clear();
}
-QColor TikzStyles::colorByIndex(int i)
-{
- return _cols[i];
-}
-
-QColor TikzStyles::colorByName(QString name)
-{
- for (int i = 0; i < _colNames.length(); ++i) {
- if (_colNames[i] == name) return _cols[i];
- }
- return QColor();
-}
-
-QString TikzStyles::nameForColor(QColor col)
+bool TikzStyles::loadStyles(QString fileName)
{
- for (int i = 0; i < _colNames.length(); ++i) {
- if (_cols[i] == col) return _colNames[i];
+ QFile file(fileName);
+ if (file.open(QIODevice::ReadOnly)) {
+ QTextStream in(&file);
+ QString styleTikz = in.readAll();
+ file.close();
+
+ clear();
+ TikzAssembler ass(this);
+ return ass.parse(styleTikz);
+ } else {
+ return false;
}
- return QString();
}
void TikzStyles::refreshModels(QStandardItemModel *nodeModel, QStandardItemModel *edgeModel)
{
nodeModel->clear();
edgeModel->clear();
+
+
//QString f = tikzit->styleFile();
//ui->styleFile->setText(f);
@@ -113,11 +85,14 @@ void TikzStyles::refreshModels(QStandardItemModel *nodeModel, QStandardItemModel it->setEditable(false);
it->setData(noneStyle->name());
nodeModel->appendRow(it);
+ it->setTextAlignment(Qt::AlignCenter);
+ it->setSizeHint(QSize(48,48));
foreach(NodeStyle *ns, _nodeStyles) {
it = new QStandardItem(ns->icon(), ns->name());
it->setEditable(false);
it->setData(ns->name());
+ it->setSizeHint(QSize(48,48));
nodeModel->appendRow(it);
}
@@ -152,3 +127,5 @@ void TikzStyles::addStyle(QString name, GraphElementData *data) _nodeStyles << new NodeStyle(name, data);
}
}
+
+
diff --git a/src/data/tikzstyles.h b/src/data/tikzstyles.h index 51392b2..26ff0a3 100644 --- a/src/data/tikzstyles.h +++ b/src/data/tikzstyles.h @@ -41,10 +41,7 @@ public: QVector<EdgeStyle *> edgeStyles() const;
void clear();
- // convenience functions for named colors
- QColor colorByIndex(int i);
- QColor colorByName(QString name);
- QString nameForColor(QColor col);
+ bool loadStyles(QString fileName);
void refreshModels(QStandardItemModel *nodeModel, QStandardItemModel *edgeModel);
signals:
diff --git a/src/gui/styleeditor.cpp b/src/gui/styleeditor.cpp index fae5e1a..9081873 100644 --- a/src/gui/styleeditor.cpp +++ b/src/gui/styleeditor.cpp @@ -1,5 +1,6 @@ #include <QColorDialog> #include <QDebug> +#include <QMessageBox> #include "tikzit.h" #include "styleeditor.h" @@ -16,7 +17,7 @@ StyleEditor::StyleEditor(QWidget *parent) : setColor(ui->tikzitFillColor, QColor(Qt::white)); setColor(ui->tikzitDrawColor, QColor(Qt::black)); - TikzStyles *styles = tikzit->styles(); + _styles = 0; _nodeModel = new QStandardItemModel(this); _edgeModel = new QStandardItemModel(this); @@ -41,28 +42,28 @@ StyleEditor::StyleEditor(QWidget *parent) : // grayscale in column 1 int pos = 0; for (int i=0; i < 5; ++i) { - QColorDialog::setStandardColor(pos, styles->colorByIndex(i)); + QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i)); pos += 1; } // rainbow in column 2 pos = 6; for (int i=5; i < 11; ++i) { - QColorDialog::setStandardColor(pos, styles->colorByIndex(i)); + QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i)); pos += 1; } // brown/green/teal spectrum in column 3 pos = 12; for (int i=11; i < 16; ++i) { - QColorDialog::setStandardColor(pos, styles->colorByIndex(i)); + QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i)); pos += 1; } // pinks in column 4 pos = 18; for (int i=16; i < 19; ++i) { - QColorDialog::setStandardColor(pos, styles->colorByIndex(i)); + QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i)); pos += 1; } @@ -76,9 +77,17 @@ StyleEditor::~StyleEditor() delete ui; } -void StyleEditor::showEvent(QShowEvent *) -{ - tikzit->styles()->refreshModels(_nodeModel, _edgeModel); +void StyleEditor::open() { + if (_styles != 0) delete _styles; + _styles = new TikzStyles; + if (_styles->loadStyles(tikzit->styleFilePath())) { + _styles->refreshModels(_nodeModel, _edgeModel); + show(); + } else { + QMessageBox::warning(0, + "Bad style file.", + "Bad style file: '" + tikzit->styleFile() + "'. Check that the file exists and is properly formatted."); + } } void StyleEditor::updateFields() @@ -176,15 +185,24 @@ void StyleEditor::updateFields() } } - void StyleEditor::on_fillColor_clicked() { - QColor col = QColorDialog::getColor( - color(ui->fillColor), - this, - "Fill Color", - QColorDialog::DontUseNativeDialog); - if (col.isValid()) setColor(ui->fillColor, col); + updateColor(ui->fillColor, "Fill Color", "fill"); +} + +void StyleEditor::on_drawColor_clicked() +{ + updateColor(ui->drawColor, "Draw Color", "draw"); +} + +void StyleEditor::on_tikzitFillColor_clicked() +{ + updateColor(ui->tikzitFillColor, "TikZiT Fill Color", "tikzit fill"); +} + +void StyleEditor::on_tikzitDrawColor_clicked() +{ + updateColor(ui->tikzitDrawColor, "TikZiT Draw Color", "tikzit draw"); } void StyleEditor::on_styleListView_clicked() @@ -194,7 +212,8 @@ void StyleEditor::on_styleListView_clicked() const QModelIndexList i = ui->styleListView->selectionModel()->selectedIndexes(); QString sty; if (!i.isEmpty()) { - sty = i[0].data().toString(); + _activeItem = _nodeModel->itemFromIndex(i[0]); + sty = _activeItem->text(); if (sty != "none") _activeNodeStyle = tikzit->styles()->nodeStyle(sty); } @@ -208,13 +227,26 @@ void StyleEditor::on_edgeStyleListView_clicked() const QModelIndexList i = ui->edgeStyleListView->selectionModel()->selectedIndexes(); QString sty; if (!i.isEmpty()) { - sty = i[0].data().toString(); + _activeItem = _edgeModel->itemFromIndex(i[0]); + sty = _activeItem->text(); if (sty != "none") _activeEdgeStyle = tikzit->styles()->edgeStyle(sty); } updateFields(); } +void StyleEditor::on_name_editingFinished() +{ + Style *s; + if (_activeNodeStyle != 0) s = _activeNodeStyle; + else if (_activeEdgeStyle != 0) s = _activeEdgeStyle; + else return; + + s->setName(ui->name->text()); + _activeItem->setText(ui->name->text()); + qDebug("got here"); +} + void StyleEditor::setColor(QPushButton *btn, QColor col) { QPalette pal = btn->palette(); @@ -228,3 +260,22 @@ QColor StyleEditor::color(QPushButton *btn) QPalette pal = btn->palette(); return pal.color(QPalette::Button); } + +void StyleEditor::updateColor(QPushButton *btn, QString name, QString propName) +{ + QColor col = QColorDialog::getColor( + color(btn), + this, + name, + QColorDialog::DontUseNativeDialog); + if (col.isValid()) { + setColor(btn, col); + if (_activeNodeStyle != 0) { + _activeNodeStyle->data()->setProperty(propName, tikzit->nameForColor(col)); + _activeItem->setIcon(_activeNodeStyle->icon()); + } else if (_activeEdgeStyle != 0) { + _activeEdgeStyle->data()->setProperty(propName, tikzit->nameForColor(col)); + _activeItem->setIcon(_activeEdgeStyle->icon()); + } + } +} diff --git a/src/gui/styleeditor.h b/src/gui/styleeditor.h index 3a8dd9d..080fb6f 100644 --- a/src/gui/styleeditor.h +++ b/src/gui/styleeditor.h @@ -3,6 +3,7 @@ #include "nodestyle.h" #include "edgestyle.h" +#include "tikzstyles.h" #include <QMainWindow> #include <QPushButton> @@ -20,22 +21,29 @@ public: explicit StyleEditor(QWidget *parent = 0); ~StyleEditor(); - void showEvent(QShowEvent *) override; void updateFields(); + void open(); public slots: - void on_fillColor_clicked(); void on_styleListView_clicked(); void on_edgeStyleListView_clicked(); + void on_name_editingFinished(); + void on_fillColor_clicked(); + void on_drawColor_clicked(); + void on_tikzitFillColor_clicked(); + void on_tikzitDrawColor_clicked(); private: Ui::StyleEditor *ui; void setColor(QPushButton *btn, QColor col); QColor color(QPushButton *btn); QStandardItemModel *_nodeModel; QStandardItemModel *_edgeModel; + QStandardItem *_activeItem; NodeStyle *_activeNodeStyle; EdgeStyle *_activeEdgeStyle; + TikzStyles *_styles; + void updateColor(QPushButton *btn, QString name, QString propName); }; #endif // STYLEEDITOR_H diff --git a/src/gui/stylepalette.ui b/src/gui/stylepalette.ui index 10b80fb..044bd02 100644 --- a/src/gui/stylepalette.ui +++ b/src/gui/stylepalette.ui @@ -60,7 +60,7 @@ <item> <widget class="QToolButton" name="buttonOpenTikzstyles"> <property name="toolTip"> - <string>New Project</string> + <string>Load Styles</string> </property> <property name="text"> <string/> @@ -74,7 +74,7 @@ <item> <widget class="QToolButton" name="buttonEditTikzstyles"> <property name="toolTip"> - <string>New Project</string> + <string>Edit styles</string> </property> <property name="text"> <string/> @@ -87,6 +87,9 @@ </item> <item> <widget class="QToolButton" name="buttonRefreshTikzstyles"> + <property name="toolTip"> + <string>Refresh styles</string> + </property> <property name="text"> <string/> </property> diff --git a/src/tikzit.cpp b/src/tikzit.cpp index 1dc8bd9..a327f9f 100644 --- a/src/tikzit.cpp +++ b/src/tikzit.cpp @@ -39,6 +39,36 @@ Tikzit::Tikzit() : _styleFile("[default]"), _activeWindow(0) void Tikzit::init(QApplication *app) { QSettings settings("tikzit", "tikzit"); + + // 19 standard xcolor colours + _colNames << + "black" << + "gray" << + "darkgray" << + "lightgray" << + "white" << + + "red" << + "orange" << + "yellow" << + "lime" << + "blue" << + "purple" << + + "brown" << + "olive" << + "green" << + "teal" << + "cyan" << + + "magenta" << + "violet" << + "pink"; + + for (int i = 0; i < _colNames.length(); ++i) { + _cols << QColor(_colNames[i]); + } + _mainMenu = new MainMenu(); QMainWindow *dummy = new QMainWindow(); @@ -64,6 +94,48 @@ void Tikzit::init(QApplication *app) // return _mainMenu; //} +QColor Tikzit::colorByIndex(int i) +{ + return _cols[i]; +} + +QColor Tikzit::colorByName(QString name) +{ + for (int i = 0; i < _colNames.length(); ++i) { + if (_colNames[i] == name) return _cols[i]; + } + + QRegExp re( + "rgb\\s*,\\s*255\\s*:\\s*" + "red\\s*,\\s*([0-9]+)\\s*;\\s*" + "green\\s*,\\s*([0-9]+)\\s*;\\s*" + "blue\\s*,\\s*([0-9]+)\\s*" + ); + + if (re.exactMatch(name)) { + QStringList cap = re.capturedTexts(); + //qDebug() << cap; + return QColor( + cap[1].toInt(), + cap[2].toInt(), + cap[3].toInt()); + } + + return QColor(); +} + +QString Tikzit::nameForColor(QColor col) +{ + for (int i = 0; i < _colNames.length(); ++i) { + if (_cols[i] == col) return _colNames[i]; + } + + // if the color is not recognised, return it in tikz-readable RBG format + return "rgb,255: red,"+ QString::number(col.red()) + + "; green," + QString::number(col.green()) + + "; blue," + QString::number(col.blue()); +} + ToolPalette *Tikzit::toolPalette() const { return _toolPalette; @@ -141,46 +213,49 @@ void Tikzit::openTikzStyles() { tr("TiKZ Style Files (*.tikzstyles)")); if (!fileName.isEmpty()) { - loadStyles(fileName); + QFileInfo fi(fileName); + if (fi.exists() && loadStyles(fileName)) { + QSettings settings("tikzit", "tikzit"); + settings.setValue("previous-tikzstyles-path", fi.absolutePath()); + settings.setValue("previous-tikzstyles-file", fileName); + } else { + // BAD STYLE FILE + } } } -void Tikzit::loadStyles(QString fileName) +bool 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"; + QFileInfo fi(fileName); + if (fi.exists()) { + TikzStyles *st = new TikzStyles(this); + if (st->loadStyles(fileName)) { + _styleFile = fi.fileName(); + _styleFilePath = fi.absoluteFilePath(); + delete _styles; + _styles = st; + + foreach (MainWindow *w, _windows) { + w->tikzScene()->reloadStyles(); + } + return true; } else { - qDebug() << "parse failed"; - } - //_stylePalette->reloadStyles(); - - foreach (MainWindow *w, _windows) { - w->tikzScene()->reloadStyles(); + QMessageBox::warning(0, + "Bad style file.", + "Bad style file: '" + fileName + "'. Check the file is properly formatted and try to load it again."); + return false; } } else { - settings.setValue("previous-tikzstyles-file", ""); - QMessageBox::warning(0, "Style file not found.", "Could not open style file: '" + fileName + "', reverting to default."); + //settings.setValue("previous-tikzstyles-file", ""); + QMessageBox::warning(0, "Style file not found.", "Could not open style file: '" + fileName + "'."); + return false; } } void Tikzit::showStyleEditor() { - _styleEditor->show(); + _styleEditor->open(); } QString Tikzit::styleFile() const @@ -198,6 +273,11 @@ void Tikzit::focusChanged(QWidget *old, QWidget *nw) // } } +QString Tikzit::styleFilePath() const +{ + return _styleFilePath; +} + //StylePalette *Tikzit::stylePalette() const //{ // return _stylePalette; diff --git a/src/tikzit.h b/src/tikzit.h index 3369962..35b19de 100644 --- a/src/tikzit.h +++ b/src/tikzit.h @@ -62,10 +62,12 @@ #include <QObject> #include <QVector> +#include <QStringList> #include <QPointF> #include <QMenuBar> #include <QMainWindow> #include <QFont> +#include <QColor> // Number of pixels between (0,0) and (1,0) at 100% zoom level. This should be // divisible by 8 to avoid rounding errors with e.g. grid-snapping. @@ -115,13 +117,20 @@ public: void quit(); void init(QApplication *app); + // convenience functions for named colors + QColor colorByIndex(int i); + QColor colorByName(QString name); + QString nameForColor(QColor col); + void openTikzStyles(); - void loadStyles(QString fileName); + bool loadStyles(QString fileName); void showStyleEditor(); TikzStyles *styles() const; QString styleFile() const; //StylePalette *stylePalette() const; + QString styleFilePath() const; + public slots: void focusChanged(QWidget *old, QWidget *nw); private: @@ -135,7 +144,10 @@ private: MainWindow *_activeWindow; TikzStyles *_styles; QString _styleFile; + QString _styleFilePath; StyleEditor *_styleEditor; + QStringList _colNames; + QVector<QColor> _cols; }; extern Tikzit *tikzit; |