1 /* This file is part of the KDE project
2 * Copyright (C) 2006-2007 Jan Hambrecht <jaham@gmx.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "star/StarShapeFactory.h"
21 #include "star/StarShape.h"
22 #include "star/StarShapeConfigWidget.h"
23
24 #include <KoShapeFactoryBase.h>
25 #include <KoShapeStroke.h>
26 #include <KoProperties.h>
27 #include <KoXmlNS.h>
28 #include <KoXmlReader.h>
29 #include <KoColorBackground.h>
30 #include <KoShapeLoadingContext.h>
31
32 #include <KoIcon.h>
33
34 #include <klocalizedstring.h>
35
36 #include "kis_pointer_utils.h"
37
StarShapeFactory()38 StarShapeFactory::StarShapeFactory()
39 : KoShapeFactoryBase(StarShapeId, i18n("A star shape"))
40 {
41 setToolTip(i18n("A star"));
42 setIconName(koIconNameCStr("star-shape"));
43 QStringList elementNames;
44 elementNames << "regular-polygon" << "custom-shape";
45 setXmlElementNames(KoXmlNS::draw, elementNames);
46 setLoadingPriority(5);
47
48 KoShapeTemplate t;
49 t.id = KoPathShapeId;
50 t.templateId = "star";
51 t.name = i18n("Star");
52 t.family = "geometric";
53 t.toolTip = i18n("A star");
54 t.iconName = koIconName("star-shape");
55 KoProperties *props = new KoProperties();
56 props->setProperty("corners", 5);
57 QVariant v;
58 v.setValue(QColor(Qt::yellow));
59 props->setProperty("background", v);
60 t.properties = props;
61 addTemplate(t);
62
63 t.id = KoPathShapeId;
64 t.templateId = "flower";
65 t.name = i18n("Flower");
66 t.family = "funny";
67 t.toolTip = i18n("A flower");
68 t.iconName = koIconName("flower-shape");
69 props = new KoProperties();
70 props->setProperty("corners", 5);
71 props->setProperty("baseRadius", 10.0);
72 props->setProperty("tipRadius", 50.0);
73 props->setProperty("baseRoundness", 0.0);
74 props->setProperty("tipRoundness", 40.0);
75 v.setValue(QColor(Qt::magenta));
76 props->setProperty("background", v);
77 t.properties = props;
78 addTemplate(t);
79
80 t.id = KoPathShapeId;
81 t.templateId = "pentagon";
82 t.name = i18n("Pentagon");
83 t.family = "geometric";
84 t.toolTip = i18n("A pentagon");
85 t.iconName = koIconName("pentagon-shape");
86 props = new KoProperties();
87 props->setProperty("corners", 5);
88 props->setProperty("convex", true);
89 props->setProperty("tipRadius", 50.0);
90 props->setProperty("tipRoundness", 0.0);
91 v.setValue(QColor(Qt::blue));
92 props->setProperty("background", v);
93 t.properties = props;
94 addTemplate(t);
95
96 t.id = KoPathShapeId;
97 t.templateId = "hexagon";
98 t.name = i18n("Hexagon");
99 t.family = "geometric";
100 t.toolTip = i18n("A hexagon");
101 t.iconName = koIconName("hexagon-shape");
102 props = new KoProperties();
103 props->setProperty("corners", 6);
104 props->setProperty("convex", true);
105 props->setProperty("tipRadius", 50.0);
106 props->setProperty("tipRoundness", 0.0);
107 v.setValue(QColor(Qt::blue));
108 props->setProperty("background", v);
109 t.properties = props;
110 addTemplate(t);
111 }
112
createDefaultShape(KoDocumentResourceManager *) const113 KoShape *StarShapeFactory::createDefaultShape(KoDocumentResourceManager *) const
114 {
115 StarShape *star = new StarShape();
116
117 star->setStroke(toQShared(new KoShapeStroke(1.0)));
118 star->setShapeId(KoPathShapeId);
119
120 return star;
121 }
122
createShape(const KoProperties * params,KoDocumentResourceManager *) const123 KoShape *StarShapeFactory::createShape(const KoProperties *params, KoDocumentResourceManager *) const
124 {
125 StarShape *star = new StarShape();
126 if (!star) {
127 return 0;
128 }
129
130 star->setCornerCount(params->intProperty("corners", 5));
131 star->setConvex(params->boolProperty("convex", false));
132 star->setBaseRadius(params->doubleProperty("baseRadius", 25.0));
133 star->setTipRadius(params->doubleProperty("tipRadius", 50.0));
134 star->setBaseRoundness(params->doubleProperty("baseRoundness", 0.0));
135 star->setTipRoundness(params->doubleProperty("tipRoundness", 0.0));
136 star->setStroke(toQShared(new KoShapeStroke(1.0)));
137 star->setShapeId(KoPathShapeId);
138 QVariant v;
139 if (params->property("background", v)) {
140 star->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(v.value<QColor>())));
141 }
142
143 return star;
144 }
145
supports(const KoXmlElement & e,KoShapeLoadingContext & context) const146 bool StarShapeFactory::supports(const KoXmlElement &e, KoShapeLoadingContext &context) const
147 {
148 Q_UNUSED(context);
149 if (e.localName() == "regular-polygon" && e.namespaceURI() == KoXmlNS::draw) {
150 return true;
151 }
152 return (e.localName() == "custom-shape" && e.namespaceURI() == KoXmlNS::draw
153 && e.attributeNS(KoXmlNS::draw, "engine", "") == "calligra:star");
154 }
155
createShapeOptionPanels()156 QList<KoShapeConfigWidgetBase *> StarShapeFactory::createShapeOptionPanels()
157 {
158 QList<KoShapeConfigWidgetBase *> panels;
159 panels.append(new StarShapeConfigWidget());
160 return panels;
161 }
162