| 1 | /* |
| 2 | * Copyright (C) 2020 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 "AbstractSnippingAreaInfoText.h" |
| 21 | |
| 22 | AbstractSnippingAreaInfoText::AbstractSnippingAreaInfoText(QObject *parent): |
| 23 | QObject(parent), |
| 24 | mRectPen(new QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)), |
| 25 | mRectBrush(new QBrush(QColor(255, 255, 255, 160))), |
| 26 | mBaseInfoTextRectSize(new QSize(500, 100)), |
| 27 | mRectOffset(30, 30), |
| 28 | mIsActive(false) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | AbstractSnippingAreaInfoText::~AbstractSnippingAreaInfoText() |
| 33 | { |
| 34 | delete mRectPen; |
| 35 | delete mRectBrush; |
| 36 | delete mBaseInfoTextRectSize; |
| 37 | } |
| 38 | |
| 39 | void AbstractSnippingAreaInfoText::paint(QPainter *painter) |
| 40 | { |
| 41 | if(mIsActive) { |
| 42 | auto fontMetric = painter->fontMetrics(); |
| 43 | auto textRect = fontMetric.boundingRect(QRect(mSnippingAreaGeometry.topLeft().toPoint() + mRectOffset, *mBaseInfoTextRectSize), Qt::TextWordWrap, mInfoText); |
| 44 | auto boundingRect = textRect.adjusted(-10, -10, 10, 10); |
| 45 | |
| 46 | if(boundingRect.contains(mCurrentMousePos.toPoint())) { |
| 47 | auto bottomPosition = mSnippingAreaGeometry.bottomRight().toPoint(); |
| 48 | textRect.moveBottomRight(bottomPosition - QPoint(40, 40)); |
| 49 | boundingRect.moveBottomRight(bottomPosition - mRectOffset); |
| 50 | } |
| 51 | |
| 52 | painter->setBrush(*mRectBrush); |
| 53 | painter->setPen(*mRectPen); |
| 54 | painter->drawRect(boundingRect); |
| 55 | painter->drawText(textRect, mInfoText); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void AbstractSnippingAreaInfoText::handleMouseMove(const QPointF &pos) |
| 60 | { |
| 61 | mCurrentMousePos = pos; |
| 62 | } |
| 63 | |
| 64 | void AbstractSnippingAreaInfoText::activate(const QRectF &snippingAreaGeometry) |
| 65 | { |
| 66 | mIsActive = true; |
| 67 | mSnippingAreaGeometry = snippingAreaGeometry; |
| 68 | updateInfoText(); |
| 69 | } |
| 70 | |
| 71 | void AbstractSnippingAreaInfoText::deactivate() |
| 72 | { |
| 73 | mIsActive = false; |
| 74 | } |
| 75 | |
| 76 | void AbstractSnippingAreaInfoText::setInfoText(const QStringList &infoTextLines) |
| 77 | { |
| 78 | auto newLine = QLatin1String("\n"); |
| 79 | mInfoText = QString(); |
| 80 | for (int i = 0; i < infoTextLines.length(); ++i) { |
| 81 | mInfoText += infoTextLines[i]; |
| 82 | if(i < infoTextLines.length() - 1){ |
| 83 | mInfoText += newLine; |
| 84 | } |
| 85 | } |
| 86 | } |