1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "styledlabel.h"
24 #include <QTextDocument>
25 #include <QTextLayout>
27 #include "graphicalui.h"
30 StyledLabel::StyledLabel(QWidget* parent)
32 , _alignment(Qt::AlignVCenter | Qt::AlignLeft)
34 setMouseTracking(true);
36 QTextOption opt = _layout.textOption();
37 opt.setWrapMode(_wrapMode);
38 opt.setAlignment(_alignment);
39 _layout.setTextOption(opt);
42 void StyledLabel::setCustomFont(const QFont& font)
45 _layout.setFont(font);
46 setText(_layout.text());
49 void StyledLabel::setWrapMode(QTextOption::WrapMode mode)
51 if (_wrapMode == mode)
55 QTextOption opt = _layout.textOption();
56 opt.setWrapMode(mode);
57 _layout.setTextOption(opt);
62 void StyledLabel::setAlignment(Qt::Alignment alignment)
64 if (_alignment == alignment)
67 _alignment = alignment;
68 QTextOption opt = _layout.textOption();
69 opt.setAlignment(alignment);
70 _layout.setTextOption(opt);
75 void StyledLabel::setResizeMode(ResizeMode mode)
77 if (_resizeMode == mode)
81 if (mode == DynamicResize)
82 setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
84 setWrapMode(QTextOption::NoWrap);
87 void StyledLabel::resizeEvent(QResizeEvent* event)
89 QFrame::resizeEvent(event);
94 QSize StyledLabel::sizeHint() const
99 void StyledLabel::updateSizeHint()
102 int padding = frameWidth() * 2;
103 sh = _layout.boundingRect().size().toSize() + QSize(padding, padding);
105 if (_sizeHint != sh) {
111 void StyledLabel::setText(const QString& text)
113 UiStyle::StyledString sstr = UiStyle::styleString(UiStyle::mircToInternal(text), UiStyle::FormatType::PlainMsg);
114 UiStyle::FormatContainer layoutList = GraphicalUi::uiStyle()->toTextLayoutList(sstr.formatList, sstr.plainText.length(), UiStyle::MessageLabel::None);
116 // Use default font rather than the style's
117 QTextLayout::FormatRange fmtRange;
118 fmtRange.format.setFont(font());
120 fmtRange.length = sstr.plainText.length();
121 layoutList << fmtRange;
124 _clickables = ClickableList::fromString(sstr.plainText);
125 foreach (Clickable click, _clickables) {
126 if (click.type() == Clickable::Url) {
127 QTextLayout::FormatRange range;
128 range.start = click.start();
129 range.length = click.length();
130 range.format.setForeground(palette().link());
135 _layout.setText(sstr.plainText);
136 UiStyle::setTextLayoutFormats(_layout, layoutList);
143 void StyledLabel::updateToolTip()
145 if (frameRect().width() - 2 * frameWidth() < _layout.minimumWidth())
146 setToolTip(QString("<qt>%1</qt>").arg(_layout.text().toHtmlEscaped())); // only rich text gets wordwrapped!
148 setToolTip(QString());
151 void StyledLabel::layout()
154 qreal w = contentsRect().width();
156 _layout.beginLayout();
159 QTextLine line = _layout.createLine();
162 line.setLineWidth(w);
163 line.setPosition(QPointF(0, h));
173 void StyledLabel::paintEvent(QPaintEvent* e)
175 QFrame::paintEvent(e);
176 QPainter painter(this);
178 qreal y = contentsRect().y() + (contentsRect().height() - _layout.boundingRect().height()) / 2;
179 _layout.draw(&painter, QPointF(contentsRect().x(), y), _extraLayoutList);
182 int StyledLabel::posToCursor(const QPointF& pos)
184 if (pos.y() < 0 || pos.y() > height())
187 for (int l = _layout.lineCount() - 1; l >= 0; l--) {
188 QTextLine line = _layout.lineAt(l);
189 if (pos.y() >= line.y()) {
190 return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
196 void StyledLabel::mouseMoveEvent(QMouseEvent* event)
198 if (event->buttons() == Qt::NoButton) {
199 Clickable click = _clickables.atCursorPos(posToCursor(event->localPos()));
201 setHoverMode(click.start(), click.length());
207 void StyledLabel::enterEvent(QEvent*)
209 if (resizeMode() == ResizeOnHover)
210 setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
213 void StyledLabel::leaveEvent(QEvent*)
216 if (resizeMode() == ResizeOnHover)
217 setWrapMode(QTextOption::NoWrap);
220 void StyledLabel::mousePressEvent(QMouseEvent* event)
222 if (event->button() == Qt::LeftButton) {
223 Clickable click = _clickables.atCursorPos(posToCursor(event->localPos()));
225 emit clickableActivated(click);
229 void StyledLabel::setHoverMode(int start, int length)
231 if (_extraLayoutList.count() >= 1 && _extraLayoutList.first().start == start && _extraLayoutList.first().length == length)
234 QTextLayout::FormatRange range;
236 range.length = length;
237 range.format.setFontUnderline(true);
238 _extraLayoutList.clear();
239 _extraLayoutList << range;
241 setCursor(Qt::PointingHandCursor);
245 void StyledLabel::endHoverMode()
247 _extraLayoutList.clear();
248 setCursor(Qt::ArrowCursor);