File indexing completed on 2025-02-02 04:15:00

0001 /*
0002  *  SPDX-FileCopyrightText: 2006-2010 Thomas Zander <zander@kde.org>
0003  *  SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev@xibo.at>
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "TestShapeContainer.h"
0007 #include <MockShapes.h>
0008 
0009 #include <KoShape.h>
0010 #include <KoShapeGroupCommand.h>
0011 #include <KoShapeUngroupCommand.h>
0012 #include <KoShapeTransformCommand.h>
0013 #include <KoShapeGroup.h>
0014 #include <KoSelection.h>
0015 
0016 #include <simpletest.h>
0017 
0018 
0019 void TestShapeContainer::testModel()
0020 {
0021     MockContainerModel *model = new MockContainerModel();
0022     MockContainer container(model);
0023     MockShape *shape1 = new MockShape();
0024 
0025     container.addShape(shape1);
0026     QCOMPARE(model->containerChangedCalled(), 0);
0027     QCOMPARE(model->childChangedCalled(), 1);
0028     QCOMPARE(model->proposeMoveCalled(), 0);
0029 
0030     shape1->setPosition(QPointF(300, 300));
0031     QCOMPARE(model->containerChangedCalled(), 0);
0032     QCOMPARE(model->childChangedCalled(), 2);
0033     QCOMPARE(model->proposeMoveCalled(), 0);
0034 
0035     shape1->rotate(10);
0036     QCOMPARE(model->containerChangedCalled(), 0);
0037     QCOMPARE(model->childChangedCalled(), 3);
0038     QCOMPARE(model->proposeMoveCalled(), 0);
0039 
0040     shape1->setAbsolutePosition(shape1->absolutePosition() + QPointF(10., 40.));
0041     QCOMPARE(model->containerChangedCalled(), 0);
0042     QCOMPARE(model->childChangedCalled(), 5); // we get a generic Matrix as well as a position change...
0043     QCOMPARE(model->proposeMoveCalled(), 0);
0044 
0045     shape1->setTransformation(QTransform());
0046     QCOMPARE(model->containerChangedCalled(), 0);
0047     QCOMPARE(model->childChangedCalled(), 6);
0048     QCOMPARE(model->proposeMoveCalled(), 0);
0049 
0050     model->resetCounts();
0051     container.setPosition(QPointF(30, 30));
0052     QCOMPARE(model->containerChangedCalled(), 1);
0053     QCOMPARE(model->childChangedCalled(), 0);
0054     QCOMPARE(model->proposeMoveCalled(), 0);
0055 }
0056 
0057 void TestShapeContainer::testSetParent()
0058 {
0059     MockContainerModel *model1 = new MockContainerModel();
0060     MockContainer container1(model1);
0061     MockContainerModel *model2 = new MockContainerModel();
0062     MockContainer container2(model2);
0063     MockShape shape;
0064     // init test
0065     QCOMPARE(model1->shapes().count(), 0);
0066     QCOMPARE(model2->shapes().count(), 0);
0067 
0068     shape.setParent(&container1);
0069     QCOMPARE(model1->shapes().count(), 1);
0070     QCOMPARE(model2->shapes().count(), 0);
0071     QCOMPARE(shape.parent(), &container1);
0072 
0073     shape.setParent(&container2);
0074     QCOMPARE(model1->shapes().count(), 0);
0075     QCOMPARE(container1.shapes().count(), 0);
0076     QCOMPARE(model2->shapes().count(), 1);
0077     QCOMPARE(container2.shapes().count(), 1);
0078     QCOMPARE(shape.parent(), &container2);
0079 
0080     // on destruction shape should have no parent
0081     shape.setParent(0);
0082 }
0083 
0084 void TestShapeContainer::testSetParent2()
0085 {
0086     MockContainerModel *model = new MockContainerModel();
0087     MockContainer container(model);
0088     MockShape *shape = new MockShape();
0089     shape->setParent(&container);
0090     QCOMPARE(model->shapes().count(), 1);
0091 
0092     shape->setParent(0);
0093     QCOMPARE(model->shapes().count(), 0);
0094 }
0095 
0096 void TestShapeContainer::testScaling()
0097 {
0098     KoShape *shape1 = new MockShape();
0099     KoShape *shape2 = new MockShape();
0100 
0101     shape1->setSize(QSizeF(10., 10.));
0102     shape1->setPosition(QPointF(20., 20.));
0103 
0104     shape2->setSize(QSizeF(30., 10.));
0105     shape2->setPosition(QPointF(10., 40.));
0106 
0107     QList<KoShape*> groupedShapes;
0108     groupedShapes.append(shape1);
0109     groupedShapes.append(shape2);
0110 
0111     KoShapeGroup *group = new KoShapeGroup();
0112     KoShapeGroupCommand* groupCommand = KoShapeGroupCommand::createCommand(group, groupedShapes);
0113     groupCommand->redo();
0114 
0115     QList<KoShape*> transformShapes;
0116     transformShapes.append(groupedShapes);
0117 
0118     QTransform matrix;
0119     matrix.scale(0.5, 0.5);
0120 
0121     QList<QTransform> oldTransformations;
0122     QList<QTransform> newTransformations;
0123     Q_FOREACH (const KoShape* shape, transformShapes) {
0124         QTransform oldTransform = shape->transformation();
0125         oldTransformations.append(oldTransform);
0126         QTransform globalTransform = shape->absoluteTransformation();
0127         QTransform localTransform = globalTransform * matrix * globalTransform.inverted();
0128         newTransformations.append(localTransform*oldTransform);
0129     }
0130 
0131     QList<QPointF> oldPositions;
0132     for(int i=0; i< transformShapes.size(); i++) {
0133         oldPositions.append(transformShapes.at(i)->absolutePosition(KoFlake::TopLeft));
0134     }
0135 
0136     KoShapeTransformCommand* transformCommand;
0137     transformCommand = new KoShapeTransformCommand(transformShapes, oldTransformations, newTransformations);
0138     transformCommand->redo();
0139 
0140     for(int i=0; i< transformShapes.size(); i++) {
0141         QCOMPARE(transformShapes.at(i)->absolutePosition(KoFlake::TopLeft), oldPositions.at(i)*0.5);
0142     }
0143 
0144     transformShapes.takeLast();
0145     KoShapeUngroupCommand* ungroupCmd = new KoShapeUngroupCommand(group, transformShapes);
0146     ungroupCmd->redo();
0147 
0148     for(int i=0; i< transformShapes.size(); i++) {
0149         QCOMPARE(transformShapes.at(i)->absolutePosition(KoFlake::TopLeft), oldPositions.at(i)*0.5);
0150     }
0151 }
0152 
0153 void TestShapeContainer::testScaling2()
0154 {
0155     KoShape *shape1 = new MockShape();
0156     KoShape *shape2 = new MockShape();
0157 
0158     shape1->setPosition(QPointF(20., 20.));
0159     shape1->setSize(QSizeF(10., 10.));
0160 
0161     shape2->setPosition(QPointF(10., 40.));
0162     shape2->setSize(QSizeF(30., 10.));
0163 
0164     QList<KoShape*> groupedShapes;
0165     groupedShapes.append(shape1);
0166     groupedShapes.append(shape2);
0167 
0168     QScopedPointer<KoShapeGroup> group(new KoShapeGroup());
0169     QScopedPointer<KoShapeGroupCommand> groupCommand(
0170         KoShapeGroupCommand::createCommand(group.data(), groupedShapes));
0171     groupCommand->redo();
0172 
0173     QScopedPointer<KoSelection> selection(new KoSelection());
0174 
0175     // the topmost shape is selected, not shape1!
0176     selection->select(shape1);
0177 
0178     QList<KoShape*> transformShapes;
0179     transformShapes.append(selection->selectedShapes());
0180 
0181     QTransform matrix;
0182     matrix.scale(0.5, 0.5);
0183 
0184     QList<QTransform> oldTransformations;
0185     QList<QTransform> newTransformations;
0186     Q_FOREACH (const KoShape* shape, transformShapes) {
0187         QTransform oldTransform = shape->transformation();
0188         oldTransformations.append(oldTransform);
0189         newTransformations.append(oldTransform*matrix);
0190     }
0191 
0192     QList<QPointF> oldPositions;
0193     for(int i=0; i< transformShapes.size(); i++) {
0194         oldPositions.append(transformShapes.at(i)->absolutePosition(KoFlake::TopLeft));
0195     }
0196 
0197     QScopedPointer<KoShapeTransformCommand> transformCommand(
0198         new KoShapeTransformCommand(transformShapes, oldTransformations, newTransformations));
0199     transformCommand->redo();
0200 
0201     QCOMPARE(selection->boundingRect(), group->boundingRect());
0202 
0203     selection->deselectAll();
0204 
0205     // the topmost shape is selected, not shape1!
0206     selection->select(shape1);
0207     QCOMPARE(selection->boundingRect(), group->boundingRect());
0208 }
0209 
0210 SIMPLE_TEST_MAIN(TestShapeContainer)