File indexing completed on 2024-04-21 15:23:38

0001 /********************************************************************************
0002  * Copyright (C) 2011-2015 by Stephen Allewell                                  *
0003  * steve.allewell@gmail.com                                                     *
0004  *                                                                              *
0005  * This program is free software; you can redistribute it and/or modify         *
0006  * it under the terms of the GNU General Public License as published by         *
0007  * the Free Software Foundation; either version 2 of the License, or            *
0008  * (at your option) any later version.                                          *
0009  ********************************************************************************/
0010 
0011 
0012 /**
0013  * @file
0014  * Implement the various QUndoCommand based commands that modify the editor
0015  * or the symbol library.
0016  */
0017 
0018 
0019 /**
0020  * @page command Commands
0021  * A series of QUndoCommand derived commands are used to make modifications to the
0022  * editor and the symbol library.  These commands are pushed onto the relevant undo
0023  * stacks associated with the editor or the symbol library causing them to be
0024  * executed. This allows undo and redo functionality to be implemented.
0025  *
0026  * All functionality, including updating the views are encapsulated within the commands
0027  * undo and redo functions and the functions called by them.
0028  */
0029 
0030 
0031 #include "Commands.h"
0032 
0033 #include <QPainterPath>
0034 #include <QMimeData>
0035 
0036 #include <KLocalizedString>
0037 
0038 #include "Editor.h"
0039 #include "SymbolLibrary.h"
0040 
0041 
0042 enum IDs {MoveTo,
0043           LineTo,
0044           CubicTo,
0045           Rectangle,
0046           Ellipse,
0047           MovePoint,
0048           UpdateSymbol,
0049           ImportLibrary,
0050           RotateLeft,
0051           RotateRight,
0052           FlipHorizontal,
0053           FlipVertical,
0054           ChangeFilled,
0055           ChangeFillRule,
0056           ChangeCapStyle,
0057           ChangeJoinStyle,
0058           DeleteSymbol,
0059           IncreaseLineWidth,
0060           DecreaseLineWidth,
0061           DragAndDrop
0062          };
0063 
0064 
0065 /**
0066  * Constructor
0067  *
0068  * @param editor a pointer to the Editor
0069  * @param to a const reference to a QPointF representing the point to move to
0070  */
0071 MoveToCommand::MoveToCommand(Editor *editor, const QPointF &to)
0072     :   QUndoCommand(i18n("Move To")),
0073         m_editor(editor),
0074         m_to(to)
0075 {
0076 }
0077 
0078 
0079 /**
0080  * Destructor
0081  */
0082 MoveToCommand::~MoveToCommand()
0083 {
0084 }
0085 
0086 
0087 /**
0088  * Undo the move to command. Restores the path saved during redo.
0089  */
0090 void MoveToCommand::undo()
0091 {
0092     m_editor->removeLast(m_path);
0093 }
0094 
0095 
0096 /**
0097  * Redo the move command. Call the Editor moveTo function with the m_to point
0098  * returning the QPainterPath existing before the moveTo which is saved for undo.
0099  */
0100 void MoveToCommand::redo()
0101 {
0102     m_path = m_editor->moveTo(m_to);
0103 }
0104 
0105 
0106 /**
0107  * Constructor
0108  *
0109  * @param editor a pointer to the Editor
0110  * @param to a const reference to a QPointF representing the point to draw to
0111  */
0112 LineToCommand::LineToCommand(Editor *editor, const QPointF &to)
0113     :   QUndoCommand(i18n("Draw To")),
0114         m_editor(editor),
0115         m_to(to)
0116 {
0117 }
0118 
0119 
0120 /**
0121  * Destructor
0122  */
0123 LineToCommand::~LineToCommand()
0124 {
0125 }
0126 
0127 
0128 /**
0129  * Undo the line command. Restore the path saved during redo.
0130  */
0131 void LineToCommand::undo()
0132 {
0133     m_editor->removeLast(m_path);
0134 }
0135 
0136 
0137 /**
0138  * Redo the line command. Call the Editor lineTo function with the m_to point
0139  * returning the QPainterPath existing before the lineTo which is saved for undo.
0140  */
0141 void LineToCommand::redo()
0142 {
0143     m_path = m_editor->lineTo(m_to);
0144 }
0145 
0146 
0147 /**
0148  * Constructor
0149  *
0150  * @param editor a pointer to the Editor
0151  * @param control1 a const reference to a QPointF representing the first control point
0152  * @param control2 a const reference to a QPointF representing the second control point
0153  * @param to a const reference to a QPointF representing the end point of the curve
0154  */
0155 CubicToCommand::CubicToCommand(Editor *editor, const QPointF &control1, const QPointF &control2, const QPointF &to)
0156     :   QUndoCommand(i18n("Cubic To")),
0157         m_editor(editor),
0158         m_control1(control1),
0159         m_control2(control2),
0160         m_to(to)
0161 {
0162 }
0163 
0164 
0165 /**
0166  * Destructor
0167  */
0168 CubicToCommand::~CubicToCommand()
0169 {
0170 }
0171 
0172 
0173 /**
0174  * Undo the cubic command. Restore the path saved during redo.
0175  */
0176 void CubicToCommand::undo()
0177 {
0178     m_editor->removeLast(m_path);
0179 }
0180 
0181 
0182 /**
0183  * Redo the cubic command. Call the Editor cubicTo function with the three points
0184  * returning the QPainterPath existing before the cubicTo which is saved for undo.
0185  */
0186 void CubicToCommand::redo()
0187 {
0188     m_path = m_editor->cubicTo(m_control1, m_control2, m_to);
0189 }
0190 
0191 
0192 /**
0193  * Constructor
0194  *
0195  * @param editor a pointer to the Editor
0196  * @param from a const reference to a QPointF representing the first corner
0197  * @param to a const reference to a QPointF representing the second corner
0198  */
0199 RectangleCommand::RectangleCommand(Editor *editor, const QPointF &from, const QPointF &to)
0200     :   QUndoCommand(i18n("Add Rectangle")),
0201         m_editor(editor),
0202         m_from(from),
0203         m_to(to)
0204 {
0205 }
0206 
0207 
0208 /**
0209  * Destructor
0210  */
0211 RectangleCommand::~RectangleCommand()
0212 {
0213 }
0214 
0215 
0216 /**
0217  * Undo the rectangle command. Restore the path saved during redo.
0218  */
0219 void RectangleCommand::undo()
0220 {
0221     m_editor->removeLast(m_path);
0222 }
0223 
0224 
0225 /**
0226  * Redo the rectangle command. Call the Editor addRectangle function with the two points
0227  * returning the QPainterPath existing before the addRectangle which is saved for undo.
0228  */
0229 void RectangleCommand::redo()
0230 {
0231     m_path = m_editor->addRectangle(m_from, m_to);
0232 }
0233 
0234 
0235 /**
0236  * Constructor
0237  *
0238  * @param editor a pointer to the Editor
0239  * @param from a const reference to a QPointF representing the first corner
0240  * @param to a const reference to a QPointF representing the second corner
0241  */
0242 EllipseCommand::EllipseCommand(Editor *editor, const QPointF &from, const QPointF &to)
0243     :   QUndoCommand(i18n("Add Ellipse")),
0244         m_editor(editor),
0245         m_from(from),
0246         m_to(to)
0247 {
0248 }
0249 
0250 
0251 /**
0252  * Destructor
0253  */
0254 EllipseCommand::~EllipseCommand()
0255 {
0256 }
0257 
0258 
0259 /**
0260  * Undo the ellipse command. Restore the path saved during redo.
0261  */
0262 void EllipseCommand::undo()
0263 {
0264     m_editor->removeLast(m_path);
0265 }
0266 
0267 
0268 /**
0269  * Redo the ellipse command. Call the Editor addEllipse function with the two points
0270  * returning the QPainterPath existing before the addEllipse which is saved for undo.
0271  */
0272 void EllipseCommand::redo()
0273 {
0274     m_path = m_editor->addEllipse(m_from, m_to);
0275 }
0276 
0277 
0278 /**
0279  * Constructor
0280  *
0281  * @param editor a pointer to the Editor
0282  * @param index the index of the point in the Editor::m_points list
0283  * @param from a const reference to a QPointF representing the original position
0284  * @param to a const reference to a QPointF representing the new position
0285  */
0286 MovePointCommand::MovePointCommand(Editor *editor, int index, const QPointF &from, const QPointF &to)
0287     :   QUndoCommand(i18n("Move point")),
0288         m_editor(editor),
0289         m_index(index),
0290         m_from(from),
0291         m_to(to)
0292 {
0293 }
0294 
0295 
0296 /**
0297  * Destructor
0298  */
0299 MovePointCommand::~MovePointCommand()
0300 {
0301 }
0302 
0303 
0304 /**
0305  * Undo the move point command. Call the Editor::movePoint function to move the
0306  * indexed point to its original from position.
0307  */
0308 void MovePointCommand::undo()
0309 {
0310     m_editor->movePoint(m_index, m_from);
0311 }
0312 
0313 
0314 /**
0315  * Redo the move point command. Call the Editor::movePoint function to move the
0316  * indexed point to the to position.
0317  */
0318 void MovePointCommand::redo()
0319 {
0320     m_editor->movePoint(m_index, m_to);
0321 }
0322 
0323 
0324 /**
0325  * Constructor
0326  *
0327  * @param library a pointer to the SymbolLibrary
0328  * @param index the index of the symbol to be updated
0329  * @param symbol a const reference to a Symbol representing the updated symbol
0330  */
0331 UpdateSymbolCommand::UpdateSymbolCommand(SymbolLibrary *library, qint16 index, const Symbol &symbol)
0332     :   QUndoCommand(i18n("Update Symbol")),
0333         m_symbolLibrary(library),
0334         m_index(index),
0335         m_symbol(symbol)
0336 {
0337 }
0338 
0339 
0340 /**
0341  * Destructor
0342  */
0343 UpdateSymbolCommand::~UpdateSymbolCommand()
0344 {
0345 }
0346 
0347 
0348 /**
0349  * Undo the update symbol command. If the original path was not empty it is restored
0350  * to the indexed symbol otherwise the indexes symbol is removed from the library.
0351  */
0352 void UpdateSymbolCommand::undo()
0353 {
0354     if (m_originalSymbol.path().isEmpty()) {
0355         m_symbolLibrary->takeSymbol(m_index);
0356         m_index = 0;
0357     } else {
0358         m_symbolLibrary->setSymbol(m_index, m_originalSymbol);
0359     }
0360 }
0361 
0362 
0363 /**
0364  * Redo the update symbol command. The original Symbol is saved for undo. This may be empty.
0365  * The new Symbol is set in the library for the given index. If the index is 0 a new index
0366  * is generated, returned and saved for undo.
0367  */
0368 void UpdateSymbolCommand::redo()
0369 {
0370     m_originalSymbol = m_symbolLibrary->symbol(m_index);
0371     m_index = m_symbolLibrary->setSymbol(m_index, m_symbol);
0372 }
0373 
0374 
0375 /**
0376  * Constructor
0377  *
0378  * @param library a pointer to the SymbolLibrary
0379  * @param imported a pointer to the imported SymbolLibrary
0380  */
0381 ImportLibraryCommand::ImportLibraryCommand(SymbolLibrary *library, SymbolLibrary *imported)
0382     :   QUndoCommand(i18n("Import Library")),
0383         m_symbolLibrary(library),
0384         m_imported(imported)
0385 {
0386 }
0387 
0388 
0389 /**
0390  * Destructor
0391  * The imported library is deleted as this class has taken ownership of it.
0392  */
0393 ImportLibraryCommand::~ImportLibraryCommand()
0394 {
0395     delete m_imported;
0396 }
0397 
0398 
0399 /**
0400  * Undo the import library command. All symbols that were added are removed
0401  * from the library. The list of added indexes is cleared.
0402  */
0403 void ImportLibraryCommand::undo()
0404 {
0405     foreach (qint16 i, m_addedIndexes) {
0406         m_symbolLibrary->takeSymbol(i);
0407     }
0408 
0409     m_addedIndexes.clear();
0410 }
0411 
0412 
0413 /**
0414  * Redo the import library command. Each symbol in the imported library are added to the current
0415  * library creating new indexes which are added to the m_addedIndexes list for undo.
0416  */
0417 void ImportLibraryCommand::redo()
0418 {
0419     foreach (qint16 i, m_imported->indexes()) {
0420         m_addedIndexes.append(m_symbolLibrary->setSymbol(0, m_imported->symbol(i))); // gets a new index
0421     }
0422 }
0423 
0424 
0425 /**
0426  * Constructor
0427  *
0428  * @param editor a pointer to the Editor
0429  */
0430 RotateLeftCommand::RotateLeftCommand(Editor *editor)
0431     :   QUndoCommand(i18n("Rotate Left")),
0432         m_editor(editor)
0433 {
0434 }
0435 
0436 
0437 /**
0438  * Destructor
0439  */
0440 RotateLeftCommand::~RotateLeftCommand()
0441 {
0442 }
0443 
0444 
0445 /**
0446  * Undo the rotate left command. Call the Editor::rotatePointRight function to reverse
0447  * the rotate left.
0448  */
0449 void RotateLeftCommand::undo()
0450 {
0451     m_editor->rotatePointsRight();
0452 }
0453 
0454 
0455 /**
0456  * Redo the rotate left command. Call the Editor::rotatePointsLeft function to rotate the points.
0457  */
0458 void RotateLeftCommand::redo()
0459 {
0460     m_editor->rotatePointsLeft();
0461 }
0462 
0463 
0464 /**
0465  * Constructor
0466  *
0467  * @param editor a pointer to the Editor
0468  */
0469 RotateRightCommand::RotateRightCommand(Editor *editor)
0470     :   QUndoCommand(i18n("Rotate Right")),
0471         m_editor(editor)
0472 {
0473 }
0474 
0475 
0476 /**
0477  * Destructor
0478  */
0479 RotateRightCommand::~RotateRightCommand()
0480 {
0481 }
0482 
0483 
0484 /**
0485  * Undo the rotate right command. Call the Editor::rotatePointsLeft function to reverse
0486  * the rotate right.
0487  */
0488 void RotateRightCommand::undo()
0489 {
0490     m_editor->rotatePointsLeft();
0491 }
0492 
0493 
0494 /**
0495  * Redo the rotate right command. Call the Editor::rotatePointsRight function to rotate the points.
0496  */
0497 void RotateRightCommand::redo()
0498 {
0499     m_editor->rotatePointsRight();
0500 }
0501 
0502 
0503 /**
0504  * Constructor
0505  *
0506  * @param editor a pointer to the Editor
0507  */
0508 FlipHorizontalCommand::FlipHorizontalCommand(Editor *editor)
0509     :   QUndoCommand(i18n("Flip Horizontal")),
0510         m_editor(editor)
0511 {
0512 }
0513 
0514 
0515 /**
0516  * Destructor
0517  */
0518 FlipHorizontalCommand::~FlipHorizontalCommand()
0519 {
0520 }
0521 
0522 
0523 /**
0524  * Undo the flip horizontal command. Call the Editor::flipPointsHorizontal to flip the points.
0525  */
0526 void FlipHorizontalCommand::undo()
0527 {
0528     m_editor->flipPointsHorizontal();
0529 }
0530 
0531 
0532 /**
0533  * Redo the flip horizontal command. Call the Editor::flipPointsHorizontal to flip the points.
0534  */
0535 void FlipHorizontalCommand::redo()
0536 {
0537     m_editor->flipPointsHorizontal();
0538 }
0539 
0540 
0541 /**
0542  * Constructor
0543  *
0544  * @param editor a pointer to the Editor
0545  */
0546 FlipVerticalCommand::FlipVerticalCommand(Editor *editor)
0547     :   QUndoCommand(i18n("Flip Vertical")),
0548         m_editor(editor)
0549 {
0550 }
0551 
0552 
0553 /**
0554  * Destructor
0555  */
0556 FlipVerticalCommand::~FlipVerticalCommand()
0557 {
0558 }
0559 
0560 
0561 /**
0562  * Undo the flip vertical command. Call the Editor::flipPointsVertical to flip the points.
0563  */
0564 void FlipVerticalCommand::undo()
0565 {
0566     m_editor->flipPointsVertical();
0567 }
0568 
0569 
0570 /**
0571  * Redo the flip vertical command. Call the Editor::flipPointsVertical to flip the points.
0572  */
0573 void FlipVerticalCommand::redo()
0574 {
0575     m_editor->flipPointsVertical();
0576 }
0577 
0578 
0579 /**
0580  * Constructor
0581  *
0582  * @param editor a pointer to the editor
0583  * @param painterPath the current path to scale
0584  * @param gridElements the number of elements along the grid side
0585  * @param borderSize the number of elements required for the border
0586  */
0587 ScalePreferredCommand::ScalePreferredCommand(Editor *editor, const QPainterPath &originalSymbol, int gridElements, int borderSize)
0588     :   QUndoCommand(i18n("Scale to Preferred Size")),
0589         m_editor(editor),
0590         m_originalSymbol(originalSymbol),
0591         m_gridElements(gridElements),
0592         m_borderSize(borderSize)
0593 {
0594 }
0595 
0596 
0597 /**
0598  * Destructor.
0599  */
0600 ScalePreferredCommand::~ScalePreferredCommand()
0601 {
0602 }
0603 
0604 
0605 /**
0606  * Undo the scale command. Restore the original path.
0607  */
0608 void ScalePreferredCommand::undo()
0609 {
0610     m_editor->setPath(m_originalSymbol);
0611 }
0612 
0613 
0614 /**
0615  * Redo the scale command. Apply the scaled symbol.
0616  */
0617 void ScalePreferredCommand::redo()
0618 {
0619     double borderSize = double(m_borderSize) / double(m_gridElements);
0620     double threshold = 0.01;
0621     QRectF fullSize(0.0, 0.0, 1.0, 1.0);
0622     QRectF preferredSize(borderSize, borderSize, 1.0 - borderSize - borderSize, 1.0 - borderSize - borderSize);
0623 
0624     QPainterPath border;
0625     border.addRect(fullSize);
0626     border.addRect(preferredSize.adjusted(-threshold, -threshold, threshold, threshold));
0627     border.setFillRule(Qt::OddEvenFill);
0628 
0629     if (m_originalSymbol.intersects(border)) {
0630         QRectF boundingRect = m_originalSymbol.boundingRect();
0631         double leftOverlap = std::max(preferredSize.left() - boundingRect.left(), 0.0);
0632         double topOverlap = std::max(preferredSize.top() - boundingRect.top(), 0.0);
0633         double rightOverlap = std::max(boundingRect.right() - preferredSize.right(), 0.0);
0634         double bottomOverlap = std::max(boundingRect.bottom() - preferredSize.bottom(), 0.0);
0635         double overlap = std::max(std::max(leftOverlap, rightOverlap), std::max(topOverlap, bottomOverlap));
0636         QRectF startingSize = preferredSize.adjusted(-overlap, -overlap, overlap, overlap);
0637         double scale = preferredSize.width() / startingSize.width();
0638         QTransform transform = QTransform::fromTranslate(-0.5, -0.5) * QTransform::fromScale(scale, scale) * QTransform::fromTranslate(0.5, 0.5);
0639         QPainterPath scaledPath = transform.map(m_originalSymbol);
0640         m_editor->setPath(scaledPath);
0641     }
0642 }
0643 
0644 
0645 /**
0646  * Constructor
0647  *
0648  * @param editor a pointer to the Editor
0649  * @param from @c true if the currently filled, @c false otherwise
0650  * @param to @c true if changing to filled, @c false otherwise
0651  */
0652 ChangeFilledCommand::ChangeFilledCommand(Editor *editor, bool from, bool to)
0653     :   QUndoCommand(i18n("Change Fill State")),
0654         m_editor(editor),
0655         m_from(from),
0656         m_to(to)
0657 {
0658 }
0659 
0660 
0661 /**
0662  * Destructor
0663  */
0664 ChangeFilledCommand::~ChangeFilledCommand()
0665 {
0666 }
0667 
0668 
0669 /**
0670  * Undo the change fill state command. Call the Editor::setFilled function to set the original value
0671  */
0672 void ChangeFilledCommand::undo()
0673 {
0674     m_editor->setFilled(m_from);
0675 }
0676 
0677 
0678 /**
0679  * Redo the change fill state command. Call the Editor::setFilled function to set the new value.
0680  */
0681 void ChangeFilledCommand::redo()
0682 {
0683     m_editor->setFilled(m_to);
0684 }
0685 
0686 
0687 /**
0688  * Constructor
0689  *
0690  * @param editor a point to the Editor
0691  * @param from a Qt::FillRule value representing the original setting
0692  * @param to a Qt::FillRule value representing the new setting
0693  */
0694 ChangeFillRuleCommand::ChangeFillRuleCommand(Editor *editor, Qt::FillRule from, Qt::FillRule to)
0695     :   QUndoCommand(i18n("Change Fill Rule")),
0696         m_editor(editor),
0697         m_from(from),
0698         m_to(to)
0699 {
0700 }
0701 
0702 
0703 /**
0704  * Destructor
0705  */
0706 ChangeFillRuleCommand::~ChangeFillRuleCommand()
0707 {
0708 }
0709 
0710 
0711 /**
0712  * Undo the change fill rule command. Call the Editor::setFillRule function to set the original value.
0713  */
0714 void ChangeFillRuleCommand::undo()
0715 {
0716     m_editor->setFillRule(m_from);
0717 }
0718 
0719 
0720 /**
0721  * Redo the change fill rule command. Call the Editor::setFillRule function to set the new value.
0722  */
0723 void ChangeFillRuleCommand::redo()
0724 {
0725     m_editor->setFillRule(m_to);
0726 }
0727 
0728 
0729 /**
0730  * Constructor
0731  *
0732  * @param editor a pointer to the Editor
0733  * @param from a Qt::PenCapStyle value representing the orginal setting
0734  * @param to a Qt::PenCapStyle value representing the new setting
0735  */
0736 ChangeCapStyleCommand::ChangeCapStyleCommand(Editor *editor, Qt::PenCapStyle from, Qt::PenCapStyle to)
0737     :   QUndoCommand(i18n("Change Cap Style")),
0738         m_editor(editor),
0739         m_from(from),
0740         m_to(to)
0741 {
0742 }
0743 
0744 
0745 /**
0746  * Destructor
0747  */
0748 ChangeCapStyleCommand::~ChangeCapStyleCommand()
0749 {
0750 }
0751 
0752 
0753 /**
0754  * Undo the change pen cap style command.
0755  */
0756 void ChangeCapStyleCommand::undo()
0757 {
0758     m_editor->setCapStyle(m_from);
0759 }
0760 
0761 
0762 /**
0763  * Redo the change pen cap style command.
0764  */
0765 void ChangeCapStyleCommand::redo()
0766 {
0767     m_editor->setCapStyle(m_to);
0768 }
0769 
0770 
0771 /**
0772  * Constructor
0773  *
0774  * @param editor a pointer to the Editor
0775  * @param from a Qt::PenJoinStyle value representing the original setting
0776  * @param to a Qt::PenJoinStyle value representing the new setting
0777  */
0778 ChangeJoinStyleCommand::ChangeJoinStyleCommand(Editor *editor, Qt::PenJoinStyle from, Qt::PenJoinStyle to)
0779     :   QUndoCommand(i18n("Change Join Style")),
0780         m_editor(editor),
0781         m_from(from),
0782         m_to(to)
0783 {
0784 }
0785 
0786 
0787 /**
0788  * Destructor
0789  */
0790 ChangeJoinStyleCommand::~ChangeJoinStyleCommand()
0791 {
0792 }
0793 
0794 
0795 /**
0796  * Undo the change pen join style command.
0797  */
0798 void ChangeJoinStyleCommand::undo()
0799 {
0800     m_editor->setJoinStyle(m_from);
0801 }
0802 
0803 
0804 /**
0805  * Redo the change pen join style command.
0806  */
0807 void ChangeJoinStyleCommand::redo()
0808 {
0809     m_editor->setJoinStyle(m_to);
0810 }
0811 
0812 
0813 /**
0814  * Constructor
0815  *
0816  * @param library a pointer to the SymbolLibrary
0817  * @param index the index of the Symbol to delete
0818  */
0819 DeleteSymbolCommand::DeleteSymbolCommand(SymbolLibrary *library, qint16 index)
0820     :   QUndoCommand(i18n("Delete Symbol")),
0821         m_symbolLibrary(library),
0822         m_index(index)
0823 {
0824 }
0825 
0826 
0827 /**
0828  * Destructor
0829  */
0830 DeleteSymbolCommand::~DeleteSymbolCommand()
0831 {
0832 }
0833 
0834 
0835 /**
0836  * Undo deleting the Symbol restoring it from the saved one.
0837  */
0838 void DeleteSymbolCommand::undo()
0839 {
0840     m_symbolLibrary->setSymbol(m_index, m_symbol);
0841 }
0842 
0843 
0844 /**
0845  * Redo deleting the Symbol. Store the removed Symbol for undo.
0846  */
0847 void DeleteSymbolCommand::redo()
0848 {
0849     m_symbol = m_symbolLibrary->takeSymbol(m_index);
0850 }
0851 
0852 
0853 /**
0854  * Constructor
0855  *
0856  * @param editor a pointer to the Editor
0857  * @param from the value of the original setting
0858  * @param to the value of the new setting
0859  */
0860 IncreaseLineWidthCommand::IncreaseLineWidthCommand(Editor *editor, qreal from, qreal to)
0861     :   QUndoCommand(i18n("Increase Line Width")),
0862         m_editor(editor),
0863         m_from(from),
0864         m_to(to)
0865 {
0866 }
0867 
0868 
0869 /**
0870  * Destructor
0871  */
0872 IncreaseLineWidthCommand::~IncreaseLineWidthCommand()
0873 {
0874 }
0875 
0876 
0877 /**
0878  * Undo the line width increase.
0879  */
0880 void IncreaseLineWidthCommand::undo()
0881 {
0882     m_editor->setLineWidth(m_from);
0883 }
0884 
0885 
0886 /**
0887  * Redo the line width increase.
0888  */
0889 void IncreaseLineWidthCommand::redo()
0890 {
0891     m_editor->setLineWidth(m_to);
0892 }
0893 
0894 
0895 /**
0896  * Get the id related to this command.
0897  *
0898  * @return int representing the value from the IDs enum
0899  */
0900 int IncreaseLineWidthCommand::id() const
0901 {
0902     return static_cast<int>(IncreaseLineWidth);
0903 }
0904 
0905 
0906 /**
0907  * Merge this command with another IncreaseLineWidthCommand.
0908  * This allows repeated increases without additional commands added to the stack.
0909  *
0910  * @param command a pointer to the additional QUndoCommand
0911  *
0912  * @return @c true if the merge succeeded, @c false otherwise
0913  */
0914 bool IncreaseLineWidthCommand::mergeWith(const QUndoCommand *command)
0915 {
0916     if (command->id() != id()) {
0917         return false;
0918     }
0919 
0920     m_to += static_cast<const IncreaseLineWidthCommand *>(command)->m_to;
0921     return true;
0922 }
0923 
0924 
0925 /**
0926  * Constructor
0927  *
0928  * @param editor a pointer to the Editor
0929  * @param from the value of the original setting
0930  * @param to the value of the new setting
0931  */
0932 DecreaseLineWidthCommand::DecreaseLineWidthCommand(Editor *editor, qreal from, qreal to)
0933     :   QUndoCommand(i18n("Decrease Line Width")),
0934         m_editor(editor),
0935         m_from(from),
0936         m_to(to)
0937 {
0938 }
0939 
0940 
0941 /**
0942  * Destructor
0943  */
0944 DecreaseLineWidthCommand::~DecreaseLineWidthCommand()
0945 {
0946 }
0947 
0948 
0949 /**
0950  * Undo the line width decrease.
0951  */
0952 void DecreaseLineWidthCommand::undo()
0953 {
0954     m_editor->setLineWidth(m_from);
0955 }
0956 
0957 
0958 /**
0959  * Redo the line width decrease.
0960  */
0961 void DecreaseLineWidthCommand::redo()
0962 {
0963     m_editor->setLineWidth(m_to);
0964 }
0965 
0966 
0967 /**
0968  * Get the id related to this command.
0969  *
0970  * @return int representing the value from the IDs enum
0971  */
0972 int DecreaseLineWidthCommand::id() const
0973 {
0974     return static_cast<int>(DecreaseLineWidth);
0975 }
0976 
0977 
0978 /**
0979  * Merge this command with another DecreaseLineWidthCommand.
0980  * This allows repeated decreases without additional commands added to the stack.
0981  *
0982  * @param command a pointer to the additional QUndoCommand
0983  *
0984  * @return @c true if the merge succeeded, @c false otherwise
0985  */
0986 bool DecreaseLineWidthCommand::mergeWith(const QUndoCommand *command)
0987 {
0988     if (command->id() != id()) {
0989         return false;
0990     }
0991 
0992     m_to -= static_cast<const DecreaseLineWidthCommand *>(command)->m_to;
0993     return true;
0994 }
0995 
0996 
0997 /**
0998  * Constructor.
0999  *
1000  * @param library pointer to the symbol library to add the new symbols to
1001  * @param mimeData pointer to the QMimeData containing the new symbols
1002  */
1003 DragAndDropCommand::DragAndDropCommand(SymbolLibrary *library, const QMimeData *mimeData)
1004     :   QUndoCommand(i18n("Add Symbols")),
1005         m_library(library)
1006 {
1007     QByteArray data = mimeData->data("application/kxstitchsymbol");
1008     QDataStream stream(&data, QIODevice::ReadOnly);
1009 
1010     while (!stream.atEnd()) {
1011         Symbol symbol;
1012         stream >> symbol;
1013         m_symbols.append(symbol);
1014     }
1015 }
1016 
1017 
1018 /**
1019  * Redo the addition of symbols dragged to the list.
1020  */
1021 void DragAndDropCommand::redo()
1022 {
1023     foreach (const Symbol & symbol, m_symbols) {
1024         m_indexes.append(m_library->setSymbol(0, symbol));
1025     }
1026 }
1027 
1028 
1029 /**
1030  * Undo the addition of the symbols added.
1031  */
1032 void DragAndDropCommand::undo()
1033 {
1034     foreach (qint16 index, m_indexes) {
1035         m_library->takeSymbol(index);
1036     }
1037 
1038     m_indexes.clear();
1039 }
1040 
1041 
1042 /**
1043  * Constructor.
1044  *
1045  * @param editor a pointer to the editor
1046  * @param path the QPainterPath representing the character glyph
1047  */
1048 AddCharacterCommand::AddCharacterCommand(Editor *editor, const QPainterPath &path)
1049     :   QUndoCommand(i18n("Add Character")),
1050         m_editor(editor),
1051         m_path(path)
1052 {
1053 }
1054 
1055 
1056 void AddCharacterCommand::redo()
1057 {
1058     m_path = m_editor->setPath(m_path);
1059 }
1060 
1061 
1062 void AddCharacterCommand::undo()
1063 {
1064     m_path = m_editor->setPath(m_path);
1065 }