Krita Source Code Documentation
Loading...
Searching...
No Matches
KoShapeGroupCommand Class Reference

The undo / redo command for grouping shapes. More...

#include <KoShapeGroupCommand.h>

+ Inheritance diagram for KoShapeGroupCommand:

Public Member Functions

 KoShapeGroupCommand (KoShapeContainer *container, const QList< KoShape * > &shapes, bool shouldNormalize, KUndo2Command *parent=0)
 
 KoShapeGroupCommand (KoShapeContainer *container, const QList< KoShape * > &shapes, KUndo2Command *parent=0)
 
void redo () override
 redo the command
 
void undo () override
 revert the actions done in redo
 
 ~KoShapeGroupCommand () override
 
- Public Member Functions inherited from KUndo2Command
QString actionText () const
 
virtual bool canAnnihilateWith (const KUndo2Command *other) const
 
const KUndo2Commandchild (int index) const
 
int childCount () const
 
virtual QTime endTime () const
 
KUndo2CommandExtraDataextraData () const
 
bool hasParent () const
 
virtual int id () const
 
virtual bool isMerged () const
 
 KUndo2Command (const KUndo2MagicString &text, KUndo2Command *parent=0)
 
 KUndo2Command (KUndo2Command *parent=0)
 
virtual QVector< KUndo2Command * > mergeCommandsVector () const
 
virtual bool mergeWith (const KUndo2Command *other)
 
virtual void redoMergedCommands ()
 
void setEndTime ()
 
virtual void setEndTime (const QTime &time)
 
void setExtraData (KUndo2CommandExtraData *data)
 
void setText (const KUndo2MagicString &text)
 
void setTime ()
 
virtual void setTime (const QTime &time)
 
virtual void setTimedID (int timedID)
 
KUndo2MagicString text () const
 
virtual QTime time () const
 
virtual int timedId () const
 
virtual bool timedMergeWith (KUndo2Command *other)
 
virtual void undoMergedCommands ()
 
virtual ~KUndo2Command ()
 

Static Public Member Functions

static KoShapeGroupCommandcreateCommand (KoShapeContainer *container, const QList< KoShape * > &shapes, bool shouldNormalize=false)
 

Protected Attributes

const QScopedPointer< KoShapeGroupCommandPrivated
 

Detailed Description

The undo / redo command for grouping shapes.

Definition at line 23 of file KoShapeGroupCommand.h.

Constructor & Destructor Documentation

◆ KoShapeGroupCommand() [1/2]

KoShapeGroupCommand::KoShapeGroupCommand ( KoShapeContainer * container,
const QList< KoShape * > & shapes,
bool shouldNormalize,
KUndo2Command * parent = 0 )

Command to group a set of shapes into a predefined container.

Parameters
containerthe container to group the shapes under.
shapesa list of all the shapes that should be grouped.
shouldNormalizeshows whether the shapes should be normalized by the container
parentthe parent command used for macro commands

Definition at line 57 of file KoShapeGroupCommand.cpp.

59 : KUndo2Command(parent),
60 d(new KoShapeGroupCommandPrivate(container, shapes, shouldNormalize))
61{
62 Q_FOREACH (KoShape* shape, d->shapes) {
63 d->oldParents.append(shape->parent());
64 }
65
66 if (d->container->shapes().isEmpty()) {
67 setText(kundo2_i18n("Group shapes"));
68 } else {
69 setText(kundo2_i18n("Add shapes to group"));
70 }
71}
void setText(const KUndo2MagicString &text)
KUndo2Command(KUndo2Command *parent=0)
const QScopedPointer< KoShapeGroupCommandPrivate > d
KoShapeContainer * parent() const
Definition KoShape.cpp:1039
KUndo2MagicString kundo2_i18n(const char *text)

References d, kundo2_i18n(), KoShape::parent(), and KUndo2Command::setText().

◆ KoShapeGroupCommand() [2/2]

KoShapeGroupCommand::KoShapeGroupCommand ( KoShapeContainer * container,
const QList< KoShape * > & shapes,
KUndo2Command * parent = 0 )

Command to group a set of shapes into a predefined container. Convenience constructor since KoShapeGroup does not allow clipping.

Parameters
containerthe group to group the shapes under.
parentthe parent command if the resulting command is a compound undo command.
shapesa list of all the shapes that should be grouped.

Definition at line 52 of file KoShapeGroupCommand.cpp.

53 : KoShapeGroupCommand(container, shapes, false, parent)
54{
55}
KoShapeGroupCommand(KoShapeContainer *container, const QList< KoShape * > &shapes, bool shouldNormalize, KUndo2Command *parent=0)

◆ ~KoShapeGroupCommand()

KoShapeGroupCommand::~KoShapeGroupCommand ( )
override

Definition at line 73 of file KoShapeGroupCommand.cpp.

74{
75}

Member Function Documentation

◆ createCommand()

KoShapeGroupCommand * KoShapeGroupCommand::createCommand ( KoShapeContainer * container,
const QList< KoShape * > & shapes,
bool shouldNormalize = false )
static

Create command to group a set of shapes into a predefined container. This uses the KoShapeGroupCommand(KoShapeGroup *container, const QList<KoShape *> &shapes, KUndo2Command *parent = 0); constructor. The createCommand will make sure that the group will have the z-index and the parent of the top most shape in the group.

Parameters
containerthe group to group the shapes under.
shapesa list of all the shapes that should be grouped.
shouldNormalizewhether the shapes should be normalized

Definition at line 18 of file KoShapeGroupCommand.cpp.

19{
20 QList<KoShape*> orderedShapes(shapes);
21 if (!orderedShapes.isEmpty()) {
22 KoShape * top = orderedShapes.last();
23 container->setParent(top->parent());
24 container->setZIndex(top->zIndex());
25 }
26
27 return new KoShapeGroupCommand(container, orderedShapes, shouldNormalize, 0);
28}
void setZIndex(qint16 zIndex)
Definition KoShape.cpp:954
void setParent(KoShapeContainer *parent)
Definition KoShape.cpp:535
qint16 zIndex() const
Definition KoShape.cpp:600

References KoShapeGroupCommand(), KoShape::parent(), KoShape::setParent(), KoShape::setZIndex(), and KoShape::zIndex().

◆ redo()

void KoShapeGroupCommand::redo ( )
overridevirtual

redo the command

Reimplemented from KUndo2Command.

Definition at line 77 of file KoShapeGroupCommand.cpp.

78{
80
81 if (d->shouldNormalize && dynamic_cast<KoShapeGroup*>(d->container)) {
82 QRectF bound = d->containerBoundingRect();
83 QPointF oldGroupPosition = d->container->absolutePosition(KoFlake::TopLeft);
84 d->container->setAbsolutePosition(bound.topLeft(), KoFlake::TopLeft);
85 d->container->setSize(bound.size());
86
87 if (d->container->shapeCount() > 0) {
88 // the group has changed position and so have the group child shapes
89 // -> we need compensate the group position change
90 QPointF positionOffset = oldGroupPosition - bound.topLeft();
91 Q_FOREACH (KoShape * child, d->container->shapes())
92 child->setAbsolutePosition(child->absolutePosition() + positionOffset);
93 }
94 }
95
96 QTransform groupTransform = d->container->absoluteTransformation().inverted();
97
98 QList<KoShape*> containerShapes(d->container->shapes());
99 std::stable_sort(containerShapes.begin(), containerShapes.end(), KoShape::compareShapeZIndex);
100
101 QList<KoShapeReorderCommand::IndexedShape> indexedShapes;
102 Q_FOREACH (KoShape *shape, containerShapes) {
103 indexedShapes.append(KoShapeReorderCommand::IndexedShape(shape));
104 }
105
107
108 Q_FOREACH (KoShape *shape, d->shapes) {
109 // test if they inherit the same parent
110
111 if (!shape->hasCommonParent(d->container) ||
112 !KoShape::compareShapeZIndex(shape, d->container)) {
113
114 indexedShapes.append(KoShapeReorderCommand::IndexedShape(shape));
115 } else {
116 prependIndexedShapes.append(KoShapeReorderCommand::IndexedShape(shape));
117 }
118 }
119
120 indexedShapes = prependIndexedShapes + indexedShapes;
121 indexedShapes = KoShapeReorderCommand::homogenizeZIndexesLazy(indexedShapes);
122
123 if (!indexedShapes.isEmpty()) {
124 d->shapesReorderCommand.reset(new KoShapeReorderCommand(indexedShapes));
125 d->shapesReorderCommand->redo();
126 }
127
128 uint shapeCount = d->shapes.count();
129 for (uint i = 0; i < shapeCount; ++i) {
130 KoShape * shape = d->shapes[i];
131
132 shape->applyAbsoluteTransformation(groupTransform);
133 d->container->addShape(shape);
134 }
135
136
137}
unsigned int uint
const KUndo2Command * child(int index) const
virtual void redo()
This command allows you to change the zIndex of a number of shapes.
static QList< KoShapeReorderCommand::IndexedShape > homogenizeZIndexesLazy(QList< IndexedShape > shapes)
bool hasCommonParent(const KoShape *shape) const
Definition KoShape.cpp:581
void applyAbsoluteTransformation(const QTransform &matrix)
Definition KoShape.cpp:400
static bool compareShapeZIndex(KoShape *s1, KoShape *s2)
Definition KoShape.cpp:434
@ TopLeft
Definition KoFlake.h:86

References KoShape::applyAbsoluteTransformation(), KUndo2Command::child(), KoShape::compareShapeZIndex(), d, KoShape::hasCommonParent(), KoShapeReorderCommand::homogenizeZIndexesLazy(), KUndo2Command::redo(), and KoFlake::TopLeft.

◆ undo()

void KoShapeGroupCommand::undo ( )
overridevirtual

revert the actions done in redo

Reimplemented from KUndo2Command.

Definition at line 139 of file KoShapeGroupCommand.cpp.

140{
142
143 QTransform ungroupTransform = d->container->absoluteTransformation();
144 for (int i = 0; i < d->shapes.count(); i++) {
145 KoShape * shape = d->shapes[i];
146 d->container->removeShape(shape);
147 if (d->oldParents.at(i)) {
148 d->oldParents.at(i)->addShape(shape);
149 }
150 shape->applyAbsoluteTransformation(ungroupTransform);
151 }
152
153 if (d->shapesReorderCommand) {
154 d->shapesReorderCommand->undo();
155 d->shapesReorderCommand.reset();
156 }
157
158 if (d->shouldNormalize && dynamic_cast<KoShapeGroup*>(d->container)) {
159 QPointF oldGroupPosition = d->container->absolutePosition(KoFlake::TopLeft);
160 if (d->container->shapeCount() > 0) {
161 bool boundingRectInitialized = false;
162 QRectF bound;
163 Q_FOREACH (KoShape * shape, d->container->shapes()) {
164 if (! boundingRectInitialized) {
165 bound = shape->boundingRect();
166 boundingRectInitialized = true;
167 } else
168 bound = bound.united(shape->boundingRect());
169 }
170 // the group has changed position and so have the group child shapes
171 // -> we need compensate the group position change
172 QPointF positionOffset = oldGroupPosition - bound.topLeft();
173 Q_FOREACH (KoShape * child, d->container->shapes())
174 child->setAbsolutePosition(child->absolutePosition() + positionOffset);
175
176 d->container->setAbsolutePosition(bound.topLeft(), KoFlake::TopLeft);
177 d->container->setSize(bound.size());
178 }
179 }
180}
virtual void undo()
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:335

References KoShape::applyAbsoluteTransformation(), KoShape::boundingRect(), KUndo2Command::child(), d, KoFlake::TopLeft, and KUndo2Command::undo().

Member Data Documentation

◆ d

const QScopedPointer<KoShapeGroupCommandPrivate> KoShapeGroupCommand::d
protected

Definition at line 64 of file KoShapeGroupCommand.h.


The documentation for this class was generated from the following files: