Implemented basic viewport scrolling in BasicViewport.
[aesalon.git] / src / artisan / gviewport / CoordinateMapper.cpp
blobde9ceb591fee492f5611721e268512731c0abb53
1 /** Aesalon, a tool to visualize program behaviour in real time.
2 Copyright (C) 2009-2011, Aesalon development team.
4 Aesalon is distributed under the terms of the GNU GPLv3. See
5 the included file LICENSE for more information.
7 @file src/artisan/gviewport/CoordinateMapper.cpp
8 */
10 #include "artisan/gviewport/CoordinateMapper.h"
12 #include "util/MessageSystem.h"
14 namespace Artisan {
15 namespace GViewport {
17 CoordinateMapper::CoordinateMapper(const Rect &dataRect, const Rect &pixelRect)
18 : m_dataRect(dataRect), m_pixelRect(pixelRect) {
22 CoordinateMapper::CoordinateMapper(const RenderedImage &image)
23 : m_dataRect(image.dataRange()), m_pixelRect(image.pixelSize()) {
26 Point CoordinateMapper::dataToPixel(const Point &dataPoint) {
27 double xPercentage = (dataPoint.x() - m_dataRect.left()) / m_dataRect.width();
28 double yPercentage = (dataPoint.y() - m_dataRect.top()) / m_dataRect.height();
30 return Point(
31 (xPercentage * m_pixelRect.width()) + m_pixelRect.left(),
32 (yPercentage * m_pixelRect.height()) + m_pixelRect.top());
35 Rect CoordinateMapper::dataToPixel(const Rect &dataRect) {
36 return Rect(dataToPixel(dataRect.topLeft()), dataToPixel(dataRect.bottomRight()));
39 Point CoordinateMapper::pixelToData(const Point &pixelPoint) {
40 double xPercentage = (pixelPoint.x() - m_pixelRect.left()) / m_pixelRect.width();
41 double yPercentage = (pixelPoint.y() - m_pixelRect.top()) / m_pixelRect.height();
43 return Point(
44 (xPercentage * m_dataRect.width()) + m_dataRect.left(),
45 (yPercentage * m_dataRect.height()) + m_dataRect.top());
48 Rect CoordinateMapper::pixelToData(const Rect &pixelRect) {
49 return Rect(pixelToData(pixelRect.topLeft()), pixelToData(pixelRect.bottomRight()));
52 Point CoordinateMapper::pixelToDataOffset(const Point &pixelPoint) {
53 double xPercentage = (pixelPoint.x() - m_pixelRect.left()) / m_pixelRect.width();
54 double yPercentage = (pixelPoint.y() - m_pixelRect.top()) / m_pixelRect.height();
56 return Point(
57 (xPercentage * m_dataRect.width()),
58 (yPercentage * m_dataRect.height()));
61 } // namespace GViewport
62 } // namespace Artisan