annotate panned.cpp @ 145:644bd31e8301

* Include the uncommitted item in general graph layout (in case it is not at the head, when other items will need to avoid it)
author Chris Cannam
date Wed, 01 Dec 2010 17:41:14 +0000
parents aaeab914f2a3
children 4bad3c5c053a
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;
Chris@133 47 centerOn(pr.center());
Chris@133 48 emit pannedRectChanged(pr);
Chris@133 49 }
Chris@133 50 }
Chris@133 51
Chris@133 52 void
Chris@133 53 Panned::setScene(QGraphicsScene *s)
Chris@133 54 {
Chris@133 55 if (!scene()) {
Chris@133 56 QGraphicsView::setScene(s);
Chris@133 57 return;
Chris@133 58 }
Chris@133 59
Chris@133 60 QPointF nearpt = mapToScene(0, 0);
Chris@133 61 QPointF farpt = mapToScene(width(), height());
Chris@133 62 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y());
Chris@133 63 QRectF pr(nearpt, sz);
Chris@133 64
Chris@133 65 QGraphicsView::setScene(s);
Chris@133 66
Chris@133 67 DEBUG << "Panned::setScene: pr = " << pr << ", sceneRect = " << sceneRect() << endl;
Chris@133 68
Chris@133 69 if (scene() && sceneRect().intersects(pr)) {
Chris@133 70 DEBUG << "Panned::setScene: restoring old rect " << pr << endl;
Chris@133 71 m_pannedRect = pr;
Chris@133 72 centerOn(pr.center());
cannam@45 73 emit pannedRectChanged(pr);
cannam@45 74 }
cannam@45 75 }
cannam@45 76
cannam@45 77 void
cannam@45 78 Panned::paintEvent(QPaintEvent *e)
cannam@45 79 {
cannam@45 80 QGraphicsView::paintEvent(e);
cannam@45 81 }
cannam@45 82
cannam@45 83 void
cannam@45 84 Panned::drawForeground(QPainter *paint, const QRectF &)
cannam@45 85 {
Chris@60 86 QPointF nearpt = mapToScene(0, 0);
Chris@60 87 QPointF farpt = mapToScene(width(), height());
Chris@60 88 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y());
Chris@60 89 QRectF pr(nearpt, sz);
cannam@45 90
cannam@45 91 if (pr != m_pannedRect) {
Chris@133 92 DEBUG << "Panned::drawForeground: visible rect " << pr << " differs from panned rect " << m_pannedRect << ", updating panned rect" <<endl;
cannam@45 93 if (pr.x() != m_pannedRect.x()) emit pannedContentsScrolled();
cannam@45 94 m_pannedRect = pr;
cannam@45 95 emit pannedRectChanged(pr);
cannam@45 96 }
cannam@45 97 }
cannam@45 98
cannam@45 99 void
Chris@53 100 Panned::zoomIn()
Chris@53 101 {
Chris@53 102 QMatrix m = matrix();
Chris@53 103 m.scale(1.0 / 1.1, 1.0 / 1.1);
Chris@53 104 setMatrix(m);
Chris@53 105 }
Chris@53 106
Chris@53 107 void
Chris@53 108 Panned::zoomOut()
Chris@53 109 {
Chris@53 110 QMatrix m = matrix();
Chris@53 111 m.scale(1.1, 1.1);
Chris@53 112 setMatrix(m);
Chris@53 113 }
Chris@53 114
Chris@53 115 void
cannam@45 116 Panned::slotSetPannedRect(QRectF pr)
cannam@45 117 {
cannam@45 118 centerOn(pr.center());
cannam@45 119 // setSceneRect(pr);
cannam@45 120 // m_pannedRect = pr;
cannam@45 121 }
cannam@45 122
cannam@45 123 void
cannam@45 124 Panned::wheelEvent(QWheelEvent *ev)
cannam@45 125 {
Chris@53 126 if (ev->modifiers() & Qt::ControlModifier) {
Chris@53 127 int d = ev->delta();
Chris@53 128 if (d > 0) {
Chris@53 129 while (d > 0) {
Chris@53 130 zoomOut();
Chris@53 131 d -= 120;
Chris@53 132 }
Chris@53 133 } else {
Chris@53 134 while (d < 0) {
Chris@53 135 zoomIn();
Chris@53 136 d += 120;
Chris@53 137 }
Chris@53 138 }
Chris@53 139 } else {
Chris@53 140 emit wheelEventReceived(ev);
Chris@53 141 QGraphicsView::wheelEvent(ev);
Chris@53 142 }
cannam@45 143 }
cannam@45 144
cannam@45 145 void
cannam@45 146 Panned::slotEmulateWheelEvent(QWheelEvent *ev)
cannam@45 147 {
cannam@45 148 QGraphicsView::wheelEvent(ev);
cannam@45 149 }
cannam@45 150
cannam@45 151 void
cannam@45 152 Panned::leaveEvent(QEvent *)
cannam@45 153 {
cannam@45 154 emit mouseLeaves();
cannam@45 155 }