From 20e06dd885819f7b16711ac5359fcdcfb8d26c86 Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Sat, 4 Aug 2018 18:16:18 +0200 Subject: style editor --- src/data/graphelementdata.cpp | 11 +- src/data/nodestyle.cpp | 17 ++-- src/data/nodestyle.h | 6 +- src/data/style.cpp | 1 + src/data/style.h | 2 +- src/data/tikzstyles.cpp | 2 - src/gui/styleeditor.cpp | 227 ++++++++++++++++++++++++++++++++---------- src/gui/styleeditor.h | 12 ++- src/gui/styleeditor.ui | 221 ++++++++++++++-------------------------- 9 files changed, 280 insertions(+), 219 deletions(-) (limited to 'src') diff --git a/src/data/graphelementdata.cpp b/src/data/graphelementdata.cpp index fcd90ea..85ae4cd 100644 --- a/src/data/graphelementdata.cpp +++ b/src/data/graphelementdata.cpp @@ -106,7 +106,7 @@ bool GraphElementData::atom(QString atom) QVariant GraphElementData::data(const QModelIndex &index, int role) const { - if (role != Qt::DisplayRole) + if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); if (index.row() >= 0 && index.row() < _properties.length()) { @@ -135,9 +135,10 @@ QModelIndex GraphElementData::index(int row, int column, const QModelIndex &pare QModelIndex GraphElementData::parent(const QModelIndex &index) const { - GraphElementProperty *p = static_cast(index.internalPointer()); - if (p == root) return QModelIndex(); - else return createIndex(0,0,static_cast(root)); + //GraphElementProperty *p = static_cast(index.internalPointer()); + //if (p == root) return QModelIndex(); + //else return createIndex(0,0,static_cast(root)); + return QModelIndex(); } int GraphElementData::rowCount(const QModelIndex &parent) const @@ -156,7 +157,7 @@ int GraphElementData::columnCount(const QModelIndex &) const Qt::ItemFlags GraphElementData::flags(const QModelIndex &index) const { - return QAbstractItemModel::flags(index); + return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; } //bool GraphElementData::setData(const QModelIndex &index, const QVariant &value, int role) diff --git a/src/data/nodestyle.cpp b/src/data/nodestyle.cpp index aec899f..9c38dfd 100644 --- a/src/data/nodestyle.cpp +++ b/src/data/nodestyle.cpp @@ -45,20 +45,21 @@ QBrush NodeStyle::brush() const return QBrush(fillColor()); } -NodeStyle::Shape NodeStyle::shape(bool tikzitOverride) const +QString NodeStyle::shape(bool tikzitOverride) const { - if (_data == 0) return NodeStyle::Circle; - - QString sh = propertyWithDefault("shape", "circle", tikzitOverride); - if (sh == "circle") return NodeStyle::Circle; - else if (sh == "rectangle") return NodeStyle::Rectangle; - else return NodeStyle::Circle; + return propertyWithDefault("shape", "circle", tikzitOverride); } QPainterPath NodeStyle::path() const { QPainterPath pth; - pth.addEllipse(QPointF(0.0f,0.0f), 30.0f, 30.0f); + QString sh = shape(); + + if (sh == "rectangle") { + pth.addRect(-30.0f, -30.0f, 60.0f, 60.0f); + } else { // default is 'circle' + pth.addEllipse(QPointF(0.0f,0.0f), 30.0f, 30.0f); + } return pth; } diff --git a/src/data/nodestyle.h b/src/data/nodestyle.h index ce36006..67219db 100644 --- a/src/data/nodestyle.h +++ b/src/data/nodestyle.h @@ -30,17 +30,13 @@ class NodeStyle : public Style { public: - enum Shape { - Rectangle, UpTriangle, DownTriangle, Circle - }; - NodeStyle(); NodeStyle(QString name, GraphElementData *data); QColor fillColor(bool tikzitOverride=true) const; QBrush brush() const; QPainterPath path() const override; - Shape shape(bool tikzitOverride=true) const; + QString shape(bool tikzitOverride=true) const; QPainterPath palettePath() const override; QIcon icon() const override; diff --git a/src/data/style.cpp b/src/data/style.cpp index 2e19c4f..0128d36 100644 --- a/src/data/style.cpp +++ b/src/data/style.cpp @@ -65,6 +65,7 @@ QPen Style::pen() const QString Style::propertyWithDefault(QString prop, QString def, bool tikzitOverride) const { + if (_data == 0) return def; QString val; if (tikzitOverride) { val = _data->property("tikzit " + prop); diff --git a/src/data/style.h b/src/data/style.h index 7a6ff18..1b0618e 100644 --- a/src/data/style.h +++ b/src/data/style.h @@ -47,9 +47,9 @@ public: virtual QPainterPath palettePath() const = 0; virtual QIcon icon() const = 0; void setName(const QString &name); + QString propertyWithDefault(QString prop, QString def, bool tikzitOverride=true) const; protected: - QString propertyWithDefault(QString prop, QString def, bool tikzitOverride=true) const; QString _name; GraphElementData *_data; }; diff --git a/src/data/tikzstyles.cpp b/src/data/tikzstyles.cpp index 4a92d6d..a743d0c 100644 --- a/src/data/tikzstyles.cpp +++ b/src/data/tikzstyles.cpp @@ -120,10 +120,8 @@ void TikzStyles::addStyle(QString name, GraphElementData *data) 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); } } diff --git a/src/gui/styleeditor.cpp b/src/gui/styleeditor.cpp index 9081873..e113c6c 100644 --- a/src/gui/styleeditor.cpp +++ b/src/gui/styleeditor.cpp @@ -11,6 +11,12 @@ StyleEditor::StyleEditor(QWidget *parent) : ui(new Ui::StyleEditor) { ui->setupUi(this); + _formWidgets << ui->name << ui->category << + ui->fillColor << ui->hasTikzitFillColor << ui->tikzitFillColor << + ui->drawColor << ui->hasTikzitDrawColor << ui->tikzitDrawColor << + ui->shape << ui->hasTikzitShape << ui->tikzitShape << + ui->leftArrow << ui->rightArrow << + ui->properties; setColor(ui->fillColor, QColor(Qt::white)); setColor(ui->drawColor, QColor(Qt::black)); @@ -27,12 +33,18 @@ StyleEditor::StyleEditor(QWidget *parent) : ui->styleListView->setMovement(QListView::Static); ui->styleListView->setGridSize(QSize(48,48)); - ui->edgeStyleListView->setModel(_edgeModel); ui->edgeStyleListView->setViewMode(QListView::IconMode); ui->edgeStyleListView->setMovement(QListView::Static); ui->edgeStyleListView->setGridSize(QSize(48,48)); + connect(ui->styleListView->selectionModel(), + SIGNAL(currentChanged(QModelIndex,QModelIndex)), + this, SLOT(nodeItemChanged(QModelIndex))); + connect(ui->edgeStyleListView->selectionModel(), + SIGNAL(currentChanged(QModelIndex,QModelIndex)), + this, SLOT(edgeItemChanged(QModelIndex))); + // setup the color dialog to display only the named colors that tikzit/xcolor knows // about as "standard colors". for (int i = 0; i < 48; ++i) { @@ -69,7 +81,7 @@ StyleEditor::StyleEditor(QWidget *parent) : _activeNodeStyle = 0; _activeEdgeStyle = 0; - updateFields(); + refreshDisplay(); } StyleEditor::~StyleEditor() @@ -82,6 +94,7 @@ void StyleEditor::open() { _styles = new TikzStyles; if (_styles->loadStyles(tikzit->styleFilePath())) { _styles->refreshModels(_nodeModel, _edgeModel); + _dirty = false; show(); } else { QMessageBox::warning(0, @@ -90,24 +103,95 @@ void StyleEditor::open() { } } -void StyleEditor::updateFields() +void StyleEditor::closeEvent(QCloseEvent *event) +{ + if (_dirty) { + QMessageBox::StandardButton resBtn = QMessageBox::question( + this, "Save Changes", + "Do you wish to save changes to " + tikzit->styleFile() + "?", + QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, + QMessageBox::Yes); + + if (resBtn == QMessageBox::Yes) { + // TODO save here + event->accept(); + } else if (resBtn == QMessageBox::No) { + event->accept(); + } else { + event->ignore(); + } + } else { + event->accept(); + } +} + +void StyleEditor::nodeItemChanged(QModelIndex sel) +{ + //ui->edgeStyleListView->blockSignals(true); + ui->edgeStyleListView->selectionModel()->clear(); + //ui->edgeStyleListView->blockSignals(false); + qDebug() << "got node item change"; + + _activeNodeStyle = 0; + _activeEdgeStyle = 0; + QString sty; + if (sel.isValid()) { + _activeItem = _nodeModel->itemFromIndex(sel); + sty = _activeItem->text(); + if (sty != "none") + _activeNodeStyle = tikzit->styles()->nodeStyle(sty); + } + refreshDisplay(); +} + +void StyleEditor::edgeItemChanged(QModelIndex sel) { - ui->name->setEnabled(false); - ui->category->setEnabled(false); - ui->fillColor->setEnabled(false); - ui->drawColor->setEnabled(false); - ui->tikzitFillColor->setEnabled(false); - ui->tikzitDrawColor->setEnabled(false); - ui->hasTikzitFillColor->setEnabled(false); - ui->hasTikzitDrawColor->setEnabled(false); - ui->shape->setEnabled(false); - ui->hasTikzitShape->setEnabled(false); - ui->tikzitShape->setEnabled(false); - ui->leftArrow->setEnabled(false); - ui->rightArrow->setEnabled(false); - ui->properties->setEnabled(false); + //ui->styleListView->blockSignals(true); + ui->styleListView->selectionModel()->clear(); + //ui->styleListView->blockSignals(false); + qDebug() << "got edge item change"; + + _activeNodeStyle = 0; + _activeEdgeStyle = 0; + + QString sty; + if (sel.isValid()) { + _activeItem = _edgeModel->itemFromIndex(sel); + sty = _activeItem->text(); + if (sty != "none") + _activeEdgeStyle = tikzit->styles()->edgeStyle(sty); + } + refreshDisplay(); +} + +void StyleEditor::refreshDisplay() +{ + // disable all fields and block signals while we set their values + foreach (QWidget *w, _formWidgets) { + w->setEnabled(false); + w->blockSignals(true); + } + + // set to default values + ui->name->setText("none"); + ui->category->setCurrentText(""); + setColor(ui->fillColor, QColor(Qt::gray)); + setColor(ui->drawColor, QColor(Qt::gray)); + setColor(ui->tikzitFillColor, QColor(Qt::gray)); + setColor(ui->tikzitDrawColor, QColor(Qt::gray)); + ui->hasTikzitDrawColor->setChecked(false); + ui->hasTikzitFillColor->setChecked(false); + ui->shape->setCurrentText(""); + ui->hasTikzitShape->setChecked(false); + ui->tikzitShape->setCurrentText(""); + ui->leftArrow->setCurrentText(""); + ui->rightArrow->setCurrentText(""); + ui->properties->setModel(0); if (_activeNodeStyle != 0) { + _activeItem->setText(_activeNodeStyle->name()); + _activeItem->setIcon(_activeNodeStyle->icon()); + ui->name->setEnabled(true); ui->name->setText(_activeNodeStyle->name()); @@ -133,26 +217,35 @@ void StyleEditor::updateFields() ui->hasTikzitFillColor->setChecked(fillOverride); ui->tikzitFillColor->setEnabled(fillOverride); - setColor(ui->tikzitFillColor, fill); + if (fillOverride) setColor(ui->tikzitFillColor, fill); ui->hasTikzitDrawColor->setEnabled(true); ui->hasTikzitDrawColor->setChecked(drawOverride); ui->tikzitDrawColor->setEnabled(drawOverride); - setColor(ui->tikzitDrawColor, draw); + if (drawOverride) setColor(ui->tikzitDrawColor, draw); - // TODO + QString realShape = _activeNodeStyle->propertyWithDefault("shape", "", false); + QString shape = _activeNodeStyle->propertyWithDefault("tikzit shape", "", false); + bool shapeOverride = shape != realShape; ui->shape->setEnabled(true); + ui->shape->setCurrentText(realShape); + ui->hasTikzitShape->setEnabled(true); - ui->tikzitShape->setEnabled(true); + ui->tikzitShape->setEnabled(shapeOverride); + if (shapeOverride) ui->tikzitShape->setCurrentText(shape); + ui->properties->setEnabled(true); + ui->properties->setModel(_activeNodeStyle->data()); + qDebug() << _activeNodeStyle->data()->tikz(); } else if (_activeEdgeStyle != 0) { + _activeItem->setText(_activeEdgeStyle->name()); + _activeItem->setIcon(_activeEdgeStyle->icon()); ui->name->setEnabled(true); ui->name->setText(_activeEdgeStyle->name()); - ui->category->setEnabled(true); // TODO - + ui->category->setEnabled(true); setColor(ui->fillColor, QColor(Qt::gray)); setColor(ui->tikzitFillColor, QColor(Qt::gray)); @@ -173,9 +266,34 @@ void StyleEditor::updateFields() ui->tikzitDrawColor->setEnabled(drawOverride); setColor(ui->tikzitDrawColor, draw); - // TODO ui->leftArrow->setEnabled(true); + + switch (_activeEdgeStyle->arrowTail()) { + case EdgeStyle::NoTip: + ui->leftArrow->setCurrentText(""); + break; + case EdgeStyle::Pointer: + ui->leftArrow->setCurrentText("<"); + break; + case EdgeStyle::Flat: + ui->leftArrow->setCurrentText("|"); + break; + } + ui->rightArrow->setEnabled(true); + switch (_activeEdgeStyle->arrowHead()) { + case EdgeStyle::NoTip: + ui->rightArrow->setCurrentText(""); + break; + case EdgeStyle::Pointer: + ui->rightArrow->setCurrentText(">"); + break; + case EdgeStyle::Flat: + ui->rightArrow->setCurrentText("|"); + break; + } + + // TODO ui->properties->setEnabled(true); } else { setColor(ui->fillColor, QColor(Qt::gray)); @@ -183,6 +301,11 @@ void StyleEditor::updateFields() setColor(ui->tikzitDrawColor, QColor(Qt::gray)); setColor(ui->tikzitFillColor, QColor(Qt::gray)); } + + // unblock signals so we are ready for user input + foreach (QWidget *w, _formWidgets) { + w->blockSignals(false); + } } void StyleEditor::on_fillColor_clicked() @@ -205,34 +328,24 @@ void StyleEditor::on_tikzitDrawColor_clicked() updateColor(ui->tikzitDrawColor, "TikZiT Draw Color", "tikzit draw"); } +void StyleEditor::on_save_clicked() +{ + save(); + close(); +} + +void StyleEditor::save() +{ + _dirty = false; + // TODO +} + void StyleEditor::on_styleListView_clicked() { - _activeNodeStyle = 0; - _activeEdgeStyle = 0; - const QModelIndexList i = ui->styleListView->selectionModel()->selectedIndexes(); - QString sty; - if (!i.isEmpty()) { - _activeItem = _nodeModel->itemFromIndex(i[0]); - sty = _activeItem->text(); - if (sty != "none") - _activeNodeStyle = tikzit->styles()->nodeStyle(sty); - } - updateFields(); } void StyleEditor::on_edgeStyleListView_clicked() { - _activeNodeStyle = 0; - _activeEdgeStyle = 0; - const QModelIndexList i = ui->edgeStyleListView->selectionModel()->selectedIndexes(); - QString sty; - if (!i.isEmpty()) { - _activeItem = _edgeModel->itemFromIndex(i[0]); - sty = _activeItem->text(); - if (sty != "none") - _activeEdgeStyle = tikzit->styles()->edgeStyle(sty); - } - updateFields(); } void StyleEditor::on_name_editingFinished() @@ -243,8 +356,19 @@ void StyleEditor::on_name_editingFinished() else return; s->setName(ui->name->text()); - _activeItem->setText(ui->name->text()); - qDebug("got here"); + //_activeItem->setText(ui->name->text()); + refreshDisplay(); + _dirty = true; +} + +void StyleEditor::on_shape_currentTextChanged() +{ + if (_activeNodeStyle != 0) { + _activeNodeStyle->data()->setProperty("shape", ui->shape->currentText()); + //_activeItem->setIcon(_activeNodeStyle->icon()); + refreshDisplay(); + _dirty = true; + } } void StyleEditor::setColor(QPushButton *btn, QColor col) @@ -272,10 +396,13 @@ void StyleEditor::updateColor(QPushButton *btn, QString name, QString propName) setColor(btn, col); if (_activeNodeStyle != 0) { _activeNodeStyle->data()->setProperty(propName, tikzit->nameForColor(col)); - _activeItem->setIcon(_activeNodeStyle->icon()); +// _activeItem->setIcon(_activeNodeStyle->icon()); } else if (_activeEdgeStyle != 0) { _activeEdgeStyle->data()->setProperty(propName, tikzit->nameForColor(col)); - _activeItem->setIcon(_activeEdgeStyle->icon()); +// _activeItem->setIcon(_activeEdgeStyle->icon()); } + + refreshDisplay(); + _dirty = true; } } diff --git a/src/gui/styleeditor.h b/src/gui/styleeditor.h index 080fb6f..82f019e 100644 --- a/src/gui/styleeditor.h +++ b/src/gui/styleeditor.h @@ -21,18 +21,26 @@ public: explicit StyleEditor(QWidget *parent = 0); ~StyleEditor(); - void updateFields(); + void refreshDisplay(); void open(); + void save(); + void closeEvent(QCloseEvent *event) override; public slots: + void nodeItemChanged(QModelIndex sel); + void edgeItemChanged(QModelIndex sel); void on_styleListView_clicked(); void on_edgeStyleListView_clicked(); void on_name_editingFinished(); + void on_shape_currentTextChanged(); void on_fillColor_clicked(); void on_drawColor_clicked(); void on_tikzitFillColor_clicked(); void on_tikzitDrawColor_clicked(); + + void on_save_clicked(); + private: Ui::StyleEditor *ui; void setColor(QPushButton *btn, QColor col); @@ -44,6 +52,8 @@ private: EdgeStyle *_activeEdgeStyle; TikzStyles *_styles; void updateColor(QPushButton *btn, QString name, QString propName); + QVector _formWidgets; + bool _dirty; }; #endif // STYLEEDITOR_H diff --git a/src/gui/styleeditor.ui b/src/gui/styleeditor.ui index 9888954..697a084 100644 --- a/src/gui/styleeditor.ui +++ b/src/gui/styleeditor.ui @@ -7,11 +7,11 @@ 0 0 580 - 519 + 517 - MainWindow + Style Editor - TikZiT @@ -33,11 +33,6 @@ 20 - - - 10 - - Name @@ -57,11 +52,6 @@ 20 - - - 10 - - Category @@ -81,11 +71,6 @@ 20 - - - 10 - - Fill Color @@ -105,11 +90,6 @@ 20 - - - 10 - - Draw Color @@ -190,15 +170,14 @@ - 380 + 390 110 - 71 + 61 17 - 10 false @@ -209,15 +188,14 @@ - 380 + 390 140 - 71 + 61 17 - 10 false @@ -272,11 +250,6 @@ 20 - - - 10 - - Shape @@ -318,15 +291,14 @@ - 280 + 290 200 - 71 + 61 17 - 10 false @@ -343,6 +315,21 @@ 22 + + + + + + + + circle + + + + + rectangle + + @@ -353,11 +340,6 @@ 20 - - - 10 - - Arrowhead @@ -373,12 +355,12 @@ 270 230 - 71 + 41 22 - true + false @@ -390,51 +372,16 @@ < - - - > - - | - - - stealth - - - - - latex - - - - - [ - - - - - ] - - - - - ( - - - - - ) - - - 350 + 320 230 16 21 @@ -452,14 +399,14 @@ - 370 + 340 230 - 71 + 41 22 - true + false @@ -471,46 +418,11 @@ > - - - < - - | - - - stealth - - - - - latex - - - - - ] - - - - - [ - - - - - ) - - - - - ( - - @@ -521,11 +433,6 @@ 20 - - - 10 - - Properties @@ -536,31 +443,11 @@ Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - 270 - 270 - 301 - 211 - - - - - Name - - - - - Value - - - 270 - 490 + 460 23 18 @@ -573,7 +460,7 @@ 300 - 490 + 460 23 18 @@ -586,7 +473,7 @@ 330 - 490 + 460 23 18 @@ -599,7 +486,7 @@ 360 - 490 + 460 23 18 @@ -612,7 +499,7 @@ 390 - 490 + 460 23 18 @@ -745,6 +632,46 @@ v + + + + 489 + 490 + 81 + 20 + + + + Save and Close + + + + + + 270 + 270 + 301 + 181 + + + + + 8 + + + + false + + + true + + + false + + + true + + -- cgit v1.2.3