1 // Copyright (c) 2008  GeometryFactory Sarl (France).
2 // All rights reserved.
3 //
4 // This file is part of CGAL (www.cgal.org).
5 //
6 // $URL: https://github.com/CGAL/cgal/blob/v5.3/GraphicsView/include/CGAL/Qt/RegularGridGraphicsItem.h $
7 // $Id: RegularGridGraphicsItem.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
8 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
9 //
10 //
11 // Author(s)     : Andreas Fabri <Andreas.Fabri@geometryfactory.com>
12 //                 Laurent Rineau <Laurent.Rineau@geometryfactory.com>
13 
14 #ifndef CGAL_QT_REGULAR_GRID_GRAPHICS_ITEM_H
15 #define CGAL_QT_REGULAR_GRID_GRAPHICS_ITEM_H
16 
17 #include <CGAL/license/GraphicsView.h>
18 
19 
20 #include <CGAL/Bbox_2.h>
21 #include <CGAL/Qt/PainterOstream.h>
22 #include <CGAL/Qt/GraphicsItem.h>
23 #include <CGAL/Qt/Converter.h>
24 #include <CGAL/Qt/utility.h>
25 
26 #include <QGraphicsScene>
27 #include <QPainter>
28 #include <QStyleOption>
29 
30 namespace CGAL {
31 namespace Qt {
32 
33   template <typename K>
34 class RegularGridGraphicsItem : public GraphicsItem
35 {
36   typedef K Geom_traits;
37   typedef typename K::Point_2 Point_2;
38   typedef typename K::Vector_2 Vector_2;
39   typedef typename K::Segment_2 Segment_2;
40 
41 public:
42   RegularGridGraphicsItem(double dx, double dy);
43 
44   void modelChanged();
45 
46 public:
47 
48   QRectF boundingRect() const;
49 
50   void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
51 
52 
verticesPen()53   const QPen& verticesPen() const
54   {
55     return vertices_pen;
56   }
57 
edgesPen()58   const QPen& edgesPen() const
59   {
60     return edges_pen;
61   }
62 
setVerticesPen(const QPen & pen)63   void setVerticesPen(const QPen& pen)
64   {
65     vertices_pen = pen;
66   }
67 
setEdgesPen(const QPen & pen)68   void setEdgesPen(const QPen& pen)
69   {
70     edges_pen = pen;
71   }
72 
visibleVertices()73   bool visibleVertices() const
74   {
75     return visible_vertices;
76   }
77 
setVisibleVertices(const bool b)78   void setVisibleVertices(const bool b)
79   {
80     visible_vertices = b;
81     update();
82   }
83 
visibleEdges()84   bool visibleEdges() const
85   {
86     return visible_edges;
87   }
88 
setVisibleEdges(const bool b)89   void setVisibleEdges(const bool b)
90   {
91     visible_edges = b;
92     update();
93   }
94 
setDelta(double x,double y)95   void setDelta(double x, double y)
96   {
97     dx = x;
98     dy = y;
99     update();
100   }
101 
102 protected:
103   void updateBoundingBox();
104   double dx, dy;
105   QPainter* m_painter;
106   PainterOstream<Geom_traits> painterostream;
107 
108   QPen vertices_pen;
109   QPen edges_pen;
110   bool visible_edges;
111   bool visible_vertices;
112 };
113 
114 
115   template <typename K>
RegularGridGraphicsItem(double dx,double dy)116   RegularGridGraphicsItem<K>::RegularGridGraphicsItem(double dx, double dy)
117     :  dx(dx), dy(dy), painterostream(0),
118      visible_edges(true), visible_vertices(true)
119 {
120   setVerticesPen(QPen(::Qt::red, 3.));
121   setZValue(-1);
122 }
123 
124   template <typename K>
125 QRectF
boundingRect()126   RegularGridGraphicsItem<K>::boundingRect() const
127 {
128   QRectF rect = CGAL::Qt::viewportsBbox(scene());
129   return rect;
130 }
131 
132 
133 
134 
135 
136 
137   template <typename K>
138 void
paint(QPainter * painter,const QStyleOptionGraphicsItem *,QWidget *)139   RegularGridGraphicsItem<K>::paint(QPainter *painter,
140                                     const QStyleOptionGraphicsItem * /*option*/,
141                                     QWidget * /*widget*/)
142 {
143   QRectF rect = boundingRect();
144   double b = rect.bottom();
145   double t = rect.top();
146   double l = rect.left();
147   double r = rect.right();
148 
149   if(b > t) std::swap(b,t); // because things are upside down in Qt
150 
151   painterostream = PainterOstream<Geom_traits>(painter);
152   painter->setPen(this->edgesPen());
153 
154   double ll = l;
155   ll = dx * static_cast<int>(ll/dx);
156 
157   for(; ll < r; ll += dx){
158     painterostream << Segment_2(Point_2(ll,b),
159                                 Point_2(ll,t));
160   }
161 
162 
163   double bb = b;
164   bb = dy * static_cast<int>(bb/dy);
165 
166   for(; bb < t; bb += dy){
167     painterostream << Segment_2(Point_2(l,bb),
168                                 Point_2(r,bb));
169   }
170 
171 
172   /*
173   painter->setPen(this->verticesPen());
174   QTransform matrix = painter->worldTransform();
175   painter->resetTransform();
176   for(int i = 0; i < nw; i++){
177     for(int j = 0; j < nh; j++){
178       painter->drawPoint(matrix.map(QPointF(i*dw, j*dh)));
179     }
180   }
181   */
182 }
183 
184   template <typename K>
185 void
updateBoundingBox()186   RegularGridGraphicsItem<K>::updateBoundingBox()
187 {}
188 
189 
190   template <typename K>
191 void
modelChanged()192   RegularGridGraphicsItem<K>::modelChanged()
193 {
194   update();
195 }
196 
197 
198 } // namespace Qt
199 } // namespace CGAL
200 
201 #endif // CGAL_QT_REGULAR_GRID_GRAPHICS_ITEM_H
202