Implemented basic scaling in BasicViewport.
[aesalon.git] / src / artisan / gviewport / BasicViewport.cpp
blob379b8a4c135ca3787f98354bebd8fa9d8b35ffca
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/BasicViewport.cpp
8 */
10 #include <QResizeEvent>
11 #include <QPaintEvent>
12 #include <QMouseEvent>
14 #include "artisan/gviewport/BasicViewport.h"
15 #include "artisan/gviewport/Renderer.h"
16 #include "artisan/gviewport/CoordinateMapper.h"
18 namespace Artisan {
19 namespace GViewport {
21 BasicViewport::BasicViewport(Data *data) : m_data(data) {
22 setMinimumSize(120, 120);
23 setMouseTracking(true);
26 BasicViewport::~BasicViewport() {
30 void BasicViewport::translate(const Point &upperLeft) {
34 void BasicViewport::scale(double factor) {
38 void BasicViewport::setViewport(const Rect &range) {
39 RenderedImage image(range, Rect(size()));
40 image.merge(m_image);
41 m_image = image;
44 void BasicViewport::acceptRenderedImage(RenderedImage *image) {
45 m_image.merge(*image);
46 delete image;
47 update();
50 void BasicViewport::updateRange(const Rect &range) {
51 RenderedImage image(range, Rect(size()));
52 image.merge(m_image);
53 m_image = image;
55 enqueue(new Renderer(m_image.dataRange(), m_image.pixelSize(), m_data));
58 void BasicViewport::resizeEvent(QResizeEvent *event) {
59 RenderedImage image(m_image.dataRange(), Rect(event->size()));
60 image.merge(m_image);
61 m_image = image;
62 enqueue(new Renderer(m_image.dataRange(), Rect(event->size()), m_data));
65 void BasicViewport::paintEvent(QPaintEvent *event) {
66 m_image.paintOnto(this);
69 void BasicViewport::mouseMoveEvent(QMouseEvent *event) {
70 if(event->buttons() & Qt::LeftButton) {
71 Point diff = -(event->pos() - m_lastPoint);
73 CoordinateMapper mapper(m_image);
74 updateRange(m_image.dataRange() + mapper.pixelToDataOffset(diff));
76 m_lastPoint = event->pos();
80 void BasicViewport::mousePressEvent(QMouseEvent *event) {
81 m_lastPoint = event->pos();
84 void BasicViewport::wheelEvent(QWheelEvent *event) {
85 double factor = 1.0 - (event->delta() / 1200.0);
87 Rect newRange = m_image.dataRange();
89 if(event->modifiers() & Qt::ShiftModifier) {
90 newRange.scaleHeight(factor);
92 else if(event->modifiers() & Qt::ControlModifier) {
93 newRange.scaleWidth(factor);
95 else {
96 newRange.scaleHeight(factor);
97 newRange.scaleWidth(factor);
100 updateRange(newRange);
103 void BasicViewport::enqueue(Renderer *renderer) {
104 connect(renderer, SIGNAL(finishedRendering(RenderedImage *)),
105 this, SLOT(acceptRenderedImage(RenderedImage *)),
106 Qt::QueuedConnection);
107 renderer->enqueue();
110 } // namespace GViewport
111 } // namespace Artisan