diff options
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/edgeitem.cpp | 87 | ||||
-rw-r--r-- | src/gui/mainmenu.cpp | 56 | ||||
-rw-r--r-- | src/gui/mainmenu.h | 10 | ||||
-rw-r--r-- | src/gui/mainmenu.ui | 111 | ||||
-rw-r--r-- | src/gui/nodeitem.cpp | 8 | ||||
-rw-r--r-- | src/gui/pathitem.cpp | 68 | ||||
-rw-r--r-- | src/gui/pathitem.h | 28 | ||||
-rw-r--r-- | src/gui/styleeditor.cpp | 81 | ||||
-rw-r--r-- | src/gui/styleeditor.h | 2 | ||||
-rw-r--r-- | src/gui/styleeditor.ui | 38 | ||||
-rw-r--r-- | src/gui/stylepalette.cpp | 85 | ||||
-rw-r--r-- | src/gui/stylepalette.h | 15 | ||||
-rw-r--r-- | src/gui/tikzscene.cpp | 299 | ||||
-rw-r--r-- | src/gui/tikzscene.h | 12 | ||||
-rw-r--r-- | src/gui/undocommands.cpp | 140 | ||||
-rw-r--r-- | src/gui/undocommands.h | 31 |
16 files changed, 952 insertions, 119 deletions
diff --git a/src/gui/edgeitem.cpp b/src/gui/edgeitem.cpp index 45ae159..675ddd7 100644 --- a/src/gui/edgeitem.cpp +++ b/src/gui/edgeitem.cpp @@ -71,7 +71,8 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge QPen pen = _edge->style()->pen(); painter->setPen(pen); painter->setBrush(Qt::NoBrush); - painter->drawPath(path()); + + if (!_edge->path()) painter->drawPath(path()); QPointF ht = _edge->headTangent(); QPointF hLeft(-ht.y(), ht.x()); @@ -83,27 +84,27 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge pen.setStyle(Qt::SolidLine); painter->setPen(pen); - - - switch (_edge->style()->arrowHead()) { - case Style::Flat: - { - painter->drawLine( - toScreen(_edge->head() + hLeft), - toScreen(_edge->head() + hRight)); - break; - } - case Style::Pointer: - { - QPainterPath pth; - pth.moveTo(toScreen(_edge->head() + ht + hLeft)); - pth.lineTo(toScreen(_edge->head())); - pth.lineTo(toScreen(_edge->head() + ht + hRight)); - painter->drawPath(pth); - break; - } - case Style::NoTip: - break; + if (!_edge->path() || _edge->path()->edges().last() == _edge) { + switch (_edge->style()->arrowHead()) { + case Style::Flat: + { + painter->drawLine( + toScreen(_edge->head() + hLeft), + toScreen(_edge->head() + hRight)); + break; + } + case Style::Pointer: + { + QPainterPath pth; + pth.moveTo(toScreen(_edge->head() + ht + hLeft)); + pth.lineTo(toScreen(_edge->head())); + pth.lineTo(toScreen(_edge->head() + ht + hRight)); + painter->drawPath(pth); + break; + } + case Style::NoTip: + break; + } } //QPen outline = QPen(Qt::red); @@ -111,26 +112,28 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge //painter->drawPath(_expPath); //painter->setPen(pen); - switch (_edge->style()->arrowTail()) { - case Style::Flat: - { - painter->drawLine( - toScreen(_edge->tail() + tLeft), - toScreen(_edge->tail() + tRight)); - break; - } - case Style::Pointer: - { - QPainterPath pth; - pth.moveTo(toScreen(_edge->tail() + tt + tLeft)); - pth.lineTo(toScreen(_edge->tail())); - pth.lineTo(toScreen(_edge->tail() + tt + tRight)); - painter->drawPath(pth); - break; - } - case Style::NoTip: - break; - } + if (!_edge->path() || _edge->path()->edges().first() == _edge) { + switch (_edge->style()->arrowTail()) { + case Style::Flat: + { + painter->drawLine( + toScreen(_edge->tail() + tLeft), + toScreen(_edge->tail() + tRight)); + break; + } + case Style::Pointer: + { + QPainterPath pth; + pth.moveTo(toScreen(_edge->tail() + tt + tLeft)); + pth.lineTo(toScreen(_edge->tail())); + pth.lineTo(toScreen(_edge->tail() + tt + tRight)); + painter->drawPath(pth); + break; + } + case Style::NoTip: + break; + } + } if (isSelected()) { QColor draw; diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp index 35ab736..efd453d 100644 --- a/src/gui/mainmenu.cpp +++ b/src/gui/mainmenu.cpp @@ -239,6 +239,24 @@ void MainMenu::on_actionMerge_Nodes_triggered() tikzit->activeWindow()->tikzScene()->mergeNodes(); } +void MainMenu::on_actionMake_Path_triggered() +{ + if (tikzit->activeWindow() != 0) + tikzit->activeWindow()->tikzScene()->makePath(false); +} + +void MainMenu::on_actionMake_Path_as_Background_triggered() +{ + if (tikzit->activeWindow() != 0) + tikzit->activeWindow()->tikzScene()->makePath(true); +} + +void MainMenu::on_actionSplit_Path_triggered() +{ + if (tikzit->activeWindow() != 0) + tikzit->activeWindow()->tikzScene()->splitPath(); +} + // Tikz void MainMenu::on_actionParse_triggered() @@ -282,6 +300,36 @@ void MainMenu::on_actionRun_LaTeX_triggered() tikzit->makePreview(); } +void MainMenu::on_actionPrevious_Node_Style_triggered() +{ + tikzit->activeWindow()->stylePalette()->previousNodeStyle(); +} + +void MainMenu::on_actionNext_Node_Style_triggered() +{ + tikzit->activeWindow()->stylePalette()->nextNodeStyle(); +} + +void MainMenu::on_actionClear_Node_Style_triggered() +{ + tikzit->activeWindow()->stylePalette()->toggleClearNodeStyle(); +} + +void MainMenu::on_actionPrevious_Edge_Style_triggered() +{ + tikzit->activeWindow()->stylePalette()->previousEdgeStyle(); +} + +void MainMenu::on_actionNext_Edge_Style_triggered() +{ + tikzit->activeWindow()->stylePalette()->nextEdgeStyle(); +} + +void MainMenu::on_actionClear_Edge_Style_triggered() +{ + tikzit->activeWindow()->stylePalette()->toggleClearEdgeStyle(); +} + void MainMenu::on_actionPreferences_triggered() { PreferenceDialog *d = new PreferenceDialog(this); @@ -301,6 +349,14 @@ void MainMenu::on_actionZoom_Out_triggered() if (tikzit->activeWindow() != 0) tikzit->activeWindow()->tikzView()->zoomOut(); } +void MainMenu::on_actionShow_Node_Labels_triggered() +{ + if (tikzit->activeWindow() != 0) { + tikzit->activeWindow()->tikzScene()->setDrawNodeLabels(ui.actionShow_Node_Labels->isChecked()); + tikzit->activeWindow()->tikzScene()->invalidate(); + } +} + void MainMenu::on_actionAbout_triggered() { QMessageBox::about(this, diff --git a/src/gui/mainmenu.h b/src/gui/mainmenu.h index 51d7d3c..d0d73e9 100644 --- a/src/gui/mainmenu.h +++ b/src/gui/mainmenu.h @@ -67,17 +67,27 @@ public slots: void on_actionExtendRight_triggered(); void on_actionReverse_Edge_Direction_triggered(); void on_actionMerge_Nodes_triggered(); + void on_actionMake_Path_triggered(); + void on_actionMake_Path_as_Background_triggered(); + void on_actionSplit_Path_triggered(); // Tools void on_actionParse_triggered(); void on_actionRevert_triggered(); void on_actionJump_to_Selection_triggered(); void on_actionRun_LaTeX_triggered(); + void on_actionPrevious_Node_Style_triggered(); + void on_actionNext_Node_Style_triggered(); + void on_actionClear_Node_Style_triggered(); + void on_actionPrevious_Edge_Style_triggered(); + void on_actionNext_Edge_Style_triggered(); + void on_actionClear_Edge_Style_triggered(); void on_actionPreferences_triggered(); // View void on_actionZoom_In_triggered(); void on_actionZoom_Out_triggered(); + void on_actionShow_Node_Labels_triggered(); // Help void on_actionAbout_triggered(); diff --git a/src/gui/mainmenu.ui b/src/gui/mainmenu.ui index 692f82e..11778db 100644 --- a/src/gui/mainmenu.ui +++ b/src/gui/mainmenu.ui @@ -58,6 +58,14 @@ <addaction name="actionRotateCW"/> <addaction name="actionRotateCCW"/> </widget> + <widget class="QMenu" name="menuPath"> + <property name="title"> + <string>Path</string> + </property> + <addaction name="actionMake_Path"/> + <addaction name="actionSplit_Path"/> + <addaction name="actionMake_Path_as_Background"/> + </widget> <addaction name="actionUndo"/> <addaction name="actionRedo"/> <addaction name="separator"/> @@ -74,16 +82,32 @@ <addaction name="menuTransform"/> <addaction name="actionReverse_Edge_Direction"/> <addaction name="actionMerge_Nodes"/> + <addaction name="separator"/> + <addaction name="menuPath"/> </widget> <widget class="QMenu" name="menuTikz"> <property name="title"> <string>Tools</string> </property> + <widget class="QMenu" name="menuNode_Style"> + <property name="title"> + <string>Set Style</string> + </property> + <addaction name="actionPrevious_Node_Style"/> + <addaction name="actionNext_Node_Style"/> + <addaction name="actionClear_Node_Style"/> + <addaction name="separator"/> + <addaction name="actionPrevious_Edge_Style"/> + <addaction name="actionNext_Edge_Style"/> + <addaction name="actionClear_Edge_Style"/> + </widget> <addaction name="actionParse"/> <addaction name="actionRevert"/> <addaction name="actionJump_to_Selection"/> <addaction name="actionRun_LaTeX"/> <addaction name="separator"/> + <addaction name="menuNode_Style"/> + <addaction name="separator"/> <addaction name="actionPreferences"/> </widget> <widget class="QMenu" name="menuView"> @@ -92,6 +116,7 @@ </property> <addaction name="actionZoom_In"/> <addaction name="actionZoom_Out"/> + <addaction name="actionShow_Node_Labels"/> </widget> <widget class="QMenu" name="menuHelp"> <property name="title"> @@ -383,6 +408,92 @@ <string>Ctrl+M</string> </property> </action> + <action name="actionShow_Node_Labels"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>true</bool> + </property> + <property name="text"> + <string>Show Node Labels</string> + </property> + <property name="shortcut"> + <string>Ctrl+Shift+L</string> + </property> + </action> + <action name="actionPrevious_Node_Style"> + <property name="text"> + <string>Previous Node Style</string> + </property> + <property name="shortcut"> + <string>Ctrl+,</string> + </property> + </action> + <action name="actionNext_Node_Style"> + <property name="text"> + <string>Next Node Style</string> + </property> + <property name="shortcut"> + <string>Ctrl+.</string> + </property> + </action> + <action name="actionClear_Node_Style"> + <property name="text"> + <string>Clear Node Style</string> + </property> + <property name="shortcut"> + <string>Ctrl+Space</string> + </property> + </action> + <action name="actionPrevious_Edge_Style"> + <property name="text"> + <string>Previous Edge Style</string> + </property> + <property name="shortcut"> + <string>Ctrl+Shift+,</string> + </property> + </action> + <action name="actionNext_Edge_Style"> + <property name="text"> + <string>Next Edge Style</string> + </property> + <property name="shortcut"> + <string>Ctrl+Shift+.</string> + </property> + </action> + <action name="actionClear_Edge_Style"> + <property name="text"> + <string>Clear Edge Style</string> + </property> + <property name="shortcut"> + <string>Ctrl+Shift+Space</string> + </property> + </action> + <action name="actionMake_Path"> + <property name="text"> + <string>Make Path</string> + </property> + <property name="shortcut"> + <string>Ctrl+P</string> + </property> + </action> + <action name="actionSplit_Path"> + <property name="text"> + <string>Split Path</string> + </property> + <property name="shortcut"> + <string>Ctrl+Shift+P</string> + </property> + </action> + <action name="actionMake_Path_as_Background"> + <property name="text"> + <string>Make Path as Background</string> + </property> + <property name="shortcut"> + <string>Ctrl+B</string> + </property> + </action> <addaction name="menuFile"/> <addaction name="menuEdit"/> <addaction name="menuView"/> diff --git a/src/gui/nodeitem.cpp b/src/gui/nodeitem.cpp index 82692f2..7ae4c4a 100644 --- a/src/gui/nodeitem.cpp +++ b/src/gui/nodeitem.cpp @@ -91,7 +91,13 @@ void NodeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge painter->drawPath(shape()); } - if (_node->label() != "") { + bool drawLabel = _node->label() != ""; + if (scene()) { + TikzScene *sc = static_cast<TikzScene*>(scene()); + drawLabel= drawLabel && sc->drawNodeLabels(); + } + + if (drawLabel) { QRectF rect = labelRect(); QPen pen(QColor(200,0,0,120)); QVector<qreal> d; diff --git a/src/gui/pathitem.cpp b/src/gui/pathitem.cpp new file mode 100644 index 0000000..b45c0a5 --- /dev/null +++ b/src/gui/pathitem.cpp @@ -0,0 +1,68 @@ +#include "pathitem.h" +#include "tikzit.h" + +PathItem::PathItem(Path *path) +{ + _path = path; + readPos(); +} + +void PathItem::readPos() +{ + QPainterPath painterPath; + + foreach (Edge *e, _path->edges()) { + e->updateControls(); + + if (e == _path->edges().first()) + painterPath.moveTo (toScreen(e->tail())); + + if (e->bend() != 0 || !e->basicBendMode()) { + painterPath.cubicTo(toScreen(e->cp1()), + toScreen(e->cp2()), + toScreen(e->head())); + } else { + painterPath.lineTo(toScreen(e->head())); + } + } + + setPainterPath(painterPath); +} + +void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + Style *st = _path->edges().first()->style(); + QPen pen = st->pen(); + QBrush brush = st->brush(); + QColor c = brush.color(); + brush.setColor(QColor(c.red(),c.green(),c.blue(),200)); + painter->setPen(pen); + painter->setBrush(brush); + painter->drawPath(painterPath()); +} + +Path *PathItem::path() const +{ + return _path; +} + +QPainterPath PathItem::painterPath() const +{ + return _painterPath; +} + +void PathItem::setPainterPath(const QPainterPath &painterPath) +{ + prepareGeometryChange(); + + _painterPath = painterPath; + float r = GLOBAL_SCALEF * 0.1; + _boundingRect = _painterPath.boundingRect().adjusted(-r,-r,r,r); + + update(); +} + +QRectF PathItem::boundingRect() const +{ + return _boundingRect; +} diff --git a/src/gui/pathitem.h b/src/gui/pathitem.h new file mode 100644 index 0000000..7dc9c80 --- /dev/null +++ b/src/gui/pathitem.h @@ -0,0 +1,28 @@ +#ifndef PATHITEM_H +#define PATHITEM_H + +#include "path.h" + +#include <QGraphicsItem> + +class PathItem : public QGraphicsItem +{ +public: + PathItem(Path *path); + void readPos(); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override; + + Path *path() const; + + QPainterPath painterPath() const; + void setPainterPath(const QPainterPath &painterPath); + + QRectF boundingRect() const override; + +private: + Path *_path; + QPainterPath _painterPath; + QRectF _boundingRect; +}; + +#endif // PATHITEM_H diff --git a/src/gui/styleeditor.cpp b/src/gui/styleeditor.cpp index e2ade45..f8972b1 100644 --- a/src/gui/styleeditor.cpp +++ b/src/gui/styleeditor.cpp @@ -32,8 +32,8 @@ StyleEditor::StyleEditor(QWidget *parent) : { ui->setupUi(this); _formWidgets << ui->name << ui->category << - ui->fillColor << ui->hasTikzitFillColor << ui->tikzitFillColor << - ui->drawColor << ui->hasTikzitDrawColor << ui->tikzitDrawColor << + ui->fillColor << ui->noFill << ui->hasTikzitFillColor << ui->tikzitFillColor << + ui->drawColor << ui->noDraw << ui->hasTikzitDrawColor << ui->tikzitDrawColor << ui->shape << ui->hasTikzitShape << ui->tikzitShape << ui->leftArrow << ui->rightArrow << ui->properties; @@ -283,8 +283,16 @@ void StyleEditor::refreshDisplay() // draw QColor realDraw = s->strokeColor(false); QColor draw = s->strokeColor(); - ui->drawColor->setEnabled(true); - setColor(ui->drawColor, realDraw); + + ui->noDraw->setEnabled(true); + if (s->hasStroke()) { + ui->drawColor->setEnabled(true); + setColor(ui->drawColor, realDraw); + ui->noDraw->setChecked(false); + } else { + ui->noDraw->setChecked(true); + ui->drawColor->setEnabled(false); + } // tikzit draw bool drawOverride = s->data()->hasProperty("tikzit draw"); @@ -294,6 +302,27 @@ void StyleEditor::refreshDisplay() ui->tikzitDrawColor->setEnabled(drawOverride); if (drawOverride) setColor(ui->tikzitDrawColor, draw); + // fill + QColor realFill = s->fillColor(false); + QColor fill = s->fillColor(); + + ui->noFill->setEnabled(true); + if (s->hasFill()) { + ui->fillColor->setEnabled(true); + setColor(ui->fillColor, realFill); + ui->noFill->setChecked(false); + } else { + ui->noFill->setChecked(true); + ui->fillColor->setEnabled(false); + } + + // tikzit fill + bool fillOverride = s->data()->hasProperty("tikzit fill"); + ui->hasTikzitFillColor->setEnabled(true); + ui->hasTikzitFillColor->setChecked(fillOverride); + ui->tikzitFillColor->setEnabled(fillOverride); + if (fillOverride) setColor(ui->tikzitFillColor, fill); + if (!s->isEdgeStyle()) { // qDebug() << "node style update"; // category @@ -301,19 +330,6 @@ void StyleEditor::refreshDisplay() ui->category->setCurrentText( s->propertyWithDefault("tikzit category", "", false)); - // fill - QColor realFill = s->fillColor(false); - QColor fill = s->fillColor(); - ui->fillColor->setEnabled(true); - setColor(ui->fillColor, realFill); - - // tikzit fill - bool fillOverride = s->data()->hasProperty("tikzit fill"); - ui->hasTikzitFillColor->setEnabled(true); - ui->hasTikzitFillColor->setChecked(fillOverride); - ui->tikzitFillColor->setEnabled(fillOverride); - if (fillOverride) setColor(ui->tikzitFillColor, fill); - // shape QString realShape = s->propertyWithDefault("shape", "", false); QString shape = s->propertyWithDefault("tikzit shape", "", false); @@ -327,13 +343,6 @@ void StyleEditor::refreshDisplay() ui->tikzitShape->setEnabled(shapeOverride); if (shapeOverride) ui->tikzitShape->setCurrentText(shape); } else { -// qDebug() << "edge style update"; - - // set fill to gray (disabled) - ui->fillColor->setEnabled(false); - ui->tikzitFillColor->setEnabled(false); - ui->hasTikzitFillColor->setEnabled(false); - ui->shape->setEnabled(false); ui->tikzitShape->setEnabled(false); ui->hasTikzitShape->setEnabled(false); @@ -425,6 +434,30 @@ void StyleEditor::on_hasTikzitDrawColor_stateChanged(int state) } } +void StyleEditor::on_noFill_stateChanged(int state) +{ + Style *s = activeStyle(); + if (s != nullptr) { + if (state == Qt::Checked) s->data()->setProperty("fill", "none"); + else s->data()->setProperty("fill", "white"); + refreshActiveStyle(); + refreshDisplay(); + setDirty(true); + } +} + +void StyleEditor::on_noDraw_stateChanged(int state) +{ + Style *s = activeStyle(); + if (s != nullptr) { + if (state == Qt::Checked) s->data()->setProperty("draw", "none"); + else s->data()->setProperty("draw", "black"); + refreshActiveStyle(); + refreshDisplay(); + setDirty(true); + } +} + void StyleEditor::on_hasTikzitShape_stateChanged(int state) { Style *s = activeStyle(); diff --git a/src/gui/styleeditor.h b/src/gui/styleeditor.h index 2c35d56..7fc62c0 100644 --- a/src/gui/styleeditor.h +++ b/src/gui/styleeditor.h @@ -68,6 +68,8 @@ public slots: void on_tikzitDrawColor_clicked(); void on_hasTikzitFillColor_stateChanged(int state); void on_hasTikzitDrawColor_stateChanged(int state); + void on_noFill_stateChanged(int state); + void on_noDraw_stateChanged(int state); void on_hasTikzitShape_stateChanged(int state); void on_tikzitShape_currentIndexChanged(int); diff --git a/src/gui/styleeditor.ui b/src/gui/styleeditor.ui index 9c06894..22b04a6 100644 --- a/src/gui/styleeditor.ui +++ b/src/gui/styleeditor.ui @@ -822,6 +822,44 @@ <bool>true</bool> </attribute> </widget> + <widget class="QCheckBox" name="noFill"> + <property name="geometry"> + <rect> + <x>340</x> + <y>110</y> + <width>71</width> + <height>17</height> + </rect> + </property> + <property name="font"> + <font> + <pointsize>9</pointsize> + <italic>false</italic> + </font> + </property> + <property name="text"> + <string>none</string> + </property> + </widget> + <widget class="QCheckBox" name="noDraw"> + <property name="geometry"> + <rect> + <x>340</x> + <y>140</y> + <width>71</width> + <height>17</height> + </rect> + </property> + <property name="font"> + <font> + <pointsize>9</pointsize> + <italic>false</italic> + </font> + </property> + <property name="text"> + <string>none</string> + </property> + </widget> </widget> </widget> <resources/> diff --git a/src/gui/stylepalette.cpp b/src/gui/stylepalette.cpp index 15ed4c2..b862902 100644 --- a/src/gui/stylepalette.cpp +++ b/src/gui/stylepalette.cpp @@ -34,6 +34,8 @@ StylePalette::StylePalette(QWidget *parent) : ui(new Ui::StylePalette) { ui->setupUi(this); + _lastStyleIndex = 0; + _lastEdgeStyleIndex = 0; ui->styleListView->setModel(tikzit->styles()->nodeStyles()); ui->styleListView->setViewMode(QListView::IconMode); @@ -73,31 +75,95 @@ void StylePalette::reloadStyles() ui->currentCategory->addItems(tikzit->styles()->categories()); ui->currentCategory->setCurrentText(cat); } + + clearNodeStyle(); + clearEdgeStyle(); } -void StylePalette::changeNodeStyle(int increment) +int StylePalette::styleIndex() { QModelIndexList i = ui->styleListView->selectionModel()->selectedIndexes(); - int row = 0; - if (!i.isEmpty()) { - int row = (i[0].row()+increment)% tikzit->styles()->nodeStyles()->numInCategory(); - if (row < 0) row += tikzit->styles()->nodeStyles()->numInCategory(); + if (!i.isEmpty()) return i[0].row(); + else return 0; +} + +void StylePalette::setStyleIndex(int i) +{ + _lastStyleIndex = styleIndex(); + if (i < 0) { + i = 0; + } else { + int max = tikzit->styles()->nodeStyles()->numInCategory(); + if (i >= max) i = max - 1; } - //QModelIndex i1 = ui->styleListView->rootIndex().child(row, 0); - QModelIndex i1 =tikzit->styles()->nodeStyles()->index(row,0); + QModelIndex i1 =tikzit->styles()->nodeStyles()->index(i,0); ui->styleListView->selectionModel()->select(i1, QItemSelectionModel::ClearAndSelect); ui->styleListView->scrollTo(i1); } +int StylePalette::edgeStyleIndex() +{ + QModelIndexList i = ui->edgeStyleListView->selectionModel()->selectedIndexes(); + if (!i.isEmpty()) return i[0].row(); + else return 0; +} + +void StylePalette::setEdgeStyleIndex(int i) +{ + _lastEdgeStyleIndex = edgeStyleIndex(); + if (i < 0) { + i = 0; + } else { + int max = tikzit->styles()->edgeStyles()->numInCategory(); + if (i >= max) i = max - 1; + } + + QModelIndex i1 =tikzit->styles()->edgeStyles()->index(i,0); + ui->edgeStyleListView->selectionModel()->select(i1, QItemSelectionModel::ClearAndSelect); + ui->edgeStyleListView->scrollTo(i1); +} + void StylePalette::nextNodeStyle() { - changeNodeStyle(1); + setStyleIndex(styleIndex()+1); } void StylePalette::previousNodeStyle() { - changeNodeStyle(-1); + setStyleIndex(styleIndex()-1); +} + +void StylePalette::clearNodeStyle() +{ + setStyleIndex(0); +} + +void StylePalette::toggleClearNodeStyle() +{ + if (styleIndex() == 0) setStyleIndex(_lastStyleIndex); + else setStyleIndex(0); +} + +void StylePalette::nextEdgeStyle() +{ + setEdgeStyleIndex(edgeStyleIndex()+1); +} + +void StylePalette::previousEdgeStyle() +{ + setEdgeStyleIndex(edgeStyleIndex()-1); +} + +void StylePalette::clearEdgeStyle() +{ + setEdgeStyleIndex(0); +} + +void StylePalette::toggleClearEdgeStyle() +{ + if (edgeStyleIndex() == 0) setEdgeStyleIndex(_lastEdgeStyleIndex); + else setEdgeStyleIndex(0); } QString StylePalette::activeNodeStyleName() @@ -164,6 +230,7 @@ void StylePalette::on_currentCategory_currentTextChanged(const QString &cat) { //tikzit->styles()->refreshModels(_nodeModel, _edgeModel, cat); tikzit->styles()->nodeStyles()->setCategory(cat); + clearNodeStyle(); } //void StylePalette::on_buttonApplyNodeStyle_clicked() diff --git a/src/gui/stylepalette.h b/src/gui/stylepalette.h index f27cd4b..6e16812 100644 --- a/src/gui/stylepalette.h +++ b/src/gui/stylepalette.h @@ -36,10 +36,14 @@ public: void reloadStyles(); void nextNodeStyle(); void previousNodeStyle(); + void clearNodeStyle(); + void toggleClearNodeStyle(); + void nextEdgeStyle(); + void previousEdgeStyle(); + void clearEdgeStyle(); + void toggleClearEdgeStyle(); QString activeNodeStyleName(); QString activeEdgeStyleName(); - - public slots: void nodeStyleDoubleClicked(const QModelIndex &); void edgeStyleDoubleClicked(const QModelIndex &); @@ -51,7 +55,12 @@ public slots: //void on_buttonApplyNodeStyle_clicked(); private: - void changeNodeStyle(int increment); + int _lastStyleIndex; + int _lastEdgeStyleIndex; + int styleIndex(); + void setStyleIndex(int i); + int edgeStyleIndex(); + void setEdgeStyleIndex(int i); Ui::StylePalette *ui; diff --git a/src/gui/tikzscene.cpp b/src/gui/tikzscene.cpp index 4577981..33e4710 100644 --- a/src/gui/tikzscene.cpp +++ b/src/gui/tikzscene.cpp @@ -27,6 +27,7 @@ #include <QDebug> #include <QClipboard> #include <QInputDialog> +#include <QMessageBox> #include <cmath> #include <delimitedstringvalidator.h> #include <QSettings> @@ -38,6 +39,7 @@ TikzScene::TikzScene(TikzDocument *tikzDocument, ToolPalette *tools, { _modifyEdgeItem = nullptr; _edgeStartNodeItem = nullptr; + _drawNodeLabels = true; _drawEdgeItem = new QGraphicsLineItem(); _rubberBandItem = new QGraphicsRectItem(); _enabled = true; @@ -95,12 +97,25 @@ void TikzScene::graphReplaced() } _edgeItems.clear(); + foreach (PathItem *pi, _pathItems) { + removeItem(pi); + delete pi; + } + _pathItems.clear(); + foreach (Edge *e, graph()->edges()) { //e->attachStyle(); //e->updateControls(); EdgeItem *ei = new EdgeItem(e); _edgeItems.insert(e, ei); addItem(ei); + + Path *p = e->path(); + if (p && p->edges().first() == e) { + PathItem *pi = new PathItem(p); + _pathItems.insert(p, pi); + addItem(pi); + } } foreach (Node *n, graph()->nodes()) { @@ -235,13 +250,18 @@ void TikzScene::mergeNodes() Node *n = _tikzDocument->graph()->nodes()[i]; if (m1.contains(n)) delNodes.insert(i, n); } + + QSet<Path*> delPaths; for (int i = 0; i < _tikzDocument->graph()->edges().length(); ++i) { Edge *e = _tikzDocument->graph()->edges()[i]; - if (m1.contains(e->source()) || m1.contains(e->target())) delEdges.insert(i, e); + if (m1.contains(e->source()) || m1.contains(e->target())) { + delEdges.insert(i, e); + if (e->path()) delPaths << e->path(); + } } - DeleteCommand *cmd = new DeleteCommand(this, delNodes, delEdges, - selNodes, selEdges); - _tikzDocument->undoStack()->push(cmd); + _tikzDocument->undoStack()->push(new SplitPathCommand(this, delPaths)); + _tikzDocument->undoStack()->push(new DeleteCommand(this, delNodes, delEdges, + selNodes, selEdges)); _tikzDocument->undoStack()->endMacro(); } @@ -294,11 +314,161 @@ void TikzScene::reverseSelectedEdges() _tikzDocument->undoStack()->push(cmd); } +void TikzScene::makePath(bool duplicateEdges) +{ + QSet<Node*> selNodes; + QSet<Edge*> selEdges; + QSet<Edge*> edges; + getSelection(selNodes, selEdges); + + edges = selEdges; + + // if no edges are selected, try to infer edges from nodes + if (edges.isEmpty()) { + foreach(Edge *e, graph()->edges()) { + if (selNodes.contains(e->source()) && selNodes.contains(e->target())) + edges << e; + } + } + + if (edges.size() < 2) { + //QMessageBox::warning(nullptr, "Error", "Paths must contain at least 2 edges."); + return; + } + + foreach (Edge *e, edges) { + if (e->path() != nullptr && !duplicateEdges) { + //QMessageBox::warning(nullptr, "Error", "Edges must not already be in another path."); + // TODO: maybe we want to automatically split paths if edges are in a path already? + return; + } + } + + _tikzDocument->undoStack()->beginMacro("Make Path"); + + QVector<Edge *> oldEdgeOrder = graph()->edges(); + QSet<Edge *> oldEdges, newEdges; + oldEdges = edges; + + if (duplicateEdges) { + foreach (Edge *e, edges) { + Edge *e1 = e->copy(); + _tikzDocument->undoStack()->push(new AddEdgeCommand(this, e1, false, selNodes, selEdges)); + newEdges << e1; + oldEdgeOrder << e1; + } + edges = newEdges; + } + + // try to turn selected edges into one contiguous chain or cycle, recording + // which edges need to be flipped. + + // n.b. this is O(n^2) in path length. This could be optimised by saving + // vertex neighbourhoods, but probably doesn't win anything for n < 100. + + QSet<Edge*> flip; + QVector<Edge*> p; + int pLen = -1; + + // keep going as long as 'p' grows + while (pLen < p.length()) { + pLen = p.length(); + Edge *e = nullptr; + foreach (e, edges) { + Node *s = e->source(); + Node *t = e->target(); + if (p.isEmpty()) { + p.append(e); + break; + } + + Node *head = (flip.contains(p.first())) ? p.first()->target() : p.first()->source(); + Node *tail = (flip.contains(p.last())) ? p.last()->source() : p.last()->target(); + + if (s == head || t == head) { + if (s == head) flip << e; + p.prepend(e); + break; + } + + if (s == tail || t == tail) { + if (t == tail) flip << e; + p.append(e); + break; + } + } + + if (e) edges.remove(e); + } + + if (!edges.isEmpty()) { + QMessageBox::warning(nullptr, "Error", "Selected edges do not form a path."); + return; + } + + _tikzDocument->undoStack()->push(new ReverseEdgesCommand(this, flip)); + + // order all of the edges together, and in the case of + // duplicate edges, just below the first original. + QVector<Edge*> newEdgeOrder; + bool firstEdge = true; + foreach (Edge *e, oldEdgeOrder) { + if (oldEdges.contains(e)) { + if (firstEdge) { + newEdgeOrder += p; + firstEdge = false; + } + + if (duplicateEdges) newEdgeOrder << e; + } else if (!newEdges.contains(e)) { + newEdgeOrder << e; + } + } + + _tikzDocument->undoStack()->push(new ReorderCommand(this, + graph()->nodes(), graph()->nodes(), oldEdgeOrder, newEdgeOrder)); + + QMap<Edge*, GraphElementData*> oldEdgeData; + foreach (Edge *e, p) { + if (e != p.first()) oldEdgeData[e] = e->data()->copy(); + } + + _tikzDocument->undoStack()->push(new MakePathCommand(this, p, oldEdgeData)); + _tikzDocument->undoStack()->endMacro(); +} + +void TikzScene::splitPath() +{ + QSet<Node*> selNodes; + QSet<Edge*> edges; + getSelection(selNodes, edges); + + // if no edges are selected, try to infer edges from nodes + if (edges.isEmpty()) { + foreach(Edge *e, graph()->edges()) { + if (selNodes.contains(e->source()) && selNodes.contains(e->target())) + edges << e; + } + } + + QSet<Path*> paths; + foreach (Edge *e, edges) { + if (e->path()) paths << e->path(); + } + + _tikzDocument->undoStack()->push(new SplitPathCommand(this, paths)); +} + void TikzScene::refreshZIndices() { qreal z = 0.0; foreach (Edge *e, graph()->edges()) { - edgeItems()[e]->setZValue(z); + if (e->path() && e == e->path()->edges().first()) { + pathItems()[e->path()]->setZValue(z); + edgeItems()[e]->setZValue(z + 0.1); + } else { + edgeItems()[e]->setZValue(z); + } z += 1.0; } @@ -317,6 +487,7 @@ void TikzScene::mousePressEvent(QGraphicsSceneMouseEvent *event) _mouseDownPos = event->scenePos(); _draggingNodes = false; + _selectingEdge = nullptr; // radius of a control point for bezier edges, in scene coordinates qreal cpR = GLOBAL_SCALEF * (0.1); @@ -397,9 +568,19 @@ void TikzScene::mousePressEvent(QGraphicsSceneMouseEvent *event) } } - auto its = items(_mouseDownPos); - if (!its.isEmpty() && dynamic_cast<NodeItem*>(its[0])) - _draggingNodes = true; + QList<QGraphicsItem*> its = items(_mouseDownPos); + if (!its.isEmpty()) { + if (dynamic_cast<NodeItem*>(its[0])) { + _draggingNodes = true; + } else { + foreach (QGraphicsItem *gi, its) { + if (EdgeItem *ei = dynamic_cast<EdgeItem*>(gi)) { + _selectingEdge = ei->edge(); + break; + } + } + } + } } break; @@ -511,6 +692,8 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } _modifyEdgeItem->readPos(); + Path *p = _modifyEdgeItem->edge()->path(); + if (p) pathItems()[p]->readPos(); } else if (_draggingNodes) { // nodes being dragged QGraphicsScene::mouseMoveEvent(event); @@ -597,6 +780,23 @@ void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) // otherwise, process mouse move normally QGraphicsScene::mouseReleaseEvent(event); + if (_selectingEdge) { + bool sel = edgeItems()[_selectingEdge]->isSelected(); + Path *p = _selectingEdge->path(); + if (p) { + foreach (Edge *e, p->edges()) { + if (e != _selectingEdge) + edgeItems()[e]->setSelected(sel); + nodeItems()[e->source()]->setSelected(sel); + nodeItems()[e->target()]->setSelected(sel); + } + } +// else { +// nodeItems()[_selectingEdge->source()]->setSelected(sel); +// nodeItems()[_selectingEdge->target()]->setSelected(sel); +// } + } + if (_rubberBandItem->isVisible()) { QPainterPath sel; sel.addRect(_rubberBandItem->rect()); @@ -707,25 +907,6 @@ void TikzScene::keyReleaseEvent(QKeyEvent *event) } - if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) { - deleteSelectedItems(); - } else if (mod == Qt::NoModifier) { - switch(event->key()) { - case Qt::Key_S: - tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::SELECT); - break; - case Qt::Key_V: - case Qt::Key_N: - tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::VERTEX); - break; - case Qt::Key_E: - tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::EDGE); - break; - case Qt::Key_B: - tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::CROP); - break; - } - } foreach (QGraphicsItem *it, selectedItems()) it->update(); } @@ -739,11 +920,6 @@ void TikzScene::keyPressEvent(QKeyEvent *event) // slower, but seems to be more reliable than event->modifiers() Qt::KeyboardModifiers mod = QApplication::queryKeyboardModifiers(); - if (event->key() == Qt::Key_QuoteLeft) { - capture = true; - _styles->nextNodeStyle(); - } - if (mod & Qt::ControlModifier) { QSet<Node*> selNodes; QSet<Edge*> selEdges; @@ -865,6 +1041,26 @@ void TikzScene::keyPressEvent(QKeyEvent *event) _tikzDocument->undoStack()->endMacro(); } } + } else { // no CTRL key + if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) { + deleteSelectedItems(); + } else if (!event->isAutoRepeat()) { + switch(event->key()) { + case Qt::Key_S: + tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::SELECT); + break; + case Qt::Key_V: + case Qt::Key_N: + tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::VERTEX); + break; + case Qt::Key_E: + tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::EDGE); + break; + case Qt::Key_B: + tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::CROP); + break; + } + } } foreach (QGraphicsItem *it, selectedItems()) it->update(); @@ -907,6 +1103,16 @@ void TikzScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) } } +bool TikzScene::drawNodeLabels() const +{ + return _drawNodeLabels; +} + +void TikzScene::setDrawNodeLabels(bool drawNodeLabels) +{ + _drawNodeLabels = drawNodeLabels; +} + bool TikzScene::highlightTails() const { return _highlightTails && getSelectedNodes().isEmpty(); @@ -956,6 +1162,7 @@ void TikzScene::deleteSelectedItems() QMap<int,Node*> deleteNodes; QMap<int,Edge*> deleteEdges; + QSet<Path*> deletePaths; for (int i = 0; i < _tikzDocument->graph()->nodes().length(); ++i) { Node *n = _tikzDocument->graph()->nodes()[i]; @@ -966,14 +1173,20 @@ void TikzScene::deleteSelectedItems() Edge *e = _tikzDocument->graph()->edges()[i]; if (selEdges.contains(e) || selNodes.contains(e->source()) || - selNodes.contains(e->target())) deleteEdges.insert(i, e); + selNodes.contains(e->target())) + { + if (e->path()) deletePaths << e->path(); + deleteEdges.insert(i, e); + } } //qDebug() << "nodes:" << deleteNodes; //qDebug() << "edges:" << deleteEdges; - DeleteCommand *cmd = new DeleteCommand(this, deleteNodes, deleteEdges, - selNodes, selEdges); - _tikzDocument->undoStack()->push(cmd); + _tikzDocument->undoStack()->beginMacro("Delete"); + _tikzDocument->undoStack()->push(new SplitPathCommand(this, deletePaths)); + _tikzDocument->undoStack()->push(new DeleteCommand(this, deleteNodes, deleteEdges, + selNodes, selEdges)); + _tikzDocument->undoStack()->endMacro(); } void TikzScene::copyToClipboard() @@ -1152,6 +1365,8 @@ void TikzScene::refreshSceneBounds() { void TikzScene::refreshAdjacentEdges(QList<Node*> nodes) { if (nodes.empty()) return; + + QSet<Path*> paths; foreach (Edge *e, _edgeItems.keys()) { EdgeItem *ei = _edgeItems[e]; @@ -1162,6 +1377,13 @@ void TikzScene::refreshAdjacentEdges(QList<Node*> nodes) ei->readPos(); } } + + // only update paths once + Path *p = ei->edge()->path(); + if (p && !paths.contains(p)) { + pathItems()[p]->readPos(); + paths << p; + } } } @@ -1188,3 +1410,8 @@ QMap<Edge*,EdgeItem*> &TikzScene::edgeItems() { return _edgeItems; } + +QMap<Path *, PathItem *> &TikzScene::pathItems() +{ + return _pathItems; +} diff --git a/src/gui/tikzscene.h b/src/gui/tikzscene.h index f8dc7ec..e1d30d2 100644 --- a/src/gui/tikzscene.h +++ b/src/gui/tikzscene.h @@ -27,6 +27,7 @@ #include "graph.h" #include "nodeitem.h" #include "edgeitem.h" +#include "pathitem.h" #include "tikzdocument.h" #include "toolpalette.h" #include "stylepalette.h" @@ -48,6 +49,7 @@ public: Graph *graph(); QMap<Node*,NodeItem*> &nodeItems(); QMap<Edge*,EdgeItem*> &edgeItems(); + QMap<Path*,PathItem*> &pathItems(); void refreshAdjacentEdges(QList<Node*> nodes); // void setBounds(QRectF bounds); @@ -81,6 +83,8 @@ public: void reverseSelectedEdges(); + void makePath(bool duplicateEdges); + void splitPath(); void getSelection(QSet<Node*> &selNodes, QSet<Edge*> &selEdges) const; QSet<Node*> getSelectedNodes() const; @@ -90,6 +94,9 @@ public: bool highlightHeads() const; bool highlightTails() const; + bool drawNodeLabels() const; + void setDrawNodeLabels(bool drawNodeLabels); + public slots: void graphReplaced(); void refreshZIndices(); @@ -107,14 +114,17 @@ private: StylePalette *_styles; QMap<Node*,NodeItem*> _nodeItems; QMap<Edge*,EdgeItem*> _edgeItems; + QMap<Path*,PathItem*> _pathItems; QGraphicsLineItem *_drawEdgeItem; QGraphicsRectItem *_rubberBandItem; EdgeItem *_modifyEdgeItem; + Edge *_selectingEdge; NodeItem *_edgeStartNodeItem; NodeItem *_edgeEndNodeItem; bool _firstControlPoint; QPointF _mouseDownPos; bool _draggingNodes; + bool _drawNodeLabels; QMap<Node*,QPointF> _oldNodePositions; qreal _oldWeight; @@ -126,6 +136,8 @@ private: bool _highlightHeads; bool _highlightTails; bool _smartTool; + + bool _ctrlWasPressed; }; #endif // TIKZSCENE_H diff --git a/src/gui/undocommands.cpp b/src/gui/undocommands.cpp index c5c26af..35345de 100644 --- a/src/gui/undocommands.cpp +++ b/src/gui/undocommands.cpp @@ -102,6 +102,9 @@ void EdgeBendCommand::undo() foreach(EdgeItem *ei, _scene->edgeItems()) { if (ei->edge() == _edge) { ei->readPos(); + + Path *p = ei->edge()->path(); + if (p) _scene->pathItems()[p]->readPos(); break; } } @@ -118,6 +121,9 @@ void EdgeBendCommand::redo() foreach(EdgeItem *ei, _scene->edgeItems()) { if (ei->edge() == _edge) { ei->readPos(); + + Path *p = ei->edge()->path(); + if (p) _scene->pathItems()[p]->readPos(); break; } } @@ -287,6 +293,9 @@ void ChangeEdgeModeCommand::undo() // FIXME: this act strangely sometimes _edge->setBasicBendMode(!_edge->basicBendMode()); _scene->edgeItems()[_edge]->readPos(); + Path *p = _edge->path(); + if (p) _scene->pathItems()[p]->readPos(); + GraphUpdateCommand::undo(); } @@ -294,6 +303,9 @@ void ChangeEdgeModeCommand::redo() { _edge->setBasicBendMode(!_edge->basicBendMode()); _scene->edgeItems()[_edge]->readPos(); + Path *p = _edge->path(); + if (p) _scene->pathItems()[p]->readPos(); + GraphUpdateCommand::redo(); } @@ -369,6 +381,16 @@ void PasteCommand::undo() { _scene->clearSelection(); + foreach (Path *p, _graph->paths()) { + PathItem *pi = _scene->pathItems()[p]; + _scene->pathItems().remove(p); + _scene->removeItem(pi); + delete pi; + + p->removeEdges(); + _scene->graph()->removePath(p); + } + foreach (Edge *e, _graph->edges()) { EdgeItem *ei = _scene->edgeItems()[e]; _scene->edgeItems().remove(e); @@ -399,6 +421,12 @@ void PasteCommand::redo() _scene->clearSelection(); _scene->graph()->insertGraph(_graph); + foreach (Path *p, _graph->paths()) { + PathItem *pi = new PathItem(p); + _scene->pathItems().insert(p, pi); + _scene->addItem(pi); + } + foreach (Edge *e, _graph->edges()) { e->attachStyle(); // in case styles have changed EdgeItem *ei = new EdgeItem(e); @@ -488,7 +516,7 @@ void ReflectNodesCommand::undo() } } - _scene->refreshAdjacentEdges(_nodes.toList()); + _scene->refreshAdjacentEdges(_nodes.values()); GraphUpdateCommand::undo(); } @@ -501,7 +529,7 @@ void ReflectNodesCommand::redo() } } - _scene->refreshAdjacentEdges(_nodes.toList()); + _scene->refreshAdjacentEdges(_nodes.values()); GraphUpdateCommand::redo(); } @@ -520,7 +548,7 @@ void RotateNodesCommand::undo() } } - _scene->refreshAdjacentEdges(_nodes.toList()); + _scene->refreshAdjacentEdges(_nodes.values()); GraphUpdateCommand::undo(); } @@ -533,7 +561,7 @@ void RotateNodesCommand::redo() } } - _scene->refreshAdjacentEdges(_nodes.toList()); + _scene->refreshAdjacentEdges(_nodes.values()); GraphUpdateCommand::redo(); } @@ -594,3 +622,107 @@ void ReverseEdgesCommand::redo() GraphUpdateCommand::redo(); } + +MakePathCommand::MakePathCommand(TikzScene *scene, + const QVector<Edge *> &edgeList, + const QMap<Edge *, GraphElementData *> &oldEdgeData, + QUndoCommand *parent) : + GraphUpdateCommand(scene, parent), + _edgeList(edgeList), _oldEdgeData(oldEdgeData) +{ +} + +void MakePathCommand::undo() +{ + Path *p = _edgeList.first()->path(); + + PathItem *pi = _scene->pathItems()[p]; + _scene->removeItem(pi); + _scene->pathItems().remove(p); + delete pi; + + p->removeEdges(); + _scene->graph()->removePath(p); + + foreach (Edge *e, _edgeList) { + if (e != _edgeList.first()) { + // setData transfers ownership, so make a copy + e->setData(_oldEdgeData[e]->copy()); + } + } + + _scene->refreshZIndices(); + GraphUpdateCommand::undo(); +} + +void MakePathCommand::redo() +{ + GraphElementData *npd = _edgeList.first()->data()->nonPathData(); + GraphElementData *d; + + Path *p = new Path(); + foreach (Edge *e, _edgeList) { + p->addEdge(e); + + if (e != _edgeList.first()) { + d = e->data()->pathData(); + d->mergeData(npd); + e->setData(d); + } + } + + delete npd; + + _scene->graph()->addPath(p); + + PathItem *pi = new PathItem(p); + _scene->addItem(pi); + _scene->pathItems().insert(p, pi); + pi->readPos(); + + _scene->refreshZIndices(); + GraphUpdateCommand::redo(); +} + +SplitPathCommand::SplitPathCommand(TikzScene *scene, + const QSet<Path *> &paths, + QUndoCommand *parent) : + GraphUpdateCommand(scene, parent), _paths(paths) +{ + foreach (Path *p, paths) _edgeLists[p] = p->edges(); +} + +void SplitPathCommand::undo() +{ + foreach (Path *p, _paths) { + foreach (Edge *e, _edgeLists[p]) { + p->addEdge(e); + } + + _scene->graph()->addPath(p); + + PathItem *pi = new PathItem(p); + _scene->addItem(pi); + _scene->pathItems().insert(p, pi); + pi->readPos(); + } + + _scene->refreshZIndices(); + GraphUpdateCommand::undo(); +} + +void SplitPathCommand::redo() +{ + foreach (Path *p, _paths) { + PathItem *pi = _scene->pathItems()[p]; + _scene->removeItem(pi); + _scene->pathItems().remove(p); + delete pi; + + p->removeEdges(); + _scene->graph()->removePath(p); + } + + _scene->refreshZIndices(); + GraphUpdateCommand::redo(); +} diff --git a/src/gui/undocommands.h b/src/gui/undocommands.h index 40f0a3b..c10eecc 100644 --- a/src/gui/undocommands.h +++ b/src/gui/undocommands.h @@ -255,4 +255,35 @@ private: QVector<Edge*> _newEdgeOrder; }; +class MakePathCommand : public GraphUpdateCommand +{ +public: + explicit MakePathCommand(TikzScene *scene, + const QVector<Edge*> &edgeList, + const QMap<Edge*,GraphElementData*> &oldEdgeData, + QUndoCommand *parent = nullptr); + void undo() override; + void redo() override; +private: + QVector<Edge*> _edgeList; + + // creating path clobbers data on all but first edge + QMap<Edge*,GraphElementData*> _oldEdgeData; +}; + +class SplitPathCommand : public GraphUpdateCommand +{ +public: + explicit SplitPathCommand(TikzScene *scene, + const QSet<Path*> &paths, + QUndoCommand *parent = nullptr); + void undo() override; + void redo() override; +private: + QSet<Path*> _paths; + + // keep a copy of the edge lists so they can be added back to each path in undo() + QMap<Path*,QVector<Edge*>> _edgeLists; +}; + #endif // UNDOCOMMANDS_H |