[Qt-interest] elastic nodes re-engineering

Sajjad dosto.walla at gmail.com
Thu Aug 4 19:36:46 CEST 2011


Hello forum,


I am playing around with the elastic node example and have the some issue to
discuss with:


When the scene loads the nodes are already connected with edges and i want
to change this attribute. The edge will be connected with two adjacent nodes
in the following manner:

1. When the user move the cursor over one of the adjacent nodes and press
the Ctrl modifier and the nodes gets highlighted and remains highlighted as
long as the Ctrl button is pressed while the cursor is over the node.

2. When the user move the mouse cursor while the Ctrl button is pressed down
, the edge arrow will be drawn from the source node. And When the user
cursor reach any of the destination node , the edge arrow will finish
drawing at the destination node.

Initially , I did the following changes inside the node class :


1. Inside the constructor , i have set the

[code]
setAcceptHoverEvents(true);
[/code]

I am having trouble with  the highlighting effect. I have over-ridden the
following functions:


      //the following events are for the items visual appearance

    void mousePressEvent(QGraphicsSceneMouseEvent *event);

     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

     //addded by sajjad

    void hoverMoveEvent(QGraphicsSceneHoverEvent *event);

    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);




And the definition is as follows:


/////////////////////////////////////////

  void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)

{

    update();

    QGraphicsItem::mousePressEvent(event);

}

 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)

{

        update();

        QGraphicsItem::mouseReleaseEvent(event);

}

 void Node::hoverMoveEvent(QGraphicsSceneHoverEvent *event)

{

    if(event->modifiers() == Qt::ControlModifier)

    {

        update();

        QGraphicsItem::hoverMoveEvent(event);

    }

}

 void Node::hoverEnterEvent(QGraphicsSceneHoverEvent *event)

{

    if(event->modifiers() == Qt::ControlModifier)

    {

        update();

        QGraphicsItem::hoverEnterEvent(event);

    }

}

  void Node::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)

{


        update();

        QGraphicsItem::hoverMoveEvent(event);

}


/////////////////////////////////////////////

And the nodes paint function is also updatd:


/////////////////////////////////////////////////////////////////

  void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem
*option, QWidget *)

{

    painter->setPen(Qt::NoPen);

    painter->setBrush(Qt::darkGray);

    painter->drawEllipse(-7, -7, 20, 20);

     QRadialGradient gradient(-3, -3, 10);

     if (option->state & QStyle::State_Sunken)

    {

        //button pressed mode

        gradient.setCenter(3, 3);

        gradient.setFocalPoint(3, 3);

        gradient.setColorAt(1, QColor(Qt::yellow).light(120));

        gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));

        //gradient.setColorAt(0, QColor(Qt::red).light(120));

 	//qDebug() << "Sunken state" << endl;

        std::cout << "It is in the sunken state" << std::endl;

    }

    else if(option->state & QStyle::State_MouseOver)

    {

        gradient.setColorAt(0, Qt::red);

        gradient.setColorAt(1, Qt::darkYellow);

         std::cout << "It is in the mosue over state" << std::endl;

    }

    else if(option->state & (!QStyle::State_Sunken & QStyle::State_MouseOver))

    {

        gradient.setColorAt(0, Qt::yellow);

        gradient.setColorAt(1, Qt::darkYellow);

    }

    else

    {

        gradient.setColorAt(0, Qt::yellow);

        gradient.setColorAt(1, Qt::darkYellow);

         std::cout << "It is in the initial state" << std::endl;

     }

     painter->setBrush(gradient);

    painter->setPen(QPen(Qt::black, 0));

    painter->drawEllipse(-10, -10, 20, 20);

}




//////////////////////////////////////////////////////////////////


When i press the mouse button over a node , it sunken and the view is
updated , but when i release the button , the node gets highlighted which
should not happen. Node will only be highlighted only when the Ctrl modifier
will be pressed.

If my explanation is not clear enough, please ask me for further
elaboration.

I need some hint to resolve the issue.



Regards
Sajjad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110804/595f1a37/attachment.html 


More information about the Qt-interest-old mailing list