Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame^] | 1 | /************************************************************************** |
| 2 | * Copyright (C) 2018 by Savoir-faire Linux * |
| 3 | * Author: Isa Nanic <isa.nanic@savoirfairelinux.com> * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "avatargraphicsview.h" |
| 20 | |
| 21 | AvatarGraphicsView::AvatarGraphicsView(QWidget* parent) |
| 22 | : QGraphicsView(parent) |
| 23 | { |
| 24 | setDragMode(QGraphicsView::ScrollHandDrag); |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | AvatarGraphicsView::wheelEvent(QWheelEvent* event) |
| 29 | { |
| 30 | QPoint numSteps = event->angleDelta() / (8*15); |
| 31 | |
| 32 | if (!numSteps.isNull()) { |
| 33 | zoomImage(numSteps); |
| 34 | } |
| 35 | event->accept(); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | AvatarGraphicsView::zoomImage(const QPoint& steps) |
| 40 | { |
| 41 | steps.y() == 1 ? scale(zoomIncrement_, zoomIncrement_) : scale(1 / zoomIncrement_, 1 / zoomIncrement_); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | AvatarGraphicsView::paintEvent(QPaintEvent* e) |
| 46 | { |
| 47 | QGraphicsView::paintEvent(e); |
| 48 | QPainter painter(viewport()); |
| 49 | |
| 50 | QPainterPath square; |
| 51 | QPainterPath circle; |
| 52 | |
| 53 | square.addRect(0, 0, width(), height()); |
| 54 | circle.addEllipse(QPoint(width() / 2 - 0.5, height() / 2 - 0.5), circleSize_ + 1, circleSize_ + 1); |
| 55 | |
| 56 | painter.setOpacity(0.3); |
| 57 | |
| 58 | painter.fillPath(square.subtracted(circle), QBrush(QColor(Qt::black))); |
| 59 | } |