|
| 1 | +/******************************************************************************* |
| 2 | +** Qt Advanced Docking System |
| 3 | +** Copyright (C) 2017 Uwe Kindler |
| 4 | +** |
| 5 | +** This library is free software; you can redistribute it and/or |
| 6 | +** modify it under the terms of the GNU Lesser General Public |
| 7 | +** License as published by the Free Software Foundation; either |
| 8 | +** version 2.1 of the License, or (at your option) any later version. |
| 9 | +** |
| 10 | +** This library 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 GNU |
| 13 | +** Lesser General Public License for more details. |
| 14 | +** |
| 15 | +** You should have received a copy of the GNU Lesser General Public |
| 16 | +** License along with this library; If not, see <http://www.gnu.org/licenses/>. |
| 17 | +******************************************************************************/ |
| 18 | + |
| 19 | + |
| 20 | +//============================================================================ |
| 21 | +/// \file ElidingLabel.cpp |
| 22 | +/// \author Uwe Kindler |
| 23 | +/// \date 05.11.2018 |
| 24 | +/// \brief Implementation of CElidingLabel |
| 25 | +//============================================================================ |
| 26 | + |
| 27 | +//============================================================================ |
| 28 | +// INCLUDES |
| 29 | +//============================================================================ |
| 30 | +#include <ElidingLabel.h> |
| 31 | +#include <QMouseEvent> |
| 32 | + |
| 33 | + |
| 34 | +namespace ads |
| 35 | +{ |
| 36 | +/** |
| 37 | + * Private data of public CClickableLabel |
| 38 | + */ |
| 39 | +struct ElidingLabelPrivate |
| 40 | +{ |
| 41 | + CElidingLabel* _this; |
| 42 | + Qt::TextElideMode ElideMode = Qt::ElideNone; |
| 43 | + QString Text; |
| 44 | + |
| 45 | + ElidingLabelPrivate(CElidingLabel* _public) : _this(_public) {} |
| 46 | + |
| 47 | + void elideText(int Width); |
| 48 | + |
| 49 | + /** |
| 50 | + * Convenience function to check if the |
| 51 | + */ |
| 52 | + bool isModeElideNone() const |
| 53 | + { |
| 54 | + return Qt::ElideNone == ElideMode; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | + |
| 59 | +//============================================================================ |
| 60 | +void ElidingLabelPrivate::elideText(int Width) |
| 61 | +{ |
| 62 | + if (isModeElideNone()) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + QFontMetrics fm = _this->fontMetrics(); |
| 67 | + QString str = fm.elidedText(Text, ElideMode, Width - _this->margin() * 2 - _this->indent()); |
| 68 | + if (str == "…") |
| 69 | + { |
| 70 | + str = Text.at(0); |
| 71 | + } |
| 72 | + _this->QLabel::setText(str); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +//============================================================================ |
| 77 | +CElidingLabel::CElidingLabel(QWidget* parent, Qt::WindowFlags f) |
| 78 | + : QLabel(parent, f), |
| 79 | + d(new ElidingLabelPrivate(this)) |
| 80 | +{ |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +//============================================================================ |
| 86 | +CElidingLabel::CElidingLabel(const QString& text, QWidget* parent, Qt::WindowFlags f) |
| 87 | + : QLabel(text, parent,f), |
| 88 | + d(new ElidingLabelPrivate(this)) |
| 89 | +{ |
| 90 | + d->Text = text; |
| 91 | + setToolTip(text); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +//============================================================================ |
| 96 | +CElidingLabel::~CElidingLabel() |
| 97 | +{ |
| 98 | + delete d; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +//============================================================================ |
| 103 | +Qt::TextElideMode CElidingLabel::elideMode() const |
| 104 | +{ |
| 105 | + return d->ElideMode; |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +//============================================================================ |
| 110 | +void CElidingLabel::setElideMode(Qt::TextElideMode mode) |
| 111 | +{ |
| 112 | + d->ElideMode = mode; |
| 113 | + d->elideText(size().width()); |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +//============================================================================ |
| 118 | +void CElidingLabel::mouseReleaseEvent(QMouseEvent* event) |
| 119 | +{ |
| 120 | + if (event->button() != Qt::LeftButton) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + emit clicked(); |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +//============================================================================ |
| 130 | +void CElidingLabel::mouseDoubleClickEvent( QMouseEvent *ev ) |
| 131 | +{ |
| 132 | + Q_UNUSED(ev) |
| 133 | + emit doubleClicked(); |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +//============================================================================ |
| 138 | +void CElidingLabel::resizeEvent(QResizeEvent *event) |
| 139 | +{ |
| 140 | + if (!d->isModeElideNone()) |
| 141 | + { |
| 142 | + d->elideText(event->size().width()); |
| 143 | + } |
| 144 | + QLabel::resizeEvent(event); |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +//============================================================================ |
| 149 | +QSize CElidingLabel::minimumSizeHint() const |
| 150 | +{ |
| 151 | + if (pixmap() != nullptr || d->isModeElideNone()) |
| 152 | + { |
| 153 | + return QLabel::minimumSizeHint(); |
| 154 | + } |
| 155 | + const QFontMetrics &fm = fontMetrics(); |
| 156 | + QSize size(fm.width("…"), fm.height()); |
| 157 | + return size; |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +//============================================================================ |
| 162 | +QSize CElidingLabel::sizeHint() const |
| 163 | +{ |
| 164 | + if (pixmap() != nullptr || d->isModeElideNone()) |
| 165 | + { |
| 166 | + return QLabel::sizeHint(); |
| 167 | + } |
| 168 | + const QFontMetrics& fm = fontMetrics(); |
| 169 | + QSize size(fm.width(d->Text), QLabel::sizeHint().height()); |
| 170 | + return size; |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +//============================================================================ |
| 175 | +void CElidingLabel::setText(const QString &text) |
| 176 | +{ |
| 177 | + if (d->isModeElideNone()) |
| 178 | + { |
| 179 | + Super::setText(text); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + d->Text = text; |
| 184 | + setToolTip( text ); |
| 185 | + d->elideText(this->size().width()); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +//============================================================================ |
| 191 | +QString CElidingLabel::text() const |
| 192 | +{ |
| 193 | + return d->Text; |
| 194 | +} |
| 195 | +} // namespace QtLabb |
| 196 | + |
| 197 | +//--------------------------------------------------------------------------- |
| 198 | +// EOF ClickableLabel.cpp |
0 commit comments