| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "AdornerMagnifyingGlass.h" |
| 21 | |
| 22 | AdornerMagnifyingGlass::AdornerMagnifyingGlass() : |
| 23 | mOffsetToMouse(QPoint(20, 20)), |
| 24 | mScaleFactor(QSize(600, 600)), |
| 25 | mZoomInAreaSize(QSize(100, 100)), |
| 26 | mBackgroundOffset(QPoint(50,50)), |
| 27 | mCrossHairPen(new QPen(Qt::red, 6)) |
| 28 | { |
| 29 | mVisibleRect.setWidth(200); |
| 30 | mVisibleRect.setHeight(200); |
| 31 | } |
| 32 | |
| 33 | AdornerMagnifyingGlass::~AdornerMagnifyingGlass() |
| 34 | { |
| 35 | delete mCrossHairPen; |
| 36 | } |
| 37 | |
| 38 | void AdornerMagnifyingGlass::update(const QPoint &mousePosition, const QRect &screenRect) |
| 39 | { |
| 40 | if (mBackgroundWithMargine.isNull()) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | updatePosition(mousePosition, screenRect); |
| 45 | updateImage(mousePosition - screenRect.topLeft()); |
| 46 | updateCrossHair(); |
| 47 | } |
| 48 | |
| 49 | void AdornerMagnifyingGlass::paint(QPainter *painter, const QColor &color) |
| 50 | { |
| 51 | if (mBackgroundWithMargine.isNull()) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | painter->setBrush(Qt::NoBrush); |
| 56 | painter->setRenderHint(QPainter::Antialiasing); |
| 57 | painter->setClipRegion(QRegion(mVisibleRect, QRegion::Ellipse)); |
| 58 | painter->drawPixmap(mVisibleRect, mImage); |
| 59 | |
| 60 | mCrossHairPen->setColor(color); |
| 61 | painter->setPen(*mCrossHairPen); |
| 62 | painter->drawLine(mCrossHairTop); |
| 63 | painter->drawLine(mCrossHairBottom); |
| 64 | painter->drawLine(mCrossHairLeft); |
| 65 | painter->drawLine(mCrossHairRight); |
| 66 | } |
| 67 | |
| 68 | void AdornerMagnifyingGlass::setBackgroundImage(const QPixmap *background) |
| 69 | { |
| 70 | if (background == nullptr) { |
| 71 | mBackgroundWithMargine = QPixmap(); |
| 72 | } else { |
| 73 | mBackgroundWithMargine = createBackgroundWithMagine(background); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void AdornerMagnifyingGlass::updatePosition(const QPoint &mousePosition, const QRect &screenRect) |
| 78 | { |
| 79 | if (isPositionTopLeftFromMouse(mousePosition, screenRect)) { |
| 80 | mVisibleRect.moveBottomRight(mousePosition); |
| 81 | } else if (isPositionBottomLeftFromMouse(mousePosition, screenRect)) { |
| 82 | mVisibleRect.moveTopRight(mousePosition); |
| 83 | } else if (isPositionTopRightFromMouse(mousePosition, screenRect)) { |
| 84 | mVisibleRect.moveBottomLeft(mousePosition); |
| 85 | } else { |
| 86 | mVisibleRect.moveTopLeft(mousePosition + mOffsetToMouse); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void AdornerMagnifyingGlass::updateImage(const QPoint &mousePosition) |
| 91 | { |
| 92 | QRect positionAroundMouse(QPoint(), mZoomInAreaSize); |
| 93 | positionAroundMouse.moveCenter(mousePosition + mBackgroundOffset); |
| 94 | |
| 95 | auto zoomedInImage = mBackgroundWithMargine.copy(positionAroundMouse).scaled(mScaleFactor); |
| 96 | QRect rectForFinalCut(mVisibleRect); |
| 97 | rectForFinalCut.moveCenter(zoomedInImage.rect().center()); |
| 98 | mImage = zoomedInImage.copy(rectForFinalCut); |
| 99 | } |
| 100 | |
| 101 | QPixmap AdornerMagnifyingGlass::createBackgroundWithMagine(const QPixmap *background) const |
| 102 | { |
| 103 | QPixmap backgroundWithMargine(background->size() + mZoomInAreaSize); |
| 104 | backgroundWithMargine.fill(Qt::black); |
| 105 | QPainter painter(&backgroundWithMargine); |
| 106 | painter.drawPixmap(mBackgroundOffset, *background); |
| 107 | return backgroundWithMargine; |
| 108 | } |
| 109 | |
| 110 | void AdornerMagnifyingGlass::updateCrossHair() |
| 111 | { |
| 112 | auto outerOffset = 20; |
| 113 | auto innerOffset = 15; |
| 114 | mCrossHairTop.setLine(mVisibleRect.center().x(), mVisibleRect.top() + outerOffset, mVisibleRect.center().x(), mVisibleRect.center().y() - innerOffset); |
| 115 | mCrossHairBottom.setLine(mVisibleRect.center().x(), mVisibleRect.bottom() - outerOffset, mVisibleRect.center().x(), mVisibleRect.center().y() + innerOffset); |
| 116 | mCrossHairLeft.setLine(mVisibleRect.left() + outerOffset, mVisibleRect.center().y(), mVisibleRect.center().x() - innerOffset, mVisibleRect.center().y()); |
| 117 | mCrossHairRight.setLine(mVisibleRect.right() - outerOffset, mVisibleRect.center().y(), mVisibleRect.center().x() + innerOffset, mVisibleRect.center().y()); |
| 118 | } |
| 119 | |
| 120 | bool AdornerMagnifyingGlass::isPositionTopRightFromMouse(const QPoint &mousePosition, const QRect &screenRect) const |
| 121 | { |
| 122 | return mousePosition.x() + mVisibleRect.width() < screenRect.width() && mousePosition.y() + mVisibleRect.height() > screenRect.height(); |
| 123 | } |
| 124 | |
| 125 | bool AdornerMagnifyingGlass::isPositionBottomLeftFromMouse(const QPoint &mousePosition, const QRect &screenRect) const |
| 126 | { |
| 127 | return mousePosition.x() + mVisibleRect.width() > screenRect.width() && mousePosition.y() + mVisibleRect.height() < screenRect.height(); |
| 128 | } |
| 129 | |
| 130 | bool AdornerMagnifyingGlass::isPositionTopLeftFromMouse(const QPoint &mousePosition, const QRect &screenRect) const |
| 131 | { |
| 132 | return mousePosition.x() + mVisibleRect.width() > screenRect.width() && mousePosition.y() + mVisibleRect.height() > screenRect.height(); |
| 133 | } |