File indexing completed on 2024-06-23 04:27:05
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2006-2007 Jan Hambrecht <jaham@gmx.net> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "star/StarShapeFactory.h" 0008 #include "star/StarShape.h" 0009 #include "star/StarShapeConfigWidget.h" 0010 0011 #include <KoShapeFactoryBase.h> 0012 #include <KoShapeStroke.h> 0013 #include <KoProperties.h> 0014 #include <KoXmlNS.h> 0015 #include <KoColorBackground.h> 0016 #include <KoShapeLoadingContext.h> 0017 0018 #include <KoIcon.h> 0019 0020 #include <klocalizedstring.h> 0021 0022 #include "kis_pointer_utils.h" 0023 0024 StarShapeFactory::StarShapeFactory() 0025 : KoShapeFactoryBase(StarShapeId, i18n("A star shape")) 0026 { 0027 setToolTip(i18n("A star")); 0028 setIconName(koIconNameCStr("star-shape")); 0029 QStringList elementNames; 0030 elementNames << "regular-polygon" << "custom-shape"; 0031 setXmlElementNames(KoXmlNS::draw, elementNames); 0032 setLoadingPriority(5); 0033 0034 KoShapeTemplate t; 0035 t.id = KoPathShapeId; 0036 t.templateId = "star"; 0037 t.name = i18n("Star"); 0038 t.family = "geometric"; 0039 t.toolTip = i18n("A star"); 0040 t.iconName = koIconName("star-shape"); 0041 KoProperties *props = new KoProperties(); 0042 props->setProperty("corners", 5); 0043 QVariant v; 0044 v.setValue(QColor(Qt::yellow)); 0045 props->setProperty("background", v); 0046 t.properties = props; 0047 addTemplate(t); 0048 0049 t.id = KoPathShapeId; 0050 t.templateId = "flower"; 0051 t.name = i18n("Flower"); 0052 t.family = "funny"; 0053 t.toolTip = i18n("A flower"); 0054 t.iconName = koIconName("flower-shape"); 0055 props = new KoProperties(); 0056 props->setProperty("corners", 5); 0057 props->setProperty("baseRadius", 10.0); 0058 props->setProperty("tipRadius", 50.0); 0059 props->setProperty("baseRoundness", 0.0); 0060 props->setProperty("tipRoundness", 40.0); 0061 v.setValue(QColor(Qt::magenta)); 0062 props->setProperty("background", v); 0063 t.properties = props; 0064 addTemplate(t); 0065 0066 t.id = KoPathShapeId; 0067 t.templateId = "pentagon"; 0068 t.name = i18n("Pentagon"); 0069 t.family = "geometric"; 0070 t.toolTip = i18n("A pentagon"); 0071 t.iconName = koIconName("pentagon-shape"); 0072 props = new KoProperties(); 0073 props->setProperty("corners", 5); 0074 props->setProperty("convex", true); 0075 props->setProperty("tipRadius", 50.0); 0076 props->setProperty("tipRoundness", 0.0); 0077 v.setValue(QColor(Qt::blue)); 0078 props->setProperty("background", v); 0079 t.properties = props; 0080 addTemplate(t); 0081 0082 t.id = KoPathShapeId; 0083 t.templateId = "hexagon"; 0084 t.name = i18n("Hexagon"); 0085 t.family = "geometric"; 0086 t.toolTip = i18n("A hexagon"); 0087 t.iconName = koIconName("hexagon-shape"); 0088 props = new KoProperties(); 0089 props->setProperty("corners", 6); 0090 props->setProperty("convex", true); 0091 props->setProperty("tipRadius", 50.0); 0092 props->setProperty("tipRoundness", 0.0); 0093 v.setValue(QColor(Qt::blue)); 0094 props->setProperty("background", v); 0095 t.properties = props; 0096 addTemplate(t); 0097 } 0098 0099 KoShape *StarShapeFactory::createDefaultShape(KoDocumentResourceManager *) const 0100 { 0101 StarShape *star = new StarShape(); 0102 0103 star->setStroke(toQShared(new KoShapeStroke(1.0))); 0104 star->setShapeId(KoPathShapeId); 0105 0106 return star; 0107 } 0108 0109 KoShape *StarShapeFactory::createShape(const KoProperties *params, KoDocumentResourceManager *) const 0110 { 0111 StarShape *star = new StarShape(); 0112 if (!star) { 0113 return 0; 0114 } 0115 0116 star->setCornerCount(params->intProperty("corners", 5)); 0117 star->setConvex(params->boolProperty("convex", false)); 0118 star->setBaseRadius(params->doubleProperty("baseRadius", 25.0)); 0119 star->setTipRadius(params->doubleProperty("tipRadius", 50.0)); 0120 star->setBaseRoundness(params->doubleProperty("baseRoundness", 0.0)); 0121 star->setTipRoundness(params->doubleProperty("tipRoundness", 0.0)); 0122 star->setStroke(toQShared(new KoShapeStroke(1.0))); 0123 star->setShapeId(KoPathShapeId); 0124 QVariant v; 0125 if (params->property("background", v)) { 0126 star->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(v.value<QColor>()))); 0127 } 0128 0129 return star; 0130 } 0131 0132 bool StarShapeFactory::supports(const QDomElement &e, KoShapeLoadingContext &context) const 0133 { 0134 Q_UNUSED(context); 0135 if (e.localName() == "regular-polygon" && e.namespaceURI() == KoXmlNS::draw) { 0136 return true; 0137 } 0138 return (e.localName() == "custom-shape" && e.namespaceURI() == KoXmlNS::draw 0139 && e.attributeNS(KoXmlNS::draw, "engine", "") == "calligra:star"); 0140 } 0141 0142 QList<KoShapeConfigWidgetBase *> StarShapeFactory::createShapeOptionPanels() 0143 { 0144 QList<KoShapeConfigWidgetBase *> panels; 0145 panels.append(new StarShapeConfigWidget()); 0146 return panels; 0147 }