File indexing completed on 2024-04-21 04:33:59

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  * Undo the move to command. Restores the path saved during redo.
0081  */
0082 void MoveToCommand::undo()
0083 {
0084     m_editor->removeLast(m_path);
0085 }
0086 
0087 
0088 /**
0089  * Redo the move command. Call the Editor moveTo function with the m_to point
0090  * returning the QPainterPath existing before the moveTo which is saved for undo.
0091  */
0092 void MoveToCommand::redo()
0093 {
0094     m_path = m_editor->moveTo(m_to);
0095 }
0096 
0097 
0098 /**
0099  * Constructor
0100  *
0101  * @param editor a pointer to the Editor
0102  * @param to a const reference to a QPointF representing the point to draw to
0103  */
0104 LineToCommand::LineToCommand(Editor *editor, const QPointF &to)
0105     :   QUndoCommand(i18n("Draw To")),
0106         m_editor(editor),
0107         m_to(to)
0108 {
0109 }
0110 
0111 
0112 /**
0113  * Undo the line command. Restore the path saved during redo.
0114  */
0115 void LineToCommand::undo()
0116 {
0117     m_editor->removeLast(m_path);
0118 }
0119 
0120 
0121 /**
0122  * Redo the line command. Call the Editor lineTo function with the m_to point
0123  * returning the QPainterPath existing before the lineTo which is saved for undo.
0124  */
0125 void LineToCommand::redo()
0126 {
0127     m_path = m_editor->lineTo(m_to);
0128 }
0129 
0130 
0131 /**
0132  * Constructor
0133  *
0134  * @param editor a pointer to the Editor
0135  * @param control1 a const reference to a QPointF representing the first control point
0136  * @param control2 a const reference to a QPointF representing the second control point
0137  * @param to a const reference to a QPointF representing the end point of the curve
0138  */
0139 CubicToCommand::CubicToCommand(Editor *editor, const QPointF &control1, const QPointF &control2, const QPointF &to)
0140     :   QUndoCommand(i18n("Cubic To")),
0141         m_editor(editor),
0142         m_control1(control1),
0143         m_control2(control2),
0144         m_to(to)
0145 {
0146 }
0147 
0148 
0149 /**
0150  * Undo the cubic command. Restore the path saved during redo.
0151  */
0152 void CubicToCommand::undo()
0153 {
0154     m_editor->removeLast(m_path);
0155 }
0156 
0157 
0158 /**
0159  * Redo the cubic command. Call the Editor cubicTo function with the three points
0160  * returning the QPainterPath existing before the cubicTo which is saved for undo.
0161  */
0162 void CubicToCommand::redo()
0163 {
0164     m_path = m_editor->cubicTo(m_control1, m_control2, m_to);
0165 }
0166 
0167 
0168 /**
0169  * Constructor
0170  *
0171  * @param editor a pointer to the Editor
0172  * @param from a const reference to a QPointF representing the first corner
0173  * @param to a const reference to a QPointF representing the second corner
0174  */
0175 RectangleCommand::RectangleCommand(Editor *editor, const QPointF &from, const QPointF &to)
0176     :   QUndoCommand(i18n("Add Rectangle")),
0177         m_editor(editor),
0178         m_from(from),
0179         m_to(to)
0180 {
0181 }
0182 
0183 
0184 /**
0185  * Undo the rectangle command. Restore the path saved during redo.
0186  */
0187 void RectangleCommand::undo()
0188 {
0189     m_editor->removeLast(m_path);
0190 }
0191 
0192 
0193 /**
0194  * Redo the rectangle command. Call the Editor addRectangle function with the two points
0195  * returning the QPainterPath existing before the addRectangle which is saved for undo.
0196  */
0197 void RectangleCommand::redo()
0198 {
0199     m_path = m_editor->addRectangle(m_from, m_to);
0200 }
0201 
0202 
0203 /**
0204  * Constructor
0205  *
0206  * @param editor a pointer to the Editor
0207  * @param from a const reference to a QPointF representing the first corner
0208  * @param to a const reference to a QPointF representing the second corner
0209  */
0210 EllipseCommand::EllipseCommand(Editor *editor, const QPointF &from, const QPointF &to)
0211     :   QUndoCommand(i18n("Add Ellipse")),
0212         m_editor(editor),
0213         m_from(from),
0214         m_to(to)
0215 {
0216 }
0217 
0218 
0219 /**
0220  * Undo the ellipse command. Restore the path saved during redo.
0221  */
0222 void EllipseCommand::undo()
0223 {
0224     m_editor->removeLast(m_path);
0225 }
0226 
0227 
0228 /**
0229  * Redo the ellipse command. Call the Editor addEllipse function with the two points
0230  * returning the QPainterPath existing before the addEllipse which is saved for undo.
0231  */
0232 void EllipseCommand::redo()
0233 {
0234     m_path = m_editor->addEllipse(m_from, m_to);
0235 }
0236 
0237 
0238 /**
0239  * Constructor
0240  *
0241  * @param editor a pointer to the Editor
0242  * @param index the index of the point in the Editor::m_points list
0243  * @param from a const reference to a QPointF representing the original position
0244  * @param to a const reference to a QPointF representing the new position
0245  */
0246 MovePointCommand::MovePointCommand(Editor *editor, int index, const QPointF &from, const QPointF &to)
0247     :   QUndoCommand(i18n("Move point")),
0248         m_editor(editor),
0249         m_index(index),
0250         m_from(from),
0251         m_to(to)
0252 {
0253 }
0254 
0255 
0256 /**
0257  * Undo the move point command. Call the Editor::movePoint function to move the
0258  * indexed point to its original from position.
0259  */
0260 void MovePointCommand::undo()
0261 {
0262     m_editor->movePoint(m_index, m_from);
0263 }
0264 
0265 
0266 /**
0267  * Redo the move point command. Call the Editor::movePoint function to move the
0268  * indexed point to the to position.
0269  */
0270 void MovePointCommand::redo()
0271 {
0272     m_editor->movePoint(m_index, m_to);
0273 }
0274 
0275 
0276 /**
0277  * Constructor
0278  *
0279  * @param library a pointer to the SymbolLibrary
0280  * @param index the index of the symbol to be updated
0281  * @param symbol a const reference to a Symbol representing the updated symbol
0282  */
0283 UpdateSymbolCommand::UpdateSymbolCommand(SymbolLibrary *library, qint16 index, const Symbol &symbol)
0284     :   QUndoCommand(i18n("Update Symbol")),
0285         m_symbolLibrary(library),
0286         m_index(index),
0287         m_symbol(symbol)
0288 {
0289 }
0290 
0291 
0292 /**
0293  * Undo the update symbol command. If the original path was not empty it is restored
0294  * to the indexed symbol otherwise the indexes symbol is removed from the library.
0295  */
0296 void UpdateSymbolCommand::undo()
0297 {
0298     if (m_originalSymbol.path().isEmpty()) {
0299         m_symbolLibrary->takeSymbol(m_index);
0300         m_index = 0;
0301     } else {
0302         m_symbolLibrary->setSymbol(m_index, m_originalSymbol);
0303     }
0304 }
0305 
0306 
0307 /**
0308  * Redo the update symbol command. The original Symbol is saved for undo. This may be empty.
0309  * The new Symbol is set in the library for the given index. If the index is 0 a new index
0310  * is generated, returned and saved for undo.
0311  */
0312 void UpdateSymbolCommand::redo()
0313 {
0314     m_originalSymbol = m_symbolLibrary->symbol(m_index);
0315     m_index = m_symbolLibrary->setSymbol(m_index, m_symbol);
0316 }
0317 
0318 
0319 /**
0320  * Constructor
0321  *
0322  * @param library a pointer to the SymbolLibrary
0323  * @param imported a pointer to the imported SymbolLibrary
0324  */
0325 ImportLibraryCommand::ImportLibraryCommand(SymbolLibrary *library, SymbolLibrary *imported)
0326     :   QUndoCommand(i18n("Import Library")),
0327         m_symbolLibrary(library),
0328         m_imported(imported)
0329 {
0330 }
0331 
0332 
0333 /**
0334  * Destructor
0335  * The imported library is deleted as this class has taken ownership of it.
0336  */
0337 ImportLibraryCommand::~ImportLibraryCommand()
0338 {
0339     delete m_imported;
0340 }
0341 
0342 
0343 /**
0344  * Undo the import library command. All symbols that were added are removed
0345  * from the library. The list of added indexes is cleared.
0346  */
0347 void ImportLibraryCommand::undo()
0348 {
0349     foreach (qint16 i, m_addedIndexes) {
0350         m_symbolLibrary->takeSymbol(i);
0351     }
0352 
0353     m_addedIndexes.clear();
0354 }
0355 
0356 
0357 /**
0358  * Redo the import library command. Each symbol in the imported library are added to the current
0359  * library creating new indexes which are added to the m_addedIndexes list for undo.
0360  */
0361 void ImportLibraryCommand::redo()
0362 {
0363     foreach (qint16 i, m_imported->indexes()) {
0364         m_addedIndexes.append(m_symbolLibrary->setSymbol(0, m_imported->symbol(i))); // gets a new index
0365     }
0366 }
0367 
0368 
0369 /**
0370  * Constructor
0371  *
0372  * @param editor a pointer to the Editor
0373  */
0374 RotateLeftCommand::RotateLeftCommand(Editor *editor)
0375     :   QUndoCommand(i18n("Rotate Left")),
0376         m_editor(editor)
0377 {
0378 }
0379 
0380 
0381 /**
0382  * Undo the rotate left command. Call the Editor::rotatePointRight function to reverse
0383  * the rotate left.
0384  */
0385 void RotateLeftCommand::undo()
0386 {
0387     m_editor->rotatePointsRight();
0388 }
0389 
0390 
0391 /**
0392  * Redo the rotate left command. Call the Editor::rotatePointsLeft function to rotate the points.
0393  */
0394 void RotateLeftCommand::redo()
0395 {
0396     m_editor->rotatePointsLeft();
0397 }
0398 
0399 
0400 /**
0401  * Constructor
0402  *
0403  * @param editor a pointer to the Editor
0404  */
0405 RotateRightCommand::RotateRightCommand(Editor *editor)
0406     :   QUndoCommand(i18n("Rotate Right")),
0407         m_editor(editor)
0408 {
0409 }
0410 
0411 
0412 /**
0413  * Undo the rotate right command. Call the Editor::rotatePointsLeft function to reverse
0414  * the rotate right.
0415  */
0416 void RotateRightCommand::undo()
0417 {
0418     m_editor->rotatePointsLeft();
0419 }
0420 
0421 
0422 /**
0423  * Redo the rotate right command. Call the Editor::rotatePointsRight function to rotate the points.
0424  */
0425 void RotateRightCommand::redo()
0426 {
0427     m_editor->rotatePointsRight();
0428 }
0429 
0430 
0431 /**
0432  * Constructor
0433  *
0434  * @param editor a pointer to the Editor
0435  */
0436 FlipHorizontalCommand::FlipHorizontalCommand(Editor *editor)
0437     :   QUndoCommand(i18n("Flip Horizontal")),
0438         m_editor(editor)
0439 {
0440 }
0441 
0442 
0443 /**
0444  * Undo the flip horizontal command. Call the Editor::flipPointsHorizontal to flip the points.
0445  */
0446 void FlipHorizontalCommand::undo()
0447 {
0448     m_editor->flipPointsHorizontal();
0449 }
0450 
0451 
0452 /**
0453  * Redo the flip horizontal command. Call the Editor::flipPointsHorizontal to flip the points.
0454  */
0455 void FlipHorizontalCommand::redo()
0456 {
0457     m_editor->flipPointsHorizontal();
0458 }
0459 
0460 
0461 /**
0462  * Constructor
0463  *
0464  * @param editor a pointer to the Editor
0465  */
0466 FlipVerticalCommand::FlipVerticalCommand(Editor *editor)
0467     :   QUndoCommand(i18n("Flip Vertical")),
0468         m_editor(editor)
0469 {
0470 }
0471 
0472 
0473 /**
0474  * Undo the flip vertical command. Call the Editor::flipPointsVertical to flip the points.
0475  */
0476 void FlipVerticalCommand::undo()
0477 {
0478     m_editor->flipPointsVertical();
0479 }
0480 
0481 
0482 /**
0483  * Redo the flip vertical command. Call the Editor::flipPointsVertical to flip the points.
0484  */
0485 void FlipVerticalCommand::redo()
0486 {
0487     m_editor->flipPointsVertical();
0488 }
0489 
0490 
0491 /**
0492  * Constructor
0493  *
0494  * @param editor a pointer to the editor
0495  * @param painterPath the current path to scale
0496  * @param gridElements the number of elements along the grid side
0497  * @param borderSize the number of elements required for the border
0498  */
0499 ScalePreferredCommand::ScalePreferredCommand(Editor *editor, const QPainterPath &originalSymbol, int gridElements, int borderSize)
0500     :   QUndoCommand(i18n("Scale to Preferred Size")),
0501         m_editor(editor),
0502         m_originalSymbol(originalSymbol),
0503         m_gridElements(gridElements),
0504         m_borderSize(borderSize)
0505 {
0506 }
0507 
0508 
0509 /**
0510  * Undo the scale command. Restore the original path.
0511  */
0512 void ScalePreferredCommand::undo()
0513 {
0514     m_editor->setPath(m_originalSymbol);
0515 }
0516 
0517 
0518 /**
0519  * Redo the scale command. Apply the scaled symbol.
0520  */
0521 void ScalePreferredCommand::redo()
0522 {
0523     double borderSize = double(m_borderSize) / double(m_gridElements);
0524     double threshold = 0.01;
0525     QRectF fullSize(0.0, 0.0, 1.0, 1.0);
0526     QRectF preferredSize(borderSize, borderSize, 1.0 - borderSize - borderSize, 1.0 - borderSize - borderSize);
0527 
0528     QPainterPath border;
0529     border.addRect(fullSize);
0530     border.addRect(preferredSize.adjusted(-threshold, -threshold, threshold, threshold));
0531     border.setFillRule(Qt::OddEvenFill);
0532 
0533     if (m_originalSymbol.intersects(border)) {
0534         QRectF boundingRect = m_originalSymbol.boundingRect();
0535         double leftOverlap = std::max(preferredSize.left() - boundingRect.left(), 0.0);
0536         double topOverlap = std::max(preferredSize.top() - boundingRect.top(), 0.0);
0537         double rightOverlap = std::max(boundingRect.right() - preferredSize.right(), 0.0);
0538         double bottomOverlap = std::max(boundingRect.bottom() - preferredSize.bottom(), 0.0);
0539         double overlap = std::max(std::max(leftOverlap, rightOverlap), std::max(topOverlap, bottomOverlap));
0540         QRectF startingSize = preferredSize.adjusted(-overlap, -overlap, overlap, overlap);
0541         double scale = preferredSize.width() / startingSize.width();
0542         QTransform transform = QTransform::fromTranslate(-0.5, -0.5) * QTransform::fromScale(scale, scale) * QTransform::fromTranslate(0.5, 0.5);
0543         QPainterPath scaledPath = transform.map(m_originalSymbol);
0544         m_editor->setPath(scaledPath);
0545     }
0546 }
0547 
0548 
0549 /**
0550  * Constructor
0551  *
0552  * @param editor a pointer to the Editor
0553  * @param from @c true if the currently filled, @c false otherwise
0554  * @param to @c true if changing to filled, @c false otherwise
0555  */
0556 ChangeFilledCommand::ChangeFilledCommand(Editor *editor, bool from, bool to)
0557     :   QUndoCommand(i18n("Change Fill State")),
0558         m_editor(editor),
0559         m_from(from),
0560         m_to(to)
0561 {
0562 }
0563 
0564 
0565 /**
0566  * Undo the change fill state command. Call the Editor::setFilled function to set the original value
0567  */
0568 void ChangeFilledCommand::undo()
0569 {
0570     m_editor->setFilled(m_from);
0571 }
0572 
0573 
0574 /**
0575  * Redo the change fill state command. Call the Editor::setFilled function to set the new value.
0576  */
0577 void ChangeFilledCommand::redo()
0578 {
0579     m_editor->setFilled(m_to);
0580 }
0581 
0582 
0583 /**
0584  * Constructor
0585  *
0586  * @param editor a point to the Editor
0587  * @param from a Qt::FillRule value representing the original setting
0588  * @param to a Qt::FillRule value representing the new setting
0589  */
0590 ChangeFillRuleCommand::ChangeFillRuleCommand(Editor *editor, Qt::FillRule from, Qt::FillRule to)
0591     :   QUndoCommand(i18n("Change Fill Rule")),
0592         m_editor(editor),
0593         m_from(from),
0594         m_to(to)
0595 {
0596 }
0597 
0598 
0599 /**
0600  * Undo the change fill rule command. Call the Editor::setFillRule function to set the original value.
0601  */
0602 void ChangeFillRuleCommand::undo()
0603 {
0604     m_editor->setFillRule(m_from);
0605 }
0606 
0607 
0608 /**
0609  * Redo the change fill rule command. Call the Editor::setFillRule function to set the new value.
0610  */
0611 void ChangeFillRuleCommand::redo()
0612 {
0613     m_editor->setFillRule(m_to);
0614 }
0615 
0616 
0617 /**
0618  * Constructor
0619  *
0620  * @param editor a pointer to the Editor
0621  * @param from a Qt::PenCapStyle value representing the orginal setting
0622  * @param to a Qt::PenCapStyle value representing the new setting
0623  */
0624 ChangeCapStyleCommand::ChangeCapStyleCommand(Editor *editor, Qt::PenCapStyle from, Qt::PenCapStyle to)
0625     :   QUndoCommand(i18n("Change Cap Style")),
0626         m_editor(editor),
0627         m_from(from),
0628         m_to(to)
0629 {
0630 }
0631 
0632 
0633 /**
0634  * Undo the change pen cap style command.
0635  */
0636 void ChangeCapStyleCommand::undo()
0637 {
0638     m_editor->setCapStyle(m_from);
0639 }
0640 
0641 
0642 /**
0643  * Redo the change pen cap style command.
0644  */
0645 void ChangeCapStyleCommand::redo()
0646 {
0647     m_editor->setCapStyle(m_to);
0648 }
0649 
0650 
0651 /**
0652  * Constructor
0653  *
0654  * @param editor a pointer to the Editor
0655  * @param from a Qt::PenJoinStyle value representing the original setting
0656  * @param to a Qt::PenJoinStyle value representing the new setting
0657  */
0658 ChangeJoinStyleCommand::ChangeJoinStyleCommand(Editor *editor, Qt::PenJoinStyle from, Qt::PenJoinStyle to)
0659     :   QUndoCommand(i18n("Change Join Style")),
0660         m_editor(editor),
0661         m_from(from),
0662         m_to(to)
0663 {
0664 }
0665 
0666 
0667 /**
0668  * Undo the change pen join style command.
0669  */
0670 void ChangeJoinStyleCommand::undo()
0671 {
0672     m_editor->setJoinStyle(m_from);
0673 }
0674 
0675 
0676 /**
0677  * Redo the change pen join style command.
0678  */
0679 void ChangeJoinStyleCommand::redo()
0680 {
0681     m_editor->setJoinStyle(m_to);
0682 }
0683 
0684 
0685 /**
0686  * Constructor
0687  *
0688  * @param library a pointer to the SymbolLibrary
0689  * @param index the index of the Symbol to delete
0690  */
0691 DeleteSymbolCommand::DeleteSymbolCommand(SymbolLibrary *library, qint16 index)
0692     :   QUndoCommand(i18n("Delete Symbol")),
0693         m_symbolLibrary(library),
0694         m_index(index)
0695 {
0696 }
0697 
0698 
0699 /**
0700  * Undo deleting the Symbol restoring it from the saved one.
0701  */
0702 void DeleteSymbolCommand::undo()
0703 {
0704     m_symbolLibrary->setSymbol(m_index, m_symbol);
0705 }
0706 
0707 
0708 /**
0709  * Redo deleting the Symbol. Store the removed Symbol for undo.
0710  */
0711 void DeleteSymbolCommand::redo()
0712 {
0713     m_symbol = m_symbolLibrary->takeSymbol(m_index);
0714 }
0715 
0716 
0717 /**
0718  * Constructor
0719  *
0720  * @param editor a pointer to the Editor
0721  * @param from the value of the original setting
0722  * @param to the value of the new setting
0723  */
0724 IncreaseLineWidthCommand::IncreaseLineWidthCommand(Editor *editor, qreal from, qreal to)
0725     :   QUndoCommand(i18n("Increase Line Width")),
0726         m_editor(editor),
0727         m_from(from),
0728         m_to(to)
0729 {
0730 }
0731 
0732 
0733 /**
0734  * Undo the line width increase.
0735  */
0736 void IncreaseLineWidthCommand::undo()
0737 {
0738     m_editor->setLineWidth(m_from);
0739 }
0740 
0741 
0742 /**
0743  * Redo the line width increase.
0744  */
0745 void IncreaseLineWidthCommand::redo()
0746 {
0747     m_editor->setLineWidth(m_to);
0748 }
0749 
0750 
0751 /**
0752  * Get the id related to this command.
0753  *
0754  * @return int representing the value from the IDs enum
0755  */
0756 int IncreaseLineWidthCommand::id() const
0757 {
0758     return static_cast<int>(IncreaseLineWidth);
0759 }
0760 
0761 
0762 /**
0763  * Merge this command with another IncreaseLineWidthCommand.
0764  * This allows repeated increases without additional commands added to the stack.
0765  *
0766  * @param command a pointer to the additional QUndoCommand
0767  *
0768  * @return @c true if the merge succeeded, @c false otherwise
0769  */
0770 bool IncreaseLineWidthCommand::mergeWith(const QUndoCommand *command)
0771 {
0772     if (command->id() != id()) {
0773         return false;
0774     }
0775 
0776     m_to += static_cast<const IncreaseLineWidthCommand *>(command)->m_to;
0777     return true;
0778 }
0779 
0780 
0781 /**
0782  * Constructor
0783  *
0784  * @param editor a pointer to the Editor
0785  * @param from the value of the original setting
0786  * @param to the value of the new setting
0787  */
0788 DecreaseLineWidthCommand::DecreaseLineWidthCommand(Editor *editor, qreal from, qreal to)
0789     :   QUndoCommand(i18n("Decrease Line Width")),
0790         m_editor(editor),
0791         m_from(from),
0792         m_to(to)
0793 {
0794 }
0795 
0796 
0797 /**
0798  * Undo the line width decrease.
0799  */
0800 void DecreaseLineWidthCommand::undo()
0801 {
0802     m_editor->setLineWidth(m_from);
0803 }
0804 
0805 
0806 /**
0807  * Redo the line width decrease.
0808  */
0809 void DecreaseLineWidthCommand::redo()
0810 {
0811     m_editor->setLineWidth(m_to);
0812 }
0813 
0814 
0815 /**
0816  * Get the id related to this command.
0817  *
0818  * @return int representing the value from the IDs enum
0819  */
0820 int DecreaseLineWidthCommand::id() const
0821 {
0822     return static_cast<int>(DecreaseLineWidth);
0823 }
0824 
0825 
0826 /**
0827  * Merge this command with another DecreaseLineWidthCommand.
0828  * This allows repeated decreases without additional commands added to the stack.
0829  *
0830  * @param command a pointer to the additional QUndoCommand
0831  *
0832  * @return @c true if the merge succeeded, @c false otherwise
0833  */
0834 bool DecreaseLineWidthCommand::mergeWith(const QUndoCommand *command)
0835 {
0836     if (command->id() != id()) {
0837         return false;
0838     }
0839 
0840     m_to -= static_cast<const DecreaseLineWidthCommand *>(command)->m_to;
0841     return true;
0842 }
0843 
0844 
0845 /**
0846  * Constructor.
0847  *
0848  * @param library pointer to the symbol library to add the new symbols to
0849  * @param mimeData pointer to the QMimeData containing the new symbols
0850  */
0851 DragAndDropCommand::DragAndDropCommand(SymbolLibrary *library, const QMimeData *mimeData)
0852     :   QUndoCommand(i18n("Add Symbols")),
0853         m_library(library)
0854 {
0855     QByteArray data = mimeData->data(QStringLiteral("application/kxstitchsymbol"));
0856     QDataStream stream(&data, QIODevice::ReadOnly);
0857 
0858     while (!stream.atEnd()) {
0859         Symbol symbol;
0860         stream >> symbol;
0861         m_symbols.append(symbol);
0862     }
0863 }
0864 
0865 
0866 /**
0867  * Redo the addition of symbols dragged to the list.
0868  */
0869 void DragAndDropCommand::redo()
0870 {
0871     foreach (const Symbol & symbol, m_symbols) {
0872         m_indexes.append(m_library->setSymbol(0, symbol));
0873     }
0874 }
0875 
0876 
0877 /**
0878  * Undo the addition of the symbols added.
0879  */
0880 void DragAndDropCommand::undo()
0881 {
0882     foreach (qint16 index, m_indexes) {
0883         m_library->takeSymbol(index);
0884     }
0885 
0886     m_indexes.clear();
0887 }
0888 
0889 
0890 /**
0891  * Constructor.
0892  *
0893  * @param editor a pointer to the editor
0894  * @param path the QPainterPath representing the character glyph
0895  */
0896 AddCharacterCommand::AddCharacterCommand(Editor *editor, const QPainterPath &path)
0897     :   QUndoCommand(i18n("Add Character")),
0898         m_editor(editor),
0899         m_path(path)
0900 {
0901 }
0902 
0903 
0904 void AddCharacterCommand::redo()
0905 {
0906     m_path = m_editor->setPath(m_path);
0907 }
0908 
0909 
0910 void AddCharacterCommand::undo()
0911 {
0912     m_path = m_editor->setPath(m_path);
0913 }