diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/DiagramPlugin.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/DiagramPlugin.cpp new file mode 100644 index 000000000..cb5f2cbe2 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/DiagramPlugin.cpp @@ -0,0 +1,33 @@ +//#include +//#include +//#include + +#include "DiagramPlugin.h" +#include "mainwindow.h" + +QString +DiagramPlugin::pluginDescription() const +{ + QString res; + res = "A qdiagram plugin" ; + + return res; +} + +QString +DiagramPlugin::pluginName() const +{ + return "QDiagram" ; +} + +QWidget* +DiagramPlugin::pluginWidget(QWidget * parent ) +{ + MainWindow* window = new MainWindow(parent); + //window->openImage(":/images/example.jpg"); + + return window; +} + + +Q_EXPORT_PLUGIN2(qdiagram_plugin, DiagramPlugin) diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/DiagramPlugin.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/DiagramPlugin.h new file mode 100644 index 000000000..c6d9e7875 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/DiagramPlugin.h @@ -0,0 +1,27 @@ +#ifndef _HWA_PLUGIN_H_ +#define _HWA_PLUGIN_H_ + +#include + +#include +#include + +#include + +#include + +class DiagramPlugin: public QObject, public PluginInterface +{ + Q_OBJECT + Q_INTERFACES(PluginInterface) + + public slots: + + virtual QString pluginDescription() const ; + virtual QString pluginName() const ; + + virtual QWidget* pluginWidget(QWidget * parent = 0) ; + +}; + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramdrawitem.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramdrawitem.cpp new file mode 100644 index 000000000..68abd20e8 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramdrawitem.cpp @@ -0,0 +1,374 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include +#include + +#include "diagramdrawitem.h" +#include "diagramscene.h" + +//! [0] +DiagramDrawItem::DiagramDrawItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene) + : DiagramItem(contextMenu,parent,scene) +{ + myPos2=pos(); + myDiagramType = diagramType; + myContextMenu = contextMenu; + + myPolygon=createPath(); + setPolygon(myPolygon); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setAcceptHoverEvents(true); + myHoverPoint=-1; + mySelPoint=-1; + myHandlerWidth=2.0; +} +//! [0] +DiagramDrawItem::DiagramDrawItem(const DiagramDrawItem& diagram) + : DiagramItem(diagram.myContextMenu,diagram.parentItem(),0) +{ + + myDiagramType=diagram.myDiagramType; + // copy from general GraphcsItem + setBrush(diagram.brush()); + setPen(diagram.pen()); + setTransform(diagram.transform()); + myPos2=diagram.myPos2; + myPolygon=createPath(); + setPolygon(myPolygon); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setAcceptHoverEvents(true); + myHoverPoint=-1; + mySelPoint=-1; + myHandlerWidth=2.0; + +} +//! [1] +QPolygonF DiagramDrawItem::createPath() +{ + qreal dx=myPos2.x(); + qreal dy=myPos2.y(); + + QPainterPath path; + QPolygonF polygon; + switch (myDiagramType) { + case Rectangle: + path.moveTo(0, 0); + path.lineTo(dx,0); + path.lineTo(dx,dy); + path.lineTo(0,dy); + path.lineTo(0,0); + polygon = path.toFillPolygon(); + break; + case Ellipse: + path.addEllipse(0,0,dx,dy); + polygon = path.toFillPolygon(); + break; + default: + break; + polygon = 0; + } + return polygon; +} +//! [4] +QPixmap DiagramDrawItem::image() const +{ + QPixmap pixmap(250, 250); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + painter.setPen(QPen(Qt::black, 8)); + painter.translate(10, 10); + painter.drawPolyline(myPolygon); + + return pixmap; +} +//! [4] + +//! [5] +void DiagramDrawItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) +{ + scene()->clearSelection(); + setSelected(true); + myContextMenu->exec(event->screenPos()); +} +//! [5] + +//! [6] +QVariant DiagramDrawItem::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + if (change == QGraphicsItem::ItemPositionChange) { + ; + } + + return value; +} +//! [6] +DiagramItem* DiagramDrawItem::copy() +{ + DiagramDrawItem* newDiagramDrawItem=new DiagramDrawItem(*this); + return dynamic_cast(newDiagramDrawItem); +} + +void DiagramDrawItem::setPos2(qreal x,qreal y) +{ + myPos2=mapFromScene(QPointF(x,y)); + myPolygon=createPath(); + setPolygon(myPolygon); +} + +void DiagramDrawItem::setPos2(QPointF newPos) +{ + prepareGeometryChange(); + myPos2=mapFromScene(newPos); + myPolygon=createPath(); + setPolygon(myPolygon); +} + +void DiagramDrawItem::setDimension(QPointF newPos) +{ + prepareGeometryChange(); + myPos2=newPos; + myPolygon=createPath(); + setPolygon(myPolygon); +} + +QPointF DiagramDrawItem::getDimension() +{ + return myPos2; +} + +void DiagramDrawItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, + QWidget *) +{ + painter->setPen(pen()); + painter->setBrush(brush()); + painter->drawPolygon(polygon()); + // selected + if(isSelected()){ + // Rect + QPen selPen=QPen(Qt::DashLine); + selPen.setColor(Qt::black); + QBrush selBrush=QBrush(Qt::NoBrush); + painter->setBrush(selBrush); + painter->setPen(selPen); + painter->drawRect(QRectF(QPointF(0,0),myPos2)); + // Draghandles + selBrush=QBrush(Qt::cyan,Qt::SolidPattern); + selPen=QPen(Qt::cyan); + painter->setBrush(selBrush); + painter->setPen(selPen); + QPointF point; + for(int i=0;i<8;i++) + { + if(i<3) point=QPointF(myPos2.x()/2*i,0); + if(i==3) point=QPointF(myPos2.x(),myPos2.y()/2); + if(i>3 && i<7) point=QPointF(myPos2.x()/2*(i-4),myPos2.y()); + if(i==7) point=QPointF(0,myPos2.y()/2); + if(i==myHoverPoint){ + painter->setBrush(QBrush(Qt::red)); + } + // Rect around valid point + painter->drawRect(QRectF(point-QPointF(2,2),point+QPointF(2,2))); + if(i==myHoverPoint){ + painter->setBrush(selBrush); + } + }// foreach + }// if +} + +void DiagramDrawItem::hoverMoveEvent(QGraphicsSceneHoverEvent *e) { +#ifdef DEBUG + std::cout << "entered" << std::endl; + std::cout << e->pos().x() << "/" << e->pos().y() << std::endl; +#endif + if (isSelected()) { + QPointF hover_point = e -> pos(); + QPointF point; + for(myHoverPoint=0;myHoverPoint<8;myHoverPoint++){ + if(myHoverPoint<3) point=QPointF(myPos2.x()/2*myHoverPoint,0); + if(myHoverPoint==3) point=QPointF(myPos2.x(),myPos2.y()/2); + if(myHoverPoint>3 && myHoverPoint<7) point=QPointF(myPos2.x()/2*(myHoverPoint-4),myPos2.y()); + if(myHoverPoint==7) point=QPointF(0,myPos2.y()/2); + if(hasClickedOn(hover_point,point)) break; + }//for + if(myHoverPoint==8) myHoverPoint=-1; + else update(); + } + DiagramItem::hoverEnterEvent(e); +} + +void DiagramDrawItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) { +#ifdef DEBUG + std::cout << "left" << std::endl; +#endif + if (isSelected()) { + if(myHoverPoint>-1){ + myHoverPoint=-1; + update(); + } + } + DiagramItem::hoverLeaveEvent(e); +} + +bool DiagramDrawItem::hasClickedOn(QPointF press_point, QPointF point) const { + return ( + press_point.x() >= point.x() - myHandlerWidth &&\ + press_point.x() < point.x() + myHandlerWidth &&\ + press_point.y() >= point.y() - myHandlerWidth &&\ + press_point.y() < point.y() + myHandlerWidth + ); +} + +QPointF DiagramDrawItem::onGrid(QPointF pos) +{ + DiagramScene* myScene = dynamic_cast(scene()); + QPointF result = myScene->onGrid(pos); + return result; +} + +QPainterPath DiagramDrawItem::shape() const { + QPainterPath myPath; + myPath.addPolygon(polygon()); + if(isSelected()){ + QPointF point; + for(int i=0;i<8;i++) + { + if(i<3) point=QPointF(myPos2.x()/2*i,0); + if(i==3) point=QPointF(myPos2.x(),myPos2.y()/2); + if(i>3 && i<7) point=QPointF(myPos2.x()/2*(i-4),myPos2.y()); + if(i==7) point=QPointF(0,myPos2.y()/2); + // Rect around valid point + myPath.addRect(QRectF(point-QPointF(myHandlerWidth,myHandlerWidth),point+QPointF(myHandlerWidth,myHandlerWidth))); + }// for + }// if + return myPath; +} + +QRectF DiagramDrawItem::boundingRect() const +{ + qreal extra = pen().width()+20 / 2.0 + myHandlerWidth; + qreal minx = myPos2.x() < 0 ? myPos2.x() : 0; + qreal maxx = myPos2.x() < 0 ? 0 : myPos2.x() ; + qreal miny = myPos2.y() < 0 ? myPos2.y() : 0; + qreal maxy = myPos2.y() < 0 ? 0 : myPos2.y() ; + + QRectF newRect = QRectF(minx,miny,maxx-minx,maxy-miny) + .adjusted(-extra, -extra, extra, extra); + return newRect; +} + +void DiagramDrawItem::mousePressEvent(QGraphicsSceneMouseEvent *e) { + if(isSelected()){ + if (e -> buttons() & Qt::LeftButton) { + QPointF mouse_point = e -> pos(); + QPointF point; + for(mySelPoint=0;mySelPoint<8;mySelPoint++){ + if(mySelPoint<3) point=QPointF(myPos2.x()/2*mySelPoint,0); + if(mySelPoint==3) point=QPointF(myPos2.x(),myPos2.y()/2); + if(mySelPoint>3 && mySelPoint<7) point=QPointF(myPos2.x()/2*(mySelPoint-4),myPos2.y()); + if(mySelPoint==7) point=QPointF(0,myPos2.y()/2); + if(hasClickedOn(mouse_point,point)) break; + }//for + if(mySelPoint==8) mySelPoint=-1; + else e->accept(); + } + } + DiagramItem::mousePressEvent(e); +} + +void DiagramDrawItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { + // left click + if ((e -> buttons() & Qt::LeftButton)&&(mySelPoint>-1)) { + QPointF mouse_point = onGrid(e -> pos()); +#ifdef DEBUG + std::cout << "Corner: " << mySelPoint << std::endl; + std::cout << "mouse: " << mouse_point.x() << "/" << mouse_point.y() << std::endl; + std::cout << "pos2: " << myPos2.x() << "/" << myPos2.y() << std::endl; +#endif + prepareGeometryChange(); + switch (mySelPoint) { + case 0: + myPos2=myPos2-mouse_point; + setPos(mapToScene(mouse_point)); + break; + case 1: + setPos(pos().x(),mapToScene(mouse_point).y()); + myPos2.setY(myPos2.y()-mouse_point.y()); + break; + case 2: + myPos2.setX(mouse_point.x()); + setPos(pos().x(),mapToScene(mouse_point).y()); + myPos2.setY(myPos2.y()-mouse_point.y()); + break; + case 3: + myPos2.setX(mouse_point.x()); + break; + case 6: + myPos2.setX(mouse_point.x()); + myPos2.setY(mouse_point.y()); + break; + case 5: + myPos2.setY(mouse_point.y()); + break; + case 4: + myPos2.setY(mouse_point.y()); + setPos(mapToScene(mouse_point).x(),pos().y()); + myPos2.setX(myPos2.x()-mouse_point.x()); + break; + case 7: + setPos(mapToScene(mouse_point).x(),pos().y()); + myPos2.setX(myPos2.x()-mouse_point.x()); + break; + default: + break; + } + myPolygon=createPath(); + setPolygon(myPolygon); + } + else + DiagramItem::mouseMoveEvent(e); +} diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramdrawitem.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramdrawitem.h new file mode 100644 index 000000000..406b7d216 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramdrawitem.h @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef DiagramDrawItem_H +#define DiagramDrawItem_H + +#include +#include +#include "diagramitem.h" + +QT_BEGIN_NAMESPACE +class QPixmap; +class QGraphicsItem; +class QGraphicsScene; +class QTextEdit; +class QGraphicsSceneMouseEvent; +class QMenu; +class QGraphicsSceneContextMenuEvent; +class QPainter; +class QStyleOptionGraphicsItem; +class QWidget; +class QPolygonF; +QT_END_NAMESPACE + +//! [0] +class DiagramDrawItem : public DiagramItem +{ +public: + enum { Type = UserType + 16 }; + enum DiagramType { Ellipse, Rectangle }; + + DiagramDrawItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + DiagramDrawItem(const DiagramDrawItem& diagram);//copy constructor + + DiagramItem* copy(); + + DiagramType diagramType() const + { return myDiagramType; } + QPolygonF polygon() const + { return myPolygon; } + QPixmap image() const; + int type() const + { return Type;} + + void setPos2(qreal x,qreal y); + void setPos2(QPointF pos); + QPointF getPos2() const + { return mapToScene(myPos2); } + + void setDimension(QPointF newPos); + QPointF getDimension(); + + +protected: + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + QPolygonF createPath(); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *); + QPainterPath shape() const; + QRectF boundingRect() const; + void hoverMoveEvent(QGraphicsSceneHoverEvent *e); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *e); + void mousePressEvent(QGraphicsSceneMouseEvent *e); + void mouseMoveEvent(QGraphicsSceneMouseEvent *e); + bool hasClickedOn(QPointF press_point, QPointF point) const ; + QPointF onGrid(QPointF pos); + +private: + DiagramType myDiagramType; + QPolygonF myPolygon; + QMenu *myContextMenu; + QPointF myPos2; + int myHoverPoint,mySelPoint; + qreal myHandlerWidth; +}; +//! [0] + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramitem.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramitem.cpp new file mode 100644 index 000000000..daf142c25 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramitem.cpp @@ -0,0 +1,183 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include + +#include "diagramitem.h" + +//! [0] +DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsPolygonItem(parent, scene) +{ + myDiagramType = diagramType; + myContextMenu = contextMenu; + + QPainterPath path; + switch (myDiagramType) { + case StartEnd: + path.moveTo(200, 50); + path.arcTo(150, 0, 50, 50, 0, 90); + path.arcTo(50, 0, 50, 50, 90, 90); + path.arcTo(50, 50, 50, 50, 180, 90); + path.arcTo(150, 50, 50, 50, 270, 90); + path.lineTo(200, 25); + myPolygon = path.toFillPolygon(); + break; + case Conditional: + myPolygon << QPointF(-100, 0) << QPointF(0, 100) + << QPointF(100, 0) << QPointF(0, -100) + << QPointF(-100, 0); + break; + case Step: + myPolygon << QPointF(-100, -100) << QPointF(100, -100) + << QPointF(100, 100) << QPointF(-100, 100) + << QPointF(-100, -100); + break; + default: + myPolygon << QPointF(-120, -80) << QPointF(-70, 80) + << QPointF(120, 80) << QPointF(70, -80) + << QPointF(-120, -80); + break; + } + setPolygon(myPolygon); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); +} + +DiagramItem::DiagramItem(QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsPolygonItem(parent, scene) +{ + myDiagramType = None; + myContextMenu = contextMenu; + + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); +} +//! [0] +DiagramItem::DiagramItem(const DiagramItem& diagram) +{ + QGraphicsPolygonItem(diagram.parentItem(),diagram.scene()); + //QGraphicsPolygonItem(static_cast(diagram)); + // copy from general GraphcsItem + setBrush(diagram.brush()); + setPen(diagram.pen()); + setTransform(diagram.transform()); + + // copy DiagramItem + myDiagramType = diagram.myDiagramType; + myContextMenu = diagram.myContextMenu; + + QPainterPath path; + switch (myDiagramType) { + case StartEnd: + path.moveTo(200, 50); + path.arcTo(150, 0, 50, 50, 0, 90); + path.arcTo(50, 0, 50, 50, 90, 90); + path.arcTo(50, 50, 50, 50, 180, 90); + path.arcTo(150, 50, 50, 50, 270, 90); + path.lineTo(200, 25); + myPolygon = path.toFillPolygon(); + break; + case Conditional: + myPolygon << QPointF(-100, 0) << QPointF(0, 100) + << QPointF(100, 0) << QPointF(0, -100) + << QPointF(-100, 0); + break; + case Step: + myPolygon << QPointF(-100, -100) << QPointF(100, -100) + << QPointF(100, 100) << QPointF(-100, 100) + << QPointF(-100, -100); + break; + default: + myPolygon << QPointF(-120, -80) << QPointF(-70, 80) + << QPointF(120, 80) << QPointF(70, -80) + << QPointF(-120, -80); + break; + } + setPolygon(myPolygon); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); +} + + +//! [4] +QPixmap DiagramItem::image() const +{ + QPixmap pixmap(250, 250); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + painter.setPen(QPen(Qt::black, 8)); + painter.translate(125, 125); + painter.drawPolyline(myPolygon); + + return pixmap; +} +//! [4] + +//! [5] +void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) +{ + scene()->clearSelection(); + setSelected(true); + myContextMenu->exec(event->screenPos()); +} +//! [5] + +//! [6] +QVariant DiagramItem::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + if (change == QGraphicsItem::ItemPositionChange) { + ; + } + + return value; +} +//! [6] +DiagramItem* DiagramItem::copy() +{ + DiagramItem* newDiagramItem=new DiagramItem(*this); + return newDiagramItem; +} diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramitem.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramitem.h new file mode 100644 index 000000000..3b70aa87d --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramitem.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef DIAGRAMITEM_H +#define DIAGRAMITEM_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QPixmap; +class QGraphicsItem; +class QGraphicsScene; +class QTextEdit; +class QGraphicsSceneMouseEvent; +class QMenu; +class QGraphicsSceneContextMenuEvent; +class QPainter; +class QStyleOptionGraphicsItem; +class QWidget; +class QPolygonF; +QT_END_NAMESPACE + +//! [0] +class DiagramItem : public QGraphicsPolygonItem +{ +public: + enum { Type = UserType + 15 }; + enum DiagramType { Step, Conditional, StartEnd, Io, None }; + + DiagramItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + DiagramItem(QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene);//constructor fuer Vererbung + DiagramItem(const DiagramItem& diagram);//copy constructor + + virtual DiagramItem* copy(); + + DiagramType diagramType() const + { return myDiagramType; } + QPolygonF polygon() const + { return myPolygon; } + QPixmap image() const; + int type() const + { return Type;} + +protected: + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + +private: + DiagramType myDiagramType; + QPolygonF myPolygon; + QMenu *myContextMenu; +}; +//! [0] + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagrampathitem.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagrampathitem.cpp new file mode 100644 index 000000000..404710619 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagrampathitem.cpp @@ -0,0 +1,401 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include +#include + +#include "diagrampathitem.h" +#include "diagramscene.h" + +#define NDEBUG + +//! [0] +DiagramPathItem::DiagramPathItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsPathItem(parent) +{ + myDiagramType = diagramType; + myContextMenu = contextMenu; + myPoints.clear(); + + len = 10.0; // Pfeillänge + breite = 4.0; // Divisor Pfeilbreite + + // standard initialize + mySelPoint=-1; + myHandlerWidth = 2.0; + myHoverPoint=-1; + + setBrush(QBrush(Qt::black)); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, false); + setAcceptHoverEvents(true); +} + +DiagramPathItem::DiagramPathItem(QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsPathItem(parent) +{ + myDiagramType = Path; + myContextMenu = contextMenu; + myPoints.clear(); + + len = 10.0; // Pfeillänge + breite = 4.0; // Divisor Pfeilbreite + + // standard initialize + mySelPoint=-1; + myHandlerWidth = 2.0; + myHoverPoint=-1; + + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setAcceptHoverEvents(true); +} +//! [0] +DiagramPathItem::DiagramPathItem(const DiagramPathItem& diagram) +{ + QGraphicsPathItem(diagram.parentItem(),diagram.scene()); + + // copy from general GraphcsItem + setBrush(diagram.brush()); + setPen(diagram.pen()); + setTransform(diagram.transform()); + + // copy DiagramPathItem + myDiagramType = diagram.myDiagramType; + myContextMenu = diagram.myContextMenu; + myPoints = diagram.myPoints; + + len = diagram.len; + + setPath(diagram.path()); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setAcceptHoverEvents(true); + + // standard initialize + mySelPoint=-1; + myHandlerWidth = 2.0; + myHoverPoint=-1; +} + +void DiagramPathItem::createPath() +{ + QPainterPath myPath=getPath(); + if(myPath.elementCount()>0) setPath(myPath); +} + +QPainterPath DiagramPathItem::getPath() const +{ + QPainterPath myPath; + QPointF p1,p2; + if(myPoints.size()>1) + { + for (int i = 1; i < myPoints.size(); ++i) { + p1=myPoints.at(i-1); + p2=myPoints.at(i); + if( (i==1)&&((myDiagramType==Start) || (myDiagramType==StartEnd)) ) + { + QPainterPath arrow = createArrow(p2,p1); + myPath.addPath(arrow); + } + + myPath.moveTo(p2); + myPath.lineTo(p1); + myPath.closeSubpath(); + } + if((myDiagramType==End) or (myDiagramType==StartEnd)){ + QPainterPath arrow = createArrow(p1,p2); + myPath.addPath(arrow); + } + } + return myPath; +} + +QPainterPath DiagramPathItem::createArrow(QPointF p1, QPointF p2) const +{ +#define pi 3.141592654 + QPainterPath arrow; + qreal dx=p1.x()-p2.x(); + qreal dy=p1.y()-p2.y(); + qreal m=sqrt(dx*dx+dy*dy); + if(m>1){ + arrow.moveTo(p2); + arrow.lineTo(-len/breite*dy/m+len*dx/m+p2.x(),len/breite*dx/m+len*dy/m+p2.y()); + arrow.lineTo(len/breite*dy/m+len*dx/m+p2.x(),-len/breite*dx/m+len*dy/m+p2.y()); + arrow.closeSubpath(); + } + return arrow; +} + +//! [4] +QPixmap DiagramPathItem::image() const +{ + QPixmap pixmap(250, 250); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + painter.setPen(QPen(Qt::black, 8)); + painter.translate(125, 125); + QPolygonF myPolygon; + myPolygon << QPointF(-100,-100) << QPointF(0,-100) << QPointF(0,100) << QPointF(100,100) ;; + painter.drawPolyline(myPolygon); + return pixmap; +} +//! [4] +QPixmap DiagramPathItem::icon() +{ + QPixmap pixmap(50, 80); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + painter.setPen(QPen(Qt::black, 8)); + myPoints.clear(); + myPoints.append(QPointF(5,40)); + myPoints.append(QPointF(45,40)); + len=10.0; + breite=1.0; + painter.drawPath(getPath()); + return pixmap; +} + + +//! [5] +void DiagramPathItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) +{ + scene()->clearSelection(); + setSelected(true); + myContextMenu->exec(event->screenPos()); +} +//! [5] + +//! [6] +void DiagramPathItem::append(const QPointF point) +{ + if(myPoints.size()>1) + { + prepareGeometryChange(); + updateLast(point); + myPoints.append(mapFromScene(point)); + } + else + { + /*myPoints.append(point-pos()); + myPoints.append(point-pos());*/ + myPoints.append(mapFromScene(point)); + myPoints.append(mapFromScene(point)); + createPath(); + } +} + +void DiagramPathItem::remove() +{ + if(myPoints.size()>1) + { + prepareGeometryChange(); + myPoints.removeLast(); + updateLast(mapToScene(myPoints.last())); + } +} + +void DiagramPathItem::updateLast(const QPointF point) +{ + int i = myPoints.size()-1; + if (i>0){ + prepareGeometryChange(); + myPoints[i]=mapFromScene(point); + createPath(); + } +} + +QVariant DiagramPathItem::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + if (change == QGraphicsItem::ItemPositionChange) { + //foreach (Arrow *arrow, arrows) { + // arrow->updatePosition(); + //} + } + + return value; +} +//! [6] +DiagramPathItem* DiagramPathItem::copy() +{ + DiagramPathItem* newDiagramPathItem=new DiagramPathItem(*this); + return newDiagramPathItem; +} + +void DiagramPathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, + QWidget *) +{ + painter->setPen(pen()); + painter->setBrush(brush()); + painter->drawPath(getPath()); + // selected + if(isSelected()){ + QBrush selBrush=QBrush(Qt::cyan); + QPen selPen=QPen(Qt::cyan); + painter->setBrush(selBrush); + painter->setPen(selPen); + QPointF point; + for(int i=0;isetBrush(QBrush(Qt::red)); + } + // Rect around valid point + painter->drawRect(QRectF(point-QPointF(2,2),point+QPointF(2,2))); + if(i==myHoverPoint){ + painter->setBrush(selBrush); + } + }// foreach + }// if +} + +QRectF DiagramPathItem::boundingRect() const +{ + qreal extra = (pen().width() + 20) / 2.0; + + qreal minx,maxx,miny,maxy; + bool first=true; + foreach(QPointF point,myPoints){ + if(first){ + minx=point.x(); + miny=point.y(); + maxx=point.x(); + maxy=point.y(); + first=false; + } + else{ + if(point.x()maxx) maxx=point.x(); + if(point.y()maxy) maxy=point.y(); + } + + } + + return QRectF(minx,miny,maxx-minx,maxy-miny) + .adjusted(-extra, -extra, extra, extra); +} + +void DiagramPathItem::mousePressEvent(QGraphicsSceneMouseEvent *e) { + if(isSelected()){ + if (e -> buttons() & Qt::LeftButton) { + QPointF mouse_point = onGrid(e -> pos()); + for(mySelPoint=0;mySelPointaccept(); + } + } + QGraphicsPathItem::mousePressEvent(e); +} + +void DiagramPathItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { + // left click + if ((e -> buttons() & Qt::LeftButton)&&(mySelPoint>-1)) { + QPointF mouse_point = onGrid(e -> pos()); + myPoints.replace(mySelPoint,onGrid(mouse_point)); + createPath(); + } +} + +QPainterPath DiagramPathItem::shape() const { + QPainterPath myPath = getPath(); + if(isSelected()){ + foreach (QPointF point, myPoints) + { + // Rect around valid point + myPath.addRect(QRectF(point-QPointF(myHandlerWidth,myHandlerWidth),point+QPointF(myHandlerWidth,myHandlerWidth))); + }// foreach + }// if + return myPath; +} + +bool DiagramPathItem::hasClickedOn(QPointF press_point, QPointF point) const { + return ( + press_point.x() >= point.x() - myHandlerWidth &&\ + press_point.x() < point.x() + myHandlerWidth &&\ + press_point.y() >= point.y() - myHandlerWidth &&\ + press_point.y() < point.y() + myHandlerWidth + ); +} + +QPointF DiagramPathItem::onGrid(QPointF pos) +{ + DiagramScene* myScene = dynamic_cast(scene()); + QPointF result = myScene->onGrid(pos); + return result; +} + +void DiagramPathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e) { +#ifdef DEBUG + std::cout << "entered" << std::endl; + std::cout << e->pos().x() << "/" << e->pos().y() << std::endl; +#endif + if (isSelected()) { + QPointF hover_point = onGrid(e -> pos()); + for(myHoverPoint=0;myHoverPoint-1){ + myHoverPoint=-1; + update(); + } + } + QGraphicsPathItem::hoverLeaveEvent(e); +} diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagrampathitem.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagrampathitem.h new file mode 100644 index 000000000..d4f057fac --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagrampathitem.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef DIAGRAMPATHITEM_H +#define DIAGRAMPATHITEM_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QPixmap; +class QGraphicsItem; +class QGraphicsScene; +class QTextEdit; +class QGraphicsSceneMouseEvent; +class QMenu; +class QGraphicsSceneContextMenuEvent; +class QPainter; +class QStyleOptionGraphicsItem; +class QWidget; +class QPolygonF; +QT_END_NAMESPACE + +//! [0] +class DiagramPathItem : public QGraphicsPathItem +{ +public: + enum { Type = UserType + 6 }; + enum DiagramType { Path, Start, End, StartEnd }; + + DiagramPathItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + DiagramPathItem(QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene);//constructor fuer Vererbung + DiagramPathItem(const DiagramPathItem& diagram);//copy constructor + + virtual DiagramPathItem* copy(); + + void append(const QPointF point); + void remove(); + void updateLast(const QPointF point); + + DiagramType diagramType() const + { return myDiagramType; } + + virtual void setDiagramType(DiagramType type) + { myDiagramType=type; } + + QPixmap image() const; + QPixmap icon(); + QPainterPath getPath() const; + QList getPoints() + { return myPoints; } + int type() const + { return Type;} + void setHandlerWidth(const qreal width) + { + myHandlerWidth = width; + } + +protected: + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + void createPath(); + QPainterPath createArrow(QPointF p1, QPointF p2) const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *); + QRectF boundingRect() const; + QPainterPath shape() const; + bool hasClickedOn(QPointF press_point, QPointF point) const; + void mousePressEvent(QGraphicsSceneMouseEvent *e); + void mouseMoveEvent(QGraphicsSceneMouseEvent *e); + void hoverEnterEvent(QGraphicsSceneHoverEvent *e); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *e); + QPointF onGrid(QPointF pos); + +private: + DiagramType myDiagramType; + QMenu *myContextMenu; + QList myPoints; + qreal len,breite; + int mySelPoint,myHoverPoint; + qreal myHandlerWidth; + +}; +//! [0] + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramscene.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramscene.cpp new file mode 100644 index 000000000..09aa738ed --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramscene.cpp @@ -0,0 +1,1246 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include +#include + +#include "diagramscene.h" + +#define NDEBUG + +//! [0] +DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent) + : QGraphicsScene(parent) +{ + myItemMenu = itemMenu; + myMode = MoveItem; + myItemType = DiagramItem::Step; + line = 0; + textItem = 0; + insertedItem = 0; + insertedDrawItem = 0; + insertedPathItem = 0; + copiedItems = 0; + myDx=0.0; + myDy=0.0; + maxZ=0; + myItemColor = Qt::white; + myTextColor = Qt::black; + myLineColor = Qt::black; + myArrow=DiagramPathItem::Path; + myGrid=10.0; + myGridVisible=false; + myGridScale=1; + // no Item in Moveitems + myMoveItems.clear(); + // initialisiere Cursor + myCursorWidth = 4.0; + myCursor.setRect(QRectF(-myCursorWidth/2,-myCursorWidth/2,myCursorWidth,myCursorWidth)); + myCursor.setPen(QPen(Qt::gray)); + myCursor.setZValue(10.0); + addItem(&myCursor); +} +//! [0] + +//! [1] +void DiagramScene::setLineColor(const QColor &color) +{ + myLineColor = color; + if(insertedPathItem!=0) insertedPathItem->setPen(myLineColor); + if(insertedItem!=0) insertedItem->setPen(myLineColor); + if(insertedDrawItem!=0) insertedDrawItem->setPen(myLineColor); + if (!selectedItems().empty()){ + foreach(QGraphicsItem* item,selectedItems()){ + switch(item->type()){ + case QGraphicsItem::UserType+3: + // Textitem does not possess Linecolor ! + break; + case QGraphicsItem::UserType+6: + qgraphicsitem_cast(item)->setPen(myLineColor); + qgraphicsitem_cast(item)->setBrush(myLineColor); + break; + default: + dynamic_cast(item)->setPen(myLineColor); + break; + } + } + } +} +//! [1] + +//! [2] +void DiagramScene::setTextColor(const QColor &color) +{ + myTextColor = color; + if (isItemChange(DiagramTextItem::Type)) { + DiagramTextItem *item = + qgraphicsitem_cast(selectedItems().first()); + item->setDefaultTextColor(myTextColor); + } +} +//! [2] + +//! [3] +void DiagramScene::setItemColor(const QColor &color) +{ + myItemColor = color; + if(insertedItem!=0) insertedItem->setBrush(myItemColor); + if(insertedDrawItem!=0) insertedDrawItem->setBrush(myItemColor); + if (!selectedItems().empty()){ + foreach(QGraphicsItem* item,selectedItems()){ + switch(item->type()){ + case QGraphicsItem::UserType+3: + // Textitem does not possess Linecolor ! + break; + case QGraphicsItem::UserType+6: + // Path does not need Backgroundcolor + break; + default: + dynamic_cast(item)->setBrush(myItemColor); + break; + } + } + } + // old code by Trolltech + /*if (isItemChange(DiagramItem::Type)) { + DiagramItem *item = + qgraphicsitem_cast(selectedItems().first()); + item->setBrush(myItemColor); + }*/ +} +//! [3] + +//! [4] +void DiagramScene::setFont(const QFont &font) +{ + myFont = font; + + if (isItemChange(DiagramTextItem::Type)) { + QGraphicsTextItem *item = + qgraphicsitem_cast(selectedItems().first()); + item->setFont(myFont); + // testing + /* + item->setTextWidth(200.0); + QTextCursor cursor = item->textCursor(); + cursor.select(QTextCursor::Document); + QTextBlockFormat bfmt = cursor.blockFormat(); + bfmt.setAlignment(Qt::AlignHCenter); + cursor.setBlockFormat(bfmt); + item->setTextCursor(cursor); + */ + } +} +//! [4] + +void DiagramScene::setMode(Mode mode,bool m_abort) +{ + if(m_abort) abort(true); + /* + if(myMode==InsertLine){ + if(insertedPathItem!=0){ + insertedPathItem->remove(); + insertedPathItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + insertedPathItem=0; + } + } + */ + myMode = mode; + switch (mode) { + case MoveItem: + enableAllItems(true); + break; + case MoveItems: + enableAllItems(true); + break; + case CopyItem: + enableAllItems(true); + break; + case CopyingItem: + enableAllItems(false); + break; + case Zoom: + enableAllItems(false); + break; + default: + enableAllItems(false); + break; + } +} + +void DiagramScene::enableAllItems(bool enable) +{ + foreach(QGraphicsItem* item,items()){ + item->setEnabled(enable); + } +} + +/*enum DiagramScene::getMode() +{ + return mode; +}*/ + +void DiagramScene::setItemType(DiagramItem::DiagramType type) +{ + myItemType = type; +} +void DiagramScene::setItemType(DiagramDrawItem::DiagramType type) +{ + myDrawItemType = type; +} + + +//! [5] +void DiagramScene::editorLostFocus(DiagramTextItem *item) +{ + //Debug +#ifdef DEBUG + std::cout << "textItem:" << int(textItem) << std::endl; + if(textItem){ + std::cout << "Focus ?:" << textItem->hasFocus() << std::endl; + } +#endif + QTextCursor cursor = item->textCursor(); + cursor.clearSelection(); + item->setTextCursor(cursor); + +#ifdef DEBUG + std::cout << item->toPlainText().toStdString() << std::endl; +#endif + + if (item->toPlainText().isEmpty()) { + removeItem(item); + item->deleteLater(); + } + // avoid double lose focus, which erroneously activates shortcuts for second textitem + if((textItem==item)or(textItem==0)){ + textItem=0; + emit editorHasLostFocus(); + } + +} +void DiagramScene::editorReceivedFocus(DiagramTextItem *item) +{ + emit editorHasReceivedFocus(); +} +//! [5] + +//! [6] +void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ +#ifdef DEBUG + std::cout << "click" << myMode << std::endl; +#endif + if (mouseEvent->button() == Qt::RightButton){ + switch (myMode) { + case InsertItem: + if (insertedItem != 0){ + insertedItem->rotate(90); + } + break; + default: + ; + } + return; + } + + if (mouseEvent->button() == Qt::MidButton){ + switch (myMode) { + case InsertLine: + if (insertedPathItem != 0){ + insertedPathItem->updateLast(onGrid(mouseEvent->scenePos())); + insertedPathItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + insertedPathItem->setEnabled(false); + insertedPathItem = 0; + } + break; + default: + ; + } + return; + } + + if (mouseEvent->button() != Qt::LeftButton) + return; + + //DiagramItem *item; + switch (myMode) { + case InsertItem: + if (insertedItem == 0){ + insertedItem = new DiagramItem(myItemType, myItemMenu); + insertedItem->setBrush(myItemColor); + insertedItem->setPen(myLineColor); + insertedItem->setZValue(maxZ); + maxZ+=0.1; + addItem(insertedItem); + } + insertedItem->setPos(onGrid(mouseEvent->scenePos())); + insertedItem->setEnabled(false); + emit itemInserted(insertedItem); + insertedItem = 0; + + break; +//! [6] //! [7] + case InsertDrawItem: + if (insertedDrawItem == 0){ + insertedDrawItem = new DiagramDrawItem(myDrawItemType, myItemMenu); + insertedDrawItem->setBrush(myItemColor); + insertedDrawItem->setPen(myLineColor); + insertedDrawItem->setZValue(maxZ); + maxZ+=0.1; + addItem(insertedDrawItem); + insertedDrawItem->setPos(onGrid(mouseEvent->scenePos())); + } + else + { + insertedDrawItem->setPos2(onGrid(mouseEvent->scenePos())); + insertedDrawItem->setEnabled(false); + insertedDrawItem = 0; + } + + break; + case MoveItems: + { + QPointF point=onGrid(mouseEvent->scenePos()); + if(!myMoveItems.isEmpty()){ + qreal dx=point.rx()-myDx; + qreal dy=point.ry()-myDy; + foreach(QGraphicsItem* item,myMoveItems){ + if(item->parentItem()!=0){ + if(!item->parentItem()->isSelected()) item->moveBy(-dx,-dy); + } + else { + item->moveBy(dx,dy); + } + } + myMoveItems.clear(); + myMode=MoveItem; + } + else + { + if(!selectedItems().isEmpty()){ + // lösche doppelte Verweise (Child&selected) + myMoveItems=selectedItems(); + foreach(QGraphicsItem* item,myMoveItems){ + if(item->parentItem()) + if(item->parentItem()->isSelected()) { + item->setSelected(false); + myMoveItems.removeOne(item); + } + } + // speichere Referenzpunkt + myDx=point.rx(); + myDy=point.ry(); + } + } + break; + } + case InsertLine: + if (insertedPathItem == 0){ + insertedPathItem = new DiagramPathItem(myArrow,myItemMenu); + insertedPathItem->setPen(myLineColor); + insertedPathItem->setBrush(myLineColor); + insertedPathItem->setZValue(maxZ); + maxZ+=0.1; + addItem(insertedPathItem); + insertedPathItem->setPos(onGrid(mouseEvent->scenePos())); + //insertedPathItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + } + insertedPathItem->append(onGrid(mouseEvent->scenePos())); + break; +//! [7] //! [8] + case InsertText: + emit editorHasReceivedFocus(); + textItem = new DiagramTextItem(); + textItem->setFont(myFont); + textItem->setTextInteractionFlags(Qt::TextEditorInteraction); + textItem->setZValue(maxZ); + maxZ+=0.1; + connect(textItem, SIGNAL(lostFocus(DiagramTextItem *)), + this, SLOT(editorLostFocus(DiagramTextItem *))); + connect(textItem, SIGNAL(receivedFocus(DiagramTextItem *)), + this, SLOT(editorReceivedFocus(DiagramTextItem *))); + connect(textItem, SIGNAL(selectedChange(QGraphicsItem *)), + this, SIGNAL(itemSelected(QGraphicsItem *))); + addItem(textItem); + textItem->setDefaultTextColor(myTextColor); + textItem->setFocus(); + textItem->setCenterPoint(onGrid(mouseEvent->scenePos())); + textItem->activateEditor(); + emit textInserted(textItem); + mouseEvent->setAccepted(true); + break; +//! [8] //! [9] + case CopyItem: + if (!selectedItems().empty()){ + copiedItems=new QList; + copiedItems->clear(); + // lösche doppelte Verweise (Child&selected) + QList myList=selectedItems(); + foreach(QGraphicsItem* item,myList){ + if(item->parentItem()) + if(item->parentItem()->isSelected()) { + item->setSelected(false); + myList.removeOne(item); + } + } + // Vorbereitung copy + QGraphicsItem *insItem; + insItem=myList.first(); + QPointF point=onGrid(mouseEvent->scenePos()); + myDx=insItem->pos().rx()-point.rx(); + myDy=insItem->pos().ry()-point.ry(); + // copy + foreach(QGraphicsItem* item,myList){ + insItem=copy(item); + addItem(insItem); + insItem->setPos(item->pos()); + copiedItems->append(item); + item->setZValue(maxZ); + maxZ+=0.1; + //check for children + if(item->childItems().count()>0){ + foreach(QGraphicsItem* item_l1,item->childItems()){ + QGraphicsItem* addedItem=copy(item_l1); + addItem(addedItem); + addedItem->setParentItem(insItem); + addedItem->setPos(item_l1->pos()); + } + } + //move original to knew position + item->setSelected(true); +#ifdef DEBUG + std::cout<< item->pos().rx()<< ","<pos().ry() << std::endl; +#endif + } + myMode=CopyingItem; + } + break; + case CopyingItem: + if (copiedItems->count() > 0){ + insertedItem=static_cast(copiedItems->first()); + QPointF point=onGrid(mouseEvent->scenePos()); + qreal dx=insertedItem->pos().rx()-point.rx()-myDx; + qreal dy=insertedItem->pos().ry()-point.ry()-myDy; + foreach(QGraphicsItem* item,*copiedItems){ + if(item->parentItem()!=0){ + if(!item->parentItem()->isSelected()) item->moveBy(-dx,-dy); + } + else { + item->moveBy(-dx,-dy); + } + } + clearSelection(); + insertedItem=0; + myDx=0.0; + myDy=0.0; + myMode=MoveItem; + } + break; + default: + ; + } +if(!mouseEvent->isAccepted()) QGraphicsScene::mousePressEvent(mouseEvent); +} +//! [9] + +//! [10] +void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + // move cursor + myCursor.setPos(onGrid(mouseEvent->scenePos())); + + // preview der Zeichnung + switch (myMode){ + case InsertLine: + if (insertedPathItem != 0) { + insertedPathItem->updateLast(onGrid(mouseEvent->scenePos())); + } + break; + case MoveItem: + QGraphicsScene::mouseMoveEvent(mouseEvent); + checkOnGrid(); + break; + case MoveItems: + { + QPointF point=onGrid(mouseEvent->scenePos()); + qreal dx=point.rx()-myDx; + qreal dy=point.ry()-myDy; + foreach(QGraphicsItem* item,myMoveItems){ + if(item->parentItem()!=0){ + if(!item->parentItem()->isSelected()) item->moveBy(dx,dy); + } + else { + item->moveBy(dx,dy); + } + } + myDx=point.rx(); + myDy=point.ry(); + break; + } + case InsertItem: + if (insertedItem == 0){ + insertedItem = new DiagramItem(myItemType, myItemMenu); + insertedItem->setBrush(myItemColor); + insertedItem->setPen(myLineColor); + insertedItem->setSelected(true); + insertedItem->setZValue(maxZ); + maxZ+=0.1; + addItem(insertedItem); + } + insertedItem->setPos(onGrid(mouseEvent->scenePos())); + break; + case InsertDrawItem: + if (insertedDrawItem != 0){ + insertedDrawItem->setPos2(onGrid(mouseEvent->scenePos())); + } + break; + case CopyingItem: + if (copiedItems->count() > 0){ + //copiedItems->setPos(onGrid(mouseEvent->scenePos())); + insertedItem=static_cast(copiedItems->first()); + QPointF point=onGrid(mouseEvent->scenePos()); + qreal dx=insertedItem->pos().rx()-point.rx()-myDx; + qreal dy=insertedItem->pos().ry()-point.ry()-myDy; + foreach(QGraphicsItem* item,*copiedItems){ + if(item->parentItem()!=0){ + if(!item->parentItem()->isSelected()) item->moveBy(-dx,-dy); + } + else { + item->moveBy(-dx,-dy); + } + } + } + break; + + default: + ; + } +} +//! [10] + +//! [11] +void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + if (myMode == Zoom) { + emit zoomRect(mouseEvent->scenePos(),mouseEvent->lastScenePos()); + return; + } +//! [12] //! [13] + line = 0; + QGraphicsScene::mouseReleaseEvent(mouseEvent); +} +//! [13] +void DiagramScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ +#ifdef DEBUG + std::cout << "d click" << myMode << std::endl; +#endif + switch (myMode){ + case InsertLine: + //insertedPathItem->updateLast(onGrid(mouseEvent->scenePos())); + insertedPathItem->remove(); + insertedPathItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + insertedPathItem->setEnabled(false); + insertedPathItem=0; + break; + default: + if(!selectedItems().isEmpty()){ + QGraphicsItem *item = selectedItems().first();//itemAt(mouseEvent->scenePos()); + if(item){ + QList children = item->childItems(); + if (children.count()>0){ + if (QGraphicsTextItem *child = (QGraphicsTextItem*) item->childItems().first()) { +#ifdef DEBUG + std::cout << "edit:" << child->toPlainText().toStdString() << std::endl; +#endif + if (child->textInteractionFlags() == Qt::NoTextInteraction) + child->setTextInteractionFlags(Qt::TextEditorInteraction); + emit editorHasReceivedFocus(); + child->setFocus(); + } + } + else { + if(item->type()!=DiagramTextItem::Type){ + // added + // text wird per Doppelclick eingefuegt + textItem = new DiagramTextItem(); + textItem->setFont(myFont); + textItem->setTextInteractionFlags(Qt::TextEditorInteraction); + textItem->setZValue(1000.0); + connect(textItem, SIGNAL(lostFocus(DiagramTextItem *)), + this, SLOT(editorLostFocus(DiagramTextItem *))); + connect(textItem, SIGNAL(receivedFocus(DiagramTextItem *)), + this, SLOT(editorReceivedFocus(DiagramTextItem *))); + connect(textItem, SIGNAL(selectedChange(QGraphicsItem *)), + this, SIGNAL(itemSelected(QGraphicsItem *))); + addItem(textItem); + textItem->setParentItem(item); + textItem->setDefaultTextColor(myTextColor); + QPointF mPos=QPointF(0,0); + // mPos adaptieren je nach DiaType ... + switch (item->type()) { + case DiagramDrawItem::Type: + mPos+=dynamic_cast(item)->getDimension()/2; + break; + default: + break; + } + //textItem->setPos(mPos); + textItem->setCenterPoint(mPos); + emit editorHasReceivedFocus(); + emit textInserted(textItem); + textItem->setFocus(Qt::OtherFocusReason); + } + else + QGraphicsScene::mouseDoubleClickEvent(mouseEvent); + } + } + } + } +} + +void DiagramScene::wheelEvent(QGraphicsSceneWheelEvent *mouseEvent) +{ + if(mouseEvent->modifiers()==Qt::ControlModifier){ + int i = mouseEvent->delta(); + qreal factor; + if(i>=0){ + factor = i/100.0; + } + else { + factor = -100.0/i; // negative Richtung ... + } + emit zoom(factor); + mouseEvent->setAccepted(true); + return; + } + QGraphicsScene::wheelEvent(mouseEvent); +} + + +//! [14] +void setArrow(const int i) +{ +} + +bool DiagramScene::isItemChange(int type) +{ + foreach (QGraphicsItem *item, selectedItems()) { + if (item->type() == type) + return true; + } + return false; +} +//! [14] +void DiagramScene::checkOnGrid() +{ + foreach (QGraphicsItem *item, selectedItems()) { + + if(item->type()==DiagramTextItem::Type){ + // not item position but center position needs to be on grid + QPointF centerPoint=dynamic_cast(item)->centerPoint(); + std::cout << "rec:" << centerPoint.x() << "/" << centerPoint.y() << std::endl; + qreal x = qRound(centerPoint.x()/10)*10.0; + qreal y = qRound(centerPoint.y()/10)*10.0; + dynamic_cast(item)->setCenterPoint(QPointF(x,y)); + } + else + { + qreal x = qRound(item->x()/10)*10.0; + qreal y = qRound(item->y()/10)*10.0; + item->setPos(x,y); + } + } +} + +QPointF DiagramScene::onGrid(QPointF pos) +{ + qreal x = qRound(pos.x()/myGrid)*myGrid; + qreal y = qRound(pos.y()/myGrid)*myGrid; + QPointF result = QPointF(x,y); + return result; +} + +void DiagramScene::abort(bool keepSelection) +{ + switch(myMode){ + case CopyingItem: + //removeItem(copiedItems); + //destroyItemGroup(copiedItems); + //removeItem(insertedItem); + break; + case InsertItem: + removeItem(insertedItem); + break; + case InsertDrawItem: + removeItem(insertedDrawItem); + break; + case InsertLine: + removeItem(insertedPathItem); + break; + default: + ; + } + + insertedItem=0; + insertedDrawItem=0; + copiedItems=0; + myMode=MoveItem; + if(!keepSelection) clearSelection(); +} + +QGraphicsItem* DiagramScene::copy(QGraphicsItem* item) +{ +#ifdef DEBUG + std::cout<<"copy: " << item->type()<type()){ + case QGraphicsItem::UserType+3: + return qgraphicsitem_cast(qgraphicsitem_cast(item)->copy()); + break; + case QGraphicsItem::UserType+6: + return qgraphicsitem_cast(qgraphicsitem_cast(item)->copy()); + break; + default: + { + DiagramItem* newItem=dynamic_cast(item)->copy(); + return dynamic_cast(newItem); + } + break; + } +} + +void DiagramScene::setArrow(const int i) +{ + myArrow=DiagramPathItem::DiagramType(i); + if(insertedPathItem!=0){ + insertedPathItem->setDiagramType(myArrow); + } + if (!selectedItems().empty()){ + foreach(QGraphicsItem* item,selectedItems()){ + switch(item->type()){ + case QGraphicsItem::UserType+3: + // Textitem does not possess Linecolor ! + break; + case QGraphicsItem::UserType+6: + qgraphicsitem_cast(item)->setDiagramType(myArrow); + break; + default: + // nothing to do + break; + } + } + } + + +} + +bool DiagramScene::event(QEvent *mEvent) +{ + if (mEvent->type()==QEvent::Enter) { + myCursor.setVisible(true); + return true; + } + if (mEvent->type()==QEvent::Leave) { + myCursor.setVisible(false); + return true; + } + return QGraphicsScene::event(mEvent); +} + +bool DiagramScene::save(QFile *file) +{ +#ifdef DEBUG + std::cout << "save..." << std::endl; +#endif + QXmlStreamWriter xmlWriter(file); + xmlWriter.setAutoFormatting(true); + xmlWriter.writeStartDocument(); + xmlWriter.writeComment("File for QDiagram"); + xmlWriter.writeStartElement("doc"); + foreach(QGraphicsItem* item, items()){ + if(item->type()>QGraphicsItem::UserType){ + xmlWriter.writeStartElement("Item"); + xmlWriter.writeAttribute("Type",QString::number(item->type())); + xmlWriter.writeEmptyElement("Pos"); + xmlWriter.writeAttribute("x",QString::number(item->pos().x())); + xmlWriter.writeAttribute("y",QString::number(item->pos().y())); + xmlWriter.writeAttribute("z",QString::number(item->zValue())); + switch (item->type()) { + case QGraphicsItem::UserType+16: + { + DiagramDrawItem *mItem = dynamic_cast(item); + //xmlWriter.writeComment("DiagramDrawItem"); + xmlWriter.writeEmptyElement("DiagramType"); + xmlWriter.writeAttribute("type",QString::number(mItem->diagramType())); + xmlWriter.writeEmptyElement("Dimensions"); + xmlWriter.writeAttribute("width",QString::number(mItem->getDimension().x())); + xmlWriter.writeAttribute("height",QString::number(mItem->getDimension().y())); + xmlWriter.writeEmptyElement("Pen"); + xmlWriter.writeAttribute("color",mItem->pen().color().name()); + xmlWriter.writeAttribute("alpha",QString::number(mItem->pen().color().alpha())); + xmlWriter.writeEmptyElement("Brush"); + xmlWriter.writeAttribute("color",mItem->brush().color().name()); + xmlWriter.writeAttribute("alpha",QString::number(mItem->brush().color().alpha())); + } + break; + case QGraphicsItem::UserType+15: + { + DiagramItem *mItem = dynamic_cast(item); + //xmlWriter.writeComment("DiagramItem"); + xmlWriter.writeEmptyElement("DiagramType"); + xmlWriter.writeAttribute("type",QString::number(mItem->diagramType())); + xmlWriter.writeEmptyElement("Pen"); + xmlWriter.writeAttribute("color",mItem->pen().color().name()); + xmlWriter.writeAttribute("alpha",QString::number(mItem->pen().color().alpha())); + xmlWriter.writeEmptyElement("Brush"); + xmlWriter.writeAttribute("color",mItem->brush().color().name()); + xmlWriter.writeAttribute("alpha",QString::number(mItem->brush().color().alpha())); + } + break; + case QGraphicsItem::UserType+3: + { + DiagramTextItem *mItem = dynamic_cast(item); + //xmlWriter.writeComment("DiagramTextItem"); + xmlWriter.writeEmptyElement("DiagramType"); + xmlWriter.writeAttribute("type","0"); + xmlWriter.writeStartElement("Text"); + xmlWriter.writeCharacters(mItem->toHtml()); + xmlWriter.writeEndElement(); + } + break; + case QGraphicsItem::UserType+6: + { + DiagramPathItem *mItem = dynamic_cast(item); + //xmlWriter.writeComment("DiagramPathItem"); + xmlWriter.writeEmptyElement("DiagramType"); + xmlWriter.writeAttribute("type",QString::number(mItem->diagramType())); + xmlWriter.writeEmptyElement("Pen"); + xmlWriter.writeAttribute("color",mItem->pen().color().name()); + xmlWriter.writeAttribute("alpha",QString::number(mItem->pen().color().alpha())); + xmlWriter.writeEmptyElement("Brush"); + xmlWriter.writeAttribute("color",mItem->brush().color().name()); + xmlWriter.writeAttribute("alpha",QString::number(mItem->brush().color().alpha())); + foreach(QPointF point, mItem->getPoints()){ + xmlWriter.writeEmptyElement("Point"); + xmlWriter.writeAttribute("x",QString::number(point.x())); + xmlWriter.writeAttribute("y",QString::number(point.y())); + } + } + break; + default: + break; + } + xmlWriter.writeEmptyElement("Transform"); + xmlWriter.writeAttribute("m11",QString::number(item->transform().m11())); + xmlWriter.writeAttribute("m12",QString::number(item->transform().m12())); + xmlWriter.writeAttribute("m21",QString::number(item->transform().m21())); + xmlWriter.writeAttribute("m22",QString::number(item->transform().m22())); + xmlWriter.writeAttribute("dx",QString::number(item->transform().dx())); + xmlWriter.writeAttribute("dy",QString::number(item->transform().dy())); + xmlWriter.writeEndElement(); + } + + } + xmlWriter.writeEndElement(); + xmlWriter.writeEndDocument(); + return true; +} +bool DiagramScene::load(QFile *file) +{ +#ifdef DEBUG + std::cout << "Load..." << std::endl; +#endif + QXmlStreamReader xmlReader(file); + int DiaType = 0; + QPointF mPos; + qreal z; + int type; + insertedItem=0; + insertedPathItem=0; + insertedDrawItem=0; + textItem=0; + while(!xmlReader.atEnd()){ + xmlReader.readNext(); +#ifdef DEBUG + std::cout << "name: " << DiaType << ":" << qPrintable(xmlReader.name().toString()) << std::endl; +#endif + if(xmlReader.isStartDocument()) continue; + if(xmlReader.isStartElement()){ + if(xmlReader.name()=="doc") continue; + if(!DiaType and (xmlReader.name()=="Item")){ + bool ok; + DiaType = xmlReader.attributes().value("Type").toString().toInt(&ok); + if(!ok) + { + xmlReader.raiseError(tr("Item: type number conversion failed")); + } + continue; + } + if(DiaType and (xmlReader.name()=="Pos")){ + bool ok,okay; + okay=true; + mPos.setX(xmlReader.attributes().value("x").toString().toDouble(&ok)); + okay&=ok; + mPos.setY(xmlReader.attributes().value("y").toString().toDouble(&ok)); + okay&=ok; + z=xmlReader.attributes().value("z").toString().toDouble(&ok); + okay&=ok; + if(!okay) + { + xmlReader.raiseError(tr("Pos: number conversion failed")); + } + continue; + } + if(DiaType and (xmlReader.name()=="DiagramType")){ + bool ok; + type = xmlReader.attributes().value("type").toString().toInt(&ok); + if(!ok) + { + xmlReader.raiseError(tr("DiagramType: type number conversion failed")); + continue; + } + switch (DiaType) { + case QGraphicsItem::UserType+15: + insertedItem = new DiagramItem(DiagramItem::DiagramType(type),myItemMenu); + addItem(insertedItem); +#ifdef DEBUG + std::cout << "DiagramItem" << std::endl; +#endif + insertedItem->setPos(mPos); + insertedItem->setZValue(z); + break; + case QGraphicsItem::UserType+16: + insertedDrawItem = new DiagramDrawItem(DiagramDrawItem::DiagramType(type),myItemMenu); + addItem(insertedDrawItem); +#ifdef DEBUG + std::cout << "DiagramDrawItem" << std::endl; +#endif + insertedDrawItem->setPos(mPos); + insertedDrawItem->setZValue(z); + break; + case QGraphicsItem::UserType+6: + insertedPathItem = new DiagramPathItem(DiagramPathItem::DiagramType(type),myItemMenu); + addItem(insertedPathItem); +#ifdef DEBUG + std::cout << "DiagramPathItem" << std::endl; +#endif + insertedPathItem->setPos(mPos); + insertedPathItem->setZValue(z); + insertedPathItem->setFlag(QGraphicsItem::ItemIsMovable, true); + insertedPathItem->setFlag(QGraphicsItem::ItemIsSelectable, true); + break; + case QGraphicsItem::UserType+3: + textItem = new DiagramTextItem(); + textItem->setTextInteractionFlags(Qt::TextEditorInteraction); + textItem->setZValue(z); + connect(textItem, SIGNAL(lostFocus(DiagramTextItem *)), + this, SLOT(editorLostFocus(DiagramTextItem *))); + connect(textItem, SIGNAL(receivedFocus(DiagramTextItem *)), + this, SLOT(editorReceivedFocus(DiagramTextItem *))); + connect(textItem, SIGNAL(selectedChange(QGraphicsItem *)), + this, SIGNAL(itemSelected(QGraphicsItem *))); + addItem(textItem); + textItem->setPos(mPos); + break; + default: + break; + } + continue; + } + //DiamgramType nicht gesetzt ? -> Fehler + if(DiaType and !(insertedItem or insertedDrawItem or textItem or insertedPathItem)){ + xmlReader.raiseError(tr("DiagramType definition missing")); + continue; + } + // weitere Properties setzen (Farbe,Punkte,etc.) + if(DiaType and (xmlReader.name()=="Pen")){ + QColor color; + color.setNamedColor(xmlReader.attributes().value("color").toString()); + if(!color.isValid()){ + xmlReader.raiseError(tr("DiagramType: type number conversion failed")); + continue; + } + bool ok; + color.setAlpha(xmlReader.attributes().value("alpha").toString().toUInt(&ok)); + if(!ok) + { + xmlReader.raiseError(tr("DiagramType: type number conversion failed")); + continue; + } + switch (DiaType) { + case QGraphicsItem::UserType+15: + insertedItem->setPen(color); + break; + case QGraphicsItem::UserType+16: + insertedDrawItem->setPen(color); + break; + case QGraphicsItem::UserType+6: + insertedPathItem->setPen(color); + break; + default: + break; + } + continue; + } + if(DiaType and (xmlReader.name()=="Brush")){ + QColor color; + bool ok; + QString colorName = xmlReader.attributes().value("color").toString(); + color.setNamedColor(colorName); + if(!color.isValid()){ + xmlReader.raiseError(tr("DiagramType: type number conversion failed")); + continue; + } + color.setAlpha(xmlReader.attributes().value("alpha").toString().toUInt(&ok)); + if(!ok) + { + xmlReader.raiseError(tr("DiagramType: type number conversion failed")); + continue; + } + switch (DiaType) { + case QGraphicsItem::UserType+15: + insertedItem->setBrush(color); + break; + case QGraphicsItem::UserType+16: + insertedDrawItem->setBrush(color); + break; + case QGraphicsItem::UserType+6: + insertedPathItem->setBrush(color); + break; + default: + break; + } + continue; + } + if(DiaType and (xmlReader.name()=="Dimensions")){ + QPointF mPos2; + bool okay,ok; + okay=true; + mPos2.setX(xmlReader.attributes().value("width").toString().toDouble(&ok)); + okay&=ok; + mPos2.setY(xmlReader.attributes().value("height").toString().toDouble(&ok)); + okay&=ok; + if(!ok) + { + xmlReader.raiseError(tr("Dimensions: number conversion failed")); + continue; + } + switch (DiaType) { + case QGraphicsItem::UserType+16: + insertedDrawItem->setDimension(mPos2); + break; + default: + break; + } + continue; + } + if(DiaType and (xmlReader.name()=="Point")){ + QPointF mPos2; + bool okay,ok; + okay=true; + mPos2.setX(xmlReader.attributes().value("x").toString().toDouble(&ok)); + okay&=ok; + mPos2.setY(xmlReader.attributes().value("y").toString().toDouble(&ok)); + okay&=ok; + if(!ok) + { + xmlReader.raiseError(tr("Point: number conversion failed")); + continue; + } + switch (DiaType) { + case QGraphicsItem::UserType+6: + insertedPathItem->append(mPos2+insertedPathItem->pos()); + break; + default: + break; + } + continue; + } + if(DiaType and (xmlReader.name()=="Text")){ + QString mText; + mText = xmlReader.readElementText(); + switch (DiaType) { + case QGraphicsItem::UserType+3: + textItem->setHtml(mText); + break; + default: + break; + } + continue; + } + if(DiaType and (xmlReader.name()=="Transform")){ + bool ok,okay; + okay=true; + qreal m11=xmlReader.attributes().value("m11").toString().toDouble(&ok); + okay&=ok; + qreal m12=xmlReader.attributes().value("m12").toString().toDouble(&ok); + okay&=ok; + qreal m21=xmlReader.attributes().value("m21").toString().toDouble(&ok); + okay&=ok; + qreal m22=xmlReader.attributes().value("m22").toString().toDouble(&ok); + okay&=ok; + qreal dx=xmlReader.attributes().value("dx").toString().toDouble(&ok); + okay&=ok; + qreal dy=xmlReader.attributes().value("dx").toString().toDouble(&ok); + okay&=ok; + if(!ok) + { + xmlReader.raiseError(tr("Transform: number conversion failed")); + continue; + } + QTransform trans=QTransform(m11,m12,m21,m22,dx,dy); + switch (DiaType) { + case QGraphicsItem::UserType+15: + insertedItem->setTransform(trans); + break; + case QGraphicsItem::UserType+16: + insertedDrawItem->setTransform(trans); + break; + case QGraphicsItem::UserType+6: + insertedPathItem->setTransform(trans); + break; + case QGraphicsItem::UserType+3: + textItem->setTransform(trans); + break; + default: + break; + } + continue; + } + else { + xmlReader.raiseError(tr("unexpected start element")); + continue; + } + + } + if(xmlReader.isEndElement()){ + if(DiaType and (xmlReader.name()=="Item")){ + DiaType = 0; + insertedItem = 0; + insertedDrawItem = 0; + insertedPathItem = 0; + textItem = 0; + } + } + + } + if(xmlReader.hasError()){ + std::cerr << "Error in XML Read-in: " + << qPrintable(xmlReader.errorString()) + << std::endl + << "Line: " + << xmlReader.lineNumber() + << std::endl; + } + // Aufräumen + insertedItem = 0; + insertedDrawItem = 0; + insertedPathItem = 0; + textItem = 0; + myMode = MoveItem; + return true; +} + +void DiagramScene::clear() +{ + foreach(QGraphicsItem *item,items()){ + if(item!=&myCursor) + { + removeItem(item); + delete item; + } + } +} + +void DiagramScene::setCursorVisible(bool vis) +{ + if(vis){ + if(myCursor.scene()==0){ + addItem(&myCursor); + } + } + else + { + if(myCursor.scene()) removeItem(&myCursor); + } +} + +void DiagramScene::drawBackground(QPainter *p, const QRectF &r) { + p -> save(); + + // desactive tout antialiasing, sauf pour le texte + p -> setRenderHint(QPainter::Antialiasing, false); + p -> setRenderHint(QPainter::TextAntialiasing, true); + p -> setRenderHint(QPainter::SmoothPixmapTransform, false); + + // dessine un fond blanc + p -> setPen(Qt::NoPen); + p -> setBrush(Qt::white); + p -> drawRect(r); + + if (myGridVisible) { + // to ease transition from qelec + int xGrid=myGridScale * (int) myGrid; + int yGrid=myGridScale * (int) myGrid; + // dessine les points de la grille + p -> setPen(Qt::black); + p -> setBrush(Qt::NoBrush); + qreal limite_x = r.x() + r.width(); + qreal limite_y = r.y() + r.height(); + + int g_x = (int)ceil(r.x()); + while (g_x % xGrid) ++ g_x; + int g_y = (int)ceil(r.y()); + while (g_y % yGrid) ++ g_y; + + for (int gx = g_x ; gx < limite_x ; gx += xGrid) { + for (int gy = g_y ; gy < limite_y ; gy += yGrid) { + p -> drawPoint(gx, gy); + } + } + } + + p -> restore(); +} diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramscene.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramscene.h new file mode 100644 index 000000000..85660dcfb --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramscene.h @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef DIAGRAMSCENE_H +#define DIAGRAMSCENE_H + +#include +#include "diagramitem.h" +#include "diagramdrawitem.h" +#include "diagramtextitem.h" +#include "diagrampathitem.h" + +QT_BEGIN_NAMESPACE +class QGraphicsSceneMouseEvent; +class QMenu; +class QPointF; +class QGraphicsLineItem; +class QFont; +class QGraphicsTextItem; +class QColor; +class QFile; +QT_END_NAMESPACE + +//! [0] +class DiagramScene : public QGraphicsScene +{ + Q_OBJECT + +public: + enum Mode { InsertItem, InsertLine, InsertText, MoveItem, CopyItem, CopyingItem, InsertDrawItem, Zoom , MoveItems}; + + DiagramScene(QMenu *itemMenu, QObject *parent = 0); + QFont font() const + { return myFont; } + QColor textColor() const + { return myTextColor; } + QColor itemColor() const + { return myItemColor; } + QColor lineColor() const + { return myLineColor; } + void setLineColor(const QColor &color); + void setTextColor(const QColor &color); + void setItemColor(const QColor &color); + void setFont(const QFont &font); + void setArrow(const int i); + void setGrid(const qreal grid) + { + myGrid=grid; + } + void setGridVisible(const bool vis) + { + myGridVisible=vis; + } + bool isGridVisible() + { + return myGridVisible; + } + void setGridScale(const int k) + { + myGridScale=k; + } + bool save(QFile *file); + bool load(QFile *file); + QPointF onGrid(QPointF pos); + void setCursorVisible(bool t); + +public slots: + void setMode(Mode mode,bool m_abort=true); + void abort(bool keepSelection=false); + void setItemType(DiagramItem::DiagramType type); + void setItemType(DiagramDrawItem::DiagramType type); + void editorLostFocus(DiagramTextItem *item); + void editorReceivedFocus(DiagramTextItem *item); + void checkOnGrid(); + void clear(); + +signals: + void itemInserted(DiagramItem *item); + void textInserted(QGraphicsTextItem *item); + void itemSelected(QGraphicsItem *item); + void editorHasLostFocus(); + void editorHasReceivedFocus(); + void zoomRect(QPointF p1,QPointF p2); + void zoom(const qreal factor); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); + void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent); + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent); + void wheelEvent(QGraphicsSceneWheelEvent *mouseEvent); + bool event(QEvent *mEvent); + QGraphicsItem* copy(QGraphicsItem* item); + void drawBackground(QPainter *p, const QRectF &r); + void enableAllItems(bool enable=true); + + +private: + bool isItemChange(int type); + + DiagramItem::DiagramType myItemType; + DiagramDrawItem::DiagramType myDrawItemType; + QMenu *myItemMenu; + Mode myMode; + bool leftButtonDown; + QPointF startPoint; + QGraphicsLineItem *line; + QFont myFont; + DiagramTextItem *textItem; + QColor myTextColor; + QColor myItemColor; + QColor myLineColor; + DiagramItem *insertedItem; + DiagramDrawItem *insertedDrawItem; + DiagramPathItem *insertedPathItem; + QList *copiedItems; + qreal myDx,myDy; + DiagramPathItem::DiagramType myArrow; + qreal myGrid; + QGraphicsRectItem myCursor; + qreal myCursorWidth; + bool myGridVisible; + int myGridScale; + QList myMoveItems; + qreal maxZ; +}; +//! [0] + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramtextitem.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramtextitem.cpp new file mode 100644 index 000000000..6254bdbfe --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramtextitem.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include + +#include "diagramtextitem.h" +#include "diagramscene.h" +#include + +//! [0] +DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsTextItem(parent, scene) +{ + setFlag(QGraphicsItem::ItemIsMovable); + setFlag(QGraphicsItem::ItemIsSelectable); + QObject::connect(this->document(), SIGNAL(contentsChanged()), + this, SLOT(textChanged())); +} +//! [0] +DiagramTextItem::DiagramTextItem(const DiagramTextItem& textItem) +{ + //QGraphicsTextItem(); + setFont(textItem.font()); + setDefaultTextColor(textItem.defaultTextColor()); + setHtml(textItem.toHtml()); + setTransform(textItem.transform()); + setFlag(QGraphicsItem::ItemIsMovable); + setFlag(QGraphicsItem::ItemIsSelectable); + m_adapt=false; +} + + +//! [1] +QVariant DiagramTextItem::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + if (change == QGraphicsItem::ItemSelectedHasChanged) + emit selectedChange(this); + if (change == QGraphicsItem::ItemPositionHasChanged) + { + if(!m_adapt) { + qreal width=boundingRect().width(); + qreal height=boundingRect().height(); + mCenterPoint=mapToParent(mapFromParent(scenePos())+QPointF(width/2,height/2)); + m_adapt=true; + prepareGeometryChange(); + return mapToParent(mapFromParent(mCenterPoint)+QPointF(-width/2,-height/2)); + } + m_adapt=false; + return value.toPointF(); + } + return value; +} +//! [1] + +//! [2] +void DiagramTextItem::focusOutEvent(QFocusEvent *event) +{ + setTextInteractionFlags(Qt::NoTextInteraction); + emit lostFocus(this); + QGraphicsTextItem::focusOutEvent(event); +} +void DiagramTextItem::focusInEvent(QFocusEvent *event) +{ + //emit receivedFocus(this); + QGraphicsTextItem::focusInEvent(event); +} +//! [2] + +//! [5] +void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +{ + if (textInteractionFlags() == Qt::NoTextInteraction) + setTextInteractionFlags(Qt::TextEditorInteraction); + emit receivedFocus(this); + QGraphicsTextItem::mouseDoubleClickEvent(event); +} +//! [5] +DiagramTextItem* DiagramTextItem::copy() +{ + DiagramTextItem* newTextItem=new DiagramTextItem(*this); + return newTextItem; +} + +void DiagramTextItem::textChanged() +{ + qreal width=boundingRect().width(); + qreal height=boundingRect().height(); + m_adapt=true; + prepareGeometryChange(); + setPos(mapToParent(mapFromParent(mCenterPoint)+QPointF(-width/2,-height/2))); +} + +void DiagramTextItem::setCenterPoint(const QPointF point) +{ + mCenterPoint=point; + textChanged(); +} + +void DiagramTextItem::activateEditor() +{ + if (textInteractionFlags() == Qt::NoTextInteraction) + setTextInteractionFlags(Qt::TextEditorInteraction); + QGraphicsSceneMouseEvent *event=new QGraphicsSceneMouseEvent(); + event->setPos(QPointF(0,0)); + QGraphicsTextItem::mousePressEvent(event); + delete event; +} diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramtextitem.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramtextitem.h new file mode 100644 index 000000000..126c150ad --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/diagramtextitem.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef DIAGRAMTEXTITEM_H +#define DIAGRAMTEXTITEM_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QFocusEvent; +class QGraphicsItem; +class QGraphicsScene; +class QGraphicsSceneMouseEvent; +QT_END_NAMESPACE + +//! [0] +class DiagramTextItem : public QGraphicsTextItem +{ + Q_OBJECT + +public: + enum { Type = UserType + 3 }; + + DiagramTextItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + DiagramTextItem(const DiagramTextItem& textItem); + + DiagramTextItem* copy(); + QPointF centerPoint(){ + return mCenterPoint; + } + void setCenterPoint(const QPointF point); + void activateEditor(); + + int type() const + { return Type; } + +signals: + void lostFocus(DiagramTextItem *item); + void receivedFocus(DiagramTextItem *item); + void selectedChange(QGraphicsItem *item); + +protected: + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + void focusOutEvent(QFocusEvent *event); + void focusInEvent(QFocusEvent *event); + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); + +protected slots: + void textChanged(); + +private: + //qreal width,height; + QPointF mCenterPoint; + bool m_adapt; +}; +//! [0] + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/mainwindow.cpp b/retroshare-gui/src/gui/plugins/qdiagram_plugin/mainwindow.cpp new file mode 100644 index 000000000..d3a5f9c1c --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/mainwindow.cpp @@ -0,0 +1,1258 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include +#include +#include + +#include "mainwindow.h" +#include "diagramitem.h" +#include "diagramdrawitem.h" +#include "diagrampathitem.h" +#include "diagramscene.h" +#include "diagramtextitem.h" + +const int InsertTextButton = 10; +const int InsertDrawItemButton = 64; + +#define NDEBUG + +//! [0] +MainWindow::MainWindow() +{ + createActions(); + createToolBox(); + createMenus(); + + scene = new DiagramScene(itemMenu); + scene->setSceneRect(QRectF(0, 0, 5000, 5000)); + myGrid=10.0; + scene->setGrid(myGrid); + connect(scene, SIGNAL(itemInserted(DiagramItem *)), + this, SLOT(itemInserted(DiagramItem *))); + connect(scene, SIGNAL(textInserted(QGraphicsTextItem *)), + this, SLOT(textInserted(QGraphicsTextItem *))); + connect(scene, SIGNAL(itemSelected(QGraphicsItem *)), + this, SLOT(itemSelected(QGraphicsItem *))); + // activate/deactivate shortcuts when text is edited in scene + connect(scene, SIGNAL(editorHasReceivedFocus()), + this, SLOT(deactivateShortcuts())); + connect(scene, SIGNAL(editorHasLostFocus()), + this, SLOT(activateShortcuts())); + connect(scene, SIGNAL(zoomRect(QPointF,QPointF)), + this, SLOT(doZoomRect(QPointF,QPointF))); + connect(scene, SIGNAL(zoom(const qreal)), + this, SLOT(zoom(const qreal))); + createToolbars(); + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(toolBox); + view = new QGraphicsView(scene); + view->setDragMode(QGraphicsView::RubberBandDrag); + view->setCacheMode(QGraphicsView::CacheBackground); + layout->addWidget(view); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + setCentralWidget(widget); + setWindowTitle(tr("QDiagram")); + + myFileName.clear(); + + myShowGrid=false; +} +//! [0] + +//! [1] +void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) +{ + QList buttons = backgroundButtonGroup->buttons(); + foreach (QAbstractButton *myButton, buttons) { + if (myButton != button) + button->setChecked(false); + } + QString text = button->text(); + if (text == tr("Blue Grid")) + scene->setBackgroundBrush(QPixmap(":/images/background1.png")); + else if (text == tr("White Grid")) + scene->setBackgroundBrush(QPixmap(":/images/background2.png")); + else if (text == tr("Gray Grid")) + scene->setBackgroundBrush(QPixmap(":/images/background3.png")); + else + scene->setBackgroundBrush(QPixmap(":/images/background4.png")); + + scene->update(); + view->update(); +} +//! [1] + +//! [2] +void MainWindow::buttonGroupClicked(int id) +{ + QList buttons = buttonGroup->buttons(); + foreach (QAbstractButton *button, buttons) { + if (buttonGroup->button(id) != button) + button->setChecked(false); + } + if (id == InsertTextButton) { + scene->setMode(DiagramScene::InsertText); + } + else + if ((id&192) == InsertDrawItemButton){ + scene->setItemType(DiagramDrawItem::DiagramType(id&63)); + scene->setMode(DiagramScene::InsertDrawItem); + } + else { + scene->setItemType(DiagramItem::DiagramType(id)); + scene->setMode(DiagramScene::InsertItem); + } + view->setDragMode(QGraphicsView::NoDrag); + //pointerTypeGroup->checkedButton()->setChecked(false); + buttons = pointerTypeGroup->buttons(); + foreach (QAbstractButton *button, buttons) { + button->setChecked(false); + } +} +//! [2] + +//! [3] +void MainWindow::deleteItem() +{ + foreach (QGraphicsItem *item, scene->selectedItems()) { + if (item->type() == DiagramItem::Type) { + //qgraphicsitem_cast(item)->removeArrows(); + } + scene->removeItem(item); + } +} +//! [3] + +//! [4] +void MainWindow::pointerGroupClicked(int id) +{ + QList buttons = pointerTypeGroup->buttons(); + foreach (QAbstractButton *button, buttons) { + if (pointerTypeGroup->button(id) != button) + button->setChecked(false); + } + if(pointerTypeGroup->checkedId()!=DiagramScene::MoveItem) view->setDragMode(QGraphicsView::NoDrag); + else view->setDragMode(QGraphicsView::RubberBandDrag); + scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); +} +//! [4] + +//! [5] +void MainWindow::bringToFront() +{ + //std::cout << "bring to front" << std::endl; + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + QList overlapItems = selectedItem->collidingItems(); + + qreal zValue = 0; + foreach (QGraphicsItem *item, overlapItems) { + if (item->zValue() >= zValue) //&& + //item->type() == DiagramItem::Type) + zValue = item->zValue() + 0.1; + } + selectedItem->setZValue(zValue); +} +//! [5] + +//! [6] +void MainWindow::sendToBack() +{ + //std::cout << "send to back" << std::endl; + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + QList overlapItems = selectedItem->collidingItems(); + + qreal zValue = 0; + foreach (QGraphicsItem *item, overlapItems) { + if (item->zValue() <= zValue)// && + //item->type() == DiagramItem::Type) + zValue = item->zValue() - 0.1; + } + selectedItem->setZValue(zValue); +} + +void MainWindow::rotateRight() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + //selectedItem->rotate(90); + QTransform trans=selectedItem->transform(); + selectedItem->setTransform(trans*QTransform().rotate(90),false); +#ifdef DEBUG + std::cout << "rotate done" << std::endl; +#endif +} + +void MainWindow::rotateLeft() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + //selectedItem->rotate(-90); + QTransform trans=selectedItem->transform(); + selectedItem->setTransform(trans*QTransform().rotate(-90),false); +#ifdef DEBUG + std::cout << "rotate left done" << std::endl; +#endif +} + +void MainWindow::flipX() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + //selectedItem->setTransform(QTransform(-1,0,0,1,0,0),true); + QTransform trans=selectedItem->transform(); + selectedItem->setTransform(trans*QTransform(-1,0,0,1,0,0),false); + //selectedItem->scale(-1,1); +#ifdef DEBUG + std::cout << trans.m11() << "," + << trans.m12() << "," + << trans.m21() << "," + << trans.m22() << "," << std::endl; + + std::cout << "flipX done" << std::endl; +#endif +} + +void MainWindow::flipY() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + QTransform trans=selectedItem->transform(); + selectedItem->setTransform(trans*QTransform(1,0,0,-1,0,0),false); + //selectedItem->scale(1,-1); +#ifdef DEBUG + std::cout << "flipY done" << std::endl; +#endif +} + +void MainWindow::moveItems() +{ + scene->setMode(DiagramScene::MoveItems); + view->setDragMode(QGraphicsView::RubberBandDrag); +#ifdef DEBUG + std::cout << "set move mode" << std::endl; +#endif +} + +void MainWindow::abort() +{ + scene->abort(); + scene->setMode(DiagramScene::MoveItem); + view->setDragMode(QGraphicsView::RubberBandDrag); +#ifdef DEBUG + std::cout << "abort done" << std::endl; +#endif +} + +void MainWindow::copyItems() +{ + scene->setMode(DiagramScene::CopyItem); + view->setDragMode(QGraphicsView::RubberBandDrag); +#ifdef DEBUG + std::cout << "copy mode" << std::endl; +#endif +} + +void MainWindow::groupItems() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItemGroup *test = scene->createItemGroup(scene->selectedItems()); + test->setFlag(QGraphicsItem::ItemIsMovable, true); + test->setFlag(QGraphicsItem::ItemIsSelectable, true); +} + +void MainWindow::ungroupItems() +{ + if (scene->selectedItems().isEmpty()) + return; +#ifdef DEBUG + std::cout << "ungroup" << std::endl; + std::cout << scene->selectedItems().count() << std::endl; +#endif + foreach (QGraphicsItem *item, scene->selectedItems()) { +#ifdef DEBUG + std::cout << item->type() << std::endl; + std::cout << typeid(item).name() << std::endl; +#endif + if (item->type()==10) { + QGraphicsItemGroup *group = (QGraphicsItemGroup*) item; + scene->destroyItemGroup(group); + } + } +#ifdef DEBUG + //std::cout << qPrintable(scene->selectedItems().first()->objectName())<< std::endl; +#endif + //QGraphicsItemGroup *group = (QGraphicsItemGroup*)scene->selectedItems().first(); + //scene->destroyItemGroup(group); + //scene->destroyItemGroup(scene->selectedItems()); +} +//! [6] + +//! [7] +void MainWindow::itemInserted(DiagramItem *item) +{ + //scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); + buttonGroup->button(int(item->diagramType()))->setChecked(false); +} +//! [7] + +//! [8] +void MainWindow::textInserted(QGraphicsTextItem *) +{ + buttonGroup->button(InsertTextButton)->setChecked(false); + scene->setMode(DiagramScene::MoveItem); + view->setDragMode(QGraphicsView::RubberBandDrag); + pointerTypeGroup->button(DiagramScene::MoveItem)->setChecked(true); +} +//! [8] +void MainWindow::activateShortcuts() +{ + foreach(QAction* item,listOfActions){ + item->setEnabled(true); + } + foreach(QShortcut* item,listOfShortcuts){ + item->setEnabled(true); + } +#ifdef DEBUG + std::cout<<"activate"<setEnabled(false); + } + foreach(QShortcut* item,listOfShortcuts){ + item->setEnabled(false); + } +#ifdef DEBUG + std::cout<<"deactivate"<(sender()); + fontColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/textpointer.png", + qVariantValue(textAction->data()))); + textButtonTriggered(); +} +//! [12] + +//! [13] +void MainWindow::itemColorChanged() +{ + fillAction = qobject_cast(sender()); + fillColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/floodfill.png", + qVariantValue(fillAction->data()))); + fillButtonTriggered(); +} +//! [13] + +//! [14] +void MainWindow::lineColorChanged() +{ + lineAction = qobject_cast(sender()); + lineColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/linecolor.png", + qVariantValue(lineAction->data()))); + lineButtonTriggered(); +} +//! [14] + +//! [15] +void MainWindow::textButtonTriggered() +{ + scene->setTextColor(qVariantValue(textAction->data())); +} +//! [15] + +//! [16] +void MainWindow::fillButtonTriggered() +{ + scene->setItemColor(qVariantValue(fillAction->data())); +} +//! [16] + +//! [17] +void MainWindow::lineButtonTriggered() +{ + scene->setLineColor(qVariantValue(lineAction->data())); +} +//! [17] + +//! [18] +void MainWindow::handleFontChange() +{ + QFont font = fontCombo->currentFont(); + font.setPointSize(fontSizeCombo->currentText().toInt()); + font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal); + font.setItalic(italicAction->isChecked()); + font.setUnderline(underlineAction->isChecked()); + + scene->setFont(font); +} +//! [18] + +//! [19] +void MainWindow::itemSelected(QGraphicsItem *item) +{ + DiagramTextItem *textItem = + qgraphicsitem_cast(item); + + QFont font = textItem->font(); + QColor color = textItem->defaultTextColor(); + fontCombo->setCurrentFont(font); + fontSizeCombo->setEditText(QString().setNum(font.pointSize())); + boldAction->setChecked(font.weight() == QFont::Bold); + italicAction->setChecked(font.italic()); + underlineAction->setChecked(font.underline()); +} +//! [19] + +//! [20] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Diagram Scene"), + tr("The Diagram Scene example shows " + "use of the graphics framework.")); +} +//! [20] +void MainWindow::print() +{ + QPrinter printer; + if (QPrintDialog(&printer).exec() == QDialog::Accepted) { + QPainter painter(&printer); + painter.setRenderHint(QPainter::Antialiasing); + scene->render(&painter); + } +} + +void MainWindow::exportImage() +{ + QFileDialog::Options options; + options = 0; + QString selectedFilter; + QString fileName = QFileDialog::getSaveFileName(this, + tr("Export Diagram to ..."), + ".jpg", + tr("Jpg (*.jpg);;Png (*.png);;Pdf (*.pdf);;Postscript (*.ps)"), + &selectedFilter, + options); + if (!fileName.isEmpty()){ +#ifdef DEBUG + std::cout << "Selected Filter: " << qPrintable(selectedFilter) << std::endl; +#endif + if((selectedFilter=="Pdf (*.pdf)")or(selectedFilter=="Postscript (*.ps)")) { + QRectF rect=scene->itemsBoundingRect(); // Bonding der Elemente in scene + QPrinter printer; + printer.setOutputFileName(fileName); + QSizeF size=printer.paperSize(QPrinter::Millimeter); // definiere Paper mit gleichen Aspectratio wie rect + size.setHeight(size.width()*rect.height()/rect.width()); + printer.setPaperSize(size,QPrinter::Millimeter); + printer.setPageMargins(0,0,0,0,QPrinter::Millimeter); + QPainter painter(&printer);// generate PDF/PS + painter.setRenderHint(QPainter::Antialiasing); + scene->render(&painter,QRectF(),rect); + } + else { + QPixmap pixmap(1000,1000); + pixmap.fill(); + QPainter painter(&pixmap); + painter.setRenderHint(QPainter::Antialiasing); + QRectF rect=scene->itemsBoundingRect(); + scene->render(&painter,QRectF(),rect); + painter.end(); + + pixmap.save(fileName); + } + + } +} + +//! [21] +void MainWindow::createToolBox() +{ + buttonGroup = new QButtonGroup; + buttonGroup->setExclusive(false); + connect(buttonGroup, SIGNAL(buttonClicked(int)), + this, SLOT(buttonGroupClicked(int))); + QGridLayout *layout = new QGridLayout; + layout->addWidget(createCellWidget(tr("Conditional"), + DiagramItem::Conditional), 0, 0); + layout->addWidget(createCellWidget(tr("Process"), + DiagramItem::Step),0, 1); + layout->addWidget(createCellWidget(tr("Input/Output"), + DiagramItem::Io), 1, 0); +// added DrawItem + layout->addWidget(createCellWidget(tr("Rectangle"), + DiagramDrawItem::Rectangle), 2, 0); + layout->addWidget(createCellWidget(tr("Ellipse"), + DiagramDrawItem::Ellipse), 2, 1); +//! [21] + + QToolButton *textButton = new QToolButton; + textButton->setCheckable(true); + buttonGroup->addButton(textButton, InsertTextButton); + textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png") + .scaled(30, 30))); + textButton->setIconSize(QSize(50, 50)); + QGridLayout *textLayout = new QGridLayout; + textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter); + textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter); + QWidget *textWidget = new QWidget; + textWidget->setLayout(textLayout); + layout->addWidget(textWidget, 1, 1); + + layout->setRowStretch(3, 10); + layout->setColumnStretch(2, 10); + + QWidget *itemWidget = new QWidget; + itemWidget->setLayout(layout); + + backgroundButtonGroup = new QButtonGroup; + connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton *)), + this, SLOT(backgroundButtonGroupClicked(QAbstractButton *))); + + QGridLayout *backgroundLayout = new QGridLayout; + backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"), + ":/images/background1.png"), 0, 0); + backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"), + ":/images/background2.png"), 0, 1); + backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"), + ":/images/background3.png"), 1, 0); + backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"), + ":/images/background4.png"), 1, 1); + + backgroundLayout->setRowStretch(2, 10); + backgroundLayout->setColumnStretch(2, 10); + + QWidget *backgroundWidget = new QWidget; + backgroundWidget->setLayout(backgroundLayout); + + +//! [22] + toolBox = new QToolBox; + toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored)); + toolBox->setMinimumWidth(itemWidget->sizeHint().width()); + toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes")); + toolBox->addItem(backgroundWidget, tr("Backgrounds")); +} +//! [22] + +//! [23] +void MainWindow::createActions() +{ + toFrontAction = new QAction(QIcon(":/images/lc_bringtofront.png"), + tr("Bring to &Front"), this); + toFrontAction->setShortcut(tr("Ctrl+F")); + toFrontAction->setStatusTip(tr("Bring item to front")); + connect(toFrontAction, SIGNAL(triggered()), + this, SLOT(bringToFront())); +//! [23] + + sendBackAction = new QAction(QIcon(":/images/lc_sendtoback.png"), + tr("Send to &Back"), this); + sendBackAction->setShortcut(tr("Ctrl+B")); + sendBackAction->setStatusTip(tr("Send item to back")); + connect(sendBackAction, SIGNAL(triggered()), + this, SLOT(sendToBack())); + + QPixmap pixmap2=QPixmap(":/images/lc_toggleobjectrotatemode.png").transformed(QTransform(-1,0,0,1,0,0)); + rotateRightAction = new QAction(QIcon(pixmap2), + tr("rotate &Right"), this); + rotateRightAction->setShortcut(tr("R")); + rotateRightAction->setStatusTip(tr("rotate item 90 degrees right")); + connect(rotateRightAction, SIGNAL(triggered()), + this, SLOT(rotateRight())); + listOfActions.append(rotateRightAction); + + rotateLeftAction = new QAction(QIcon(":/images/lc_toggleobjectrotatemode.png"), + tr("rotate &Left"), this); + rotateLeftAction->setShortcut(tr("Shift+R")); + rotateLeftAction->setStatusTip(tr("rotate item 90 degrees left")); + connect(rotateLeftAction, SIGNAL(triggered()), + this, SLOT(rotateLeft())); + listOfActions.append(rotateLeftAction); + + groupAction = new QAction(QIcon(":/images/lc_group.png"), + tr("&group Items"), this); + groupAction->setShortcut(tr("Ctrl+G")); + groupAction->setStatusTip(tr("group Items")); + connect(groupAction, SIGNAL(triggered()), + this, SLOT(groupItems())); + + ungroupAction = new QAction(QIcon(":/images/lc_ungroup.png"), + tr("&ungroup Item"), this); + ungroupAction->setShortcut(tr("Shift+Ctrl+G")); + ungroupAction->setStatusTip(tr("ungruoup Items")); + connect(ungroupAction, SIGNAL(triggered()), + this, SLOT(ungroupItems())); + + deleteAction = new QAction(QIcon(":/images/delete.png"), + tr("&Delete"), this); + deleteAction->setShortcut(tr("Delete")); + deleteAction->setStatusTip(tr("Delete item from diagram")); + connect(deleteAction, SIGNAL(triggered()), + this, SLOT(deleteItem())); + listOfActions.append(deleteAction); + + printAction = new QAction(tr("&Print"), this); + printAction->setStatusTip(tr("Print Diagram")); + connect(printAction, SIGNAL(triggered()), this, SLOT(print())); + + exportAction = new QAction(tr("&Export Diagram"), this); + exportAction->setStatusTip(tr("Export Diagram to image")); + connect(exportAction, SIGNAL(triggered()), this, SLOT(exportImage())); + + exitAction = new QAction(tr("E&xit"), this); + exitAction->setShortcut(tr("Ctrl+X")); + exitAction->setStatusTip(tr("Quit Scenediagram example")); + connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); + + boldAction = new QAction(tr("Bold"), this); + boldAction->setCheckable(true); + QPixmap pixmap(":/images/lc_bold.png"); + boldAction->setIcon(QIcon(pixmap)); + //boldAction->setShortcut(tr("Ctrl+B")); + connect(boldAction, SIGNAL(triggered()), + this, SLOT(handleFontChange())); + + italicAction = new QAction(QIcon(":/images/lc_italic.png"), + tr("Italic"), this); + italicAction->setCheckable(true); + //italicAction->setShortcut(tr("Ctrl+I")); + connect(italicAction, SIGNAL(triggered()), + this, SLOT(handleFontChange())); + + underlineAction = new QAction(QIcon(":/images/lc_underline.png"), + tr("Underline"), this); + underlineAction->setCheckable(true); + //underlineAction->setShortcut(tr("Ctrl+U")); + connect(underlineAction, SIGNAL(triggered()), + this, SLOT(handleFontChange())); + + aboutAction = new QAction(tr("A&bout"), this); + //aboutAction->setShortcut(tr("Ctrl+B")); + connect(aboutAction, SIGNAL(triggered()), + this, SLOT(about())); + + copyAction = new QAction(tr("&Copy"), this); + copyAction->setShortcut(tr("c")); + connect(copyAction, SIGNAL(triggered()), + this, SLOT(copyItems())); + listOfActions.append(copyAction); + + moveAction = new QAction(tr("&Move"), this); + moveAction->setShortcut(tr("m")); + connect(moveAction, SIGNAL(triggered()), + this, SLOT(moveItems())); + listOfActions.append(moveAction); + + flipXAction = new QAction(QIcon(":/images/lc_flipvertical.png"), + tr("Flip &X"), this); + flipXAction->setShortcut(tr("f")); + connect(flipXAction, SIGNAL(triggered()), + this, SLOT(flipX())); + listOfActions.append(flipXAction); + + flipYAction = new QAction(QIcon(":/images/lc_fliphorizontal.png"),tr("Flip &Y"), this); + flipYAction->setShortcut(tr("Shift+F")); + connect(flipYAction, SIGNAL(triggered()), + this, SLOT(flipY())); + listOfActions.append(flipYAction); + + escShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), + this); + connect(escShortcut,SIGNAL(activated()),this,SLOT(abort())); + + // Zoom in/out + zoomInAction = new QAction(QIcon(":/images/lc_zoomin.png"),tr("Zoom &in"), this); + zoomInAction->setShortcut(tr("Shift+z")); + connect(zoomInAction, SIGNAL(triggered()), + this, SLOT(zoomIn())); + listOfActions.append(zoomInAction); + + zoomOutAction = new QAction(QIcon(":/images/lc_zoomminus.png"),tr("Zoom &out"), this); + zoomOutAction->setShortcut(tr("Ctrl+z")); + connect(zoomOutAction, SIGNAL(triggered()), + this, SLOT(zoomOut())); + + zoomAction = new QAction(QIcon(":/images/lc_zoomoptimal.png"),tr("&Zoom area"), this); + zoomAction->setShortcut(tr("z")); + connect(zoomAction, SIGNAL(triggered()), + this, SLOT(zoomRect())); + listOfActions.append(zoomAction); + + zoomFitAction = new QAction(QIcon(":/images/lc_zoompagewidth.png"),tr("Zoom &Fit"), this); + zoomFitAction->setShortcut(tr("v")); + connect(zoomFitAction, SIGNAL(triggered()), + this, SLOT(zoomFit())); + listOfActions.append(zoomFitAction); + + showGridAction = new QAction(QIcon(":/images/lc_gridvisible.png"),tr("Show &Grid"), this); + showGridAction->setCheckable(true); + showGridAction->setChecked(false); + connect(showGridAction, SIGNAL(toggled(bool)), + this, SLOT(toggleGrid(bool))); + + loadAction = new QAction(QIcon(":/images/lc_open.png"),tr("L&oad ..."), this); + loadAction->setShortcut(tr("Ctrl+o")); + connect(loadAction, SIGNAL(triggered()), + this, SLOT(load())); + + saveAction = new QAction(QIcon(":/images/lc_save.png"),tr("&Save ..."), this); + saveAction->setShortcut(tr("Ctrl+s")); + connect(saveAction, SIGNAL(triggered()), + this, SLOT(save())); + + saveAsAction = new QAction(QIcon(":/images/lc_saveas.png"),tr("Save &As ..."), this); + saveAsAction->setShortcut(tr("Ctrl+s")); + connect(saveAsAction, SIGNAL(triggered()), + this, SLOT(saveAs())); +} + +//! [24] +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(loadAction); + fileMenu->addAction(saveAction); + fileMenu->addAction(saveAsAction); + fileMenu->addAction(printAction); + fileMenu->addAction(exportAction); + fileMenu->addAction(exitAction); + + viewMenu = menuBar()->addMenu(tr("&View")); + viewMenu->addAction(zoomInAction); + viewMenu->addAction(zoomOutAction); + viewMenu->addAction(zoomAction); + viewMenu->addAction(zoomFitAction); + viewMenu->addSeparator(); + viewMenu->addAction(showGridAction); + + itemMenu = menuBar()->addMenu(tr("&Item")); + itemMenu->addAction(deleteAction); + itemMenu->addAction(copyAction); + itemMenu->addAction(moveAction); + itemMenu->addSeparator(); + itemMenu->addAction(toFrontAction); + itemMenu->addAction(sendBackAction); + itemMenu->addSeparator(); + itemMenu->addAction(rotateRightAction); + itemMenu->addAction(rotateLeftAction); + itemMenu->addAction(flipXAction); + itemMenu->addAction(flipYAction); + itemMenu->addAction(groupAction); + itemMenu->addAction(ungroupAction); + + aboutMenu = menuBar()->addMenu(tr("&Help")); + aboutMenu->addAction(aboutAction); +} +//! [24] + +//! [25] +void MainWindow::createToolbars() +{ +//! [25] + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(deleteAction); + editToolBar->addAction(toFrontAction); + editToolBar->addAction(sendBackAction); + editToolBar->addAction(flipXAction); + editToolBar->addAction(flipYAction); + editToolBar->addAction(rotateRightAction); + editToolBar->addAction(rotateLeftAction); + + fontCombo = new QFontComboBox(); + fontSizeCombo = new QComboBox(); + connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)), + this, SLOT(currentFontChanged(const QFont &))); + + fontSizeCombo = new QComboBox; + fontSizeCombo->setEditable(true); + for (int i = 8; i < 30; i = i + 2) + fontSizeCombo->addItem(QString().setNum(i)); + QIntValidator *validator = new QIntValidator(2, 64, this); + fontSizeCombo->setValidator(validator); + connect(fontSizeCombo, SIGNAL(currentIndexChanged(const QString &)), + this, SLOT(fontSizeChanged(const QString &))); + + fontColorToolButton = new QToolButton; + fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); + fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()), + Qt::black)); + textAction = fontColorToolButton->menu()->defaultAction(); + fontColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/textpointer.png", Qt::black)); + fontColorToolButton->setAutoFillBackground(true); + connect(fontColorToolButton, SIGNAL(clicked()), + this, SLOT(textButtonTriggered())); + +//! [26] + fillColorToolButton = new QToolButton; + fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); + fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()), + Qt::white)); + fillAction = fillColorToolButton->menu()->defaultAction(); + fillColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/floodfill.png", Qt::white)); + connect(fillColorToolButton, SIGNAL(clicked()), + this, SLOT(fillButtonTriggered())); +//! [26] + + lineColorToolButton = new QToolButton; + lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); + lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()), + Qt::black)); + lineAction = lineColorToolButton->menu()->defaultAction(); + lineColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/linecolor.png", Qt::black)); + connect(lineColorToolButton, SIGNAL(clicked()), + this, SLOT(lineButtonTriggered())); + + textToolBar = addToolBar(tr("Font")); + textToolBar->addWidget(fontCombo); + textToolBar->addWidget(fontSizeCombo); + textToolBar->addAction(boldAction); + textToolBar->addAction(italicAction); + textToolBar->addAction(underlineAction); + + colorToolBar = addToolBar(tr("Color")); + colorToolBar->addWidget(fontColorToolButton); + colorToolBar->addWidget(fillColorToolButton); + colorToolBar->addWidget(lineColorToolButton); + + QToolButton *pointerButton = new QToolButton; + pointerButton->setCheckable(true); + pointerButton->setChecked(true); + pointerButton->setIcon(QIcon(":/images/pointer.png")); + linePointerButton = new QToolButton; + linePointerButton->setCheckable(true); + //linePointerButton->setIcon(QIcon(":/images/linepointer.png")); + linePointerButton->setIcon(createArrowIcon(0)); + linePointerButton->setPopupMode(QToolButton::MenuButtonPopup); + linePointerButton->setMenu(createArrowMenu(SLOT(lineArrowChanged()), + 0)); + arrowAction = linePointerButton->menu()->defaultAction(); + connect(linePointerButton, SIGNAL(clicked()), + this, SLOT(lineArrowButtonTriggered())); + + pointerTypeGroup = new QButtonGroup; + pointerTypeGroup->setExclusive(false); + pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem)); + pointerTypeGroup->addButton(linePointerButton, + int(DiagramScene::InsertLine)); + connect(pointerTypeGroup, SIGNAL(buttonClicked(int)), + this, SLOT(pointerGroupClicked(int))); + + pointerToolbar = addToolBar(tr("Pointer type")); + pointerToolbar->addWidget(pointerButton); + pointerToolbar->addWidget(linePointerButton); + + zoomToolbar = addToolBar(tr("Zoom")); + zoomToolbar->addAction(zoomInAction); + zoomToolbar->addAction(zoomOutAction); + zoomToolbar->addAction(zoomAction); + zoomToolbar->addAction(zoomFitAction); +//! [27] +} +//! [27] + +//! [28] +QWidget *MainWindow::createBackgroundCellWidget(const QString &text, + const QString &image) +{ + QToolButton *button = new QToolButton; + button->setText(text); + button->setIcon(QIcon(image)); + button->setIconSize(QSize(50, 50)); + button->setCheckable(true); + backgroundButtonGroup->addButton(button); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(button, 0, 0, Qt::AlignHCenter); + layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + return widget; +} +//! [28] + +//! [29] +QWidget *MainWindow::createCellWidget(const QString &text, + DiagramItem::DiagramType type) +{ + + DiagramItem item(type, itemMenu); + QIcon icon(item.image()); + + QToolButton *button = new QToolButton; + button->setIcon(icon); + button->setIconSize(QSize(50, 50)); + button->setCheckable(true); + buttonGroup->addButton(button, int(type)); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(button, 0, 0, Qt::AlignHCenter); + layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + return widget; +} +//! [29] +QWidget *MainWindow::createCellWidget(const QString &text, + DiagramDrawItem::DiagramType type) +{ + + DiagramDrawItem item(type, itemMenu); + item.setPos2(230,230); + QIcon icon(item.image()); + + QToolButton *button = new QToolButton; + button->setIcon(icon); + button->setIconSize(QSize(50, 50)); + button->setCheckable(true); + buttonGroup->addButton(button, int(type)+64); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(button, 0, 0, Qt::AlignHCenter); + layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + return widget; +} + +//! [30] +QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor) +{ + QList colors; + colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow; + QStringList names; + names << tr("black") << tr("white") << tr("red") << tr("blue") + << tr("yellow"); + + QMenu *colorMenu = new QMenu; + for (int i = 0; i < colors.count(); ++i) { + QAction *action = new QAction(names.at(i), this); + action->setData(colors.at(i)); + action->setIcon(createColorIcon(colors.at(i))); + connect(action, SIGNAL(triggered()), + this, slot); + colorMenu->addAction(action); + if (colors.at(i) == defaultColor) { + colorMenu->setDefaultAction(action); + } + } + return colorMenu; +} +//! [30] + +//! [31] +QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, + QColor color) +{ + QPixmap pixmap(50, 80); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + QPixmap image(imageFile); + QRect target(0, 0, 50, 60); + QRect source(0, 0, 42, 42); + painter.fillRect(QRect(0, 60, 50, 80), color); + painter.drawPixmap(target, image, source); + + return QIcon(pixmap); +} +//! [31] + +//! [32] +QIcon MainWindow::createColorIcon(QColor color) +{ + QPixmap pixmap(20, 20); + QPainter painter(&pixmap); + painter.setPen(Qt::NoPen); + painter.fillRect(QRect(0, 0, 20, 20), color); + + return QIcon(pixmap); +} +//! [32] + +QMenu *MainWindow::createArrowMenu(const char *slot, const int def) +{ + QStringList names; + names << tr("Path") << tr("Start") << tr("End") << tr("StartEnd"); + QMenu *arrowMenu = new QMenu; + for (int i = 0; i < names.count(); ++i) { + QAction *action = new QAction(names.at(i), this); + action->setData(i); + action->setIcon(createArrowIcon(i)); + connect(action, SIGNAL(triggered()), + this, slot); + arrowMenu->addAction(action); + if (i == def) { + arrowMenu->setDefaultAction(action); + } + } + return arrowMenu; +} + +QIcon MainWindow::createArrowIcon(const int i) +{ + QPixmap pixmap(50, 80); + DiagramPathItem* item=new DiagramPathItem(DiagramPathItem::DiagramType(i),0,0); + pixmap=item->icon(); + delete item; + + return QIcon(pixmap); +} + +void MainWindow::lineArrowButtonTriggered() +{ + scene->setArrow(qVariantValue(arrowAction->data())); + pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(false); +} + +void MainWindow::lineArrowChanged() +{ + arrowAction = qobject_cast(sender()); + linePointerButton->setIcon(createArrowIcon(qVariantValue(arrowAction->data()))); + lineArrowButtonTriggered(); +} + +void MainWindow::zoomIn() +{ + zoom(2.0); +} + +void MainWindow::zoomOut() +{ + zoom(0.5); +} + +void MainWindow::zoom(const qreal factor) +{ + QPointF topLeft = view->mapToScene( 0, 0 ); + QPointF bottomRight = view->mapToScene( view->viewport()->width() - 1, view->viewport()->height() - 1 ); + qreal width=bottomRight.x()-topLeft.x(); + qreal height=bottomRight.y()-topLeft.y(); + //std::cout << width << "/" << height << " : " << factor << std::endl; + if((width/factor<=5000)&&(height/factor<=5000)){ + QMatrix oldMatrix = view->matrix(); + qreal newScale=oldMatrix.m11()*factor; + view->resetMatrix(); + view->translate(oldMatrix.dx(), oldMatrix.dy()); + view->scale(newScale, newScale); + + setGrid(); + } +} + +void MainWindow::zoomRect() +{ + scene->setMode(DiagramScene::Zoom); + view->setDragMode(QGraphicsView::RubberBandDrag); + setGrid(); +} + +void MainWindow::doZoomRect(QPointF p1,QPointF p2) +{ + view->fitInView(QRectF(p1,p2),Qt::KeepAspectRatio); + setGrid(); +} + +void MainWindow::zoomFit() +{ + scene->setCursorVisible(false); + view->fitInView(scene->itemsBoundingRect(),Qt::KeepAspectRatio); + scene->setCursorVisible(true); + setGrid(); +} + +void MainWindow::toggleGrid(bool grid) +{ + scene->setGridVisible(grid); + //view->repaint(); + QPointF topLeft = view->mapToScene( 0, 0 ); + QPointF bottomRight = view->mapToScene( view->viewport()->width() - 1, view->viewport()->height() - 1 ); + scene->invalidate(topLeft.x(),topLeft.y(),bottomRight.x()-topLeft.x(),bottomRight.y()-topLeft.y()); + //scene->update(); + //view->update(); + +} + +void MainWindow::setGrid() +{ + if(scene->isGridVisible()){ + QPointF topLeft = view->mapToScene( 0, 0 ); + QPointF bottomRight = view->mapToScene( view->viewport()->width() - 1, view->viewport()->height() - 1 ); + qreal width=bottomRight.x()-topLeft.x(); + qreal height=bottomRight.y()-topLeft.y(); + qreal zw=width; + std::cout << width << "/" << height << std::endl; + if(zw50) + { + k=k*2; + } + std::cout << k << std::endl; + scene->setGridScale(k); + scene->update(); + } +} + +void MainWindow::saveAs() +{ + QFileDialog::Options options; + options = 0; + QString selectedFilter; + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save Diagram as ..."), + ".qdiag", + tr("QDiagram (*.qdiag)"), + &selectedFilter, + options); + if (!fileName.isEmpty()){ + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){ + QMessageBox::warning(this,tr("File operation error"),file.errorString()); + } + else + { + if(scene->save(&file)){ + myFileName=fileName; + } + file.close(); + if(file.error()){ + std::cerr << "Error: cannot write file " + << qPrintable(file.fileName()) + << qPrintable(file.errorString()) + << std::endl ; + } + } + } +} + +void MainWindow::save() +{ + if (!myFileName.isEmpty()){ + QFile file(myFileName); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){ + QMessageBox::warning(this,tr("File operation error"),file.errorString()); + } + else + { + scene->save(&file); + } + } +} + +void MainWindow::load() +{ + QFileDialog::Options options; + options = 0; + QString selectedFilter; + QString fileName = QFileDialog::getOpenFileName(this, + tr("Save Diagram as ..."), + ".qdiag", + tr("QDiagram (*.qdiag)"), + &selectedFilter, + options); + if (!fileName.isEmpty()){ + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){ + QMessageBox::warning(this,tr("File operation error"),file.errorString()); + } + else + { + scene->clear(); + scene->load(&file); + myFileName=fileName; + } + + } +} diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/mainwindow.h b/retroshare-gui/src/gui/plugins/qdiagram_plugin/mainwindow.h new file mode 100644 index 000000000..2095bda54 --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/mainwindow.h @@ -0,0 +1,220 @@ +/**************************************************************************** +** +** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the example classes of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License versions 2.0 or 3.0 as published by the Free Software +** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Alternatively you may (at +** your option) use any later version of the GNU General Public +** License if such license has been publicly approved by Trolltech ASA +** (or its successors, if any) and the KDE Free Qt Foundation. In +** addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.2, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** Please review the following information to ensure GNU General +** Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. If +** you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech, as the sole +** copyright holder for Qt Designer, grants users of the Qt/Eclipse +** Integration plug-in the right for the Qt/Eclipse Integration to +** link to functionality provided by Qt Designer and its related +** libraries. +** +** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, +** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly +** granted herein. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +#include "diagramitem.h" +#include "diagramdrawitem.h" +#include "diagrampathitem.h" + +class DiagramScene; + +QT_BEGIN_NAMESPACE +class QAction; +class QToolBox; +class QSpinBox; +class QComboBox; +class QFontComboBox; +class QButtonGroup; +class QLineEdit; +class QGraphicsTextItem; +class QFont; +class QToolButton; +class QAbstractButton; +class QGraphicsView; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void backgroundButtonGroupClicked(QAbstractButton *button); + void buttonGroupClicked(int id); + void deleteItem(); + void pointerGroupClicked(int id); + void bringToFront(); + void sendToBack(); + void rotateRight(); + void rotateLeft(); + void flipX(); + void flipY(); + void abort(); + void print(); + void exportImage(); + void copyItems(); + void groupItems(); + void ungroupItems(); + void itemInserted(DiagramItem *item); + void textInserted(QGraphicsTextItem *item); + void currentFontChanged(const QFont &font); + void fontSizeChanged(const QString &size); + void textColorChanged(); + void itemColorChanged(); + void lineColorChanged(); + void lineArrowChanged(); + void textButtonTriggered(); + void fillButtonTriggered(); + void lineButtonTriggered(); + void lineArrowButtonTriggered(); + void handleFontChange(); + void itemSelected(QGraphicsItem *item); + void about(); + void activateShortcuts(); + void deactivateShortcuts(); + void zoomIn(); + void zoomOut(); + void zoom(const qreal factor); + void zoomRect(); + void doZoomRect(QPointF p1,QPointF p2); + void zoomFit(); + void toggleGrid(bool grid); + void save(); + void saveAs(); + void load(); + void moveItems(); + +private: + void createToolBox(); + void createActions(); + void createMenus(); + void createToolbars(); + void setGrid(); + QWidget *createBackgroundCellWidget(const QString &text, + const QString &image); + QWidget *createCellWidget(const QString &text, + DiagramItem::DiagramType type); + QWidget *createCellWidget(const QString &text, + DiagramDrawItem::DiagramType type); + QMenu *createColorMenu(const char *slot, QColor defaultColor); + QIcon createColorToolButtonIcon(const QString &image, QColor color); + QIcon createColorIcon(QColor color); + + QMenu *createArrowMenu(const char *slot, const int def); + QIcon createArrowIcon(const int i); + + DiagramScene *scene; + QGraphicsView *view; + + QAction *exitAction; + QAction *addAction; + QAction *deleteAction; + + QAction *toFrontAction; + QAction *sendBackAction; + QAction *rotateRightAction; + QAction *rotateLeftAction; + QAction *flipXAction; + QAction *flipYAction; + QAction *copyAction; + QAction *moveAction; + QAction *groupAction; + QAction *ungroupAction; + QAction *aboutAction; + + QAction *zoomInAction; + QAction *zoomOutAction; + QAction *zoomAction; + QAction *zoomFitAction; + QAction *showGridAction; + + QAction *printAction; + QAction *exportAction; + + QShortcut *escShortcut; + + QMenu *fileMenu; + QMenu *viewMenu; + QMenu *itemMenu; + QMenu *aboutMenu; + + QToolBar *textToolBar; + QToolBar *editToolBar; + QToolBar *colorToolBar; + QToolBar *pointerToolbar; + QToolBar *zoomToolbar; + + QComboBox *itemColorCombo; + QComboBox *textColorCombo; + QComboBox *fontSizeCombo; + QFontComboBox *fontCombo; + + QToolBox *toolBox; + QButtonGroup *buttonGroup; + QButtonGroup *pointerTypeGroup; + QButtonGroup *backgroundButtonGroup; + QToolButton *fontColorToolButton; + QToolButton *fillColorToolButton; + QToolButton *lineColorToolButton; + QToolButton *linePointerButton; + QAction *boldAction; + QAction *underlineAction; + QAction *italicAction; + QAction *textAction; + QAction *fillAction; + QAction *lineAction; + QAction *arrowAction; + QAction *loadAction; + QAction *saveAction; + QAction *saveAsAction; + + QList listOfActions; + QList listOfShortcuts; + + bool myShowGrid; // Grid visible ? + qreal myGrid; // Grid distance (dx=dy) + + QString myFileName; // aktueller Filename + +}; +//! [0] + +#endif diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/qdiagram.pro b/retroshare-gui/src/gui/plugins/qdiagram_plugin/qdiagram.pro new file mode 100644 index 000000000..fe35d03ea --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/qdiagram.pro @@ -0,0 +1,50 @@ +#=== this part is common (similar) for all plugin projects ===================== +TEMPLATE = lib +CONFIG += plugin debug + +# this is directory, where PluginInterface.h is located +INCLUDEPATH += ../ + +# and, the result (*.so or *.dll) should appear in this directory +DESTDIR = ../bin +OBJECTS_DIR = temp/obj +RCC_DIR = temp/qrc +UI_DIR = temp/ui +MOC_DIR = temp/moc + + +# the name of the result file; +TARGET = $$qtLibraryTarget(qdiagram_plugin) + +HEADERS += ../PluginInterface.h \ + DiagramPlugin.h +SOURCES += DiagramPlugin.cpp + +#=============================================================================== + + +HEADERS = diagrampathitem.h \ + diagramdrawitem.h \ + mainwindow.h \ + diagramitem.h \ + diagramscene.h \ + diagramtextitem.h +SOURCES = diagrampathitem.cpp \ + diagramdrawitem.cpp \ + mainwindow.cpp \ + diagramitem.cpp \ + diagramtextitem.cpp \ + diagramscene.cpp +RESOURCES = qdiagram.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/diagramscene +sources.files = $$SOURCES \ + $$HEADERS \ + $$RESOURCES \ + $$FORMS \ + diagramscene.pro \ + images +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/diagramscene +INSTALLS += target \ + sources diff --git a/retroshare-gui/src/gui/plugins/qdiagram_plugin/qdiagram.qrc b/retroshare-gui/src/gui/plugins/qdiagram_plugin/qdiagram.qrc new file mode 100644 index 000000000..854e048ae --- /dev/null +++ b/retroshare-gui/src/gui/plugins/qdiagram_plugin/qdiagram.qrc @@ -0,0 +1,53 @@ + + + images/background1.png + images/background2.png + images/background3.png + images/background4.png + images/bold.png + images/bringtofront.png + images/delete.png + images/floodfill.png + images/italic.png + images/lc_aligncenter.png + images/lc_aligndown.png + images/lc_alignmiddle.png + images/lc_alignup.png + images/lc_backward.png + images/lc_bold.png + images/lc_bringtofront.png + images/lc_copy.png + images/lc_cut.png + images/lc_fliphorizontal.png + images/lc_flipvertical.png + images/lc_forward.png + images/lc_grid.png + images/lc_griduse.png + images/lc_gridvisible.png + images/lc_group.png + images/lc_italic.png + images/lc_objectalign.png + images/lc_objectalignleft.png + images/lc_objectalignright.png + images/lc_open.png + images/lc_redo.png + images/lc_save.png + images/lc_saveas.png + images/lc_sendtoback.png + images/lc_toggleobjectrotatemode.png + images/lc_underline.png + images/lc_undo.png + images/lc_ungroup.png + images/lc_zoom.png + images/lc_zoomin.png + images/lc_zoomminus.png + images/lc_zoomoptimal.png + images/lc_zoompagewidth.png + images/linecolor.png + images/linepointer.png + images/pointer.png + images/sendtoback.png + images/textpointer.png + images/underline.png + +