Minor changes here and there.
[aesalon.git] / src / artisan / gviewport / BasicViewport.cpp
blob7739ac62334b396f920d079029ddea7f32fe1c01
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::acceptRenderedImage(RenderedImage *image) {
31 m_image.merge(*image);
32 delete image;
33 update();
36 void BasicViewport::translate(const Point &upperLeft) {
37 RenderedImage image(
38 Rect(upperLeft, m_image.dataRange().width(), m_image.dataRange().height()),
39 m_image.pixelSize());
40 image.merge(m_image);
41 update();
43 enqueue(m_image.dataRange());
46 void BasicViewport::setViewport(const Rect &range) {
47 RenderedImage image(range, Rect(size()));
48 image.merge(m_image);
49 m_image = image;
50 update();
52 enqueue(m_image.dataRange());
55 void BasicViewport::updateRange(const Rect &range) {
56 enqueue(range);
59 void BasicViewport::resizeEvent(QResizeEvent *event) {
60 RenderedImage image(m_image.dataRange(), Rect(event->size()));
61 image.merge(m_image);
62 m_image = image;
64 enqueue(m_image.dataRange());
67 void BasicViewport::paintEvent(QPaintEvent *event) {
68 m_image.paintOnto(this);
71 void BasicViewport::mouseMoveEvent(QMouseEvent *event) {
72 if(event->buttons() & Qt::LeftButton) {
73 Point diff = -(event->pos() - m_lastPoint);
75 CoordinateMapper mapper(m_image);
76 translate(m_image.dataRange().topLeft() + mapper.pixelToDataOffset(diff));
78 m_lastPoint = event->pos();
82 void BasicViewport::mousePressEvent(QMouseEvent *event) {
83 m_lastPoint = event->pos();
86 void BasicViewport::wheelEvent(QWheelEvent *event) {
87 double factor = 1.0 - (event->delta() / 1200.0);
89 Rect newRange = m_image.dataRange();
91 if(event->modifiers() & Qt::ShiftModifier) {
92 newRange.scaleHeight(factor);
94 else if(event->modifiers() & Qt::ControlModifier) {
95 newRange.scaleWidth(factor);
97 else {
98 newRange.scaleHeight(factor);
99 newRange.scaleWidth(factor);
102 setViewport(newRange);
105 void BasicViewport::enqueue(const Rect &rect) {
106 CoordinateMapper mapper(m_image);
107 Renderer *renderer = new Renderer(rect, mapper.dataToPixel(rect), m_data);
109 connect(renderer, SIGNAL(finishedRendering(RenderedImage *)),
110 this, SLOT(acceptRenderedImage(RenderedImage *)),
111 Qt::QueuedConnection);
112 renderer->enqueue();
115 } // namespace GViewport
116 } // namespace Artisan