added missing macros
[quassel.git] / src / uisupport / styledlabel.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include <QPainter>
22 #include <QTextDocument>
23 #include <QTextLayout>
24
25 #include "graphicalui.h"
26 #include "styledlabel.h"
27 #include "uistyle.h"
28
29 StyledLabel::StyledLabel(QWidget *parent)
30 : QFrame(parent),
31   _wrapMode(QTextOption::NoWrap),
32   _alignment(Qt::AlignVCenter|Qt::AlignLeft),
33   _toolTipEnabled(true)
34 {
35   QTextOption opt = _layout.textOption();
36   opt.setWrapMode(_wrapMode);
37   opt.setAlignment(_alignment);
38   _layout.setTextOption(opt);
39 }
40
41 void StyledLabel::setWrapMode(QTextOption::WrapMode mode) {
42   if(_wrapMode == mode)
43     return;
44
45   _wrapMode = mode;
46   QTextOption opt = _layout.textOption();
47   opt.setWrapMode(mode);
48   _layout.setTextOption(opt);
49
50   layout();
51 }
52
53 void StyledLabel::setAlignment(Qt::Alignment alignment) {
54   if(_alignment == alignment)
55     return;
56
57   _alignment = alignment;
58   QTextOption opt = _layout.textOption();
59   opt.setAlignment(alignment);
60   _layout.setTextOption(opt);
61
62   layout();
63 }
64
65 void StyledLabel::resizeEvent(QResizeEvent *event) {
66   QFrame::resizeEvent(event);
67
68   layout();
69 }
70
71 QSize StyledLabel::sizeHint() const {
72   return _sizeHint;
73 }
74
75 void StyledLabel::updateSizeHint() {
76   QSize sh;
77   int padding = frameWidth() * 2;
78   sh = _layout.boundingRect().size().toSize() + QSize(padding, padding);
79
80   if(_sizeHint != sh) {
81     _sizeHint = sh;
82     updateGeometry();
83   }
84 }
85
86 void StyledLabel::setText(const QString &text) {
87   UiStyle *style = GraphicalUi::uiStyle();
88
89   UiStyle::StyledString sstr = style->styleString(style->mircToInternal(text), UiStyle::PlainMsg);
90   QList<QTextLayout::FormatRange> layoutList = style->toTextLayoutList(sstr.formatList, sstr.plainText.length(), 0);
91
92   _layout.setText(sstr.plainText);
93   _layout.setAdditionalFormats(layoutList);
94
95   layout();
96 }
97
98 void StyledLabel::updateToolTip() {
99   if(frameRect().width() - 2*frameWidth() < _layout.minimumWidth())
100     setToolTip(QString("<qt>%1</qt>").arg(Qt::escape(_layout.text()))); // only rich text gets wordwrapped!
101   else
102     setToolTip(QString());
103 }
104
105 void StyledLabel::layout() {
106   qreal h = 0;
107   qreal w = frameRect().width() - 2*frameWidth();
108
109   _layout.beginLayout();
110   forever {
111     QTextLine line = _layout.createLine();
112     if(!line.isValid())
113       break;
114     line.setLineWidth(w);
115     line.setPosition(QPointF(0, h));
116     h += line.height();
117   }
118   _layout.endLayout();
119
120   updateSizeHint();
121   updateToolTip();
122   update();
123 }
124
125 void StyledLabel::paintEvent(QPaintEvent *) {
126   QPainter painter(this);
127
128   qreal y = (frameRect().height() - _layout.boundingRect().height()) / 2;
129   _layout.draw(&painter, QPointF(0, y), QVector<QTextLayout::FormatRange>());
130 }