1 /***************************************************************************
2 * *
3 * copyright : (C) 2011 Joshua Netterfield *
4 * joshua.netterfield@gmail.com *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 ***************************************************************************/
12
13 #include "viewitemscriptinterface.h"
14 #include "viewitem.h"
15 #include "lineedititem.h"
16 #include "buttonitem.h"
17
18 #include <QStringBuilder>
19
20 namespace Kst {
21
doCommand(QString x)22 QString LayoutTabSI::doCommand(QString x) {
23 if(x.startsWith("getLayout")) {
24 x.remove(0,9);
25 QSizeF f;
26 if(x.contains("Margin")) {
27 f=vi->layoutMargins();
28 } else if(x.contains("Spacing")) {
29 f=vi->layoutSpacing();
30 } else {
31 return "";
32 }
33 if(x.contains("Horizontal")) {
34 return QString::number(f.width());
35 } else if(x.contains("Vertical")) {
36 return QString::number(f.height());
37 }
38 } else if(x.startsWith("setLayout")) {
39 x.remove(0,9);
40 QSizeF f;
41 if(x.contains("Margin")) {
42 f=vi->layoutMargins();
43 } else if(x.contains("Spacing")) {
44 f=vi->layoutSpacing();
45 } else {
46 return "";
47 }
48 if(x.contains("Horizontal")) {
49 x.remove(0,x.indexOf("("));
50 x.remove(x.indexOf(")"),99999);
51 f.setWidth(x.toInt());
52 } else if(x.contains("Vertical")) {
53 x.remove(0,x.indexOf("("));
54 x.remove(x.indexOf(")"),99999);
55 f.setWidth(x.toInt());
56 } else {
57 return "";
58 }
59
60 if(x.contains("Margin")) {
61 vi->setLayoutMargins(f);
62 } else if(x.contains("Spacing")) {
63 vi->setLayoutSpacing(f);
64 }
65 return "Done";
66 }
67 return "";
68 }
69
doCommand(QString x)70 QString FillTabSI::doCommand(QString x) {
71 if(!x.startsWith("setFillColor")&&!x.startsWith("setIndexOfFillStyle(")) {
72 return "";
73 }
74 QBrush b=item->brush();
75 QColor this_color = (x.startsWith("setFillColor(")&&x!="setFillColor()") ? QColor(x.remove("setFillColor(").remove(')')) : b.color();
76 Qt::BrushStyle this_style = b.style();
77 if(x.startsWith("setIndexOfFillStyle(")) {
78 x.remove("setIndexOfFillStyle(").remove(')');
79 Qt::BrushStyle arr[]={Qt::NoBrush,Qt::SolidPattern,Qt::Dense1Pattern,Qt::Dense2Pattern,Qt::Dense3Pattern,Qt::Dense4Pattern,Qt::Dense5Pattern,
80 Qt::Dense6Pattern,Qt::Dense7Pattern,Qt::HorPattern,Qt::VerPattern,Qt::CrossPattern,Qt::BDiagPattern,Qt::FDiagPattern,
81 Qt::DiagCrossPattern};
82 this_style=arr[x.toInt()];
83 }
84 b.setColor(this_color);
85 b.setStyle(this_style);
86 item->setBrush(b);
87 return "Done";
88 }
89
doCommand(QString x)90 QString StrokeTabSI::doCommand(QString x) {
91 if(!x.startsWith("setIndexOfStrokeStyle")&&!x.startsWith("setIndexOfStrokeBrushStyle(")&&!x.startsWith("setIndexOfStrokeJoinStyle")&&
92 !x.startsWith("setIndexOfStrokeCapStyle")&&!x.startsWith("setStrokeWidth")&&!x.startsWith("setStrokeBrushColor")) {
93 return "";
94 }
95
96 QPen p=item->pen();
97 QBrush b = p.brush();
98
99 Qt::PenStyle this_style = p.style();
100 qreal this_width = p.widthF();
101 QColor this_brushColor = b.color();
102 Qt::BrushStyle this_brushStyle = b.style();
103
104 Qt::PenJoinStyle this_joinStyle = p.joinStyle();
105 Qt::PenCapStyle this_capStyle = p.capStyle();
106
107 if(x.startsWith("setIndexOfStrokeStyle(")) {
108 x.remove("setIndexOfStrokeStyle(").remove(')');
109 Qt::PenStyle arr[]={Qt::NoPen,Qt::SolidLine,Qt::DashLine,Qt::DotLine,Qt::DashDotLine,Qt::DashDotDotLine,Qt::CustomDashLine};
110 this_style=arr[x.toInt()];
111 } else if(x.startsWith("setIndexOfStrokeBrushStyle(")) {
112 x.remove("setIndexOfStrokeBrushStyle(").remove(')');
113 Qt::BrushStyle arr[]={Qt::NoBrush,Qt::SolidPattern,Qt::Dense1Pattern,Qt::Dense2Pattern,Qt::Dense3Pattern,Qt::Dense4Pattern,Qt::Dense5Pattern,
114 Qt::Dense6Pattern,Qt::Dense7Pattern,Qt::HorPattern,Qt::VerPattern,Qt::CrossPattern,Qt::BDiagPattern,Qt::FDiagPattern,
115 Qt::DiagCrossPattern};
116 this_brushStyle=arr[x.toInt()];
117 } else if(x.startsWith("setIndexOfStrokeJoinStyle(")) {
118 x.remove("setIndexOfStrokeJoinStyle(").remove(')');
119 Qt::PenJoinStyle arr[]={Qt::MiterJoin,Qt::BevelJoin,Qt::RoundJoin,Qt::SvgMiterJoin};
120 this_joinStyle=arr[x.toInt()];
121 } else if(x.startsWith("setIndexOfStrokeCapStyle(")) {
122 x.remove("setIndexOfStrokeCapStyle(").remove(')');
123 Qt::PenCapStyle arr[]={Qt::FlatCap,Qt::SquareCap,Qt::RoundCap};
124 this_capStyle=arr[x.toInt()];
125 } else if(x.startsWith("setStrokeWidth(")) {
126 this_width=x.remove("setStrokeWidth(").remove(')').toFloat();
127 } else if(x.startsWith("setStrokeBrushColor(")) {
128 this_brushColor=QColor(x.remove("setStrokeBrushColor(").remove(')'));
129 }
130
131 p.setStyle(this_style);
132 p.setWidthF(this_width);
133
134 b.setColor(this_brushColor);
135 b.setStyle(this_brushStyle);
136
137 p.setJoinStyle(this_joinStyle);
138 p.setCapStyle(this_capStyle);
139 p.setBrush(b);
140 #ifdef Q_OS_WIN32
141 if (p.isCosmetic()) {
142 p.setWidth(1);
143 }
144 #endif
145
146 item->storePen(p);
147 return "Done";
148 }
149
150 /* Note: this is a brute force command handler, and probably shouldn't be coppied. *
151 * Look at vectorscriptinterface instead. Its the way all the cool kids do it. */
doCommand(QString x)152 QString DimensionTabSI::doCommand(QString x) {
153
154 QString command = x.left(x.indexOf('('));
155 QStringList args = ScriptInterface::getArgs(x);
156
157 if (command == "setLockPosToData") {
158 if (args.at(0).toLower() == "true") {
159 item->setLockPosToData(true);
160 } else {
161 item->setLockPosToData(false);
162 }
163 return "Done";
164 }
165
166 if (command == "updateParent") {
167 item->updateViewItemParent();
168 return "Done";
169 }
170
171 if (command == "parentTopLevel") {
172 item->updateViewItemParent(true);
173 return "Done";
174 }
175
176 if(command=="fixAspectRatioIsChecked") {
177 return item->lockAspectRatio()?"true":"false";
178 }
179
180 if (command == "position") {
181 QString retval;
182 if(item->dataPosLockable() && item->lockPosToData()) {
183 retval = QString("(%1, %2)").arg(item->dataRelativeRect().center().x()).arg(item->dataRelativeRect().center().y());
184 } else {
185 retval = QString("(%1, %2)").arg(item->relativeCenter().x()).arg(item->relativeCenter().y());
186 }
187 return retval;
188 }
189
190 if (command == "dimensions") {
191 QString retval;
192 if(item->dataPosLockable() && item->lockPosToData()) {
193 retval = QString("(%1, %2)").arg(item->dataRelativeRect().width()).arg(item->dataRelativeRect().height());
194 } else {
195 retval = QString("(%1, %2)").arg(item->relativeWidth()).arg(item->relativeHeight());
196 }
197 return retval;
198 }
199
200 if (command == "setPos") {
201 item->setItemPos(args.at(0).toDouble(), args.at(1).toDouble());
202
203 QTransform transform;
204
205 item->setTransform(transform);
206 item->updateRelativeSize();
207
208 return "Done";
209 }
210
211 if (command == "setSize") {
212 if (args.size() == 1) {
213 item->setItemSize(args.at(0).toDouble());
214 } else {
215 item->setItemSize(args.at(0).toDouble(), args.at(1).toDouble());
216 }
217
218 QTransform transform;
219
220 item->setTransform(transform);
221 item->updateRelativeSize();
222
223 return "Done";
224 }
225
226 if (command == "lockAspectRatio") {
227 if (args.at(0).toLower() == "true") {
228 item->setLockAspectRatio(true);
229 } else {
230 item->setLockAspectRatio(false);
231 }
232 return "Done";
233 }
234
235 if (command == "setRotation") {
236
237 QTransform transform;
238 transform.rotate(args.at(0).toDouble());
239
240 item->setTransform(transform);
241 item->updateRelativeSize();
242 return "Done.";
243
244 }
245
246 if (command == "setLineEndpoints") {
247 if (args.size() == 4) {
248 LineItem *lineItem = qobject_cast<LineItem*>(item);
249 if (lineItem) {
250 double x1 = args.at(0).toDouble();
251 double y1 = args.at(1).toDouble();
252 double x2 = args.at(2).toDouble();
253 double y2 = args.at(3).toDouble();
254
255 lineItem->setEndpoints(x1, y1, x2, y2);
256 }
257 }
258 }
259
260 return QString(); // command not recognized, so return empty string.
261 }
262
ViewItemSI(ViewItem * it)263 ViewItemSI::ViewItemSI(ViewItem *it) : layout(new LayoutTabSI), dim(new DimensionTabSI), fill(new FillTabSI), stroke(new StrokeTabSI) {
264 layout->vi=it;
265 dim->item=it;
266 fill->item=it;
267 stroke->item=it;
268 }
269
doCommand(QString x)270 QString ViewItemSI::doCommand(QString x) {
271
272 QString v=doNamedObjectCommand(x, dim->item);
273
274 if (v.isEmpty()) {
275 v=layout->doCommand(x);
276 }
277 if (v.isEmpty()) {
278 v=dim->doCommand(x);
279 }
280 if (v.isEmpty()) {
281 v=fill->doCommand(x);
282 }
283 if (v.isEmpty()) {
284 v=stroke->doCommand(x);
285 }
286 if (v.isEmpty()&&x.startsWith("setText(")) {
287 if(qobject_cast<ButtonItem*>(layout->vi)) {
288 qobject_cast<ButtonItem*>(layout->vi)->setText(x.remove("setText(").remove(')'));
289 v="Done";
290 } else if(qobject_cast<LineEditItem*>(layout->vi)) {
291 qobject_cast<LineEditItem*>(layout->vi)->setText(x.remove("setText(").remove(')'));
292 v="Done";
293 }
294 }
295 return v.isEmpty()?"No command":v;
296 }
297
isValid()298 bool ViewItemSI::isValid() {
299 return dim->item;
300 }
301
newBox()302 ScriptInterface* ViewItemSI::newBox() {
303 BoxItem* bi=new BoxItem(kstApp->mainWindow()->tabWidget()->currentView());
304 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
305 return new ViewItemSI(bi);
306 }
307
308
newButton()309 ScriptInterface* ViewItemSI::newButton() {
310 ButtonItem* bi=new ButtonItem(kstApp->mainWindow()->tabWidget()->currentView());
311 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
312 return new ViewItemSI(bi);
313 }
314
newLineEdit()315 ScriptInterface* ViewItemSI::newLineEdit() {
316 LineEditItem* bi=new LineEditItem(kstApp->mainWindow()->tabWidget()->currentView());
317 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
318 return new ViewItemSI(bi);
319 }
320
newCircle()321 ScriptInterface* ViewItemSI::newCircle() {
322 CircleItem* bi=new CircleItem(kstApp->mainWindow()->tabWidget()->currentView());
323 bi->setViewRect(-0.1/2.0, -0.1/2.0, 0.1/2.0, 0.1/2.0);
324 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
325 return new ViewItemSI(bi);
326 }
327
newEllipse()328 ScriptInterface* ViewItemSI::newEllipse() {
329 EllipseItem* bi=new EllipseItem(kstApp->mainWindow()->tabWidget()->currentView());
330 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
331 return new ViewItemSI(bi);
332 }
333
newLine()334 ScriptInterface* ViewItemSI::newLine() {
335 LineItem* bi=new LineItem(kstApp->mainWindow()->tabWidget()->currentView());
336 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
337 return new ViewItemSI(bi);
338 }
339
newPicture(QByteArray picf)340 ScriptInterface* ViewItemSI::newPicture(QByteArray picf) {
341 PictureItem* bi=new PictureItem(kstApp->mainWindow()->tabWidget()->currentView(),QImage(QString::fromLocal8Bit(picf)));
342 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
343 bi->setViewRect(0.9,0.9,1.0,1.0,true);
344 bi->setVisible(1);
345 //bi->updateViewItemParent();
346 return new ViewItemSI(bi);
347 }
348
349 #ifndef KST_NO_SVG
newSvgItem(QByteArray path)350 ScriptInterface* ViewItemSI::newSvgItem(QByteArray path) {
351 SvgItem* bi=new SvgItem(kstApp->mainWindow()->tabWidget()->currentView(),path);
352 kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
353 bi->setViewRect(0.9,0.9,1.0,1.0,true);
354 bi->setVisible(1);
355 //bi->updateViewItemParent();
356 return new ViewItemSI(bi);
357 }
358 #endif
359
360
361
362 }
363