1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include <QTextDocument>
23 #include <QTextLayout>
25 #include "graphicalui.h"
26 #include "styledlabel.h"
29 StyledLabel::StyledLabel(QWidget *parent)
31 _wrapMode(QTextOption::NoWrap),
32 _alignment(Qt::AlignVCenter|Qt::AlignLeft),
35 QTextOption opt = _layout.textOption();
36 opt.setWrapMode(_wrapMode);
37 opt.setAlignment(_alignment);
38 _layout.setTextOption(opt);
41 void StyledLabel::setWrapMode(QTextOption::WrapMode mode) {
46 QTextOption opt = _layout.textOption();
47 opt.setWrapMode(mode);
48 _layout.setTextOption(opt);
53 void StyledLabel::setAlignment(Qt::Alignment alignment) {
54 if(_alignment == alignment)
57 _alignment = alignment;
58 QTextOption opt = _layout.textOption();
59 opt.setAlignment(alignment);
60 _layout.setTextOption(opt);
65 void StyledLabel::resizeEvent(QResizeEvent *event) {
66 QFrame::resizeEvent(event);
71 QSize StyledLabel::sizeHint() const {
75 void StyledLabel::updateSizeHint() {
77 int padding = frameWidth() * 2;
78 sh = _layout.boundingRect().size().toSize() + QSize(padding, padding);
86 void StyledLabel::setText(const QString &text) {
87 UiStyle *style = GraphicalUi::uiStyle();
89 UiStyle::StyledString sstr = style->styleString(style->mircToInternal(text), UiStyle::PlainMsg);
90 QList<QTextLayout::FormatRange> layoutList = style->toTextLayoutList(sstr.formatList, sstr.plainText.length(), 0);
92 _layout.setText(sstr.plainText);
93 _layout.setAdditionalFormats(layoutList);
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!
102 setToolTip(QString());
105 void StyledLabel::layout() {
107 qreal w = frameRect().width() - 2*frameWidth();
109 _layout.beginLayout();
111 QTextLine line = _layout.createLine();
114 line.setLineWidth(w);
115 line.setPosition(QPointF(0, h));
125 void StyledLabel::paintEvent(QPaintEvent *) {
126 QPainter painter(this);
128 qreal y = (frameRect().height() - _layout.boundingRect().height()) / 2;
129 _layout.draw(&painter, QPointF(0, y), QVector<QTextLayout::FormatRange>());