File indexing completed on 2024-12-22 04:17:50

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2011 Joshua Netterfield                               *
0004  *                   joshua.netterfield@gmail.com                          *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "viewitemscriptinterface.h"
0014 #include "viewitem.h"
0015 #include "lineedititem.h"
0016 #include "buttonitem.h"
0017 
0018 #include <QStringBuilder>
0019 
0020 namespace Kst {
0021 
0022 QString LayoutTabSI::doCommand(QString x) {
0023     if(x.startsWith("getLayout")) {
0024         x.remove(0,9);
0025         QSizeF f;
0026         if(x.contains("Margin")) {
0027             f=vi->layoutMargins();
0028         } else if(x.contains("Spacing")) {
0029             f=vi->layoutSpacing();
0030         } else {
0031             return "";
0032         }
0033         if(x.contains("Horizontal")) {
0034             return QString::number(f.width());
0035         } else if(x.contains("Vertical")) {
0036             return QString::number(f.height());
0037         }
0038     } else if(x.startsWith("setLayout")) {
0039         x.remove(0,9);
0040         QSizeF f;
0041         if(x.contains("Margin")) {
0042             f=vi->layoutMargins();
0043         } else if(x.contains("Spacing")) {
0044             f=vi->layoutSpacing();
0045         } else {
0046             return "";
0047         }
0048         if(x.contains("Horizontal")) {
0049             x.remove(0,x.indexOf("("));
0050             x.remove(x.indexOf(")"),99999);
0051             f.setWidth(x.toInt());
0052         } else if(x.contains("Vertical")) {
0053             x.remove(0,x.indexOf("("));
0054             x.remove(x.indexOf(")"),99999);
0055             f.setWidth(x.toInt());
0056         } else {
0057             return "";
0058         }
0059 
0060         if(x.contains("Margin")) {
0061             vi->setLayoutMargins(f);
0062         } else if(x.contains("Spacing")) {
0063             vi->setLayoutSpacing(f);
0064         }
0065         return "Done";
0066     }
0067     return "";
0068 }
0069 
0070 QString FillTabSI::doCommand(QString x) {
0071     if(!x.startsWith("setFillColor")&&!x.startsWith("setIndexOfFillStyle(")) {
0072         return "";
0073     }
0074     QBrush b=item->brush();
0075     QColor this_color = (x.startsWith("setFillColor(")&&x!="setFillColor()") ? QColor(x.remove("setFillColor(").remove(')')) : b.color();
0076     Qt::BrushStyle this_style = b.style();
0077     if(x.startsWith("setIndexOfFillStyle(")) {
0078         x.remove("setIndexOfFillStyle(").remove(')');
0079         Qt::BrushStyle arr[]={Qt::NoBrush,Qt::SolidPattern,Qt::Dense1Pattern,Qt::Dense2Pattern,Qt::Dense3Pattern,Qt::Dense4Pattern,Qt::Dense5Pattern,
0080                               Qt::Dense6Pattern,Qt::Dense7Pattern,Qt::HorPattern,Qt::VerPattern,Qt::CrossPattern,Qt::BDiagPattern,Qt::FDiagPattern,
0081                               Qt::DiagCrossPattern};
0082         this_style=arr[x.toInt()];
0083     }
0084     b.setColor(this_color);
0085     b.setStyle(this_style);
0086     item->setBrush(b);
0087     return "Done";
0088 }
0089 
0090 QString StrokeTabSI::doCommand(QString x) {
0091     if(!x.startsWith("setIndexOfStrokeStyle")&&!x.startsWith("setIndexOfStrokeBrushStyle(")&&!x.startsWith("setIndexOfStrokeJoinStyle")&&
0092             !x.startsWith("setIndexOfStrokeCapStyle")&&!x.startsWith("setStrokeWidth")&&!x.startsWith("setStrokeBrushColor")) {
0093         return "";
0094     }
0095 
0096     QPen p=item->pen();
0097     QBrush b = p.brush();
0098 
0099     Qt::PenStyle this_style = p.style();
0100     qreal this_width = p.widthF();
0101     QColor this_brushColor = b.color();
0102     Qt::BrushStyle this_brushStyle = b.style();
0103 
0104     Qt::PenJoinStyle this_joinStyle = p.joinStyle();
0105     Qt::PenCapStyle this_capStyle = p.capStyle();
0106 
0107     if(x.startsWith("setIndexOfStrokeStyle(")) {
0108         x.remove("setIndexOfStrokeStyle(").remove(')');
0109         Qt::PenStyle arr[]={Qt::NoPen,Qt::SolidLine,Qt::DashLine,Qt::DotLine,Qt::DashDotLine,Qt::DashDotDotLine,Qt::CustomDashLine};
0110         this_style=arr[x.toInt()];
0111     } else if(x.startsWith("setIndexOfStrokeBrushStyle(")) {
0112         x.remove("setIndexOfStrokeBrushStyle(").remove(')');
0113         Qt::BrushStyle arr[]={Qt::NoBrush,Qt::SolidPattern,Qt::Dense1Pattern,Qt::Dense2Pattern,Qt::Dense3Pattern,Qt::Dense4Pattern,Qt::Dense5Pattern,
0114                               Qt::Dense6Pattern,Qt::Dense7Pattern,Qt::HorPattern,Qt::VerPattern,Qt::CrossPattern,Qt::BDiagPattern,Qt::FDiagPattern,
0115                               Qt::DiagCrossPattern};
0116         this_brushStyle=arr[x.toInt()];
0117     } else if(x.startsWith("setIndexOfStrokeJoinStyle(")) {
0118         x.remove("setIndexOfStrokeJoinStyle(").remove(')');
0119         Qt::PenJoinStyle arr[]={Qt::MiterJoin,Qt::BevelJoin,Qt::RoundJoin,Qt::SvgMiterJoin};
0120         this_joinStyle=arr[x.toInt()];
0121     } else if(x.startsWith("setIndexOfStrokeCapStyle(")) {
0122         x.remove("setIndexOfStrokeCapStyle(").remove(')');
0123         Qt::PenCapStyle arr[]={Qt::FlatCap,Qt::SquareCap,Qt::RoundCap};
0124         this_capStyle=arr[x.toInt()];
0125     } else if(x.startsWith("setStrokeWidth(")) {
0126         this_width=x.remove("setStrokeWidth(").remove(')').toFloat();
0127     } else if(x.startsWith("setStrokeBrushColor(")) {
0128         this_brushColor=QColor(x.remove("setStrokeBrushColor(").remove(')'));
0129     }
0130 
0131     p.setStyle(this_style);
0132     p.setWidthF(this_width);
0133 
0134     b.setColor(this_brushColor);
0135     b.setStyle(this_brushStyle);
0136 
0137     p.setJoinStyle(this_joinStyle);
0138     p.setCapStyle(this_capStyle);
0139     p.setBrush(b);
0140 #ifdef Q_OS_WIN32
0141     if (p.isCosmetic()) {
0142         p.setWidth(1);
0143     }
0144 #endif
0145 
0146     item->storePen(p);
0147     return "Done";
0148 }
0149 
0150 /* Note: this is a brute force command handler, and probably shouldn't be coppied. *
0151  * Look at vectorscriptinterface instead.  Its the way all the cool kids do it.    */
0152 QString DimensionTabSI::doCommand(QString x) {
0153 
0154     QString command = x.left(x.indexOf('('));
0155     QStringList args = ScriptInterface::getArgs(x);
0156 
0157     if (command == "setLockPosToData") {
0158       if (args.at(0).toLower() == "true") {
0159         item->setLockPosToData(true);
0160       } else {
0161         item->setLockPosToData(false);
0162       }
0163       return "Done";
0164     }
0165 
0166     if (command == "updateParent") {
0167       item->updateViewItemParent();
0168       return "Done";
0169     }
0170 
0171     if (command == "parentTopLevel") {
0172       item->updateViewItemParent(true);
0173       return "Done";
0174     }
0175 
0176     if(command=="fixAspectRatioIsChecked") {
0177         return item->lockAspectRatio()?"true":"false";
0178     }
0179 
0180     if (command == "position") {
0181       QString retval;
0182       if(item->dataPosLockable() && item->lockPosToData()) {
0183         retval = QString("(%1, %2)").arg(item->dataRelativeRect().center().x()).arg(item->dataRelativeRect().center().y());
0184       } else {
0185         retval = QString("(%1, %2)").arg(item->relativeCenter().x()).arg(item->relativeCenter().y());
0186       }
0187       return retval;
0188     }
0189 
0190     if (command == "dimensions") {
0191       QString retval;
0192       if(item->dataPosLockable() && item->lockPosToData()) {
0193         retval = QString("(%1, %2)").arg(item->dataRelativeRect().width()).arg(item->dataRelativeRect().height());
0194       } else {
0195         retval = QString("(%1, %2)").arg(item->relativeWidth()).arg(item->relativeHeight());
0196       }
0197       return retval;
0198     }
0199 
0200     if (command == "setPos") {
0201       item->setItemPos(args.at(0).toDouble(), args.at(1).toDouble());
0202 
0203       QTransform transform;
0204 
0205       item->setTransform(transform);
0206       item->updateRelativeSize();
0207 
0208       return "Done";
0209     }
0210 
0211     if (command == "setSize") {
0212       if (args.size() == 1) {
0213         item->setItemSize(args.at(0).toDouble());
0214       } else {
0215         item->setItemSize(args.at(0).toDouble(), args.at(1).toDouble());
0216       }
0217 
0218       QTransform transform;
0219 
0220       item->setTransform(transform);
0221       item->updateRelativeSize();
0222 
0223       return "Done";
0224     }
0225 
0226     if (command == "lockAspectRatio") {
0227       if (args.at(0).toLower() == "true") {
0228         item->setLockAspectRatio(true);
0229       } else {
0230         item->setLockAspectRatio(false);
0231       }
0232       return "Done";
0233     }
0234 
0235     if (command == "setRotation") {
0236 
0237       QTransform transform;
0238       transform.rotate(args.at(0).toDouble());
0239 
0240       item->setTransform(transform);
0241       item->updateRelativeSize();
0242       return "Done.";
0243 
0244     }
0245 
0246     if (command == "setLineEndpoints") {
0247       if (args.size() == 4) {
0248         LineItem *lineItem = qobject_cast<LineItem*>(item);
0249         if (lineItem) {
0250           double x1 = args.at(0).toDouble();
0251           double y1 = args.at(1).toDouble();
0252           double x2 = args.at(2).toDouble();
0253           double y2 = args.at(3).toDouble();
0254 
0255           lineItem->setEndpoints(x1, y1, x2, y2);
0256         }
0257       }
0258     }
0259 
0260     return QString(); // command not recognized, so return empty string.
0261 }
0262 
0263 ViewItemSI::ViewItemSI(ViewItem *it) : layout(new LayoutTabSI), dim(new DimensionTabSI), fill(new FillTabSI), stroke(new StrokeTabSI) {
0264     layout->vi=it;
0265     dim->item=it;
0266     fill->item=it;
0267     stroke->item=it;
0268 }
0269 
0270 QString ViewItemSI::doCommand(QString x) {
0271 
0272   QString v=doNamedObjectCommand(x, dim->item);
0273 
0274   if (v.isEmpty()) {
0275     v=layout->doCommand(x);
0276   }
0277   if (v.isEmpty()) {
0278     v=dim->doCommand(x);
0279   }
0280   if (v.isEmpty()) {
0281     v=fill->doCommand(x);
0282   }
0283   if (v.isEmpty()) {
0284     v=stroke->doCommand(x);
0285   }
0286   if (v.isEmpty()&&x.startsWith("setText(")) {
0287     if(qobject_cast<ButtonItem*>(layout->vi)) {
0288       qobject_cast<ButtonItem*>(layout->vi)->setText(x.remove("setText(").remove(')'));
0289       v="Done";
0290     } else if(qobject_cast<LineEditItem*>(layout->vi)) {
0291       qobject_cast<LineEditItem*>(layout->vi)->setText(x.remove("setText(").remove(')'));
0292       v="Done";
0293     }
0294   }
0295   return v.isEmpty()?"No command":v;
0296 }
0297 
0298 bool ViewItemSI::isValid() {
0299     return dim->item;
0300 }
0301 
0302 ScriptInterface* ViewItemSI::newBox() {
0303     BoxItem* bi=new BoxItem(kstApp->mainWindow()->tabWidget()->currentView());
0304     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0305     return new ViewItemSI(bi);
0306 }
0307 
0308 
0309 ScriptInterface* ViewItemSI::newButton() {
0310     ButtonItem* bi=new ButtonItem(kstApp->mainWindow()->tabWidget()->currentView());
0311     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0312     return new ViewItemSI(bi);
0313 }
0314 
0315 ScriptInterface* ViewItemSI::newLineEdit() {
0316     LineEditItem* bi=new LineEditItem(kstApp->mainWindow()->tabWidget()->currentView());
0317     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0318     return new ViewItemSI(bi);
0319 }
0320 
0321 ScriptInterface* ViewItemSI::newCircle() {
0322     CircleItem* bi=new CircleItem(kstApp->mainWindow()->tabWidget()->currentView());
0323     bi->setViewRect(-0.1/2.0, -0.1/2.0, 0.1/2.0, 0.1/2.0);
0324     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0325     return new ViewItemSI(bi);
0326 }
0327 
0328 ScriptInterface* ViewItemSI::newEllipse() {
0329     EllipseItem* bi=new EllipseItem(kstApp->mainWindow()->tabWidget()->currentView());
0330     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0331     return new ViewItemSI(bi);
0332 }
0333 
0334 ScriptInterface* ViewItemSI::newLine() {
0335     LineItem* bi=new LineItem(kstApp->mainWindow()->tabWidget()->currentView());
0336     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0337     return new ViewItemSI(bi);
0338 }
0339 
0340 ScriptInterface* ViewItemSI::newPicture(QByteArray picf) {
0341     PictureItem* bi=new PictureItem(kstApp->mainWindow()->tabWidget()->currentView(),QImage(QString::fromLocal8Bit(picf)));
0342     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0343     bi->setViewRect(0.9,0.9,1.0,1.0,true);
0344     bi->setVisible(1);
0345     //bi->updateViewItemParent();
0346     return new ViewItemSI(bi);
0347 }
0348 
0349 #ifndef KST_NO_SVG
0350 ScriptInterface* ViewItemSI::newSvgItem(QByteArray path) {
0351     SvgItem* bi=new SvgItem(kstApp->mainWindow()->tabWidget()->currentView(),path);
0352     kstApp->mainWindow()->tabWidget()->currentView()->scene()->addItem(bi);
0353     bi->setViewRect(0.9,0.9,1.0,1.0,true);
0354     bi->setVisible(1);
0355     //bi->updateViewItemParent();
0356     return new ViewItemSI(bi);
0357 }
0358 #endif
0359 
0360 
0361 
0362 }