2014-01-20 15:03:24 -05:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
** You may use this file under the terms of the BSD license as follows:
|
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
|
|
|
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
|
|
|
** of its contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ElidedLabel.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
2015-08-29 15:06:36 -04:00
|
|
|
#include <QPainter>
|
2014-01-20 15:03:24 -05:00
|
|
|
#include <QStyleOption>
|
2015-08-29 15:06:36 -04:00
|
|
|
#include <QTextDocument>
|
|
|
|
#include <QTextLayout>
|
|
|
|
#include <QToolTip>
|
2014-01-20 15:03:24 -05:00
|
|
|
|
|
|
|
ElidedLabel::ElidedLabel(const QString &text, QWidget *parent)
|
|
|
|
: QLabel(parent)
|
2014-12-14 12:05:43 -05:00
|
|
|
, mElided(false)
|
2015-08-29 15:06:36 -04:00
|
|
|
, mOnlyPlainText(false)
|
2014-12-14 12:05:43 -05:00
|
|
|
, mContent(text)
|
2016-10-16 09:48:50 -04:00
|
|
|
, mTextColor(QColor())
|
2014-01-20 15:03:24 -05:00
|
|
|
{
|
2016-10-16 09:48:50 -04:00
|
|
|
setStyleSheet("background-color: rgba(0,0,0,0%)");
|
2015-08-29 15:06:36 -04:00
|
|
|
mRectElision = QRect();
|
2014-01-20 15:03:24 -05:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
}
|
|
|
|
|
|
|
|
ElidedLabel::ElidedLabel(QWidget *parent)
|
|
|
|
: QLabel(parent)
|
2014-12-14 12:05:43 -05:00
|
|
|
, mElided(false)
|
2015-08-29 15:06:36 -04:00
|
|
|
, mOnlyPlainText(false)
|
|
|
|
, mContent("")
|
2016-10-16 09:48:50 -04:00
|
|
|
, mTextColor(QColor())
|
2014-01-20 15:03:24 -05:00
|
|
|
{
|
2016-10-16 09:48:50 -04:00
|
|
|
setStyleSheet("background-color: rgba(0,0,0,0%)");
|
2015-08-29 15:06:36 -04:00
|
|
|
mRectElision = QRect();
|
2014-01-20 15:03:24 -05:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedLabel::setText(const QString &newText)
|
|
|
|
{
|
2014-12-14 12:05:43 -05:00
|
|
|
mContent = newText;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2015-08-29 15:06:36 -04:00
|
|
|
void ElidedLabel::setOnlyPlainText(const bool &value)
|
|
|
|
{
|
|
|
|
mOnlyPlainText = value;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-12-14 12:05:43 -05:00
|
|
|
void ElidedLabel::clear()
|
|
|
|
{
|
|
|
|
mContent.clear();
|
2014-01-20 15:03:24 -05:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElidedLabel::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QLabel::paintEvent(event);
|
2015-08-29 15:06:36 -04:00
|
|
|
QList<QPair<QTextLine,QPoint> > lLines;
|
|
|
|
QString elidedLastLine = "";
|
2014-01-20 15:03:24 -05:00
|
|
|
QPainter painter(this);
|
|
|
|
QFontMetrics fontMetrics = painter.fontMetrics();
|
|
|
|
QRect cr = contentsRect();
|
|
|
|
cr.adjust(margin(), margin(), -margin(), -margin());
|
2016-10-16 09:48:50 -04:00
|
|
|
|
2014-01-20 15:03:24 -05:00
|
|
|
bool didElide = false;
|
2015-08-29 15:06:36 -04:00
|
|
|
QChar ellipsisChar(0x2026);//= "…"
|
2014-01-20 15:03:24 -05:00
|
|
|
int lineSpacing = fontMetrics.lineSpacing();
|
2015-08-29 15:06:36 -04:00
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
QString plainText = "";
|
|
|
|
if (mOnlyPlainText)
|
|
|
|
{
|
|
|
|
plainText = mContent;
|
|
|
|
} else {
|
|
|
|
QTextDocument td;
|
|
|
|
td.setHtml(mContent);
|
|
|
|
plainText = td.toPlainText();
|
|
|
|
}
|
|
|
|
plainText = plainText.replace("\n",QChar(QChar::LineSeparator));
|
|
|
|
plainText = plainText.replace("\r",QChar(QChar::LineSeparator));
|
|
|
|
|
|
|
|
QTextLayout textLayout(plainText, painter.font());
|
|
|
|
QTextOption to = textLayout.textOption();
|
|
|
|
to.setAlignment(alignment());
|
|
|
|
to.setWrapMode(wordWrap() ? QTextOption::WrapAtWordBoundaryOrAnywhere : QTextOption::NoWrap);
|
|
|
|
textLayout.setTextOption(to);
|
|
|
|
|
2014-01-20 15:03:24 -05:00
|
|
|
textLayout.beginLayout();
|
|
|
|
forever {
|
2015-08-29 15:06:36 -04:00
|
|
|
//Get new line for text.
|
2014-01-20 15:03:24 -05:00
|
|
|
QTextLine line = textLayout.createLine();
|
2015-08-29 15:06:36 -04:00
|
|
|
|
2014-01-20 15:03:24 -05:00
|
|
|
if (!line.isValid())
|
2015-08-29 15:06:36 -04:00
|
|
|
break;// No more line to write
|
|
|
|
|
|
|
|
line.setLineWidth(cr.width());
|
2014-01-20 15:03:24 -05:00
|
|
|
int nextLineY = y + lineSpacing;
|
2015-08-29 15:06:36 -04:00
|
|
|
|
|
|
|
if ((cr.height() >= nextLineY + lineSpacing) && wordWrap()) {
|
|
|
|
//Line written normaly, next line will too
|
|
|
|
lLines.append(QPair<QTextLine, QPoint>(line, QPoint(0, y)));
|
2014-01-20 15:03:24 -05:00
|
|
|
y = nextLineY;
|
|
|
|
} else {
|
2015-08-29 15:06:36 -04:00
|
|
|
//The next line can't be written.
|
|
|
|
QString lastLine = plainText.mid(line.textStart()).split(QChar(QChar::LineSeparator)).at(0);
|
|
|
|
QTextLine lineEnd = textLayout.createLine();
|
|
|
|
if (!lineEnd.isValid() && (wordWrap()
|
|
|
|
|| (fontMetrics.width(lastLine) < cr.width()))) {
|
|
|
|
//No more text for next line so this one is OK
|
|
|
|
lLines.append(QPair<QTextLine, QPoint>(line, QPoint(0, y)));
|
|
|
|
elidedLastLine="";
|
|
|
|
didElide = false;
|
|
|
|
} else {
|
|
|
|
//Text is left, so get elided text
|
|
|
|
if (lastLine == "") {
|
|
|
|
elidedLastLine = ellipsisChar;
|
|
|
|
} else {
|
|
|
|
elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, cr.width()-1);
|
|
|
|
if (elidedLastLine.right(1) != ellipsisChar)
|
|
|
|
elidedLastLine.append(ellipsisChar);//New line at end
|
|
|
|
}
|
|
|
|
didElide = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-20 15:03:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
textLayout.endLayout();
|
2015-08-29 15:06:36 -04:00
|
|
|
|
|
|
|
int iTransX, iTransY = iTransX = 0;
|
|
|
|
int iHeight = lLines.count() * lineSpacing;
|
|
|
|
if (didElide) iHeight += lineSpacing;
|
|
|
|
|
|
|
|
//Compute lines translation with alignment
|
|
|
|
if (alignment() & Qt::AlignTop)
|
|
|
|
iTransY = 0;
|
|
|
|
if (alignment() & Qt::AlignBottom)
|
|
|
|
iTransY = cr.height() - iHeight;
|
|
|
|
if (alignment() & Qt::AlignVCenter)
|
|
|
|
iTransY = (cr.height() - iHeight) / 2;
|
|
|
|
|
|
|
|
QPair<QTextLine,QPoint> pair;
|
|
|
|
QPoint lastPos(-1,-1);
|
|
|
|
//Now we know how many lines to redraw at good position
|
|
|
|
foreach (pair, lLines){
|
2016-11-25 16:14:08 -05:00
|
|
|
lastPos = pair.second + QPoint(0+ cr.left(), iTransY + cr.top());
|
2015-08-29 15:06:36 -04:00
|
|
|
pair.first.draw(&painter, lastPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Print last elided line
|
|
|
|
if (didElide) {
|
|
|
|
int width = fontMetrics.width(elidedLastLine);
|
|
|
|
if (lastPos.y() == -1){
|
|
|
|
y = iTransY;// Only one line
|
|
|
|
} else {
|
|
|
|
y = lastPos.y() + lineSpacing;
|
|
|
|
}
|
|
|
|
if (width < cr.width()){
|
|
|
|
//Text don't taking all line (with line break), so align it
|
|
|
|
if (alignment() & Qt::AlignLeft)
|
|
|
|
iTransX = 0;
|
|
|
|
if (alignment() & Qt::AlignRight)
|
|
|
|
iTransX = cr.width() - width;
|
|
|
|
if (alignment() & Qt::AlignHCenter)
|
|
|
|
iTransX = (cr.width() - width) / 2;
|
|
|
|
if (alignment() & Qt::AlignJustify)
|
|
|
|
iTransX = 0;
|
|
|
|
}
|
|
|
|
|
2016-11-25 16:14:08 -05:00
|
|
|
painter.drawText(QPoint(iTransX + cr.left(), y + fontMetrics.ascent() + cr.top()), elidedLastLine);
|
2015-08-29 15:06:36 -04:00
|
|
|
//Draw button to get ToolTip
|
2016-11-25 16:14:08 -05:00
|
|
|
mRectElision = QRect(iTransX + width - fontMetrics.width(ellipsisChar) + cr.left()
|
|
|
|
, y + cr.top()
|
2015-08-29 15:06:36 -04:00
|
|
|
, fontMetrics.width(ellipsisChar)
|
|
|
|
, fontMetrics.height() - 1);
|
|
|
|
painter.drawRoundRect(mRectElision);
|
|
|
|
} else {
|
|
|
|
mRectElision = QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Send signal if changed
|
2014-12-14 12:05:43 -05:00
|
|
|
if (didElide != mElided) {
|
|
|
|
mElided = didElide;
|
2014-01-20 15:03:24 -05:00
|
|
|
emit elisionChanged(didElide);
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 15:06:36 -04:00
|
|
|
|
|
|
|
void ElidedLabel::mousePressEvent(QMouseEvent *ev)
|
|
|
|
{
|
|
|
|
if (mElided && (ev->buttons()==Qt::LeftButton) && (mRectElision.contains(ev->pos()))){
|
|
|
|
QToolTip::showText(mapToGlobal(QPoint(0, 0)),QString("<FONT>") + mContent + QString("</FONT>"));
|
2015-09-09 16:37:38 -04:00
|
|
|
return; // eat event
|
2015-08-29 15:06:36 -04:00
|
|
|
}
|
2015-09-09 16:37:38 -04:00
|
|
|
QLabel::mousePressEvent(ev);
|
2015-08-29 15:06:36 -04:00
|
|
|
}
|
2016-10-16 09:48:50 -04:00
|
|
|
|
|
|
|
void ElidedLabel::setTextColor(const QColor &color)
|
|
|
|
{
|
|
|
|
QPalette tmpPalette = palette();
|
|
|
|
tmpPalette.setColor(foregroundRole(), color);
|
|
|
|
setPalette(tmpPalette);
|
|
|
|
mTextColor = color;
|
|
|
|
}
|