Chris@57: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@45: cannam@45: /* Chris@57: EasyMercurial cannam@45: Chris@57: Based on HgExplorer by Jari Korhonen Chris@57: Copyright (c) 2010 Jari Korhonen Chris@57: Copyright (c) 2010 Chris Cannam Chris@57: Copyright (c) 2010 Queen Mary, University of London Chris@57: cannam@45: This program is free software; you can redistribute it and/or cannam@45: modify it under the terms of the GNU General Public License as cannam@45: published by the Free Software Foundation; either version 2 of the cannam@45: License, or (at your option) any later version. See the file cannam@45: COPYING included with this distribution for more information. cannam@45: */ cannam@45: cannam@45: #include "panned.h" Chris@131: #include "debug.h" cannam@45: cannam@45: #include Chris@53: #include cannam@45: cannam@45: #include cannam@45: cannam@45: Panned::Panned() cannam@45: { Chris@56: setRenderHints(QPainter::Antialiasing | Chris@56: QPainter::TextAntialiasing); cannam@45: } cannam@45: cannam@45: void cannam@45: Panned::resizeEvent(QResizeEvent *ev) cannam@45: { Chris@131: DEBUG << "Panned::resizeEvent()" << endl; Chris@131: Chris@60: QPointF nearpt = mapToScene(0, 0); Chris@60: QPointF farpt = mapToScene(width(), height()); Chris@60: QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y()); Chris@60: QRectF pr(nearpt, sz); cannam@45: Chris@132: QGraphicsView::resizeEvent(ev); Chris@132: cannam@45: if (pr != m_pannedRect) { Chris@132: DEBUG << "Panned: setting panned rect to " << pr << endl; cannam@45: m_pannedRect = pr; Chris@133: centerOn(pr.center()); Chris@133: emit pannedRectChanged(pr); Chris@133: } Chris@133: } Chris@133: Chris@133: void Chris@133: Panned::setScene(QGraphicsScene *s) Chris@133: { Chris@133: if (!scene()) { Chris@133: QGraphicsView::setScene(s); Chris@133: return; Chris@133: } Chris@133: Chris@133: QPointF nearpt = mapToScene(0, 0); Chris@133: QPointF farpt = mapToScene(width(), height()); Chris@133: QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y()); Chris@133: QRectF pr(nearpt, sz); Chris@133: Chris@133: QGraphicsView::setScene(s); Chris@133: Chris@133: DEBUG << "Panned::setScene: pr = " << pr << ", sceneRect = " << sceneRect() << endl; Chris@133: Chris@133: if (scene() && sceneRect().intersects(pr)) { Chris@133: DEBUG << "Panned::setScene: restoring old rect " << pr << endl; Chris@133: m_pannedRect = pr; Chris@133: centerOn(pr.center()); cannam@45: emit pannedRectChanged(pr); cannam@45: } cannam@45: } cannam@45: cannam@45: void cannam@45: Panned::paintEvent(QPaintEvent *e) cannam@45: { cannam@45: QGraphicsView::paintEvent(e); cannam@45: } cannam@45: cannam@45: void cannam@45: Panned::drawForeground(QPainter *paint, const QRectF &) cannam@45: { Chris@60: QPointF nearpt = mapToScene(0, 0); Chris@60: QPointF farpt = mapToScene(width(), height()); Chris@60: QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y()); Chris@60: QRectF pr(nearpt, sz); cannam@45: cannam@45: if (pr != m_pannedRect) { Chris@133: DEBUG << "Panned::drawForeground: visible rect " << pr << " differs from panned rect " << m_pannedRect << ", updating panned rect" <modifiers() & Qt::ControlModifier) { Chris@53: int d = ev->delta(); Chris@53: if (d > 0) { Chris@53: while (d > 0) { Chris@53: zoomOut(); Chris@53: d -= 120; Chris@53: } Chris@53: } else { Chris@53: while (d < 0) { Chris@53: zoomIn(); Chris@53: d += 120; Chris@53: } Chris@53: } Chris@53: } else { Chris@53: emit wheelEventReceived(ev); Chris@53: QGraphicsView::wheelEvent(ev); Chris@53: } cannam@45: } cannam@45: cannam@45: void cannam@45: Panned::slotEmulateWheelEvent(QWheelEvent *ev) cannam@45: { cannam@45: QGraphicsView::wheelEvent(ev); cannam@45: } cannam@45: cannam@45: void cannam@45: Panned::leaveEvent(QEvent *) cannam@45: { cannam@45: emit mouseLeaves(); cannam@45: }