From 38582ebd4b974150cbe446ac92c9a4a7024f5f09 Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Tue, 9 Oct 2018 10:33:08 +0200 Subject: style editor is finished --- src/data/stylelist.cpp | 35 ++++++++++ src/data/stylelist.h | 6 ++ src/gui/styleeditor.cpp | 182 ++++++++++++++++++++++++++++++++++++++++++------ src/gui/styleeditor.h | 14 ++++ 4 files changed, 217 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/data/stylelist.cpp b/src/data/stylelist.cpp index c8baf3f..14302c7 100644 --- a/src/data/stylelist.cpp +++ b/src/data/stylelist.cpp @@ -41,6 +41,13 @@ void StyleList::addStyle(Style *s) } } +void StyleList::removeNthStyle(int n) +{ + beginRemoveRows(QModelIndex(), n, n); + _styles.remove(nthInCategory(n)); + endRemoveRows(); +} + void StyleList::clear() { int n = numInCategory(); @@ -113,6 +120,34 @@ int StyleList::rowCount(const QModelIndex &/*parent*/) const return numInCategory(); } +bool StyleList::moveRows(const QModelIndex &sourceParent, + int sourceRow, + int /*count*/, + const QModelIndex &destinationParent, + int destinationRow) +{ + if (sourceRow >= 1 && sourceRow < numInCategory() && + destinationRow >= 1 && destinationRow <= numInCategory()) + { + beginMoveRows(sourceParent, sourceRow, sourceRow, destinationParent, destinationRow); + int sourceIndex = nthInCategory(sourceRow); + int destinationIndex = nthInCategory(destinationRow); + if (destinationIndex == -1) + destinationIndex = _styles.length(); + Style *s = _styles[sourceIndex]; + _styles.remove(sourceIndex); + if (sourceIndex < destinationIndex) { + _styles.insert(destinationIndex - 1, s); + } else { + _styles.insert(destinationIndex, s); + } + endMoveRows(); + return true; + } else { + return false; + } +} + QString StyleList::category() const { return _category; diff --git a/src/data/stylelist.h b/src/data/stylelist.h index f698761..eb1c43a 100644 --- a/src/data/stylelist.h +++ b/src/data/stylelist.h @@ -14,6 +14,7 @@ public: Style *style(int i); int length() const; void addStyle(Style *s); + void removeNthStyle(int n); void clear(); QString tikz(); @@ -23,6 +24,11 @@ public: QVariant data(const QModelIndex &index, int role) const override; int rowCount(const QModelIndex &/*parent*/) const override; + bool moveRows(const QModelIndex &sourceParent, + int sourceRow, + int /*count*/, + const QModelIndex &destinationParent, + int destinationChild); QString category() const; diff --git a/src/gui/styleeditor.cpp b/src/gui/styleeditor.cpp index 36ae93a..63d347b 100644 --- a/src/gui/styleeditor.cpp +++ b/src/gui/styleeditor.cpp @@ -18,6 +18,7 @@ StyleEditor::StyleEditor(QWidget *parent) : ui->leftArrow << ui->rightArrow << ui->properties; + setWindowIcon(QIcon(":/images/logo.png")); _styles = nullptr; _activeStyle = nullptr; @@ -92,7 +93,7 @@ void StyleEditor::open() { this, SLOT(edgeItemChanged(QModelIndex))); if (_styles->loadStyles(tikzit->styleFilePath())) { - _dirty = false; + setDirty(false); refreshCategories(); refreshDisplay(); show(); @@ -105,7 +106,7 @@ void StyleEditor::open() { void StyleEditor::closeEvent(QCloseEvent *event) { - if (_dirty) { + if (dirty()) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, "Save Changes", "Do you wish to save changes to " + tikzit->styleFile() + "?", @@ -113,7 +114,7 @@ void StyleEditor::closeEvent(QCloseEvent *event) QMessageBox::Yes); if (resBtn == QMessageBox::Yes) { - // TODO save here + save(); event->accept(); } else if (resBtn == QMessageBox::No) { event->accept(); @@ -127,6 +128,7 @@ void StyleEditor::closeEvent(QCloseEvent *event) void StyleEditor::nodeItemChanged(QModelIndex sel) { + qDebug() << "nodeItemChanged, new index:" << sel.row(); if (sel.isValid()) { ui->edgeStyleListView->selectionModel()->clear(); _activeStyle = _styles->nodeStyles()->styleInCategory(sel.row()); @@ -154,7 +156,7 @@ void StyleEditor::categoryChanged() if (s != nullptr && s->data()->property("tikzit category") != cat) { if (cat.isEmpty()) s->data()->unsetProperty("tikzit category"); else s->data()->setProperty("tikzit category", cat); - _dirty = true; + setDirty(true); refreshCategories(); if (_styles->nodeStyles()->category() != "") { @@ -225,7 +227,7 @@ void StyleEditor::propertyChanged() } else if (_edgeStyleIndex.isValid()) { emit _styles->edgeStyles()->dataChanged(_edgeStyleIndex, _edgeStyleIndex); } - _dirty = true; + setDirty(true); refreshDisplay(); } @@ -385,7 +387,7 @@ void StyleEditor::on_addProperty_clicked() Style *s = activeStyle(); if (s != 0) { s->data()->add(GraphElementProperty("new property", "")); - _dirty = true; + setDirty(true); } } @@ -394,7 +396,7 @@ void StyleEditor::on_addAtom_clicked() Style *s = activeStyle(); if (s != 0) { s->data()->add(GraphElementProperty("new atom")); - _dirty = true; + setDirty(true); } } @@ -405,7 +407,7 @@ void StyleEditor::on_removeProperty_clicked() QModelIndexList sel = ui->properties->selectionModel()->selectedRows(); if (!sel.isEmpty()) { s->data()->removeRows(sel[0].row(), 1, sel[0].parent()); - _dirty = true; + setDirty(true); } } } @@ -421,7 +423,7 @@ void StyleEditor::on_propertyUp_clicked() sel[0].row(), 1, sel[0].parent(), sel[0].row() - 1); - _dirty = true; + setDirty(true); } } } @@ -437,7 +439,7 @@ void StyleEditor::on_propertyDown_clicked() sel[0].row(), 1, sel[0].parent(), sel[0].row() + 2); - _dirty = true; + setDirty(true); } } } @@ -467,12 +469,123 @@ void StyleEditor::on_addStyle_clicked() _styles->nodeStyles()->addStyle(s); // set dirty flag and select the newly-added style - _dirty = true; -// ui->styleListView->selectionModel()->clear(); -// ui->edgeStyleListView->selectionModel()->clear(); - ui->styleListView->selectionModel()->setCurrentIndex( - _styles->nodeStyles()->index(_styles->nodeStyles()->numInCategory()-1), - QItemSelectionModel::ClearAndSelect); + setDirty(true); + selectNodeStyle(_styles->nodeStyles()->numInCategory()-1); +} + +void StyleEditor::on_removeStyle_clicked() +{ + if (_nodeStyleIndex.isValid()) { + int i = _nodeStyleIndex.row(); + if (i > 0) { + ui->styleListView->selectionModel()->clear(); + _styles->nodeStyles()->removeNthStyle(i); + setDirty(true); + if (i < _styles->nodeStyles()->numInCategory()) { + selectNodeStyle(i); + } + } + } +} + +void StyleEditor::on_styleUp_clicked() +{ + if (_nodeStyleIndex.isValid()) { + int r = _nodeStyleIndex.row(); + if (_styles->nodeStyles()->moveRows( + _nodeStyleIndex.parent(), + r, 1, + _nodeStyleIndex.parent(), + r - 1)) + { + setDirty(true); + nodeItemChanged(_styles->nodeStyles()->index(r - 1)); + } + } +} + +void StyleEditor::on_styleDown_clicked() +{ + if (_nodeStyleIndex.isValid()) { + int r = _nodeStyleIndex.row(); + if (_styles->nodeStyles()->moveRows( + _nodeStyleIndex.parent(), + r, 1, + _nodeStyleIndex.parent(), + r + 2)) + { + setDirty(true); + nodeItemChanged(_styles->nodeStyles()->index(r + 1)); + } + } +} + +void StyleEditor::on_addEdgeStyle_clicked() +{ + int i = 0; + + // get a fresh name + QString name; + while (true) { + name = QString("new edge style ") + QString::number(i); + if (_styles->edgeStyles()->style(name) == nullptr) break; + ++i; + } + + // add the style (edge styles only have one category: "") + Style *s = new Style(name, new GraphElementData({GraphElementProperty("-")})); + _styles->edgeStyles()->addStyle(s); + + // set dirty flag and select the newly-added style + setDirty(true); + selectEdgeStyle(_styles->edgeStyles()->numInCategory()-1); +} + +void StyleEditor::on_removeEdgeStyle_clicked() +{ + if (_edgeStyleIndex.isValid()) { + int i = _edgeStyleIndex.row(); + if (i > 0) { + ui->edgeStyleListView->selectionModel()->clear(); + _styles->edgeStyles()->removeNthStyle(i); + setDirty(true); + if (i < _styles->edgeStyles()->numInCategory()) { + selectEdgeStyle(i); + } + } + } +} + +void StyleEditor::on_edgeStyleUp_clicked() +{ + if (_edgeStyleIndex.isValid()) { + int r = _edgeStyleIndex.row(); + if (_styles->edgeStyles()->moveRows( + _edgeStyleIndex.parent(), + r, 1, + _edgeStyleIndex.parent(), + r - 1)) + { + setDirty(true); + edgeItemChanged(_styles->edgeStyles()->index(r - 1)); + } + } +} + +void StyleEditor::on_edgeStyleDown_clicked() +{ + if (_edgeStyleIndex.isValid()) { + int r = _edgeStyleIndex.row(); + if (_styles->edgeStyles()->moveRows( + _edgeStyleIndex.parent(), + r, 1, + _edgeStyleIndex.parent(), + r + 2)) + { + setDirty(true); + edgeItemChanged(_styles->edgeStyles()->index(r + 1)); + } + } } void StyleEditor::on_save_clicked() @@ -492,7 +605,7 @@ void StyleEditor::save() QString p = tikzit->styleFilePath(); if (_styles->saveStyles(p)) { - _dirty = false; + setDirty(false); tikzit->loadStyles(p); } else { QMessageBox::warning(0, @@ -517,7 +630,7 @@ void StyleEditor::on_name_editingFinished() s->setName(ui->name->text()); refreshActiveStyle(); // refreshDisplay(); - _dirty = true; + setDirty(true); } } @@ -528,7 +641,7 @@ void StyleEditor::on_shape_currentTextChanged() s->data()->setProperty("shape", ui->shape->currentText()); refreshActiveStyle(); // refreshDisplay(); - _dirty = true; + setDirty(true); } } @@ -602,6 +715,35 @@ void StyleEditor::updateColor(QPushButton *btn, QString name, QString propName) s->data()->setProperty(propName, tikzit->nameForColor(col)); refreshActiveStyle(); // refreshDisplay(); - _dirty = true; + setDirty(true); + } +} + +void StyleEditor::selectNodeStyle(int i) +{ + ui->styleListView->selectionModel()->setCurrentIndex( + _styles->nodeStyles()->index(i), + QItemSelectionModel::ClearAndSelect); +} + +void StyleEditor::selectEdgeStyle(int i) +{ + ui->edgeStyleListView->selectionModel()->setCurrentIndex( + _styles->edgeStyles()->index(i), + QItemSelectionModel::ClearAndSelect); +} + +bool StyleEditor::dirty() const +{ + return _dirty; +} + +void StyleEditor::setDirty(bool dirty) +{ + _dirty = dirty; + if (dirty) { + setWindowTitle("Style Editor* - TikZiT"); + } else { + setWindowTitle("Style Editor - TikZiT"); } } diff --git a/src/gui/styleeditor.h b/src/gui/styleeditor.h index bc548f0..b8bf646 100644 --- a/src/gui/styleeditor.h +++ b/src/gui/styleeditor.h @@ -24,6 +24,9 @@ public: void save(); void closeEvent(QCloseEvent *event) override; + bool dirty() const; + void setDirty(bool dirty); + public slots: void refreshDisplay(); void nodeItemChanged(QModelIndex sel); @@ -49,6 +52,14 @@ public slots: void on_propertyDown_clicked(); void on_addStyle_clicked(); + void on_removeStyle_clicked(); + void on_styleUp_clicked(); + void on_styleDown_clicked(); + + void on_addEdgeStyle_clicked(); + void on_removeEdgeStyle_clicked(); + void on_edgeStyleUp_clicked(); + void on_edgeStyleDown_clicked(); void on_save_clicked(); @@ -65,6 +76,9 @@ private: void refreshActiveStyle(); TikzStyles *_styles; void updateColor(QPushButton *btn, QString name, QString propName); + void selectNodeStyle(int i); + void selectEdgeStyle(int i); + QVector _formWidgets; bool _dirty; -- cgit v1.2.3