Results 1 to 13 of 13

Thread: diagram scene example - intersection test

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

    Default diagram scene example - intersection test

    Hello forum,

    I believe almost everyone in the forum have gone through the diagramscene example that comes along with in the Qt Demo.

    I have included a new shape - ellipse in the editor. And i need some hint on how to do the intersection test while rendering the arrow head in the paint(....) function in arrow.cpp



    Any hint would be very helpful .


    Regards
    Sajjad

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

    Default Re: diagram scene example - intersection test

    Hello,

    Sorry forum, i believe that my previous post was very confusing. Let me elaborate again. I would like to draw the arrow between two nodes. The diagram scene example already is drawing the arrow between two QGraphicsPolygonItem. I included a new shape in the editor of type QGraphicsItem and i want to be able to draw arrow and arrow-heads between two nodes.


    From the source i see that while drawing the arrow between the two QGraphicsPolygonItem, intersection test has been performed. How i do the intersection test if the want to draw the arrow between the two nodes. I think those intersection functions are ony avialable for the polygon types.


    Any suggestion will be very helpful.



    Regards
    Sajjad

  3. #3
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: diagram scene example - intersection test

    If I were you, I would write my own function to check the interception point by reffering to following page:
    http://mathworld.wolfram.com/Ellipse...ersection.html
    Don't write a post just to thank someone, use "Thanks" button.

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

    Default Re: diagram scene example - intersection test

    Thanks for the suggestion,

    In that case i think i have to over-ride the function the bool QGraphicsItem::collidesWithItem(...).

    I need some example where this function has been over-ridden.


    Could you please refer me to any examples ?



    Regards
    Sajjad

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: diagram scene example - intersection test

    Quote Originally Posted by sajis997 View Post
    I think those intersection functions are ony avialable for the polygon types.
    Looks to me like you could use QGraphicsEllipseItem::shape().toFillPolygon() without changing the intersection code.

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

    Default Re: diagram scene example - intersection test

    Hi

    I have created the node as a subclass of the QGraphicsItem as follows:

    Qt Code:
    1. class Node : public QGraphicsItem
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    And inside this class i have implemented the shape() function. So in that case i do not have to over-ride the collission detection function, right?

    Now if you take a look at the diagramscene example where the line and arrow head is added to the polygon items based on the intersection test.


    I want to do the same thing on the customized item i have created. I would like to have some hint on doing it.

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: diagram scene example - intersection test

    No need to subclass. For an ellipse try this:

    diagramitem.h
    Qt Code:
    1. enum DiagramType { Step, Conditional, StartEnd, Io, Ellipse };
    To copy to clipboard, switch view to plain text mode 
    diagramitem.cpp
    Qt Code:
    1. case Ellipse:
    2. {
    3. QGraphicsEllipseItem item(-100,-50,200,100);
    4. myPolygon = item.shape().toFillPolygon();
    5. break;
    6. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow::createToolBox()
    Qt Code:
    1. layout->addWidget(createCellWidget(tr("Ellipse"),DiagramItem::Ellipse), 2, 0);
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to norobro for this useful post:

    sajis997 (28th August 2011)

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

    Default Re: diagram scene example - intersection test

    Hello forum,

    Thanks for the useful hints.

    Now in the example diagramscene example the arrow between the diagramitems is only drawn when the the line tool button is enabled.


    If i want to draw the arrow only when the mouse is moved while Ctrl key is pressed down, what are the issues i have to look into.?

    1. I am confused about tracking the mouse event position in side the mouseMoveEvent() function. To draw the line i need to different position. The mouse move event with the Ctrl modifier i get the starting position of the line. and the line ends when the Ctrl modifer is released. Do i have to consider the keyReleaseEvent() as well?


    Please provide me with some suggestion here as well.


    Regards
    Sajjad

  10. #9
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: diagram scene example - intersection test

    Try using QGraphicsSceneMouseEvent::modifiers() in DiagramScene::mousePressEvent() and modify "myMode"

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

    Default Re: diagram scene example - intersection test

    Hello norobro,

    I have tried with the following code snippet inside the mouseMoveEvent() of diagramscne. I have appended another else if as follows:

    Qt Code:
    1. else if(mouseEvent->modifiers() == Qt::CTRL /* && line == 0 */)
    2. {
    3.  
    4. std::cout << "Insert line mode to insert the line and moving mode" << std::endl;
    5.  
    6. //create a new line item with the mouse event
    7. line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
    8. mouseEvent->scenePos()));
    9.  
    10. //set the line color
    11. line->setPen(QPen(myLineColor, 2));
    12.  
    13. //add the line item
    14. addItem(line);
    15. }
    To copy to clipboard, switch view to plain text mode 


    I am having the following output:

    1. When i hold down the Ctrl button while the mouse is moving, , i do get inside the condition but no line is drawn.



    Any more hint?


    Regards
    Sajjad

  12. #11
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: diagram scene example - intersection test

    Hi Sajjad,

    Try this:
    Qt Code:
    1. void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    2. {
    3. if (mouseEvent->button() != Qt::LeftButton)
    4. return;
    5.  
    6. if(mouseEvent->modifiers()==Qt::ControlModifier ) myMode = InsertLine; // add this line
    To copy to clipboard, switch view to plain text mode 
    You might want to save the original mode and reset "myMode" in mouseReleaseEvent(). Don't know if there are any side effects.

    HTH

  13. The following user says thank you to norobro for this useful post:

    sajis997 (14th September 2011)

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

    Default Re: diagram scene example - intersection test

    Hello

    1. Does the following function setMouseTracking(true) has any effect on the mouseMoveEvent() to sub-class of the QGraphicsScene. I think you cannot even call this function inside the definition of the constructor. So in this case mouseMoveEvent() should only be captured only when the mouse button(left) is down while the mouse is moving. I have overriden the mouseMoveEvent(..) for the customized QGraphicsScene class and i do not need to press the left mouse button down while moving the mouse. I think it is strange. The code snippet is as follows:



    Qt Code:
    1. void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
    2. {
    3.  
    4. //std::cout << "Mouse with the ctrl modifiers in the scene " << std::endl;
    5.  
    6. if (myMode == InsertLine && line != 0)
    7. {
    8. std::cout << "Insert line mode to insert the line in the move event" << std::endl;
    9.  
    10. QLineF newLine(line->line().p1(), mouseEvent->scenePos());
    11. line->setLine(newLine);
    12. }
    13. else if(mouseEvent->modifiers() == Qt::ControlModifier /* && line == 0 */)
    14. {
    15. myMode = InsertLine;
    16. // std::cout << "Insert line mode to insert the line and moving mode" << std::endl;
    17.  
    18. //create a new line item with the mouse event
    19. line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
    20. mouseEvent->scenePos()));
    21.  
    22. //set the line color
    23. line->setPen(QPen(myLineColor, 2));
    24.  
    25. //add the line item
    26. addItem(line);
    27.  
    28. std::cout << "Line addead while the Ctrl is down" << std::endl;
    29. }
    30. else if (myMode == MoveItem)
    31. {
    32. //std::cout << "Mouse with the ctrl modifiers in the scene " << std::endl;
    33. QGraphicsScene::mouseMoveEvent(mouseEvent);
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: diagram scene example - intersection test

    Hello forum,

    I think i am close to what i wanted with the existing diagramscene example. Let me explain it briefly again:

    1. With the existing diagramscene example we can connect two diagram item with an arrow only when the InsertLine mode is enabled and with the mouse press event.

    I wanted to add something more to it.

    1. I should be able to do this with mouse move event with the Ctrl modifier. And to get it done i have made the following changes:

    1.1. Inside the mouseMoveEvent of diagramscene i have added the following condition:


    Qt Code:
    1. else if(mouseEvent->modifiers() == Qt::ControlModifier && line == 0 )
    2. {
    3. myMode = InsertLine;
    4.  
    5.  
    6. //create a new line item with the mouse event
    7. line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
    8. mouseEvent->scenePos()));
    9.  
    10. //set the line color
    11. line->setPen(QPen(myLineColor, 2));
    12.  
    13. //add the line item
    14. addItem(line);
    To copy to clipboard, switch view to plain text mode 

    1.2. I have added a keyReleaseEvent() as follows:

    Qt Code:
    1. void DiagramScene::keyReleaseEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Control)
    4. {
    5.  
    6. if(myMode == InsertLine)
    7. {
    8. QList<QGraphicsItem *> startItems = items(line->line().p1());
    9.  
    10. if (startItems.count() && startItems.first() == line)
    11. startItems.removeFirst();
    12.  
    13. QList<QGraphicsItem *> endItems = items(line->line().p2());
    14. if (endItems.count() && endItems.first() == line)
    15. endItems.removeFirst();
    16.  
    17. removeItem(line);
    18. delete line;
    19.  
    20.  
    21. if (startItems.count() > 0 && endItems.count() > 0 &&
    22. startItems.first()->type() == DiagramItem::Type &&
    23. endItems.first()->type() == DiagramItem::Type &&
    24. startItems.first() != endItems.first())
    25. {
    26. DiagramItem *startItem =
    27. qgraphicsitem_cast<DiagramItem *>(startItems.first());
    28. DiagramItem *endItem =
    29. qgraphicsitem_cast<DiagramItem *>(endItems.first());
    30. Arrow *arrow = new Arrow(startItem, endItem);
    31. arrow->setColor(myLineColor);
    32. startItem->addArrow(arrow);
    33. endItem->addArrow(arrow);
    34. arrow->setZValue(-1000.0);
    35. addItem(arrow);
    36. arrow->updatePosition();
    37. }
    38.  
    39. myMode = MoveItem;
    40. }
    41. }
    42.  
    43. line = 0;
    44. QGraphicsScene::keyReleaseEvent(event);
    45. }
    To copy to clipboard, switch view to plain text mode 


    I get it almost right , except the following behavior.

    When i run the application, i take the mouse cursor over the scene and move the mouse while the Ctrl button is down.Then i get the line drawn. Since there is no items beneath the mouse positions, the line should be deleted from the scene and no arrow will be drawn when the Ctrl button is released. This is how is should behave. BUT i am having the line drawn even after the Ctrl is released. It seems that Ctrl release is not captured. Please see the attached image.Screenshot-3.jpg

    The behavior that i have just described does not show up if just do a mouse press initially when the application is started. Then the Ctrl release is also captured.


    I would request someone in the forum to make the above changes in the code and see if you are getting same behavior and suggest me to debug this scenario.

    Please let me know if there is anything missing in the post.



    Regards
    Sajjad

Similar Threads

  1. How to find the intersection point of polylines
    By arashadsaifi in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2009, 10:12
  2. Finding intersection point
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2009, 08:49
  3. Extending the Diagram Scene Example
    By dosto.walla in forum Newbie
    Replies: 1
    Last Post: 7th October 2008, 18:02
  4. finding the Intersection of two lines
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 8th June 2008, 13:16
  5. Find point of intersection/overlaping?
    By Morea in forum Qt Programming
    Replies: 9
    Last Post: 19th April 2007, 14:42

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.