// Copyright (c) 2008 GeometryFactory Sarl (France). // All rights reserved. // // This file is part of CGAL (www.cgal.org). // // $URL: https://github.com/CGAL/cgal/blob/v5.1/GraphicsView/include/CGAL/Qt/GraphicsViewIsoRectangleInput.h $ // $Id: GraphicsViewIsoRectangleInput.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial // // // Author(s) : Andreas Fabri // Laurent Rineau #ifndef CGAL_QT_GRAPHICS_VIEW_ISO_RECTANGLE_INPUT_H #define CGAL_QT_GRAPHICS_VIEW_ISO_RECTANGLE_INPUT_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace CGAL { namespace Qt { template class GraphicsViewIsoRectangleInput : public GraphicsViewInput { public: GraphicsViewIsoRectangleInput(QObject *parent, QGraphicsScene* s); ~GraphicsViewIsoRectangleInput(); protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void keyPressEvent(QKeyEvent *event); bool eventFilter(QObject *obj, QEvent *event); private: typedef typename K::Point_2 Point_2; QPointF qp, qq, qr; Point_2 p, q, r; QGraphicsRectItem *rectItem; QPointF rect_first_point; QGraphicsScene *scene_; Converter convert; }; template GraphicsViewIsoRectangleInput::GraphicsViewIsoRectangleInput(QObject *parent, QGraphicsScene* s) : GraphicsViewInput(parent), rectItem(new QGraphicsRectItem), scene_(s) { rectItem->setBrush(QBrush()); scene_->addItem(rectItem); rectItem->hide(); rectItem->setZValue(10000); } template GraphicsViewIsoRectangleInput::~GraphicsViewIsoRectangleInput() { delete rectItem; } template void GraphicsViewIsoRectangleInput::mousePressEvent(QGraphicsSceneMouseEvent *event) { if(event->modifiers() & ::Qt::ShiftModifier){ return; } if(event->button() == ::Qt::LeftButton) { if(rectItem->isVisible()) { // we have clicked twice Q_EMIT generate(CGAL::make_object(convert(rectItem->rect()))); rectItem->hide(); } else { // we enter a first point rect_first_point = event->scenePos(); rectItem->setRect(QRectF(rect_first_point, rect_first_point)); rectItem->show(); } } } template void GraphicsViewIsoRectangleInput::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { // todo: only do this if no modifiers are pressed at the same time if(rectItem->isVisible()) { rectItem->setRect(QRectF(rect_first_point, event->scenePos())); } } template void GraphicsViewIsoRectangleInput::keyPressEvent ( QKeyEvent * event ) { } template bool GraphicsViewIsoRectangleInput::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::GraphicsSceneMousePress) { QGraphicsSceneMouseEvent *mouseEvent = static_cast(event); mousePressEvent(mouseEvent); return true; } else if (event->type() == QEvent::GraphicsSceneMouseMove) { QGraphicsSceneMouseEvent *mouseEvent = static_cast(event); mouseMoveEvent(mouseEvent); return true; } else if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(event); keyPressEvent(keyEvent); return true; } else{ // standard event processing return QObject::eventFilter(obj, event); } } } // namespace Qt } // namespace CGAL #endif // CGAL_QT_GRAPHICS_VIEW_ISO_RECTANGLE_INPUT_H