src: Yearly copyright bump
[quassel.git] / src / uisupport / styledlabel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "styledlabel.h"
22
23 #include <QPainter>
24 #include <QTextDocument>
25 #include <QTextLayout>
26
27 #include "graphicalui.h"
28 #include "uistyle.h"
29
30 StyledLabel::StyledLabel(QWidget* parent)
31     : QFrame(parent)
32     , _alignment(Qt::AlignVCenter | Qt::AlignLeft)
33 {
34     setMouseTracking(true);
35
36     QTextOption opt = _layout.textOption();
37     opt.setWrapMode(_wrapMode);
38     opt.setAlignment(_alignment);
39     _layout.setTextOption(opt);
40 }
41
42 void StyledLabel::setCustomFont(const QFont& font)
43 {
44     setFont(font);
45     _layout.setFont(font);
46     setText(_layout.text());
47 }
48
49 void StyledLabel::setWrapMode(QTextOption::WrapMode mode)
50 {
51     if (_wrapMode == mode)
52         return;
53
54     _wrapMode = mode;
55     QTextOption opt = _layout.textOption();
56     opt.setWrapMode(mode);
57     _layout.setTextOption(opt);
58
59     layout();
60 }
61
62 void StyledLabel::setAlignment(Qt::Alignment alignment)
63 {
64     if (_alignment == alignment)
65         return;
66
67     _alignment = alignment;
68     QTextOption opt = _layout.textOption();
69     opt.setAlignment(alignment);
70     _layout.setTextOption(opt);
71
72     layout();
73 }
74
75 void StyledLabel::setResizeMode(ResizeMode mode)
76 {
77     if (_resizeMode == mode)
78         return;
79
80     _resizeMode = mode;
81     if (mode == DynamicResize)
82         setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
83     else
84         setWrapMode(QTextOption::NoWrap);
85 }
86
87 void StyledLabel::resizeEvent(QResizeEvent* event)
88 {
89     QFrame::resizeEvent(event);
90
91     layout();
92 }
93
94 QSize StyledLabel::sizeHint() const
95 {
96     return _sizeHint;
97 }
98
99 void StyledLabel::updateSizeHint()
100 {
101     QSize sh;
102     int padding = frameWidth() * 2;
103     sh = _layout.boundingRect().size().toSize() + QSize(padding, padding);
104
105     if (_sizeHint != sh) {
106         _sizeHint = sh;
107         updateGeometry();
108     }
109 }
110
111 void StyledLabel::setText(const QString& text)
112 {
113     UiStyle* style = GraphicalUi::uiStyle();
114
115     UiStyle::StyledString sstr = style->styleString(style->mircToInternal(text), UiStyle::FormatType::PlainMsg);
116     QList<QTextLayout::FormatRange> layoutList = style->toTextLayoutList(sstr.formatList, sstr.plainText.length(), UiStyle::MessageLabel::None);
117
118     // Use default font rather than the style's
119     QTextLayout::FormatRange fmtRange;
120     fmtRange.format.setFont(font());
121     fmtRange.start = 0;
122     fmtRange.length = sstr.plainText.length();
123     layoutList << fmtRange;
124
125     // Mark URLs
126     _clickables = ClickableList::fromString(sstr.plainText);
127     foreach (Clickable click, _clickables) {
128         if (click.type() == Clickable::Url) {
129             QTextLayout::FormatRange range;
130             range.start = click.start();
131             range.length = click.length();
132             range.format.setForeground(palette().link());
133             layoutList << range;
134         }
135     }
136
137     _layout.setText(sstr.plainText);
138     _layout.setAdditionalFormats(layoutList);
139
140     layout();
141
142     endHoverMode();
143 }
144
145 void StyledLabel::updateToolTip()
146 {
147     if (frameRect().width() - 2 * frameWidth() < _layout.minimumWidth())
148         setToolTip(QString("<qt>%1</qt>").arg(_layout.text().toHtmlEscaped()));  // only rich text gets wordwrapped!
149     else
150         setToolTip(QString());
151 }
152
153 void StyledLabel::layout()
154 {
155     qreal h = 0;
156     qreal w = contentsRect().width();
157
158     _layout.beginLayout();
159     forever
160     {
161         QTextLine line = _layout.createLine();
162         if (!line.isValid())
163             break;
164         line.setLineWidth(w);
165         line.setPosition(QPointF(0, h));
166         h += line.height();
167     }
168     _layout.endLayout();
169
170     updateSizeHint();
171     updateToolTip();
172     update();
173 }
174
175 void StyledLabel::paintEvent(QPaintEvent* e)
176 {
177     QFrame::paintEvent(e);
178     QPainter painter(this);
179
180     qreal y = contentsRect().y() + (contentsRect().height() - _layout.boundingRect().height()) / 2;
181     _layout.draw(&painter, QPointF(contentsRect().x(), y), _extraLayoutList);
182 }
183
184 int StyledLabel::posToCursor(const QPointF& pos)
185 {
186     if (pos.y() < 0 || pos.y() > height())
187         return -1;
188
189     for (int l = _layout.lineCount() - 1; l >= 0; l--) {
190         QTextLine line = _layout.lineAt(l);
191         if (pos.y() >= line.y()) {
192             return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter);
193         }
194     }
195     return -1;
196 }
197
198 void StyledLabel::mouseMoveEvent(QMouseEvent* event)
199 {
200     if (event->buttons() == Qt::NoButton) {
201         Clickable click = _clickables.atCursorPos(posToCursor(event->localPos()));
202         if (click.isValid())
203             setHoverMode(click.start(), click.length());
204         else
205             endHoverMode();
206     }
207 }
208
209 void StyledLabel::enterEvent(QEvent*)
210 {
211     if (resizeMode() == ResizeOnHover)
212         setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
213 }
214
215 void StyledLabel::leaveEvent(QEvent*)
216 {
217     endHoverMode();
218     if (resizeMode() == ResizeOnHover)
219         setWrapMode(QTextOption::NoWrap);
220 }
221
222 void StyledLabel::mousePressEvent(QMouseEvent* event)
223 {
224     if (event->button() == Qt::LeftButton) {
225         Clickable click = _clickables.atCursorPos(posToCursor(event->localPos()));
226         if (click.isValid())
227             emit clickableActivated(click);
228     }
229 }
230
231 void StyledLabel::setHoverMode(int start, int length)
232 {
233     if (_extraLayoutList.count() >= 1 && _extraLayoutList.first().start == start && _extraLayoutList.first().length == length)
234         return;
235
236     QTextLayout::FormatRange range;
237     range.start = start;
238     range.length = length;
239     range.format.setFontUnderline(true);
240     _extraLayoutList.clear();
241     _extraLayoutList << range;
242
243     setCursor(Qt::PointingHandCursor);
244     update();
245 }
246
247 void StyledLabel::endHoverMode()
248 {
249     _extraLayoutList.clear();
250     setCursor(Qt::ArrowCursor);
251     update();
252 }