SymbolEditor  1.3.0
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
Commands.cpp
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2011 by Stephen Allewell *
3  * sallewell@users.sourceforge.net *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ********************************************************************************/
10 
11 
31 #include "Commands.h"
32 
33 #include <QPainterPath>
34 #include <QMimeData>
35 
36 #include <KLocale>
37 
38 #include "Editor.h"
39 #include "SymbolLibrary.h"
40 
41 
42 enum IDs {MoveTo,
62  };
63 
64 
71 MoveToCommand::MoveToCommand(Editor *editor, const QPointF &to)
72  : QUndoCommand(i18n("Move to")),
73  m_editor(editor),
74  m_to(to)
75 {
76 }
77 
78 
83 {
84 }
85 
86 
91 {
93 }
94 
95 
101 {
103 }
104 
105 
112 LineToCommand::LineToCommand(Editor *editor, const QPointF &to)
113  : QUndoCommand(i18n("Line to")),
114  m_editor(editor),
115  m_to(to)
116 {
117 }
118 
119 
124 {
125 }
126 
127 
132 {
134 }
135 
136 
142 {
144 }
145 
146 
155 CubicToCommand::CubicToCommand(Editor *editor, const QPointF &control1, const QPointF &control2, const QPointF &to)
156  : QUndoCommand(i18n("Cubic to")),
157  m_editor(editor),
158  m_control1(control1),
159  m_control2(control2),
160  m_to(to)
161 {
162 }
163 
164 
169 {
170 }
171 
172 
177 {
179 }
180 
181 
187 {
189 }
190 
191 
199 RectangleCommand::RectangleCommand(Editor *editor, const QPointF &from, const QPointF &to)
200  : QUndoCommand(i18n("Add Rectangle")),
201  m_editor(editor),
202  m_from(from),
203  m_to(to)
204 {
205 }
206 
207 
212 {
213 }
214 
215 
220 {
222 }
223 
224 
230 {
232 }
233 
234 
242 EllipseCommand::EllipseCommand(Editor *editor, const QPointF &from, const QPointF &to)
243  : QUndoCommand(i18n("Add Ellipse")),
244  m_editor(editor),
245  m_from(from),
246  m_to(to)
247 {
248 }
249 
250 
255 {
256 }
257 
258 
263 {
265 }
266 
267 
273 {
275 }
276 
277 
286 MovePointCommand::MovePointCommand(Editor *editor, int index, const QPointF &from, const QPointF &to)
287  : QUndoCommand(i18n("Move point")),
288  m_editor(editor),
289  m_index(index),
290  m_from(from),
291  m_to(to)
292 {
293 }
294 
295 
300 {
301 }
302 
303 
309 {
311 }
312 
313 
319 {
321 }
322 
323 
331 UpdateSymbolCommand::UpdateSymbolCommand(SymbolLibrary *library, qint16 index, const Symbol &symbol)
332  : QUndoCommand(i18n("Update Symbol")),
333  m_symbolLibrary(library),
334  m_index(index),
335  m_symbol(symbol)
336 {
337 }
338 
339 
344 {
345 }
346 
347 
353 {
354  if (m_originalSymbol.path().isEmpty()) {
356  m_index = 0;
357  } else {
359  }
360 }
361 
362 
369 {
372 }
373 
374 
382  : QUndoCommand(i18n("Import Library")),
383  m_symbolLibrary(library),
384  m_imported(imported)
385 {
386 }
387 
388 
394 {
395  delete m_imported;
396 }
397 
398 
404 {
405  foreach (qint16 i, m_addedIndexes) {
407  }
408 
409  m_addedIndexes.clear();
410 }
411 
412 
418 {
419  foreach (qint16 i, m_imported->indexes()) {
420  m_addedIndexes.append(m_symbolLibrary->setSymbol(0, m_imported->symbol(i))); // gets a new index
421  }
422 }
423 
424 
431  : QUndoCommand(i18n("Rotate Left")),
432  m_editor(editor)
433 {
434 }
435 
436 
441 {
442 }
443 
444 
450 {
452 }
453 
454 
459 {
461 }
462 
463 
470  : QUndoCommand(i18n("Rotate Right")),
471  m_editor(editor)
472 {
473 }
474 
475 
480 {
481 }
482 
483 
489 {
491 }
492 
493 
498 {
500 }
501 
502 
509  : QUndoCommand(i18n("Flip Horizontally")),
510  m_editor(editor)
511 {
512 }
513 
514 
519 {
520 }
521 
522 
527 {
529 }
530 
531 
536 {
538 }
539 
540 
547  : QUndoCommand(i18n("Flip Vertical")),
548  m_editor(editor)
549 {
550 }
551 
552 
557 {
558 }
559 
560 
565 {
567 }
568 
569 
574 {
576 }
577 
578 
587 ScalePreferredCommand::ScalePreferredCommand(Editor *editor, const QPainterPath &originalSymbol, int gridElements, int borderSize)
588  : QUndoCommand(i18n("Scale to preferred size")),
589  m_editor(editor),
590  m_originalSymbol(originalSymbol),
591  m_gridElements(gridElements),
592  m_borderSize(borderSize)
593 {
594 }
595 
596 
601 {
602 }
603 
604 
609 {
611 }
612 
613 
618 {
619  double borderSize = double(m_borderSize) / double(m_gridElements);
620  double threshold = 0.01;
621  QRectF fullSize(0.0, 0.0, 1.0, 1.0);
622  QRectF preferredSize(borderSize, borderSize, 1.0 - borderSize - borderSize, 1.0 - borderSize - borderSize);
623 
624  QPainterPath border;
625  border.addRect(fullSize);
626  border.addRect(preferredSize.adjusted(-threshold, -threshold, threshold, threshold));
627  border.setFillRule(Qt::OddEvenFill);
628 
629  if (m_originalSymbol.intersects(border)) {
630  QRectF boundingRect = m_originalSymbol.boundingRect();
631  double leftOverlap = std::max(preferredSize.left() - boundingRect.left(), 0.0);
632  double topOverlap = std::max(preferredSize.top() - boundingRect.top(), 0.0);
633  double rightOverlap = std::max(boundingRect.right() - preferredSize.right(), 0.0);
634  double bottomOverlap = std::max(boundingRect.bottom() - preferredSize.bottom(), 0.0);
635  double overlap = std::max(std::max(leftOverlap, rightOverlap), std::max(topOverlap, bottomOverlap));
636  QRectF startingSize = preferredSize.adjusted(-overlap, -overlap, overlap, overlap);
637  double scale = preferredSize.width() / startingSize.width();
638  QTransform transform = QTransform::fromTranslate(-0.5, -0.5) * QTransform::fromScale(scale, scale) * QTransform::fromTranslate(0.5, 0.5);
639  QPainterPath scaledPath = transform.map(m_originalSymbol);
640  m_editor->setPath(scaledPath);
641  }
642 }
643 
644 
653  : QUndoCommand(i18n("Change Fill State")),
654  m_editor(editor),
655  m_from(from),
656  m_to(to)
657 {
658 }
659 
660 
665 {
666 }
667 
668 
673 {
675 }
676 
677 
682 {
684 }
685 
686 
694 ChangeFillRuleCommand::ChangeFillRuleCommand(Editor *editor, Qt::FillRule from, Qt::FillRule to)
695  : QUndoCommand(i18n("Change Fill Rule")),
696  m_editor(editor),
697  m_from(from),
698  m_to(to)
699 {
700 }
701 
702 
707 {
708 }
709 
710 
715 {
717 }
718 
719 
724 {
726 }
727 
728 
736 ChangeCapStyleCommand::ChangeCapStyleCommand(Editor *editor, Qt::PenCapStyle from, Qt::PenCapStyle to)
737  : QUndoCommand(i18n("Change Cap Style")),
738  m_editor(editor),
739  m_from(from),
740  m_to(to)
741 {
742 }
743 
744 
749 {
750 }
751 
752 
757 {
759 }
760 
761 
766 {
768 }
769 
770 
778 ChangeJoinStyleCommand::ChangeJoinStyleCommand(Editor *editor, Qt::PenJoinStyle from, Qt::PenJoinStyle to)
779  : QUndoCommand(i18n("Change Join Style")),
780  m_editor(editor),
781  m_from(from),
782  m_to(to)
783 {
784 }
785 
786 
791 {
792 }
793 
794 
799 {
801 }
802 
803 
808 {
810 }
811 
812 
820  : QUndoCommand(i18n("Delete Symbol")),
821  m_symbolLibrary(library),
822  m_index(index)
823 {
824 }
825 
826 
831 {
832 }
833 
834 
839 {
841 }
842 
843 
848 {
850 }
851 
852 
861  : QUndoCommand(i18n("Increase Line Width")),
862  m_editor(editor),
863  m_from(from),
864  m_to(to)
865 {
866 }
867 
868 
873 {
874 }
875 
876 
881 {
883 }
884 
885 
890 {
892 }
893 
894 
901 {
902  return static_cast<int>(IncreaseLineWidth);
903 }
904 
905 
914 bool IncreaseLineWidthCommand::mergeWith(const QUndoCommand *command)
915 {
916  if (command->id() != id()) {
917  return false;
918  }
919 
920  m_to += static_cast<const IncreaseLineWidthCommand *>(command)->m_to;
921  return true;
922 }
923 
924 
933  : QUndoCommand(i18n("Decrease Line Width")),
934  m_editor(editor),
935  m_from(from),
936  m_to(to)
937 {
938 }
939 
940 
945 {
946 }
947 
948 
953 {
955 }
956 
957 
962 {
964 }
965 
966 
973 {
974  return static_cast<int>(DecreaseLineWidth);
975 }
976 
977 
986 bool DecreaseLineWidthCommand::mergeWith(const QUndoCommand *command)
987 {
988  if (command->id() != id()) {
989  return false;
990  }
991 
992  m_to -= static_cast<const DecreaseLineWidthCommand *>(command)->m_to;
993  return true;
994 }
995 
996 
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 
1022 {
1023  foreach (const Symbol & symbol, m_symbols) {
1024  m_indexes.append(m_library->setSymbol(0, symbol));
1025  }
1026 }
1027 
1028 
1033 {
1034  foreach (qint16 index, m_indexes) {
1035  m_library->takeSymbol(index);
1036  }
1037 
1038  m_indexes.clear();
1039 }
1040 
1041 
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 
1057 {
1059 }
1060 
1061 
1063 {
1065 }