1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
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. *
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. *
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>
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());
53 QAbstractGraphicsShapeItem
*shape
= dynamic_cast<QAbstractGraphicsShapeItem
*>(src
);
54 QAbstractGraphicsShapeItem
*shapeDst
= qgraphicsitem_cast
<QAbstractGraphicsShapeItem
*>(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() );
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
)
77 Path
*path
= new Path( item
->parentItem(), 0);
85 ppath
= qgraphicsitem_cast
<Path
*>(item
)->path();
90 ppath
.addRect(qgraphicsitem_cast
<Rect
*>(item
)->rect());
95 ppath
.addEllipse(qgraphicsitem_cast
<Ellipse
*>(item
)->rect());
100 QGraphicsItem
*data
= qgraphicsitem_cast
<Proxy
*>(item
)->item();
101 // data->setPos(item->scenePos());
102 qgraphicsitem_cast
<Proxy
*>(item
)->setItem(convertToPath(data
));
105 return convertToPath(data
);
110 QLineF line
= qgraphicsitem_cast
<Line
*>(item
)->line();
111 ppath
.moveTo(line
.p1());
112 ppath
.lineTo(line
.p2());
117 dWarning() << "Converter::convertToPath no support groups";
124 dWarning() << "Converter::convertToPath use default";
125 ppath
= item
->shape(); // TODO
130 path
->setPath(ppath
);
132 Converter::copyProperties( item
, path
);
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() )
151 ellipse
->setRect(qgraphicsitem_cast
<QGraphicsPathItem
*>(item
)->path().boundingRect());
156 ellipse
->setRect(qgraphicsitem_cast
<QGraphicsEllipseItem
*>(item
)->rect());
162 Converter::copyProperties( item
, ellipse
);
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() )
181 rect
->setRect(qgraphicsitem_cast
<QGraphicsPathItem
*>(item
)->path().boundingRect());
186 rect
->setRect(qgraphicsitem_cast
<QGraphicsEllipseItem
*>(item
)->rect());
192 Converter::copyProperties( item
, rect
);
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() )
209 QRectF rect
= qgraphicsitem_cast
<QGraphicsPathItem
*>(item
)->path().boundingRect();
210 line
->setLine(QLineF(rect
.topLeft(), rect
.bottomRight()));
215 QRectF rect
= qgraphicsitem_cast
<QGraphicsEllipseItem
*>(item
)->rect();
216 line
->setLine(QLineF(rect
.topLeft(), rect
.bottomRight()));
221 Converter::copyProperties( item
, line
);
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
)
236 case Item::Path::Type
: // Path
238 return Converter::convertToPath( item
);
241 case Item::Rect::Type
: // Rect
243 Item::Rect
*rect
= Converter::convertToRect( item
);
248 case Item::Ellipse::Type
: // Ellipse
250 Item::Ellipse
*ellipse
= Converter::convertToEllipse( item
);
254 case Item::Proxy::Type
:
256 return new Item::Proxy( item
);
259 case Item::Line::Type
:
261 return Converter::convertToLine( item
);
266 dWarning() << "unknown item " << toType
;