1 // Copyright (c) 2012, 2020 Tel-Aviv University (Israel).
2 // All rights reserved.
3 //
4 // This file is part of CGAL (www.cgal.org).
5 //
6 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
7 //
8 // Author(s): Alex Tsui <alextsui05@gmail.com>
9 //            Ahmed Essam <theartful.ae@gmail.com>
10 
11 #include "GraphicsSceneMixin.h"
12 #include <QGraphicsScene>
13 #include <QGraphicsView>
14 
GraphicsSceneMixin(QGraphicsScene * scene_)15 GraphicsSceneMixin::GraphicsSceneMixin(QGraphicsScene* scene_) : scene{scene_}
16 {
17 }
18 
~GraphicsSceneMixin()19 GraphicsSceneMixin::~GraphicsSceneMixin() { }
20 
setScene(QGraphicsScene * scene_)21 void GraphicsSceneMixin::setScene(QGraphicsScene* scene_)
22 {
23   this->scene = scene_;
24 }
25 
getScene() const26 QGraphicsScene* GraphicsSceneMixin::getScene() const { return this->scene; }
27 
viewportRect() const28 QRectF GraphicsSceneMixin::viewportRect() const
29 {
30   QGraphicsView* view = this->getView();
31   return view ? view->mapToScene(QRect{0, 0, view->width(), view->height()})
32                   .boundingRect()
33               : QRectF{};
34 }
35 
fromScene(QPointF p)36 QPoint GraphicsSceneMixin::fromScene(QPointF p)
37 {
38   QGraphicsView* viewport = this->getView();
39   return viewport ? viewport->mapFromScene(p) : QPoint{};
40 }
41 
toScene(QPoint p) const42 QPointF GraphicsSceneMixin::toScene(QPoint p) const
43 {
44   QGraphicsView* viewport = this->getView();
45   return viewport ? viewport->mapToScene(p) : QPointF{};
46 }
47 
getView() const48 QGraphicsView* GraphicsSceneMixin::getView() const
49 {
50   if (!this->scene) return nullptr;
51 
52   QList<QGraphicsView*> views = this->scene->views();
53   if (views.size() == 0) return nullptr;
54   // assumes the first view is the right one
55   return views.first();
56 }
57