Results 1 to 6 of 6

Thread: elastic node re-engineering

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default elastic node re-engineering

    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

    Qt Code:
    1. setAcceptHoverEvents(true);
    To copy to clipboard, switch view to plain text mode 

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


    Qt Code:
    1. //the following events are for the items visual appearance
    2. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    3. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    4. //addded by sajjad
    5. void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
    6. void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    7. void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    To copy to clipboard, switch view to plain text mode 

    And the definition is as follows:

    Qt Code:
    1. /////////////////////////////////////////
    2.  
    3. void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
    4. {
    5. update();
    6. QGraphicsItem::mousePressEvent(event);
    7. }
    8. void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    9. {
    10. update();
    11. QGraphicsItem::mouseReleaseEvent(event);
    12. }
    13. void Node::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
    14. {
    15. if(event->modifiers() == Qt::ControlModifier)
    16. {
    17. update();
    18. QGraphicsItem::hoverMoveEvent(event);
    19. }
    20. }
    21. void Node::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
    22. {
    23. if(event->modifiers() == Qt::ControlModifier)
    24. {
    25. update();
    26. QGraphicsItem::hoverEnterEvent(event);
    27. }
    28. }
    29. void Node::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
    30. {
    31.  
    32. update();
    33. QGraphicsItem::hoverMoveEvent(event);
    34. }
    To copy to clipboard, switch view to plain text mode 
    /////////////////////////////////////////////

    And the nodes paint function is also updatd:


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

    Qt Code:
    1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    2. {
    3. painter->setPen(Qt::NoPen);
    4. painter->setBrush(Qt::darkGray);
    5. painter->drawEllipse(-7, -7, 20, 20);
    6. QRadialGradient gradient(-3, -3, 10);
    7. if (option->state & QStyle::State_Sunken)
    8. {
    9. //button pressed mode
    10. gradient.setCenter(3, 3);
    11. gradient.setFocalPoint(3, 3);
    12. gradient.setColorAt(1, QColor(Qt::yellow).light(120));
    13. gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
    14. //gradient.setColorAt(0, QColor(Qt::red).light(120));
    15. //qDebug() << "Sunken state" << endl;
    16. std::cout << "It is in the sunken state" << std::endl;
    17. }
    18. else if(option->state & QStyle::State_MouseOver)
    19. {
    20. gradient.setColorAt(0, Qt::red);
    21. gradient.setColorAt(1, Qt::darkYellow);
    22. std::cout << "It is in the mosue over state" << std::endl;
    23. }
    24. else if(option->state & (!QStyle::State_Sunken & QStyle::State_MouseOver))
    25. {
    26. gradient.setColorAt(0, Qt::yellow);
    27. gradient.setColorAt(1, Qt::darkYellow);
    28. }
    29. else
    30. {
    31. gradient.setColorAt(0, Qt::yellow);
    32. gradient.setColorAt(1, Qt::darkYellow);
    33. std::cout << "It is in the initial state" << std::endl;
    34. }
    35. painter->setBrush(gradient);
    36. painter->setPen(QPen(Qt::black, 0));
    37. painter->drawEllipse(-10, -10, 20, 20);
    38. }
    To copy to clipboard, switch view to plain text mode 

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


    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: elastic node re-engineering

    What you want goes far beyond the architecture of elastic nodes example and using hover events is likely a wrong approach to the problem. Instead of focusing on painting, you should focus on data. Painting only presents the data. Currently you are trying to implement logic inside painting, this doesn't make much sense. If you want something done on Ctrl key then override keyPressEvent for something that has focus and can accept a key event (like the scene or the view). There manipulate your data and let it update its visual representation based on its logical representation and not based on the current state of the environment around it (like the position of the mouse pointer).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: elastic node re-engineering

    Quote Originally Posted by wysota View Post
    What you want goes far beyond the architecture of elastic nodes example and using hover events is likely a wrong approach to the problem. Instead of focusing on painting, you should focus on data. Painting only presents the data. Currently you are trying to implement logic inside painting, this doesn't make much sense. If you want something done on Ctrl key then override keyPressEvent for something that has focus and can accept a key event (like the scene or the view). There manipulate your data and let it update its visual representation based on its logical representation and not based on the current state of the environment around it (like the position of the mouse pointer).
    Thanks for the useful suggestion, i shall look into this. It will get easier to follow if you can forward me to any existing Qt application where it has something which is i am trying to achieve.

    It seems that you have already gone through and solved the issue. Can you provide me with the link of such application having this behavior ?


    Thanks
    Sajjad

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: elastic node re-engineering

    I don't have such an application at hand, I just know how things work inside Qt and how some things should be implemented. There is no single example you can base your work on. If you have specific problems, we'll help you to solve them. Now it's important to get your thinking on the right track.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: elastic node re-engineering

    Quote Originally Posted by wysota View Post
    What you want goes far beyond the architecture of elastic nodes example and using hover events is likely a wrong approach to the problem.
    Should i go for mouseMoveEvent() instead. Mouse Move Event is only functional when the left mouse button is hold down unless we declare the mouse tracking is switched on. I can switch it on inside the constructors. Now i need the Ctrl modifier. And i was thinking of using the modifier inside the mouseMoveEvent. In that case do we need to over-ride the keyPressEvent() ? I am not sure how to define the mouse modifier inside the keyPressEvent().


    Quote Originally Posted by wysota View Post
    Instead of focusing on painting, you should focus on data. Painting only presents the data.
    So i should move the data conditions inside the data() function instead

    Quote Originally Posted by wysota View Post
    Currently you are trying to implement logic inside painting, this doesn't make much sense. If you want something done on Ctrl key then override keyPressEvent for something that has focus and can accept a key event (like the scene or the view).
    Sorry, i believe that i misunderstood . Did you mean to over-ride the keyPressEvent() for the view or the item instead?


    Hope to hear more from you about this issue.


    Thanks
    Sajjad

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: elastic node re-engineering

    Quote Originally Posted by sajis997 View Post
    Should i go for mouseMoveEvent() instead. Mouse Move Event is only functional when the left mouse button is hold down unless we declare the mouse tracking is switched on. I can switch it on inside the constructors. Now i need the Ctrl modifier. And i was thinking of using the modifier inside the mouseMoveEvent. In that case do we need to over-ride the keyPressEvent() ? I am not sure how to define the mouse modifier inside the keyPressEvent().
    It's one or the other. The choice is yours to make -- you can check the state of key modifiers in mouse events or check the mouse cursor position in key events.


    So i should move the data conditions inside the data() function instead
    You should add the concept of state to your object. Painting will reflect the current state.

    Sorry, i believe that i misunderstood . Did you mean to over-ride the keyPressEvent() for the view or the item instead?
    There is no single correct way of solving a problem. I would probably override events for the view or the scene (depending on the detailed behaviour I'd want) instead of the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. elastic nodes example
    By sajis997 in forum Newbie
    Replies: 2
    Last Post: 3rd August 2011, 15:08
  2. Reverse engineering tools
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 29th September 2010, 17:48
  3. elastic QGraphicsItem (layout problem)
    By petar in forum Newbie
    Replies: 9
    Last Post: 11th November 2009, 17:12
  4. how to justfy a node is leaf node or not
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2009, 07:40
  5. Reverse engineering of ui-file
    By wallyqt in forum Qt Tools
    Replies: 5
    Last Post: 18th November 2007, 22:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.