From 2bd2b4bf6fc8cd68a4effa43fc6f5e209beab95c Mon Sep 17 00:00:00 2001 From: Jorge Cuadrado Date: Sat, 22 Sep 2007 18:51:18 -0500 Subject: [PATCH] Implemented Item::Polygon --- examples/paintarea/main.cpp | 15 ++- tests/model/libraryobject/test_libraryobject.cpp | 2 +- yamf/item/item.pro | 6 +- yamf/item/polygon.cpp | 113 +++++++++++++++++++++++ yamf/item/polygon.h | 65 +++++++++++++ 5 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 yamf/item/polygon.cpp create mode 100644 yamf/item/polygon.h diff --git a/examples/paintarea/main.cpp b/examples/paintarea/main.cpp index 1d45987..b3463af 100644 --- a/examples/paintarea/main.cpp +++ b/examples/paintarea/main.cpp @@ -21,6 +21,7 @@ #include #include +#include #include @@ -46,6 +47,18 @@ int main(int argc, char** argv) YAMF::Item::Button *button = new YAMF::Item::Button; frm->addItem(button); + YAMF::Item::Polygon *polygon = new YAMF::Item::Polygon(6, QRect(0, 0, 100, 100)); + polygon->setPos(10,10); + frm->addItem(polygon); + polygon->setBrush(Qt::green); + + polygon = new YAMF::Item::Polygon(5, QRect(0, 0, 100, 100)); + polygon->setPos(100,100); + frm->addItem(polygon); + + polygon->setBrush(Qt::red); + + /* for(int i = 0; i < 50; i++ ) { YAMF::Item::Rect *rect = new YAMF::Item::Rect; @@ -59,7 +72,7 @@ int main(int argc, char** argv) frm->addItem(rect); } - + */ paintarea->setDragMode(QGraphicsView::RubberBandDrag); view->show(); diff --git a/tests/model/libraryobject/test_libraryobject.cpp b/tests/model/libraryobject/test_libraryobject.cpp index 3976595..6e3c3fa 100644 --- a/tests/model/libraryobject/test_libraryobject.cpp +++ b/tests/model/libraryobject/test_libraryobject.cpp @@ -22,7 +22,7 @@ class TestLibraryObject: public QObject void TestLibraryObject::initTestCase() { - libraryObject = new YAMF::Model::LibraryObject(); + libraryObject = new YAMF::Model::LibraryObject(0); } void TestLibraryObject::cleanupTestCase() diff --git a/yamf/item/item.pro b/yamf/item/item.pro index e279baf..8545198 100644 --- a/yamf/item/item.pro +++ b/yamf/item/item.pro @@ -20,7 +20,8 @@ tweener.cpp \ tweenerstep.cpp \ private/svg2qt.cpp \ converter.cpp \ -proxy.cpp +proxy.cpp \ +polygon.cpp HEADERS += builder.h \ button.h \ ellipse.h \ @@ -35,7 +36,8 @@ tweener.h \ tweenerstep.h \ private/svg2qt.h \ converter.h \ -proxy.h +proxy.h \ +polygon.h include(../../config.pri) TARGET = yamf_item diff --git a/yamf/item/polygon.cpp b/yamf/item/polygon.cpp new file mode 100644 index 0000000..06dd91b --- /dev/null +++ b/yamf/item/polygon.cpp @@ -0,0 +1,113 @@ +/*************************************************************************** + * Copyright (C) 2007 by Jorge Cuadrado * + * kuadrosxx@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "polygon.h" +#include + +namespace YAMF { + +namespace Item { + +struct Polygon::Private +{ + Private(int corners) : corners(corners) {}; + + int corners; + QRectF rect; + + QPainterPath polygon; + +}; + +Polygon::Polygon(int corners, QGraphicsItem * parent, QGraphicsScene * scene): QAbstractGraphicsShapeItem(parent, scene), d(new Private(corners)) +{ + +} + +Polygon::Polygon(int corners, const QRectF & rect, QGraphicsItem * parent, QGraphicsScene * scene): QAbstractGraphicsShapeItem(parent, scene), d(new Private(corners)) +{ + setRect(rect); +} + +Polygon::~Polygon() +{ + delete d; +} + + +void Polygon::fromXml(const QString &xml) +{ + +} + +QDomElement Polygon::toXml(QDomDocument &doc) const +{ + +} + +QRectF Polygon::boundingRect() const +{ + return d->rect; +} + +void Polygon::paint( QPainter * painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + painter->save(); + + + painter->setPen(pen()); + painter->setBrush(brush()); + + painter->drawPath(d->polygon); + painter->restore(); +} + + + +void Polygon::setRect(const QRectF& rect) +{ + d->rect = rect; + QPainterPath ellipse; + + ellipse.addEllipse( sceneBoundingRect() ); + + d->polygon = QPainterPath(); + + d->polygon.moveTo(ellipse.pointAtPercent(0)); + + for(double i = 1.0/d->corners; i <= 1; i += 1.0/d->corners) + { + d->polygon.lineTo(ellipse.pointAtPercent(i)); + } +} + +QRectF Polygon::rect() const +{ + return d->rect; +} + +QPainterPath Polygon::shape () const +{ + return d->polygon; +} + +} + +} diff --git a/yamf/item/polygon.h b/yamf/item/polygon.h new file mode 100644 index 0000000..c540eed --- /dev/null +++ b/yamf/item/polygon.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (C) 2007 by Jorge Cuadrado * + * kuadrosxx@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef YAMF_ITEMPOLYGON_H +#define YAMF_ITEMPOLYGON_H + +#include + +#include +#include + +namespace YAMF { + +namespace Item { + +/** + * @author Jorge Cuadrado +*/ +class YAMF_EXPORT Polygon : public Common::AbstractSerializable, public QAbstractGraphicsShapeItem +{ + public: + Polygon(int corners, QGraphicsItem * parent = 0, QGraphicsScene * scene = 0); + Polygon(int corners, const QRectF & rect, QGraphicsItem * parent = 0, QGraphicsScene * scene = 0); + + ~Polygon(); + + virtual void fromXml(const QString &xml); + virtual QDomElement toXml(QDomDocument &doc) const; + + QRectF boundingRect () const; + void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ); + + void setRect(const QRectF& rect); + QRectF rect() const; + + QPainterPath shape () const; + + private: + struct Private; + Private *const d; + +}; + +} + +} + +#endif -- 2.11.4.GIT