void KoPathSegmentTypeCommand::redo() { KUndo2Command::redo(); QList<KoPathPointData>::const_iterator it(m_pointDataList.constBegin()); for (; it != m_pointDataList.constEnd(); ++it) { KoPathShape * pathShape = it->pathShape; pathShape->update(); KoPathSegment segment = pathShape->segmentByIndex(it->pointIndex); if (m_segmentType == Curve) { // we change type to curve -> set control point positions QPointF pointDiff = segment.second()->point() - segment.first()->point(); segment.first()->setControlPoint2(segment.first()->point() + pointDiff / 3.0); segment.second()->setControlPoint1(segment.first()->point() + pointDiff * 2.0 / 3.0); } else { // we are changing type to line -> remove control points segment.first()->removeControlPoint2(); segment.second()->removeControlPoint1(); } pathShape->normalize(); pathShape->update(); } }
KoShape *KoPathShapeFactory::createDefaultShape(KoDocumentResourceManager *) const { KoPathShape* path = new KoPathShape(); path->moveTo(QPointF(0, 50)); path->curveTo(QPointF(0, 120), QPointF(50, 120), QPointF(50, 50)); path->curveTo(QPointF(50, -20), QPointF(100, -20), QPointF(100, 50)); path->normalize(); path->setStroke(new KoShapeStroke(1.0)); return path; }
void KoPathControlPointMoveCommand::redo() { KUndo2Command::redo(); KoPathShape * pathShape = m_pointData.pathShape; KoPathPoint * point = pathShape->pointByIndex(m_pointData.pointIndex); if (point) { pathShape->update(); if (m_pointType == KoPathPoint::ControlPoint1) { point->setControlPoint1(point->controlPoint1() + m_offset); if (point->properties() & KoPathPoint::IsSymmetric) { // set the other control point so that it lies on the line between the moved // control point and the point, with the same distance to the point as the moved point point->setControlPoint2(2.0 * point->point() - point->controlPoint1()); } else if (point->properties() & KoPathPoint::IsSmooth) { // move the other control point so that it lies on the line through point and control point // keeping its distance to the point QPointF direction = point->point() - point->controlPoint1(); direction /= sqrt(direction.x() * direction.x() + direction.y() * direction.y()); QPointF distance = point->point() - point->controlPoint2(); qreal length = sqrt(distance.x() * distance.x() + distance.y() * distance.y()); point->setControlPoint2(point->point() + length * direction); } } else if (m_pointType == KoPathPoint::ControlPoint2) { point->setControlPoint2(point->controlPoint2() + m_offset); if (point->properties() & KoPathPoint::IsSymmetric) { // set the other control point so that it lies on the line between the moved // control point and the point, with the same distance to the point as the moved point point->setControlPoint1(2.0 * point->point() - point->controlPoint2()); } else if (point->properties() & KoPathPoint::IsSmooth) { // move the other control point so that it lies on the line through point and control point // keeping its distance to the point QPointF direction = point->point() - point->controlPoint2(); direction /= sqrt(direction.x() * direction.x() + direction.y() * direction.y()); QPointF distance = point->point() - point->controlPoint1(); qreal length = sqrt(distance.x() * distance.x() + distance.y() * distance.y()); point->setControlPoint1(point->point() + length * direction); } } pathShape->normalize(); pathShape->update(); } }
void KoSubpathJoinCommand::redo() { KUndo2Command::redo(); KoPathShape * pathShape = m_pointData1.pathShape; bool closeSubpath = m_pointData1.pointIndex.first == m_pointData2.pointIndex.first; KoPathPoint * point1 = pathShape->pointByIndex(m_pointData1.pointIndex); KoPathPoint * point2 = pathShape->pointByIndex(m_pointData2.pointIndex); // if the endpoint is has a control point create a contol point for the new segment to be // at the symetric position to the exiting one if (m_reverse & ReverseFirst || closeSubpath) { if (point1->activeControlPoint2()) point1->setControlPoint1(2.0 * point1->point() - point1->controlPoint2()); } else if (point1->activeControlPoint1()) point1->setControlPoint2(2.0 * point1->point() - point1->controlPoint1()); if (m_reverse & ReverseSecond || closeSubpath) { if (point2->activeControlPoint1()) point2->setControlPoint2(2.0 * point2->point() - point2->controlPoint1()); } else if (point2->activeControlPoint2()) point2->setControlPoint1(2.0 * point2->point() - point2->controlPoint2()); if (closeSubpath) { pathShape->closeSubpath(m_pointData1.pointIndex); } else { if (m_reverse & ReverseFirst) { pathShape->reverseSubpath(m_pointData1.pointIndex.first); } if (m_reverse & ReverseSecond) { pathShape->reverseSubpath(m_pointData2.pointIndex.first); } pathShape->moveSubpath(m_pointData2.pointIndex.first, m_pointData1.pointIndex.first + 1); m_splitIndex = m_pointData1.pointIndex; m_splitIndex.second = pathShape->subpathPointCount(m_pointData1.pointIndex.first) - 1; pathShape->join(m_pointData1.pointIndex.first); } pathShape->normalize(); pathShape->update(); }
void KisShapeSelectionTest::testAddChild() { const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8(); KisImageSP image = new KisImage(new KisUndoAdapter(0), 300, 300, cs, "test"); KisSelectionSP selection = new KisSelection(); QVERIFY(selection->hasPixelSelection() == false); QVERIFY(selection->hasShapeSelection() == false); KisPixelSelectionSP pixelSelection = selection->getOrCreatePixelSelection(); pixelSelection->select(QRect(0, 0, 100, 100)); // Selection is using the pixel selection as datamanager so no projection update // needed QCOMPARE(pixelSelection->selected(25, 25), MAX_SELECTED); QCOMPARE(selection->selectedExactRect(), QRect(0, 0, 100, 100)); QRect rect(50, 50, 100, 100); QTransform matrix; matrix.scale(1 / image->xRes(), 1 / image->yRes()); rect = matrix.mapRect(rect); KoPathShape* shape = new KoPathShape(); shape->setShapeId(KoPathShapeId); shape->moveTo(rect.topLeft()); shape->lineTo(rect.topLeft() + QPointF(rect.width(), 0)); shape->lineTo(rect.bottomRight()); shape->lineTo(rect.topLeft() + QPointF(0, rect.height())); shape->close(); shape->normalize(); KisShapeSelection * shapeSelection = new KisShapeSelection(image, selection); selection->setShapeSelection(shapeSelection); shapeSelection->addShape(shape); QCOMPARE(pixelSelection->selected(25, 25), MAX_SELECTED); QCOMPARE(selection->selectedExactRect(), QRect(0, 0, 150, 150)); selection->updateProjection(); }
void KoSubpathJoinCommand::undo() { KUndo2Command::undo(); KoPathShape * pathShape = m_pointData1.pathShape; pathShape->update(); if (m_pointData1.pointIndex.first == m_pointData2.pointIndex.first) { pathShape->openSubpath(m_pointData1.pointIndex); } else { pathShape->breakAfter(m_splitIndex); pathShape->moveSubpath(m_pointData1.pointIndex.first + 1, m_pointData2.pointIndex.first); if (m_reverse & ReverseSecond) { pathShape->reverseSubpath(m_pointData2.pointIndex.first); } if (m_reverse & ReverseFirst) { pathShape->reverseSubpath(m_pointData1.pointIndex.first); } } KoPathPoint * point1 = pathShape->pointByIndex(m_pointData1.pointIndex); KoPathPoint * point2 = pathShape->pointByIndex(m_pointData2.pointIndex); // restore the old end points if (m_reverse & ReverseFirst) point1->setControlPoint1(pathShape->documentToShape(m_oldControlPoint1)); else point1->setControlPoint2(pathShape->documentToShape(m_oldControlPoint1)); point1->setProperties(m_oldProperties1); if (m_reverse & ReverseSecond) point2->setControlPoint1(pathShape->documentToShape(m_oldControlPoint2)); else point2->setControlPoint2(pathShape->documentToShape(m_oldControlPoint2)); point2->setProperties(m_oldProperties2); pathShape->normalize(); pathShape->update(); }
KoShape* KisShapeToolHelper::createEllipseShape(const QRectF& rect) { KoShape* shape; KoShapeFactoryBase *rectFactory = KoShapeRegistry::instance()->value("EllipseShape"); if (rectFactory) { shape = rectFactory->createDefaultShape(); shape->setSize(rect.size()); shape->setPosition(rect.topLeft()); } else { //Fallback if the plugin wasn't found KoPathShape* path = new KoPathShape(); path->setShapeId(KoPathShapeId); QPointF rightMiddle = QPointF(rect.left() + rect.width(), rect.top() + rect.height() / 2); path->moveTo(rightMiddle); path->arcTo(rect.width() / 2, rect.height() / 2, 0, 360.0); path->close(); path->normalize(); shape = path; } return shape; }
KoShape* KisShapeToolHelper::createRectangleShape(const QRectF& rect) { KoShape* shape; KoShapeFactoryBase *rectFactory = KoShapeRegistry::instance()->value("RectangleShape"); if (rectFactory) { shape = rectFactory->createDefaultShape(); shape->setSize(rect.size()); shape->setPosition(rect.topLeft()); } else { //Fallback if the plugin wasn't found KoPathShape* path = new KoPathShape(); path->setShapeId(KoPathShapeId); path->moveTo(rect.topLeft()); path->lineTo(rect.topLeft() + QPointF(rect.width(), 0)); path->lineTo(rect.bottomRight()); path->lineTo(rect.topLeft() + QPointF(0, rect.height())); path->close(); path->normalize(); shape = path; } return shape; }