File indexing completed on 2024-06-23 04:27:04
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "RectangleShapeFactory.h" 0008 #include "RectangleShape.h" 0009 #include "RectangleShapeConfigWidget.h" 0010 #include "KoShapeStroke.h" 0011 #include <KoXmlNS.h> 0012 #include <KoGradientBackground.h> 0013 #include <KoShapeLoadingContext.h> 0014 #include <KoProperties.h> 0015 #include "kis_assert.h" 0016 0017 #include <KoIcon.h> 0018 #include <klocalizedstring.h> 0019 0020 #include "kis_pointer_utils.h" 0021 0022 RectangleShapeFactory::RectangleShapeFactory() 0023 : KoShapeFactoryBase(RectangleShapeId, i18n("Rectangle")) 0024 { 0025 setToolTip(i18n("A rectangle")); 0026 setIconName(koIconNameCStr("rectangle-shape")); 0027 setFamily("geometric"); 0028 setLoadingPriority(1); 0029 0030 QList<QPair<QString, QStringList> > elementNamesList; 0031 elementNamesList.append(qMakePair(QString(KoXmlNS::draw), QStringList("rect"))); 0032 elementNamesList.append(qMakePair(QString(KoXmlNS::svg), QStringList("rect"))); 0033 setXmlElements(elementNamesList); 0034 } 0035 0036 KoShape *RectangleShapeFactory::createDefaultShape(KoDocumentResourceManager *) const 0037 { 0038 RectangleShape *rect = new RectangleShape(); 0039 0040 rect->setStroke(toQShared(new KoShapeStroke(1.0))); 0041 rect->setShapeId(KoPathShapeId); 0042 0043 QLinearGradient *gradient = new QLinearGradient(QPointF(0, 0), QPointF(1, 1)); 0044 gradient->setCoordinateMode(QGradient::ObjectBoundingMode); 0045 0046 gradient->setColorAt(0.0, Qt::white); 0047 gradient->setColorAt(1.0, Qt::green); 0048 rect->setBackground(QSharedPointer<KoGradientBackground>(new KoGradientBackground(gradient))); 0049 0050 return rect; 0051 } 0052 0053 KoShape *RectangleShapeFactory::createShape(const KoProperties *params, KoDocumentResourceManager *documentResources) const 0054 { 0055 KoShape *shape = createDefaultShape(documentResources); 0056 RectangleShape *rectShape = dynamic_cast<RectangleShape*>(shape); 0057 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(rectShape, shape); 0058 0059 rectShape->setSize( 0060 QSizeF(params->doubleProperty("width", rectShape->size().width()), 0061 params->doubleProperty("height", rectShape->size().height()))); 0062 0063 rectShape->setAbsolutePosition( 0064 QPointF(params->doubleProperty("x", rectShape->absolutePosition(KoFlake::TopLeft).x()), 0065 params->doubleProperty("y", rectShape->absolutePosition(KoFlake::TopLeft).y())), 0066 KoFlake::TopLeft); 0067 0068 0069 rectShape->setCornerRadiusX(params->doubleProperty("rx", 0.0)); 0070 rectShape->setCornerRadiusY(params->doubleProperty("ry", 0.0)); 0071 0072 return shape; 0073 } 0074 0075 bool RectangleShapeFactory::supports(const QDomElement &e, KoShapeLoadingContext &/*context*/) const 0076 { 0077 Q_UNUSED(e); 0078 return (e.localName() == "rect" && e.namespaceURI() == KoXmlNS::draw); 0079 } 0080 0081 QList<KoShapeConfigWidgetBase *> RectangleShapeFactory::createShapeOptionPanels() 0082 { 0083 QList<KoShapeConfigWidgetBase *> panels; 0084 panels.append(new RectangleShapeConfigWidget()); 0085 return panels; 0086 } 0087