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/GraphicsViewPolylineInput.h $ 7 // $Id: GraphicsViewPolylineInput.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_GRAPHICS_VIEW_POLYLINE_INPUT_H 15 #define CGAL_QT_GRAPHICS_VIEW_POLYLINE_INPUT_H 16 17 #include <CGAL/license/GraphicsView.h> 18 19 20 #include <CGAL/auto_link/Qt.h> 21 #include <CGAL/export/Qt.h> 22 23 #include <QPolygonF> 24 #include <QPointF> 25 #include <QKeyEvent> 26 27 #include <CGAL/Qt/GraphicsViewInput.h> 28 #include <CGAL/Qt/Converter.h> 29 #include <QGraphicsLineItem> 30 31 class QGraphicsScene; 32 class QGraphicsSceneMouseEvent; 33 class QGraphicsItem; 34 class QGraphicsPathItem; 35 class QGraphicsLineItem; 36 class QKeyEvent; 37 class QEvent; 38 class QObject; 39 40 namespace CGAL { 41 namespace Qt { 42 43 class CGAL_QT_EXPORT GraphicsViewPolylineInput_non_templated_base : public GraphicsViewInput 44 { 45 public: setNumberOfVertices(int n)46 void setNumberOfVertices(int n) 47 { 48 n_ = n; 49 } 50 51 bool eventFilter(QObject *obj, QEvent *event); 52 53 protected: 54 // protected constructor 55 GraphicsViewPolylineInput_non_templated_base(QObject* parent, 56 QGraphicsScene* s, 57 int n = 0, 58 bool closed = true); 59 60 61 // mousePressEvent returns true iff the event is consummed 62 bool mousePressEvent(QGraphicsSceneMouseEvent *event); 63 64 void mouseMoveEvent(QGraphicsSceneMouseEvent *event); 65 66 // keyPressEvent returns true iff the event is consummed 67 bool keyPressEvent(QKeyEvent *event); 68 69 void rubberbands(const QPointF& p); 70 71 virtual void generate_polygon() = 0; 72 73 protected: 74 QPolygonF polygon; 75 bool closed_; 76 77 private: 78 QGraphicsPathItem *path_item; 79 QGraphicsLineItem *b, *e; 80 int n_; 81 QPointF sp; 82 QGraphicsScene *scene_; 83 }; // end class GraphicsViewPolylineInput_non_templated_base 84 85 template <typename K> 86 class GraphicsViewPolylineInput : public GraphicsViewPolylineInput_non_templated_base 87 { 88 public: 89 GraphicsViewPolylineInput(QObject* parent, QGraphicsScene* s, int n = 0, bool closed = true) GraphicsViewPolylineInput_non_templated_base(parent,s,n,closed)90 : GraphicsViewPolylineInput_non_templated_base(parent, s, n, closed) 91 { 92 } 93 94 protected: generate_polygon()95 void generate_polygon() { 96 std::list<typename K::Point_2> points; 97 Converter<K> convert; 98 convert(points, this->polygon); 99 if(closed_ && points.size()>2){ 100 points.push_back(points.front()); 101 } 102 Q_EMIT( generate(CGAL::make_object(points))); 103 } 104 }; // end class GraphicsViewPolylineInput 105 106 } // namespace Qt 107 } // namespace CGAL 108 109 #ifdef CGAL_HEADER_ONLY 110 #include <CGAL/Qt/GraphicsViewPolylineInput_impl.h> 111 #endif // CGAL_HEADER_ONLY 112 113 #endif // CGAL_QT_GRAPHICS_VIEW_POLYLINE_INPUT_H 114