1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29 
30 // Interface header.
31 #include "mousecoordinatestracker.h"
32 
33 // Qt headers.
34 #include <QEvent>
35 #include <QLabel>
36 #include <QMouseEvent>
37 #include <QPoint>
38 #include <QString>
39 #include <QWidget>
40 
41 // Standard headers.
42 #include <cassert>
43 
44 using namespace foundation;
45 using namespace std;
46 
47 namespace appleseed {
48 namespace studio {
49 
MouseCoordinatesTracker(QWidget * widget,QLabel * label)50 MouseCoordinatesTracker::MouseCoordinatesTracker(
51     QWidget*    widget,
52     QLabel*     label)
53   : m_widget(widget)
54   , m_label(label)
55   , m_content_width(widget->width())
56   , m_content_height(widget->height())
57 {
58     m_widget->installEventFilter(this);
59 }
60 
~MouseCoordinatesTracker()61 MouseCoordinatesTracker::~MouseCoordinatesTracker()
62 {
63     m_widget->removeEventFilter(this);
64 }
65 
widget_to_ndc(const QPoint & point) const66 Vector2d MouseCoordinatesTracker::widget_to_ndc(const QPoint& point) const
67 {
68     return
69         Vector2d(
70             static_cast<double>(point.x()) / m_widget->width(),
71             static_cast<double>(point.y()) / m_widget->height());
72 }
73 
widget_to_pixel(const QPoint & point) const74 Vector2i MouseCoordinatesTracker::widget_to_pixel(const QPoint& point) const
75 {
76     const Vector2d ndc = widget_to_ndc(point);
77 
78     return
79         Vector2i(
80             static_cast<int>(ndc.x * m_content_width),
81             static_cast<int>(ndc.y * m_content_height));
82 }
83 
eventFilter(QObject * object,QEvent * event)84 bool MouseCoordinatesTracker::eventFilter(QObject* object, QEvent* event)
85 {
86     assert(object == m_widget);
87 
88     switch (event->type())
89     {
90       case QEvent::MouseMove:
91         set_label_text(static_cast<QMouseEvent*>(event)->pos());
92         break;
93 
94       case QEvent::Leave:
95         m_label->clear();
96         break;
97     }
98 
99     return QObject::eventFilter(object, event);
100 }
101 
set_label_text(const QPoint & point) const102 void MouseCoordinatesTracker::set_label_text(const QPoint& point) const
103 {
104     const Vector2i pix = widget_to_pixel(point);
105     const Vector2d ndc = widget_to_ndc(point);
106 
107     m_label->setText(
108         QString("Pixel: %1, %2 - NDC: %3, %4")
109             .arg(QString::number(pix.x), 4, ' ')
110             .arg(QString::number(pix.y), 4, ' ')
111             .arg(QString::number(ndc.x, 'f', 5))
112             .arg(QString::number(ndc.y, 'f', 5)));
113 }
114 
115 }   // namespace studio
116 }   // namespace appleseed
117