| 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 "AdornerSizeInfo.h" |
| 21 | |
| 22 | AdornerSizeInfo::AdornerSizeInfo() : |
| 23 | mFontMetric(new QFontMetrics(mFont)), |
| 24 | mSizeInfoPen(new QPen(Qt::red, 1)) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | AdornerSizeInfo::~AdornerSizeInfo() |
| 29 | { |
| 30 | delete mFontMetric; |
| 31 | delete mSizeInfoPen; |
| 32 | } |
| 33 | |
| 34 | void AdornerSizeInfo::update(const QRect &captureRect) |
| 35 | { |
| 36 | updateWidthInfo(captureRect); |
| 37 | updateHeightInfo(captureRect); |
| 38 | updateSizeInfoText(captureRect); |
| 39 | } |
| 40 | |
| 41 | void AdornerSizeInfo::paint(QPainter *painter, const QColor &color) |
| 42 | { |
| 43 | mSizeInfoPen->setColor(color); |
| 44 | painter->setBrush(Qt::NoBrush); |
| 45 | painter->setPen(*mSizeInfoPen); |
| 46 | painter->drawPath(mWidthInfo); |
| 47 | painter->drawPath(mHeightInfo); |
| 48 | painter->drawText(mWidthTextPosition, mWidthInfoText); |
| 49 | painter->drawText(mHeightTextPosition, mHeightInfoText); |
| 50 | } |
| 51 | |
| 52 | void AdornerSizeInfo::updateWidthInfo(const QRect &captureRect) |
| 53 | { |
| 54 | auto topLeft = captureRect.topLeft(); |
| 55 | auto topRight = captureRect.topRight(); |
| 56 | auto offsetLine = QPoint(0, 10); |
| 57 | auto offsetBorderBottom = QPoint(0, 7); |
| 58 | auto offsetBorderTop = QPoint(0, 13); |
| 59 | |
| 60 | QPainterPath newPath(topLeft - offsetBorderBottom); |
| 61 | newPath.lineTo(topLeft - offsetBorderTop); |
| 62 | newPath.lineTo(topLeft - offsetLine); |
| 63 | newPath.lineTo(topRight - offsetLine); |
| 64 | newPath.lineTo(topRight - offsetBorderTop); |
| 65 | newPath.lineTo(topRight - offsetBorderBottom); |
| 66 | mWidthInfo.swap(newPath); |
| 67 | } |
| 68 | |
| 69 | void AdornerSizeInfo::updateHeightInfo(const QRect &captureRect) |
| 70 | { |
| 71 | auto topLeft = captureRect.topLeft(); |
| 72 | auto bottomLeft = captureRect.bottomLeft(); |
| 73 | auto offsetLine = QPoint(10, 0); |
| 74 | auto offsetBorderLeft = QPoint(7, 0); |
| 75 | auto offsetBorderRight = QPoint(13, 0); |
| 76 | |
| 77 | QPainterPath newPath(topLeft - offsetBorderLeft); |
| 78 | newPath.lineTo(topLeft - offsetBorderRight); |
| 79 | newPath.lineTo(topLeft - offsetLine); |
| 80 | newPath.lineTo(bottomLeft - offsetLine); |
| 81 | newPath.lineTo(bottomLeft - offsetBorderRight); |
| 82 | newPath.lineTo(bottomLeft - offsetBorderLeft); |
| 83 | mHeightInfo.swap(newPath); |
| 84 | } |
| 85 | |
| 86 | void AdornerSizeInfo::updateSizeInfoText(const QRect &captureRect) |
| 87 | { |
| 88 | auto textOffset = 13; |
| 89 | mWidthInfoText = QString::number(captureRect.width()); |
| 90 | mHeightInfoText = QString::number(captureRect.height()); |
| 91 | auto widthTextBoundingRect = mFontMetric->boundingRect(mWidthInfoText); |
| 92 | auto heightTextBoundingRect = mFontMetric->boundingRect(mHeightInfoText); |
| 93 | mWidthTextPosition.setX(captureRect.center().x() - widthTextBoundingRect.width() / 2); |
| 94 | mWidthTextPosition.setY(captureRect.top() - textOffset); |
| 95 | mHeightTextPosition.setX(captureRect.left() - textOffset - heightTextBoundingRect.width()); |
| 96 | mHeightTextPosition.setY(captureRect.center().y() + heightTextBoundingRect.height() / 2); |
| 97 | } |