* Added qdiagram to the plugins

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@980 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
defnax 2009-02-02 14:24:43 +00:00
parent 0a6f1bbe87
commit a30de82df8
16 changed files with 4605 additions and 0 deletions

View File

@ -0,0 +1,33 @@
//#include <QApplication>
//#include <QString>
//#include <QPushButton>
#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)

View File

@ -0,0 +1,27 @@
#ifndef _HWA_PLUGIN_H_
#define _HWA_PLUGIN_H_
#include <QObject>
#include <QString>
#include <QWidget>
#include <PluginInterface.h>
#include <QDebug>
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

View File

@ -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 <QtGui>
#include <iostream>
#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<DiagramItem*>(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<DiagramScene*>(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);
}

View File

@ -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 <QGraphicsPixmapItem>
#include <QList>
#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

View File

@ -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 <QtGui>
#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<QGraphicsPolygonItem>(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;
}

View File

@ -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 <QGraphicsPixmapItem>
#include <QList>
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

View File

@ -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 <QtGui>
#include <iostream>
#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;i<myPoints.count();i++)
{
point = myPoints.at(i);
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
}
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()<minx) minx=point.x();
if(point.x()>maxx) maxx=point.x();
if(point.y()<miny) miny=point.y();
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;mySelPoint<myPoints.count();mySelPoint++){
if(hasClickedOn(mouse_point,myPoints.at(mySelPoint))) break;
}
if(mySelPoint==myPoints.count()) mySelPoint=-1;
else e->accept();
}
}
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<DiagramScene*>(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<myPoints.count();myHoverPoint++){
if(hasClickedOn(hover_point,myPoints.at(myHoverPoint))) break;
}//for
if(myHoverPoint==myPoints.count()) myHoverPoint=-1;
else update();
}
QGraphicsPathItem::hoverEnterEvent(e);
}
void DiagramPathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) {
#ifdef DEBUG
std::cout << "left" << std::endl;
#endif
if (isSelected()) {
if(myHoverPoint>-1){
myHoverPoint=-1;
update();
}
}
QGraphicsPathItem::hoverLeaveEvent(e);
}

View File

@ -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 <QGraphicsPixmapItem>
#include <QList>
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<QPointF> 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<QPointF> myPoints;
qreal len,breite;
int mySelPoint,myHoverPoint;
qreal myHandlerWidth;
};
//! [0]
#endif

File diff suppressed because it is too large Load Diff

View File

@ -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 <QGraphicsScene>
#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<QGraphicsItem *> *copiedItems;
qreal myDx,myDy;
DiagramPathItem::DiagramType myArrow;
qreal myGrid;
QGraphicsRectItem myCursor;
qreal myCursorWidth;
bool myGridVisible;
int myGridScale;
QList<QGraphicsItem*> myMoveItems;
qreal maxZ;
};
//! [0]
#endif

View File

@ -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 <QtGui>
#include "diagramtextitem.h"
#include "diagramscene.h"
#include <iostream>
//! [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;
}

View File

@ -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 <QGraphicsTextItem>
#include <QPen>
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

File diff suppressed because it is too large Load Diff

View File

@ -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 <QMainWindow>
#include <QShortcut>
#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<QAction*> listOfActions;
QList<QShortcut*> listOfShortcuts;
bool myShowGrid; // Grid visible ?
qreal myGrid; // Grid distance (dx=dy)
QString myFileName; // aktueller Filename
};
//! [0]
#endif

View File

@ -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

View File

@ -0,0 +1,53 @@
<RCC>
<qresource prefix="/" >
<file>images/background1.png</file>
<file>images/background2.png</file>
<file>images/background3.png</file>
<file>images/background4.png</file>
<file>images/bold.png</file>
<file>images/bringtofront.png</file>
<file>images/delete.png</file>
<file>images/floodfill.png</file>
<file>images/italic.png</file>
<file>images/lc_aligncenter.png</file>
<file>images/lc_aligndown.png</file>
<file>images/lc_alignmiddle.png</file>
<file>images/lc_alignup.png</file>
<file>images/lc_backward.png</file>
<file>images/lc_bold.png</file>
<file>images/lc_bringtofront.png</file>
<file>images/lc_copy.png</file>
<file>images/lc_cut.png</file>
<file>images/lc_fliphorizontal.png</file>
<file>images/lc_flipvertical.png</file>
<file>images/lc_forward.png</file>
<file>images/lc_grid.png</file>
<file>images/lc_griduse.png</file>
<file>images/lc_gridvisible.png</file>
<file>images/lc_group.png</file>
<file>images/lc_italic.png</file>
<file>images/lc_objectalign.png</file>
<file>images/lc_objectalignleft.png</file>
<file>images/lc_objectalignright.png</file>
<file>images/lc_open.png</file>
<file>images/lc_redo.png</file>
<file>images/lc_save.png</file>
<file>images/lc_saveas.png</file>
<file>images/lc_sendtoback.png</file>
<file>images/lc_toggleobjectrotatemode.png</file>
<file>images/lc_underline.png</file>
<file>images/lc_undo.png</file>
<file>images/lc_ungroup.png</file>
<file>images/lc_zoom.png</file>
<file>images/lc_zoomin.png</file>
<file>images/lc_zoomminus.png</file>
<file>images/lc_zoomoptimal.png</file>
<file>images/lc_zoompagewidth.png</file>
<file>images/linecolor.png</file>
<file>images/linepointer.png</file>
<file>images/pointer.png</file>
<file>images/sendtoback.png</file>
<file>images/textpointer.png</file>
<file>images/underline.png</file>
</qresource>
</RCC>