annotate panned.cpp @ 132:16ceeee30e2a

* Ensure panned rect stays current when size changes; update panner cache when scene changes
author Chris Cannam
date Tue, 30 Nov 2010 11:41:46 +0000
parents 2550aaa09240
children aaeab914f2a3
rev   line source
Chris@57 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@45 2
cannam@45 3 /*
Chris@57 4 EasyMercurial
cannam@45 5
Chris@57 6 Based on HgExplorer by Jari Korhonen
Chris@57 7 Copyright (c) 2010 Jari Korhonen
Chris@57 8 Copyright (c) 2010 Chris Cannam
Chris@57 9 Copyright (c) 2010 Queen Mary, University of London
Chris@57 10
cannam@45 11 This program is free software; you can redistribute it and/or
cannam@45 12 modify it under the terms of the GNU General Public License as
cannam@45 13 published by the Free Software Foundation; either version 2 of the
cannam@45 14 License, or (at your option) any later version. See the file
cannam@45 15 COPYING included with this distribution for more information.
cannam@45 16 */
cannam@45 17
cannam@45 18 #include "panned.h"
Chris@131 19 #include "debug.h"
cannam@45 20
cannam@45 21 #include <QScrollBar>
Chris@53 22 #include <QWheelEvent>
cannam@45 23
cannam@45 24 #include <iostream>
cannam@45 25
cannam@45 26 Panned::Panned()
cannam@45 27 {
Chris@56 28 setRenderHints(QPainter::Antialiasing |
Chris@56 29 QPainter::TextAntialiasing);
cannam@45 30 }
cannam@45 31
cannam@45 32 void
cannam@45 33 Panned::resizeEvent(QResizeEvent *ev)
cannam@45 34 {
Chris@131 35 DEBUG << "Panned::resizeEvent()" << endl;
Chris@131 36
Chris@60 37 QPointF nearpt = mapToScene(0, 0);
Chris@60 38 QPointF farpt = mapToScene(width(), height());
Chris@60 39 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y());
Chris@60 40 QRectF pr(nearpt, sz);
cannam@45 41
Chris@132 42 QGraphicsView::resizeEvent(ev);
Chris@132 43
cannam@45 44 if (pr != m_pannedRect) {
Chris@132 45 DEBUG << "Panned: setting panned rect to " << pr << endl;
cannam@45 46 m_pannedRect = pr;
cannam@45 47 emit pannedRectChanged(pr);
cannam@45 48 }
cannam@45 49 }
cannam@45 50
cannam@45 51 void
cannam@45 52 Panned::paintEvent(QPaintEvent *e)
cannam@45 53 {
cannam@45 54 QGraphicsView::paintEvent(e);
cannam@45 55 }
cannam@45 56
cannam@45 57 void
cannam@45 58 Panned::drawForeground(QPainter *paint, const QRectF &)
cannam@45 59 {
Chris@60 60 QPointF nearpt = mapToScene(0, 0);
Chris@60 61 QPointF farpt = mapToScene(width(), height());
Chris@60 62 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y());
Chris@60 63 QRectF pr(nearpt, sz);
cannam@45 64
cannam@45 65 if (pr != m_pannedRect) {
cannam@45 66 if (pr.x() != m_pannedRect.x()) emit pannedContentsScrolled();
cannam@45 67 m_pannedRect = pr;
cannam@45 68 emit pannedRectChanged(pr);
cannam@45 69 }
cannam@45 70 }
cannam@45 71
cannam@45 72 void
Chris@53 73 Panned::zoomIn()
Chris@53 74 {
Chris@53 75 QMatrix m = matrix();
Chris@53 76 m.scale(1.0 / 1.1, 1.0 / 1.1);
Chris@53 77 setMatrix(m);
Chris@53 78 }
Chris@53 79
Chris@53 80 void
Chris@53 81 Panned::zoomOut()
Chris@53 82 {
Chris@53 83 QMatrix m = matrix();
Chris@53 84 m.scale(1.1, 1.1);
Chris@53 85 setMatrix(m);
Chris@53 86 }
Chris@53 87
Chris@53 88 void
cannam@45 89 Panned::slotSetPannedRect(QRectF pr)
cannam@45 90 {
cannam@45 91 centerOn(pr.center());
cannam@45 92 // setSceneRect(pr);
cannam@45 93 // m_pannedRect = pr;
cannam@45 94 }
cannam@45 95
cannam@45 96 void
cannam@45 97 Panned::wheelEvent(QWheelEvent *ev)
cannam@45 98 {
Chris@53 99 if (ev->modifiers() & Qt::ControlModifier) {
Chris@53 100 int d = ev->delta();
Chris@53 101 if (d > 0) {
Chris@53 102 while (d > 0) {
Chris@53 103 zoomOut();
Chris@53 104 d -= 120;
Chris@53 105 }
Chris@53 106 } else {
Chris@53 107 while (d < 0) {
Chris@53 108 zoomIn();
Chris@53 109 d += 120;
Chris@53 110 }
Chris@53 111 }
Chris@53 112 } else {
Chris@53 113 emit wheelEventReceived(ev);
Chris@53 114 QGraphicsView::wheelEvent(ev);
Chris@53 115 }
cannam@45 116 }
cannam@45 117
cannam@45 118 void
cannam@45 119 Panned::slotEmulateWheelEvent(QWheelEvent *ev)
cannam@45 120 {
cannam@45 121 QGraphicsView::wheelEvent(ev);
cannam@45 122 }
cannam@45 123
cannam@45 124 void
cannam@45 125 Panned::leaveEvent(QEvent *)
cannam@45 126 {
cannam@45 127 emit mouseLeaves();
cannam@45 128 }