diff --git a/agui2/ALIB.pro b/agui2/ALIB.pro index c6e4ce11cfefd079ce7bde631a4c9913137125d5..5411d7090fd206d4a34f80b8a095237a883cdf33 100644 --- a/agui2/ALIB.pro +++ b/agui2/ALIB.pro @@ -31,13 +31,15 @@ SOURCES += main.cpp\ graphicsbox.cpp \ inputdialog.cpp \ graphicsconnection.cpp \ - graphicsscene.cpp + graphicsscene.cpp \ + connectionhelper.cpp HEADERS += mainwindow.h \ graphicsbox.h \ inputdialog.h \ graphicsconnection.h \ - graphicsscene.h + graphicsscene.h \ + connectionhelper.h FORMS += mainwindow.ui \ inputdialog.ui diff --git a/agui2/connectionhelper.cpp b/agui2/connectionhelper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1479089795bd4b099eeced627125a83560c2837 --- /dev/null +++ b/agui2/connectionhelper.cpp @@ -0,0 +1,29 @@ +#include "connectionhelper.h" + +ConnectionHelper &ConnectionHelper::getInstance() +{ + static ConnectionHelper instance; + return instance; +} + +void ConnectionHelper::startConnection(GraphicsBox *origin) +{ + m_originBox = origin; +} + +void ConnectionHelper::completeConnection(GraphicsBox *target) +{ + if( target == NULL || m_originBox == NULL ) + { + m_originBox = NULL; + return; + } + m_Connection = new GraphicsConnection( m_originBox, target, NULL); + m_originBox->addOutConnection(m_Connection); + target->addInConnection(m_Connection); +} + +ConnectionHelper::ConnectionHelper() +{ + m_Connection = NULL; +} diff --git a/agui2/connectionhelper.h b/agui2/connectionhelper.h new file mode 100644 index 0000000000000000000000000000000000000000..3d273ecd2ea0979ce7a01b7bf05a6e3c5901855d --- /dev/null +++ b/agui2/connectionhelper.h @@ -0,0 +1,19 @@ +#ifndef CONNECTIONHELPER_H +#define CONNECTIONHELPER_H + +#include "graphicsconnection.h" +#include "graphicsbox.h" + +class ConnectionHelper +{ +public: + static ConnectionHelper &getInstance(); + void startConnection( GraphicsBox * origin ); + void completeConnection( GraphicsBox * target ); +private: + GraphicsConnection* m_Connection; + GraphicsBox* m_originBox; + ConnectionHelper(); +}; + +#endif // CONNECTIONHELPER_H diff --git a/agui2/graphicsbox.cpp b/agui2/graphicsbox.cpp index 3a8fac830290e392aca5b7fdba5fa2f63c279bdb..a5434db2c22329191c347481ee8245f89d10a1e2 100644 --- a/agui2/graphicsbox.cpp +++ b/agui2/graphicsbox.cpp @@ -8,6 +8,7 @@ #include "inputdialog.h" #include "graphicsconnection.h" #include "graphicsscene.h" +#include "connectionhelper.h" GraphicsBox::GraphicsBox(QGraphicsItem * parent, QString text, qreal x, qreal y) : QGraphicsObject(parent) @@ -23,6 +24,13 @@ GraphicsBox::GraphicsBox(QGraphicsItem * parent, QString text, qreal x, qreal y) painter.setFont(m_font); m_boundRect = painter.fontMetrics().boundingRect(m_text); m_boundRect.adjust(-BOX_MARGIN, -BOX_MARGIN, BOX_MARGIN, BOX_MARGIN); + + m_OutConnection = NULL; +} + +GraphicsBox::~GraphicsBox() +{ + delete m_OutConnection; } QRectF GraphicsBox::boundingRect() const @@ -38,6 +46,26 @@ void GraphicsBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio painter->drawText(m_boundRect, Qt::AlignCenter, m_text ); } +QPointF GraphicsBox::getConnectionOrigin() +{ + return QPointF(m_boundRect.right(), m_boundRect.center().y()); +} + +QPointF GraphicsBox::getConnectionTarget() +{ + return QPointF(m_boundRect.left(), m_boundRect.center().y()); +} + +bool GraphicsBox::addInConnection(GraphicsConnection *connection) +{ + m_InConnections.append(connection); +} + +bool GraphicsBox::addOutConnection(GraphicsConnection *connection) +{ + m_OutConnection = connection; +} + void GraphicsBox::on_SetInput() { InputDialog * inputDlg = new InputDialog(); @@ -47,9 +75,7 @@ void GraphicsBox::on_SetInput() void GraphicsBox::on_Connect() { - QPointF point(m_boundRect.right(), m_boundRect.center().y()); - point = mapToScene(point); - GraphicsConnection* connection = ((GraphicsScene*)scene())->createConnection(point); + ConnectionHelper::getInstance().startConnection(this); } void GraphicsBox::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) @@ -68,15 +94,11 @@ void GraphicsBox::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) void GraphicsBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if( event->button() != Qt::LeftButton ) - { - this->QGraphicsItem::mouseReleaseEvent(event); - return; - } - if( ((GraphicsScene*)scene())->inConnectingState() ) + if( event->button() == Qt::LeftButton ) { - QPointF point(m_boundRect.left(), m_boundRect.center().y()); - ((GraphicsScene*)scene())->attachConnection(this, point); + event->accept(); + ConnectionHelper::getInstance().completeConnection( this ); } this->QGraphicsItem::mouseReleaseEvent(event); + return; } diff --git a/agui2/graphicsbox.h b/agui2/graphicsbox.h index 02b740dec76ebbde1dafead12d134f44028aa004..9409d7419c062f3cd0c4d023a8a6ad7cbe64ad41 100644 --- a/agui2/graphicsbox.h +++ b/agui2/graphicsbox.h @@ -1,12 +1,16 @@ #ifndef GRAPHICSBOX_H #define GRAPHICSBOX_H +#include "graphicsconnection.h" + #include <QPainter> #include <QGraphicsItem> #include <QFont> #define BOX_MARGIN 30 +class GraphicsConnection; + enum BoxTypes { BOX_INPUT, @@ -15,22 +19,25 @@ enum BoxTypes BOX_DOUBLE }; - class GraphicsBox : public QGraphicsObject { Q_OBJECT public: - explicit GraphicsBox(QGraphicsItem * parent, QString text, qreal x, qreal y); - GraphicsBox(QGraphicsItem * parent): QGraphicsObject(parent){}; + GraphicsBox(QGraphicsItem * parent, QString text, qreal x, qreal y); + GraphicsBox(QGraphicsItem * parent): QGraphicsObject(parent) {} + virtual ~GraphicsBox(); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget); - virtual ~GraphicsBox() {}; - QString m_text; + QPointF getConnectionOrigin(); + QPointF getConnectionTarget(); + bool addInConnection( GraphicsConnection* connection ); + bool addOutConnection(GraphicsConnection* connection ); private: - + QString m_text; QRectF m_boundRect; QFont m_font; - + QList<GraphicsConnection*> m_InConnections; + GraphicsConnection* m_OutConnection; private slots: void on_SetInput(); void on_Connect(); diff --git a/agui2/graphicsconnection.cpp b/agui2/graphicsconnection.cpp index d0d6325a46b574a2a274fb0d2553e33488ad0283..34b32701017461c34d334f5c35c759a8f7c02546 100644 --- a/agui2/graphicsconnection.cpp +++ b/agui2/graphicsconnection.cpp @@ -1,22 +1,23 @@ #include "graphicsconnection.h" #include <QPainter> +#include <QGraphicsScene> -GraphicsConnection::GraphicsConnection(qreal x, qreal y, QGraphicsItem * parent) +GraphicsConnection::GraphicsConnection(GraphicsBox * origin, GraphicsBox * target, QGraphicsItem * parent) :QGraphicsItem(parent) { - setPos(x, y); + setPos(0, 0); setZValue(0.5); - m_target = NULL; - m_boundRect = new QRectF(); + m_origin = origin; + m_target = target; + QPointF originPoint = mapFromItem(m_origin, m_origin->getConnectionOrigin()); + QPointF targetPoint = mapFromItem(m_target, m_target->getConnectionTarget()); + m_boundRect = new QRectF( originPoint, targetPoint ); + origin->scene()->addItem(this); } -GraphicsConnection::GraphicsConnection(QPointF point, QGraphicsItem *parent) - : GraphicsConnection( point.x(), point.y(), parent ){} - GraphicsConnection::~GraphicsConnection() { - delete m_target; delete m_boundRect; } @@ -27,35 +28,44 @@ QRectF GraphicsConnection::boundingRect() const void GraphicsConnection::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) { + + QPointF originPoint = mapFromItem(m_origin, m_origin->getConnectionOrigin()); + QPointF targetPoint = mapFromItem(m_target, m_target->getConnectionTarget()); + recalculateBoundingRect(); + // Wrong values to create Rect painter->setPen(QPen(Qt::black,3)); - if(m_target != NULL) - { - painter->drawLine(0, 0, m_target->x()/2.0,0); - painter->drawLine(m_target->x()/2, 0, m_target->x()/2.0, m_target->y()); - painter->drawLine(m_target->x()/2, m_target->y(), m_target->x(), m_target->y()); - } + + painter->drawLine(originPoint.x(), originPoint.y(), + targetPoint.x()/2.0, originPoint.y()); + painter->drawLine(targetPoint.x()/2.0, originPoint.y(), + targetPoint.x()/2.0, targetPoint.y()); + painter->drawLine(targetPoint.x()/2.0, targetPoint.y(), + targetPoint.x(), targetPoint.y()); + painter->setPen(QPen(Qt::red,1)); painter->drawRect(*m_boundRect); } -bool GraphicsConnection::setTarget(QGraphicsItem * target, QPointF targetPoint) +void GraphicsConnection::recalculateBoundingRect() { - m_target = new QPointF( mapFromItem(target, targetPoint) ); - prepareGeometryChange(); - m_boundRect->setRect(0,0,m_target->x(),m_target->y()); -} + QPointF originPoint = mapFromItem(m_origin, m_origin->getConnectionOrigin()); + QPointF targetPoint = mapFromItem(m_target, m_target->getConnectionTarget()); -void GraphicsConnection::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) -{ - this->QGraphicsItem::mouseReleaseEvent(event); -// QList<QGraphicsItem*> list = this->collidingItems(Qt::IntersectsItemBoundingRect); -// if( !list.isEmpty() ) -// { -// if(list.contains(parentItem())) -// { -// return; -// } -// } + qreal x = 0.0; + qreal y = 0.0; + if( originPoint.x() <= targetPoint.x() ) + x = originPoint.x(); + else + x = targetPoint.x(); + if( originPoint.y() <= targetPoint.y() ) + y = originPoint.y(); + else + y = targetPoint.y(); + qreal width = qAbs( originPoint.x() - targetPoint.x()); + qreal height = qAbs( originPoint.y() - targetPoint.y()); + + prepareGeometryChange(); + m_boundRect->setRect( x, y, width, height ); } diff --git a/agui2/graphicsconnection.h b/agui2/graphicsconnection.h index 0f522e9619d264981b812da5f28d15240f28b6b2..6b2eabf8ab7949bfcf039a143b21fa058f40e64d 100644 --- a/agui2/graphicsconnection.h +++ b/agui2/graphicsconnection.h @@ -1,22 +1,24 @@ #ifndef GRAPHICSCONNECTION_H #define GRAPHICSCONNECTION_H +#include "graphicsbox.h" + #include <QGraphicsItem> #include <QGraphicsSceneMouseEvent> +class GraphicsBox; + class GraphicsConnection : public QGraphicsItem { public: - GraphicsConnection(qreal x, qreal y, QGraphicsItem *parent); - GraphicsConnection(QPointF point, QGraphicsItem *parent); + GraphicsConnection(GraphicsBox * origin, GraphicsBox * target, QGraphicsItem * parent); ~GraphicsConnection(); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget); - bool setTarget(QGraphicsItem * target, QPointF targetPoint); -protected: - void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); private: - QPointF * m_target; + void recalculateBoundingRect(); + GraphicsBox * m_target; + GraphicsBox * m_origin; QRectF * m_boundRect; }; diff --git a/agui2/graphicsscene.cpp b/agui2/graphicsscene.cpp index caed1fc6b2b49160c674ccef0d33cb0cddbbc8b8..46a7580376edd2f27720fb7b7fa019a4202337b4 100644 --- a/agui2/graphicsscene.cpp +++ b/agui2/graphicsscene.cpp @@ -5,27 +5,6 @@ GraphicsScene::GraphicsScene(QObject *parent) : QGraphicsScene(parent) { - m_Connecting = false; -} - -GraphicsConnection *GraphicsScene::createConnection(QPointF origin) -{ - m_Connecting = true; - GraphicsConnection * connection = new GraphicsConnection( origin, 0 ); - m_OpenConnection = connection; - this->addItem(connection); - return connection; -} - -GraphicsConnection *GraphicsScene::attachConnection(QGraphicsItem * target, QPointF targetPoint) -{ - m_OpenConnection->setTarget(target, targetPoint); - m_Connecting = false; -} - -bool GraphicsScene::inConnectingState() -{ - return m_Connecting; } void GraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event) @@ -39,11 +18,6 @@ void GraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event) void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { QGraphicsItem* item = itemAt(event->scenePos(),views().first()->transform()); - if( item == NULL ) - { - m_Connecting = false; - if( m_Connecting) - delete m_OpenConnection; - } +// if( item == NULL ) this->QGraphicsScene::mouseReleaseEvent( event ); } diff --git a/agui2/graphicsscene.h b/agui2/graphicsscene.h index 660e7ea1d1ef06dd41742a0f6455e5619d3073fa..2d13d862699f0074400b358d080a5ab40482f42c 100644 --- a/agui2/graphicsscene.h +++ b/agui2/graphicsscene.h @@ -14,17 +14,9 @@ class GraphicsScene : public QGraphicsScene Q_OBJECT public: GraphicsScene(QObject * parent); - GraphicsConnection* createConnection(QPointF origin); - GraphicsConnection *attachConnection(QGraphicsItem *target, QPointF targetPoint); - bool inConnectingState(); -private: - bool m_Connecting = false; - GraphicsConnection * m_OpenConnection; protected: void wheelEvent(QGraphicsSceneWheelEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); -public slots: - }; #endif // GRAPHICSSCENE_H