Install tools
[yamf.git] / yamf / item / converter.cpp
blob65f65bb55f489b9434ae0ce5764ad6b0337f1b90
1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "converter.h"
23 #include "item/rect.h"
24 #include "item/path.h"
25 #include "item/line.h"
26 #include "item/ellipse.h"
27 #include "item/proxy.h"
28 #include "item/group.h"
30 #include <dcore/debug.h>
32 #include <QAbstractGraphicsShapeItem>
33 #include <QBrush>
34 #include <QPen>
36 namespace YAMF {
37 namespace Item {
39 /**
40 * @~spanish
41 * Copia las propiedades del item @p src al item @p dest.
43 void Converter::copyProperties(QGraphicsItem *src, QGraphicsItem *dest)
45 dest->setMatrix(src->matrix());
46 dest->setPos(src->scenePos());
47 dest->setFlags(src->flags() );
49 dest->setSelected(src->isSelected());
52 // Shapes
53 QAbstractGraphicsShapeItem *shape = dynamic_cast<QAbstractGraphicsShapeItem*>(src);
54 QAbstractGraphicsShapeItem *shapeDst = qgraphicsitem_cast<QAbstractGraphicsShapeItem*>(dest);
56 if ( shape && dest )
58 QBrush shapeBrush = shape->brush();
60 if ( shapeBrush.color().isValid() || shapeBrush.gradient() ||
61 !shapeBrush.texture().isNull() )
62 shapeDst->setBrush( shape->brush() );
64 shapeDst->setPen(shape->pen() );
68 /**
69 * @~spanish
70 * Retorna un la transformacion de @p item a un Item::Path,
71 * si no lo puede transoformar retorna 0.
73 QGraphicsItem *Converter::convertToPath(QGraphicsItem *item)
75 if(!item) return 0;
77 Path *path = new Path( item->parentItem(), 0);
79 QPainterPath ppath;
81 switch(item->type() )
83 case Path::Type:
85 ppath = qgraphicsitem_cast<Path *>(item)->path();
87 break;
88 case Rect::Type:
90 ppath.addRect(qgraphicsitem_cast<Rect *>(item)->rect());
92 break;
93 case Ellipse::Type:
95 ppath.addEllipse(qgraphicsitem_cast<Ellipse *>(item)->rect());
97 break;
98 case Proxy::Type:
100 QGraphicsItem *data = qgraphicsitem_cast<Proxy*>(item)->item();
101 // data->setPos(item->scenePos());
102 qgraphicsitem_cast<Proxy*>(item)->setItem(convertToPath(data));
103 return item;
105 return convertToPath(data);
107 break;
108 case Line::Type:
110 QLineF line = qgraphicsitem_cast<Line *>(item)->line();
111 ppath.moveTo(line.p1());
112 ppath.lineTo(line.p2());
114 break;
115 case Group::Type:
117 dWarning() << "Converter::convertToPath no support groups";
118 delete path;
119 return 0;
121 break;
122 default:
124 dWarning() << "Converter::convertToPath use default";
125 ppath = item->shape(); // TODO
127 break;
130 path->setPath(ppath);
132 Converter::copyProperties( item, path);
134 return path;
139 * @~spanish
140 * Retorna un la transformacion de @p item a un Item::Ellipse,
141 * si no lo puede transoformar retorna 0.
143 Ellipse *Converter::convertToEllipse(QGraphicsItem *item)
145 Ellipse *ellipse = new Ellipse(item->parentItem());
147 switch(item->type() )
149 case Path::Type:
151 ellipse->setRect(qgraphicsitem_cast<QGraphicsPathItem *>(item)->path().boundingRect());
153 break;
154 case Ellipse::Type:
156 ellipse->setRect(qgraphicsitem_cast<QGraphicsEllipseItem *>(item)->rect());
158 break;
159 // TODO
162 Converter::copyProperties( item, ellipse);
164 return ellipse;
169 * @~spanish
170 * Retorna un la transformacion de @p item a un Item::Rect,
171 * si no lo puede transoformar retorna 0.
173 Rect *Converter::convertToRect(QGraphicsItem *item)
175 Rect *rect = new Rect(item->parentItem());
177 switch(item->type() )
179 case Path::Type:
181 rect->setRect(qgraphicsitem_cast<QGraphicsPathItem *>(item)->path().boundingRect());
183 break;
184 case Ellipse::Type:
186 rect->setRect(qgraphicsitem_cast<QGraphicsEllipseItem *>(item)->rect());
188 break;
189 // TODO
192 Converter::copyProperties( item, rect);
194 return rect;
198 * @~spanish
199 * Retorna un la transformacion de @p item a un Item::Line,
200 * si no lo puede transoformar retorna 0.
202 Line *Converter::convertToLine(QGraphicsItem *item)
204 Line *line = new Line(item->parentItem());
205 switch(item->type() )
207 case Path::Type:
209 QRectF rect = qgraphicsitem_cast<QGraphicsPathItem *>(item)->path().boundingRect();
210 line->setLine(QLineF(rect.topLeft(), rect.bottomRight()));
212 break;
213 case Ellipse::Type:
215 QRectF rect = qgraphicsitem_cast<QGraphicsEllipseItem *>(item)->rect();
216 line->setLine(QLineF(rect.topLeft(), rect.bottomRight()));
218 break;
219 // TODO
221 Converter::copyProperties( item, line);
223 return line;
228 * @~spanish
229 * Retorna un la transformacion de @p item a un item de tipo @p toType,
230 * si no lo puede transoformar retorna 0.
232 QGraphicsItem *Converter::convertTo(QGraphicsItem *item, int toType)
234 switch(toType)
236 case Item::Path::Type: // Path
238 return Converter::convertToPath( item );
240 break;
241 case Item::Rect::Type: // Rect
243 Item::Rect *rect = Converter::convertToRect( item );
245 return rect;
247 break;
248 case Item::Ellipse::Type: // Ellipse
250 Item::Ellipse *ellipse = Converter::convertToEllipse( item );
251 return ellipse;
253 break;
254 case Item::Proxy::Type:
256 return new Item::Proxy( item );
258 break;
259 case Item::Line::Type:
261 return Converter::convertToLine( item );
263 break;
264 default:
266 dWarning() << "unknown item " << toType ;
268 break;
270 return 0;