From: Manuel Nickschas Date: Fri, 21 Aug 2009 22:02:26 +0000 (+0200) Subject: Introducing StyledLabel X-Git-Tag: 0.5-rc1~52 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=148964099b6e799121402d297c854393f9988be8;ds=sidebyside Introducing StyledLabel This widget evolved from TopicLabel and provides a QFrame that contains a mIRC-formatted string (e.g. it accepts mIRC's format codes in the input). Rather than the old drag-to-scroll behavior, this widget provides a tooltip. It also uses proper QTextLayouts instead of assembling text snippets manually. Note that URLs are not yet clickable. This will be added later. --- diff --git a/src/uisupport/CMakeLists.txt b/src/uisupport/CMakeLists.txt index 549cee2a..9a538070 100644 --- a/src/uisupport/CMakeLists.txt +++ b/src/uisupport/CMakeLists.txt @@ -27,7 +27,9 @@ set(SOURCES nickview.cpp nickviewfilter.cpp qssparser.cpp + resizingstackedwidget.cpp settingspage.cpp + styledlabel.cpp tabcompleter.cpp toolbaractionprovider.cpp uisettings.cpp @@ -56,7 +58,9 @@ set(MOC_HDRS networkmodelcontroller.h nickview.h nickviewfilter.h + resizingstackedwidget.h settingspage.h + styledlabel.h tabcompleter.h toolbaractionprovider.h uistyle.h diff --git a/src/uisupport/styledlabel.cpp b/src/uisupport/styledlabel.cpp new file mode 100644 index 00000000..13690df3 --- /dev/null +++ b/src/uisupport/styledlabel.cpp @@ -0,0 +1,130 @@ +/*************************************************************************** +* Copyright (C) 2005-09 by the Quassel Project * +* devel@quassel-irc.org * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) version 3. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ + +#include +#include +#include + +#include "graphicalui.h" +#include "styledlabel.h" +#include "uistyle.h" + +StyledLabel::StyledLabel(QWidget *parent) +: QFrame(parent), + _wrapMode(QTextOption::NoWrap), + _alignment(Qt::AlignVCenter|Qt::AlignLeft), + _toolTipEnabled(true) +{ + QTextOption opt = _layout.textOption(); + opt.setWrapMode(_wrapMode); + opt.setAlignment(_alignment); + _layout.setTextOption(opt); +} + +void StyledLabel::setWrapMode(QTextOption::WrapMode mode) { + if(_wrapMode == mode) + return; + + _wrapMode = mode; + QTextOption opt = _layout.textOption(); + opt.setWrapMode(mode); + _layout.setTextOption(opt); + + layout(); +} + +void StyledLabel::setAlignment(Qt::Alignment alignment) { + if(_alignment == alignment) + return; + + _alignment = alignment; + QTextOption opt = _layout.textOption(); + opt.setAlignment(alignment); + _layout.setTextOption(opt); + + layout(); +} + +void StyledLabel::resizeEvent(QResizeEvent *event) { + QFrame::resizeEvent(event); + + layout(); +} + +QSize StyledLabel::sizeHint() const { + return _sizeHint; +} + +void StyledLabel::updateSizeHint() { + QSize sh; + int padding = frameWidth() * 2; + sh = _layout.boundingRect().size().toSize() + QSize(padding, padding); + + if(_sizeHint != sh) { + _sizeHint = sh; + updateGeometry(); + } +} + +void StyledLabel::setText(const QString &text) { + UiStyle *style = GraphicalUi::uiStyle(); + + UiStyle::StyledString sstr = style->styleString(style->mircToInternal(text), UiStyle::PlainMsg); + QList layoutList = style->toTextLayoutList(sstr.formatList, sstr.plainText.length(), 0); + + _layout.setText(sstr.plainText); + _layout.setAdditionalFormats(layoutList); + + layout(); +} + +void StyledLabel::updateToolTip() { + if(frameRect().width() - 2*frameWidth() < _layout.minimumWidth()) + setToolTip(QString("%1").arg(Qt::escape(_layout.text()))); // only rich text gets wordwrapped! + else + setToolTip(QString()); +} + +void StyledLabel::layout() { + qreal h = 0; + qreal w = frameRect().width() - 2*frameWidth(); + + _layout.beginLayout(); + forever { + QTextLine line = _layout.createLine(); + if(!line.isValid()) + break; + line.setLineWidth(w); + line.setPosition(QPointF(0, h)); + h += line.height(); + } + _layout.endLayout(); + + updateSizeHint(); + updateToolTip(); + update(); +} + +void StyledLabel::paintEvent(QPaintEvent *) { + QPainter painter(this); + + qreal y = (frameRect().height() - _layout.boundingRect().height()) / 2; + _layout.draw(&painter, QPointF(0, y), QVector()); +} diff --git a/src/uisupport/styledlabel.h b/src/uisupport/styledlabel.h new file mode 100644 index 00000000..4eefb6a0 --- /dev/null +++ b/src/uisupport/styledlabel.h @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (C) 2005-09 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef STYLEDLABEL_H +#define STYLEDLABEL_H + +#include + +#include "uistyle.h" + +class StyledLabel : public QFrame { + Q_OBJECT + +public: + StyledLabel(QWidget *parent = 0); + + void setText(const QString &text); + + virtual QSize sizeHint() const; + //virtual QSize minimumSizeHint() const; + + inline QTextOption::WrapMode wrapMode() const { return _wrapMode; } + void setWrapMode(QTextOption::WrapMode mode); + + inline Qt::Alignment alignment() const { return _alignment; } + void setAlignment(Qt::Alignment alignment); + + inline bool toolTipEnabled() const { return _toolTipEnabled; } + void setToolTipEnabled(bool); + +protected: + virtual void paintEvent(QPaintEvent *event); + virtual void resizeEvent(QResizeEvent *event); + + //void mouseMoveEvent(QMouseEvent *event); + //void mousePressEvent(QMouseEvent *event); + //void mouseReleaseEvent(QMouseEvent *event); + //void mouseDoubleClickEvent(QMouseEvent *event); + +private: + QSize _sizeHint; + QTextOption::WrapMode _wrapMode; + Qt::Alignment _alignment; + QTextLayout _layout; + bool _toolTipEnabled; + + QList _layoutList; + + void layout(); + void updateSizeHint(); + void updateToolTip(); +}; + +#endif