Changeset View
Changeset View
Standalone View
Standalone View
lib/squeezedlinklabel.cpp
- This file was added.
| 1 | /* | ||||
|---|---|---|---|---|---|
| 2 | Gwenview: an image viewer | ||||
| 3 | Copyright 2018 Henrik Fehlauer <rkflx@lab12.net> | ||||
| 4 | | ||||
| 5 | This program is free software; you can redistribute it and/or | ||||
| 6 | modify it under the terms of the GNU General Public License | ||||
| 7 | as published by the Free Software Foundation; either version 2 | ||||
| 8 | of the License, or (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, write to the Free Software | ||||
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| 18 | | ||||
| 19 | */ | ||||
| 20 | | ||||
| 21 | #include "squeezedlinklabel.h" | ||||
| 22 | | ||||
| 23 | #include <QDebug> | ||||
| 24 | | ||||
| 25 | class SqueezedLinkLabelPrivate | ||||
| 26 | { | ||||
| 27 | public: | ||||
| 28 | QString fullText; | ||||
| 29 | QString linkTarget; | ||||
| 30 | SqueezedLinkLabel::LabelType labelType; | ||||
| 31 | }; | ||||
| 32 | | ||||
| 33 | SqueezedLinkLabel::SqueezedLinkLabel(QWidget *parent) | ||||
| 34 | : QLabel(parent), | ||||
| 35 | d(new SqueezedLinkLabelPrivate) | ||||
| 36 | { | ||||
| 37 | d->labelType = Link; | ||||
| 38 | } | ||||
| 39 | | ||||
| 40 | QSize SqueezedLinkLabel::minimumSizeHint() const | ||||
| 41 | { | ||||
| 42 | return QSize(-1, QLabel::minimumSizeHint().height()); | ||||
| 43 | } | ||||
| 44 | | ||||
| 45 | QSize SqueezedLinkLabel::sizeHint() const | ||||
| 46 | { | ||||
| 47 | const int textWidth = fontMetrics().width(d->fullText); | ||||
| 48 | const int chromeWidth = width() - contentsRect().width(); // no support for indent and margin yet | ||||
| 49 | return QSize(textWidth + chromeWidth, QLabel::sizeHint().height()); | ||||
| 50 | } | ||||
| 51 | | ||||
| 52 | void SqueezedLinkLabel::setText(const QString& text, const QString& linkTarget, const LabelType& labelType) | ||||
| 53 | { | ||||
| 54 | d->fullText = text; | ||||
| 55 | d->linkTarget = linkTarget; | ||||
| 56 | d->labelType = labelType; | ||||
| 57 | squeezeTextToLabel(); | ||||
| 58 | } | ||||
| 59 | | ||||
| 60 | void SqueezedLinkLabel::resizeEvent(QResizeEvent*) | ||||
| 61 | { | ||||
| 62 | squeezeTextToLabel(); | ||||
| 63 | } | ||||
| 64 | | ||||
| 65 | void SqueezedLinkLabel::squeezeTextToLabel() | ||||
| 66 | { | ||||
| 67 | const int labelWidth = contentsRect().width(); | ||||
| 68 | const int lineWidth = fontMetrics().width(d->fullText); | ||||
| 69 | QString text; | ||||
| 70 | | ||||
| 71 | if (lineWidth > labelWidth) { | ||||
| 72 | text = fontMetrics().elidedText(d->fullText, Qt::ElideRight, labelWidth); | ||||
| 73 | setToolTip(d->fullText); | ||||
| 74 | } else { | ||||
| 75 | text = d->fullText; | ||||
| 76 | setToolTip(QString()); | ||||
| 77 | } | ||||
| 78 | | ||||
| 79 | if (d->labelType == Link) { | ||||
| 80 | text = QStringLiteral("<a href='%2'>%1</a>").arg(text, d->linkTarget); | ||||
| 81 | } | ||||
| 82 | QLabel::setText(text); | ||||
| 83 | } | ||||