File: ArcsGraphicsItem.h

package info (click to toggle)
cgal 4.9-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 85,584 kB
  • sloc: cpp: 640,841; ansic: 140,696; sh: 708; fortran: 131; makefile: 114; python: 92
file content (180 lines) | stat: -rw-r--r-- 3,954 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef CGAL_QT_ARCS_GRAPHICS_ITEM_H
#define CGAL_QT_ARCS_GRAPHICS_ITEM_H

#include <CGAL/Bbox_2.h>
#include <CGAL/Qt/PainterOstream.h>
#include <CGAL/Qt/GraphicsItem.h>
#include <CGAL/Qt/Converter.h>

#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>

namespace CGAL {
namespace Qt {

template <typename CK>
class ArcsGraphicsItem : public GraphicsItem
{
  std::vector<CGAL::Object>& arcs, &intersections;
  typedef typename CK::Circle_2 Circle_2;
  typedef typename CK::Circular_arc_2 Circular_arc_2;
  typedef typename CK::Circular_arc_point_2 Circular_arc_point_2;
  typedef typename CK::Line_arc_2 Line_arc_2;

public:
  ArcsGraphicsItem(std::vector<CGAL::Object>& arcs_, std::vector<CGAL::Object>& intersections_);

  void modelChanged();

public:
  QRectF boundingRect() const;
  
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  

  const QPen& intersectionsPen() const
  {
    return intersections_pen;
  }

  const QPen& inputPen() const
  {
    return input_pen;
  }

  void setIntersectionsPen(const QPen& pen)
  {
    intersections_pen = pen;
  }

  void setInputPen(const QPen& pen)
  {
    input_pen = pen;
  }

protected:
  void updateBoundingBox();

  QPainter* m_painter;
  PainterOstream<CK> painterostream;

  QRectF bounding_rect;

  QPen intersections_pen;
  QPen input_pen;  
  Converter<CK> convert;
};


template <typename CK>
ArcsGraphicsItem<CK>::ArcsGraphicsItem(std::vector<CGAL::Object>& arcs_, std::vector<CGAL::Object>& intersections_)
  :  arcs(arcs_), intersections(intersections_), painterostream(0)
{
  setIntersectionsPen(QPen(::Qt::red, 3.));
  if(arcs.empty()){
    this->hide();
  }
  updateBoundingBox();
  setZValue(3);
}

template <typename CK>
QRectF 
ArcsGraphicsItem<CK>::boundingRect() const
{
  return bounding_rect;
}



template <typename CK>
void 
ArcsGraphicsItem<CK>::paint(QPainter *painter, 
                                    const QStyleOptionGraphicsItem *,
                                    QWidget * )
{
  painter->setPen(this->inputPen());
  painterostream = PainterOstream<CK>(painter);
 
  for(std::vector<CGAL::Object>::iterator it = arcs.begin(); it != arcs.end(); ++it){
    Circular_arc_2 ca;
    Line_arc_2 la;
    if(assign(ca, *it)){
      painterostream << ca;
    } else if(assign(la, *it)){
      painterostream << la;
    }
  }


  painter->setPen(this->intersectionsPen());
  painterostream = PainterOstream<CK>(painter);
  for(std::vector<CGAL::Object>::iterator it = intersections.begin(); it != intersections.end(); ++it){
    std::pair<Circular_arc_point_2,unsigned> cap_ui;
    Circular_arc_2 ca;
    Line_arc_2 la;

    if(assign(cap_ui, *it)){
      QMatrix matrix = painter->matrix();
      painter->resetMatrix();
      painter->drawPoint(matrix.map(convert(cap_ui.first)));
      painter->setMatrix(matrix);
    }if(assign(ca, *it)){
      painterostream << ca;
    } else if(assign(la, *it)){
      painterostream << la;
    }
  }
}

template <typename CK>
void 
ArcsGraphicsItem<CK>::updateBoundingBox()
{
  bounding_rect = QRectF(0,0,100, 100);
  Bbox_2 bb;
  bool initialized = false;
  for(std::vector<CGAL::Object>::iterator it = arcs.begin(); it != arcs.end(); ++it){
    Circular_arc_2 ca;
    Line_arc_2 la;
    if(assign(ca, *it)){
      if(initialized){
	bb = bb + ca.supporting_circle().bbox();
      } else {
	initialized = true;
	bb = ca.supporting_circle().bbox();
      }
    } else if(assign(la, *it)){
      if(initialized){
	bb = bb + la.bbox();
      } else {
	initialized = true;
	bb = la.bbox();
      }
    }
  }

  prepareGeometryChange();
  bounding_rect = convert(bb);
}


template <typename CK>
void 
ArcsGraphicsItem<CK>::modelChanged()
{
  if((arcs.empty()) ){
    this->hide();
  } else if((! arcs.empty()) && (! this->isVisible())){
    this->show();
  }
  updateBoundingBox();
  update();
}


} // namespace Qt
} // namespace CGAL

#endif // CGAL_QT_ARCS_GRAPHICS_ITEM_H