#ifndef CGAL_QT_NEAREST_NEIGHBOR_H #define CGAL_QT_NEAREST_NEIGHBOR_H #include #include #include #include #include #include #include #include #include namespace CGAL { namespace Qt { template class NearestNeighbor : public GraphicsViewInput { typedef typename T::Point_d Point_2; public: NearestNeighbor(QGraphicsScene* s, typename T::Tree * t_, QObject* parent, int N); ~NearestNeighbor(); void setPen(const QPen& pen); void show(); void hide(); void setN(int n) { N = n; } protected: virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); bool eventFilter(QObject *obj, QEvent *event); private: int N; typename T::Tree * t; QGraphicsScene *scene_; QGraphicsEllipseItem* circle; std::vector points; CGAL::Qt::PointsGraphicsItem > * pgi; }; template NearestNeighbor::NearestNeighbor(QGraphicsScene* s, typename T::Tree * t, QObject* parent, int N) : GraphicsViewInput(parent), N(N), t(t), scene_(s) { pgi = new PointsGraphicsItem >(&points); pgi->setVerticesPen(QPen(::Qt::red, 3, ::Qt::SolidLine, ::Qt::RoundCap, ::Qt::RoundJoin)); pgi->setZValue(10); scene_->addItem(pgi); circle = new QGraphicsEllipseItem(); circle->hide(); scene_->addItem(circle); } template NearestNeighbor::~NearestNeighbor() { } template void NearestNeighbor::setPen(const QPen& pen) { circle->setPen(pen); } template void NearestNeighbor::show() { circle->show(); } template void NearestNeighbor::hide() { circle->hide(); } template void NearestNeighbor::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if(t->empty()){ points.clear(); circle->hide(); return; } Point_2 query(event->scenePos().x(), event->scenePos().y()); T search(*t, query, N); points.clear(); double sr = 0; for(typename T::iterator it = search.begin(); it != search.end(); ++it){ if(sr < it->second){ points.push_back(it->first); sr = it->second; } } pgi->modelChanged(); typename CGAL::Kernel_traits::Kernel::Circle_2 c(query, sr); CGAL::Bbox_2 bb = c.bbox(); circle->setRect(bb.xmin(), bb.ymin(), bb.xmax()-bb.xmin(), bb.ymax()-bb.ymin()); circle->show(); } template bool NearestNeighbor::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::GraphicsSceneMouseMove) { QGraphicsSceneMouseEvent *mouseEvent = static_cast(event); mouseMoveEvent(mouseEvent); return false; // don't consume the event } else{ // standard event processing return QObject::eventFilter(obj, event); } } } // namespace Qt } // namespace CGAL #endif // CGAL_QT_NEAREST_NEIGHBOR_H