Advertisement
Guest User

PointsGraphicsItem.h

a guest
Apr 4th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Copyright (c) 2008  GeometryFactory Sarl (France).
  2. // All rights reserved.
  3. //
  4. // This file is part of CGAL (www.cgal.org).
  5. // You can redistribute it and/or modify it under the terms of the GNU
  6. // General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. //
  9. // Licensees holding a valid commercial license may use this file in
  10. // accordance with the commercial license agreement provided with the software.
  11. //
  12. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  13. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  14. //
  15. // $URL$
  16. // $Id$
  17. //
  18. //
  19. // Author(s)     : Andreas Fabri <Andreas.Fabri@geometryfactory.com>
  20. //                 Laurent Rineau <Laurent.Rineau@geometryfactory.com>
  21.  
  22. #ifndef CGAL_QT_POINTS_GRAPHICS_ITEM_H
  23. #define CGAL_QT_POINTS_GRAPHICS_ITEM_H
  24.  
  25. #include <CGAL/Bbox_2.h>
  26. #include <CGAL/bounding_box.h>
  27. #include <CGAL/Qt/PainterOstream.h>
  28. #include <CGAL/Qt/GraphicsItem.h>
  29. #include <CGAL/Qt/Converter.h>
  30.  
  31. #include <QGraphicsScene>
  32. #include <QPainter>
  33. #include <QStyleOption>
  34.  
  35. namespace CGAL {
  36. namespace Qt {
  37.  
  38. template <typename P>
  39. class PointsGraphicsItem : public GraphicsItem
  40. {
  41.   typedef typename std::iterator_traits<typename P::iterator>::value_type Point_2;
  42.   typedef typename CGAL::Kernel_traits<Point_2>::Kernel Traits;
  43.  
  44. public:
  45.   PointsGraphicsItem(P* p_);
  46.  
  47.   void modelChanged();
  48.  
  49. public:
  50.   QRectF boundingRect() const;
  51.  
  52.   void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  53.  
  54.  
  55.   const QPen& verticesPen() const
  56.   {
  57.     return vertices_pen;
  58.   }
  59.  
  60.   void setVerticesPen(const QPen& pen)
  61.   {
  62.     vertices_pen = pen;
  63.   }
  64.  
  65.   bool drawVertices() const
  66.   {
  67.     return draw_vertices;
  68.   }
  69.  
  70.   void setDrawVertices(const bool b)
  71.   {
  72.     draw_vertices = b;
  73.     update();
  74.   }
  75.  
  76. protected:
  77.   void updateBoundingBox();
  78.  
  79.   P * points;
  80.   QPainter* m_painter;
  81.   PainterOstream<Traits> painterostream;
  82.  
  83.  
  84.   QRectF bounding_rect;
  85.  
  86.   QPen vertices_pen;
  87.   bool draw_vertices;
  88. };
  89.  
  90.  
  91. template <typename P>
  92. PointsGraphicsItem<P>::PointsGraphicsItem(P * p_)
  93.   :  points(p_), painterostream(0),  draw_vertices(true)  
  94. {
  95.   setVerticesPen(QPen(::Qt::red, 3.));
  96.   if(points->size() == 0){
  97.     this->hide();
  98.   }
  99.   updateBoundingBox();
  100.   setZValue(3);
  101. }
  102.  
  103. template <typename P>
  104. QRectF
  105. PointsGraphicsItem<P>::boundingRect() const
  106. {
  107.   return bounding_rect;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. template <typename P>
  114. void
  115. PointsGraphicsItem<P>::paint(QPainter *painter,
  116.                                     const QStyleOptionGraphicsItem * /*option*/,
  117.                                     QWidget * /*widget*/)
  118. {
  119.   if(drawVertices()) {
  120.     Converter<Traits> convert;
  121.  
  122.     painter->setPen(verticesPen());
  123.     QMatrix matrix = painter->matrix();
  124.     painter->resetMatrix();
  125.     for(typename P::iterator it = points->begin();
  126.         it != points->end();
  127.         it++){
  128.       QPointF point = matrix.map(convert(*it));
  129.       painter->drawPoint(point);
  130.     }
  131.   }
  132. }
  133.  
  134. // We let the bounding box only grow, so that when vertices get removed
  135. // the maximal bbox gets refreshed in the GraphicsView
  136. template <typename P>
  137. void
  138. PointsGraphicsItem<P>::updateBoundingBox()
  139. {
  140.   Converter<Traits> convert;
  141.   prepareGeometryChange();
  142.   if(points->size() == 0){
  143.     return;
  144.   }
  145.   bounding_rect = convert(CGAL::bounding_box(points->begin(), points->end()));
  146. }
  147.  
  148.  
  149. template <typename P>
  150. void
  151. PointsGraphicsItem<P>::modelChanged()
  152. {
  153.   if((points->size() == 0) ){
  154.     this->hide();
  155.   } else if((points->size() > 0) && (! this->isVisible())){
  156.     this->show();
  157.   }
  158.   updateBoundingBox();
  159.   update();
  160. }
  161.  
  162.  
  163. } // namespace Qt
  164. } // namespace CGAL
  165.  
  166. #endif // CGAL_QT_POINTS_GRAPHICS_ITEM_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement