2018-11-05 19:00:56 +08:00
|
|
|
/*******************************************************************************
|
|
|
|
** Qt Advanced Docking System
|
|
|
|
** Copyright (C) 2017 Uwe Kindler
|
|
|
|
**
|
|
|
|
** This library is free software; you can redistribute it and/or
|
|
|
|
** modify it under the terms of the GNU Lesser General Public
|
|
|
|
** License as published by the Free Software Foundation; either
|
|
|
|
** version 2.1 of the License, or (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU Lesser General Public
|
|
|
|
** License along with this library; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
/// \file ElidingLabel.cpp
|
|
|
|
/// \author Uwe Kindler
|
|
|
|
/// \date 05.11.2018
|
|
|
|
/// \brief Implementation of CElidingLabel
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
// INCLUDES
|
|
|
|
//============================================================================
|
2019-10-07 05:47:36 +08:00
|
|
|
#include "ElidingLabel.h"
|
2018-11-05 19:00:56 +08:00
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
|
|
|
|
namespace ads
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Private data of public CClickableLabel
|
|
|
|
*/
|
|
|
|
struct ElidingLabelPrivate
|
|
|
|
{
|
|
|
|
CElidingLabel* _this;
|
|
|
|
Qt::TextElideMode ElideMode = Qt::ElideNone;
|
|
|
|
QString Text;
|
|
|
|
|
|
|
|
ElidingLabelPrivate(CElidingLabel* _public) : _this(_public) {}
|
|
|
|
|
|
|
|
void elideText(int Width);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to check if the
|
|
|
|
*/
|
|
|
|
bool isModeElideNone() const
|
|
|
|
{
|
|
|
|
return Qt::ElideNone == ElideMode;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void ElidingLabelPrivate::elideText(int Width)
|
|
|
|
{
|
|
|
|
if (isModeElideNone())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QFontMetrics fm = _this->fontMetrics();
|
|
|
|
QString str = fm.elidedText(Text, ElideMode, Width - _this->margin() * 2 - _this->indent());
|
|
|
|
if (str == "…")
|
|
|
|
{
|
|
|
|
str = Text.at(0);
|
|
|
|
}
|
|
|
|
_this->QLabel::setText(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
CElidingLabel::CElidingLabel(QWidget* parent, Qt::WindowFlags f)
|
|
|
|
: QLabel(parent, f),
|
|
|
|
d(new ElidingLabelPrivate(this))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
CElidingLabel::CElidingLabel(const QString& text, QWidget* parent, Qt::WindowFlags f)
|
|
|
|
: QLabel(text, parent,f),
|
|
|
|
d(new ElidingLabelPrivate(this))
|
|
|
|
{
|
|
|
|
d->Text = text;
|
2019-01-23 14:43:07 +08:00
|
|
|
#ifndef QT_NO_TOOLTIP
|
2018-11-05 19:00:56 +08:00
|
|
|
setToolTip(text);
|
2019-01-23 14:43:07 +08:00
|
|
|
#endif
|
2018-11-05 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
CElidingLabel::~CElidingLabel()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
Qt::TextElideMode CElidingLabel::elideMode() const
|
|
|
|
{
|
|
|
|
return d->ElideMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CElidingLabel::setElideMode(Qt::TextElideMode mode)
|
|
|
|
{
|
|
|
|
d->ElideMode = mode;
|
|
|
|
d->elideText(size().width());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CElidingLabel::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
2018-11-08 17:04:29 +08:00
|
|
|
Super::mouseReleaseEvent(event);
|
2018-11-05 19:00:56 +08:00
|
|
|
if (event->button() != Qt::LeftButton)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CElidingLabel::mouseDoubleClickEvent( QMouseEvent *ev )
|
|
|
|
{
|
|
|
|
Q_UNUSED(ev)
|
|
|
|
emit doubleClicked();
|
2018-11-08 17:04:29 +08:00
|
|
|
Super::mouseDoubleClickEvent(ev);
|
2018-11-05 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CElidingLabel::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->isModeElideNone())
|
|
|
|
{
|
|
|
|
d->elideText(event->size().width());
|
|
|
|
}
|
2018-11-08 17:04:29 +08:00
|
|
|
Super::resizeEvent(event);
|
2018-11-05 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
QSize CElidingLabel::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
if (pixmap() != nullptr || d->isModeElideNone())
|
|
|
|
{
|
|
|
|
return QLabel::minimumSizeHint();
|
|
|
|
}
|
|
|
|
const QFontMetrics &fm = fontMetrics();
|
2019-09-09 21:05:37 +08:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
|
|
|
|
QSize size(fm.horizontalAdvance(d->Text.left(2) + "…"), fm.height());
|
|
|
|
#else
|
|
|
|
QSize size(fm.width(d->Text.left(2) + "…"), fm.height());
|
|
|
|
#endif
|
2018-11-05 19:00:56 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
QSize CElidingLabel::sizeHint() const
|
|
|
|
{
|
|
|
|
if (pixmap() != nullptr || d->isModeElideNone())
|
|
|
|
{
|
|
|
|
return QLabel::sizeHint();
|
|
|
|
}
|
|
|
|
const QFontMetrics& fm = fontMetrics();
|
2019-09-09 21:05:37 +08:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
|
|
|
|
QSize size(fm.horizontalAdvance(d->Text), QLabel::sizeHint().height());
|
|
|
|
#else
|
|
|
|
QSize size(fm.width(d->Text), QLabel::sizeHint().height());
|
|
|
|
#endif
|
2018-11-05 19:00:56 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CElidingLabel::setText(const QString &text)
|
|
|
|
{
|
|
|
|
if (d->isModeElideNone())
|
|
|
|
{
|
|
|
|
Super::setText(text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->Text = text;
|
2019-01-23 14:43:07 +08:00
|
|
|
#ifndef QT_NO_TOOLTIP
|
2018-11-05 19:00:56 +08:00
|
|
|
setToolTip( text );
|
2019-01-23 14:43:07 +08:00
|
|
|
#endif
|
2018-11-05 19:00:56 +08:00
|
|
|
d->elideText(this->size().width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
QString CElidingLabel::text() const
|
|
|
|
{
|
|
|
|
return d->Text;
|
|
|
|
}
|
|
|
|
} // namespace QtLabb
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// EOF ClickableLabel.cpp
|