annotate panner.cpp @ 48:996b3c4037ef

* Add IndirectPainting opt flag, makes a big difference to rendering Panner; rename project
author Chris Cannam
date Wed, 10 Nov 2010 22:07:03 +0000
parents bd3accba9b3f
children 3c46b2ac45d3
rev   line source
cannam@45 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@45 2
cannam@45 3 /*
cannam@45 4 Rosegarden
cannam@45 5 A MIDI and audio sequencer and musical notation editor.
cannam@45 6 Copyright 2000-2010 the Rosegarden development team.
cannam@45 7
cannam@45 8 Other copyrights also apply to some parts of this work. Please
cannam@45 9 see the AUTHORS file and individual file headers for details.
cannam@45 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 "panner.h"
cannam@45 19 #include "panned.h"
cannam@45 20
cannam@45 21 #include <QPolygon>
cannam@45 22 #include <QMouseEvent>
cannam@45 23 #include <QColor>
Chris@46 24 #include <QGLWidget>
cannam@45 25
cannam@45 26 #include <iostream>
cannam@45 27
cannam@45 28 class PannerScene : public QGraphicsScene
cannam@45 29 {
cannam@45 30 public:
cannam@45 31 friend class Panner;
cannam@45 32 };
cannam@45 33
cannam@45 34 Panner::Panner() :
cannam@45 35 m_clicked(false)
cannam@45 36 {
Chris@46 37 // setViewport(new QGLWidget());
cannam@45 38 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
cannam@45 39 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Chris@48 40 setOptimizationFlags(QGraphicsView::DontSavePainterState |
Chris@48 41 QGraphicsView::IndirectPainting);
cannam@45 42 setMouseTracking(true);
cannam@45 43 setInteractive(false);
cannam@45 44 }
cannam@45 45
cannam@45 46 void
cannam@45 47 Panner::setScene(QGraphicsScene *s)
cannam@45 48 {
cannam@45 49 if (scene()) {
cannam@45 50 disconnect(scene(), SIGNAL(sceneRectChanged(const QRectF &)),
cannam@45 51 this, SLOT(slotSceneRectChanged(const QRectF &)));
cannam@45 52 }
cannam@45 53 QGraphicsView::setScene(s);
cannam@45 54 if (scene()) fitInView(sceneRect(), Qt::IgnoreAspectRatio);
cannam@45 55 m_cache = QPixmap();
cannam@45 56 connect(scene(), SIGNAL(sceneRectChanged(const QRectF &)),
cannam@45 57 this, SLOT(slotSceneRectChanged(const QRectF &)));
cannam@45 58 }
cannam@45 59
cannam@45 60 void
cannam@45 61 Panner::connectToPanned(Panned *p)
cannam@45 62 {
cannam@45 63 connect(p, SIGNAL(pannedRectChanged(QRectF)),
cannam@45 64 this, SLOT(slotSetPannedRect(QRectF)));
cannam@45 65
cannam@45 66 connect(this, SIGNAL(pannedRectChanged(QRectF)),
cannam@45 67 p, SLOT(slotSetPannedRect(QRectF)));
cannam@45 68 }
cannam@45 69
cannam@45 70 void
cannam@45 71 Panner::slotSetPannedRect(QRectF rect)
cannam@45 72 {
cannam@45 73 m_pannedRect = rect;
cannam@45 74 viewport()->update();
cannam@45 75 }
cannam@45 76
cannam@45 77 void
cannam@45 78 Panner::resizeEvent(QResizeEvent *)
cannam@45 79 {
cannam@45 80 if (scene()) fitInView(sceneRect(), Qt::IgnoreAspectRatio);
cannam@45 81 m_cache = QPixmap();
cannam@45 82 }
cannam@45 83
cannam@45 84 void
cannam@45 85 Panner::slotSceneRectChanged(const QRectF &newRect)
cannam@45 86 {
cannam@45 87 if (!scene()) return; // spurious
cannam@45 88 fitInView(newRect, Qt::IgnoreAspectRatio);
cannam@45 89 m_cache = QPixmap();
cannam@45 90 viewport()->update();
cannam@45 91 }
cannam@45 92
cannam@45 93 void
cannam@45 94 Panner::paintEvent(QPaintEvent *e)
cannam@45 95 {
cannam@45 96 QPaintEvent *e2 = new QPaintEvent(e->region().boundingRect());
cannam@45 97 QGraphicsView::paintEvent(e2);
cannam@45 98
cannam@45 99 QPainter paint;
cannam@45 100 paint.begin(viewport());
cannam@45 101 paint.setClipRegion(e->region());
cannam@45 102
cannam@45 103 QPainterPath path;
cannam@45 104 path.addRect(rect());
cannam@45 105 path.addPolygon(mapFromScene(m_pannedRect));
cannam@45 106
cannam@45 107 QColor c(QColor::fromHsv(211, 194, 238));
cannam@45 108 c.setAlpha(80);
cannam@45 109 paint.setPen(Qt::NoPen);
cannam@45 110 paint.setBrush(c);
cannam@45 111 paint.drawPath(path);
cannam@45 112
cannam@45 113 paint.setBrush(Qt::NoBrush);
cannam@45 114 paint.setPen(QPen(QColor::fromHsv(211, 194, 238), 0));
cannam@45 115 paint.drawConvexPolygon(mapFromScene(m_pannedRect));
cannam@45 116
cannam@45 117 paint.end();
cannam@45 118
cannam@45 119 emit pannerChanged(m_pannedRect);
cannam@45 120 }
cannam@45 121
cannam@45 122 void
cannam@45 123 Panner::updateScene(const QList<QRectF> &rects)
cannam@45 124 {
cannam@45 125 if (!m_cache.isNull()) m_cache = QPixmap();
cannam@45 126 QGraphicsView::updateScene(rects);
cannam@45 127 }
cannam@45 128
cannam@45 129 void
cannam@45 130 Panner::drawItems(QPainter *painter, int numItems,
cannam@45 131 QGraphicsItem *items[],
cannam@45 132 const QStyleOptionGraphicsItem options[])
cannam@45 133 {
cannam@45 134 if (m_cache.size() != viewport()->size()) {
cannam@45 135
Chris@48 136 std::cerr << "Panner: recreating cache" << std::endl;
Chris@48 137
cannam@45 138 QGraphicsScene *s = scene();
cannam@45 139 if (!s) return;
cannam@45 140 PannerScene *ps = static_cast<PannerScene *>(s);
cannam@45 141
cannam@45 142 m_cache = QPixmap(viewport()->size());
cannam@45 143 m_cache.fill(Qt::transparent);
cannam@45 144 QPainter cachePainter;
cannam@45 145 cachePainter.begin(&m_cache);
cannam@45 146 cachePainter.setTransform(viewportTransform());
cannam@45 147 ps->drawItems(&cachePainter, numItems, items, options);
cannam@45 148 cachePainter.end();
cannam@45 149 }
cannam@45 150
cannam@45 151 painter->save();
cannam@45 152 painter->setTransform(QTransform());
cannam@45 153 painter->drawPixmap(0, 0, m_cache);
cannam@45 154 painter->restore();
cannam@45 155 }
cannam@45 156
cannam@45 157 void
cannam@45 158 Panner::mousePressEvent(QMouseEvent *e)
cannam@45 159 {
cannam@45 160 if (e->button() != Qt::LeftButton) {
cannam@45 161 QGraphicsView::mouseDoubleClickEvent(e);
cannam@45 162 return;
cannam@45 163 }
cannam@45 164 m_clicked = true;
cannam@45 165 m_clickedRect = m_pannedRect;
cannam@45 166 m_clickedPoint = e->pos();
cannam@45 167 }
cannam@45 168
cannam@45 169 void
cannam@45 170 Panner::mouseDoubleClickEvent(QMouseEvent *e)
cannam@45 171 {
cannam@45 172 if (e->button() != Qt::LeftButton) {
cannam@45 173 QGraphicsView::mouseDoubleClickEvent(e);
cannam@45 174 return;
cannam@45 175 }
cannam@45 176
cannam@45 177 moveTo(e->pos());
cannam@45 178 }
cannam@45 179
cannam@45 180 void
cannam@45 181 Panner::mouseMoveEvent(QMouseEvent *e)
cannam@45 182 {
cannam@45 183 if (!m_clicked) return;
cannam@45 184 QPointF cp = mapToScene(m_clickedPoint);
cannam@45 185 QPointF mp = mapToScene(e->pos());
cannam@45 186 QPointF delta = mp - cp;
cannam@45 187 QRectF nr = m_clickedRect;
cannam@45 188 nr.translate(delta);
cannam@45 189 slotSetPannedRect(nr);
cannam@45 190 emit pannedRectChanged(m_pannedRect);
cannam@45 191 viewport()->update();
cannam@45 192 }
cannam@45 193
cannam@45 194 void
cannam@45 195 Panner::mouseReleaseEvent(QMouseEvent *e)
cannam@45 196 {
cannam@45 197 if (e->button() != Qt::LeftButton) {
cannam@45 198 QGraphicsView::mouseDoubleClickEvent(e);
cannam@45 199 return;
cannam@45 200 }
cannam@45 201
cannam@45 202 if (m_clicked) {
cannam@45 203 mouseMoveEvent(e);
cannam@45 204 }
cannam@45 205
cannam@45 206 m_clicked = false;
cannam@45 207 viewport()->update();
cannam@45 208 }
cannam@45 209
cannam@45 210 void
cannam@45 211 Panner::wheelEvent(QWheelEvent *e)
cannam@45 212 {
cannam@45 213 if (e->delta() > 0) {
cannam@45 214 emit zoomOut();
cannam@45 215 } else {
cannam@45 216 emit zoomIn();
cannam@45 217 }
cannam@45 218 }
cannam@45 219
cannam@45 220 void
cannam@45 221 Panner::moveTo(QPoint p)
cannam@45 222 {
cannam@45 223 QPointF sp = mapToScene(p);
cannam@45 224 QRectF nr = m_pannedRect;
cannam@45 225 double d = sp.x() - nr.center().x();
cannam@45 226 nr.translate(d, 0);
cannam@45 227 slotSetPannedRect(nr);
cannam@45 228 emit pannedRectChanged(m_pannedRect);
cannam@45 229 viewport()->update();
cannam@45 230 }
cannam@45 231