X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fstyledlabel.cpp;h=cf3822966bf8c0912f22777b52e4e9c225382adb;hp=38250ea27fd18308855bb29e05ac40616ef6b22d;hb=694f9bfbf7f1af19108461c7e00d133e55082bce;hpb=61c8d84d1c849373e0f115dc748ed45cff95287d diff --git a/src/uisupport/styledlabel.cpp b/src/uisupport/styledlabel.cpp index 38250ea2..cf382296 100644 --- a/src/uisupport/styledlabel.cpp +++ b/src/uisupport/styledlabel.cpp @@ -27,210 +27,245 @@ #include "uistyle.h" StyledLabel::StyledLabel(QWidget *parent) -: QFrame(parent), - _wrapMode(QTextOption::NoWrap), - _alignment(Qt::AlignVCenter|Qt::AlignLeft), - _toolTipEnabled(true), - _resizeMode(NoResize) + : QFrame(parent), + _wrapMode(QTextOption::NoWrap), + _alignment(Qt::AlignVCenter|Qt::AlignLeft), + _toolTipEnabled(true), + _resizeMode(NoResize) { - setMouseTracking(true); + setMouseTracking(true); - QTextOption opt = _layout.textOption(); - opt.setWrapMode(_wrapMode); - opt.setAlignment(_alignment); - _layout.setTextOption(opt); + QTextOption opt = _layout.textOption(); + opt.setWrapMode(_wrapMode); + opt.setAlignment(_alignment); + _layout.setTextOption(opt); } -void StyledLabel::setCustomFont(const QFont &font) { - setFont(font); - _layout.setFont(font); - setText(_layout.text()); + +void StyledLabel::setCustomFont(const QFont &font) +{ + setFont(font); + _layout.setFont(font); + setText(_layout.text()); } -void StyledLabel::setWrapMode(QTextOption::WrapMode mode) { - if(_wrapMode == mode) - return; - _wrapMode = mode; - QTextOption opt = _layout.textOption(); - opt.setWrapMode(mode); - _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(); + layout(); } -void StyledLabel::setAlignment(Qt::Alignment alignment) { - if(_alignment == alignment) - return; - _alignment = alignment; - QTextOption opt = _layout.textOption(); - opt.setAlignment(alignment); - _layout.setTextOption(opt); +void StyledLabel::setAlignment(Qt::Alignment alignment) +{ + if (_alignment == alignment) + return; + + _alignment = alignment; + QTextOption opt = _layout.textOption(); + opt.setAlignment(alignment); + _layout.setTextOption(opt); - layout(); + layout(); } -void StyledLabel::setResizeMode(ResizeMode mode) { - if(_resizeMode == mode) - return; - _resizeMode = mode; - if(mode == DynamicResize) - setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); - else - setWrapMode(QTextOption::NoWrap); +void StyledLabel::setResizeMode(ResizeMode mode) +{ + if (_resizeMode == mode) + return; + + _resizeMode = mode; + if (mode == DynamicResize) + setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); + else + setWrapMode(QTextOption::NoWrap); } -void StyledLabel::resizeEvent(QResizeEvent *event) { - QFrame::resizeEvent(event); - layout(); -} +void StyledLabel::resizeEvent(QResizeEvent *event) +{ + QFrame::resizeEvent(event); -QSize StyledLabel::sizeHint() const { - return _sizeHint; + layout(); } -void StyledLabel::updateSizeHint() { - QSize sh; - int padding = frameWidth() * 2; - sh = _layout.boundingRect().size().toSize() + QSize(padding, padding); - if(_sizeHint != sh) { - _sizeHint = sh; - updateGeometry(); - } +QSize StyledLabel::sizeHint() const +{ + return _sizeHint; } -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); +void StyledLabel::updateSizeHint() +{ + QSize sh; + int padding = frameWidth() * 2; + sh = _layout.boundingRect().size().toSize() + QSize(padding, padding); + + if (_sizeHint != sh) { + _sizeHint = sh; + updateGeometry(); + } +} - // Use default font rather than the style's - QTextLayout::FormatRange fmtRange; - fmtRange.format.setFont(font()); - fmtRange.start = 0; - fmtRange.length = sstr.plainText.length(); - layoutList << fmtRange; - // Mark URLs - _clickables = ClickableList::fromString(sstr.plainText); - foreach(Clickable click, _clickables) { - if(click.type() == Clickable::Url) { - QTextLayout::FormatRange range; - range.start = click.start(); - range.length = click.length(); - range.format.setForeground(palette().link()); - layoutList << range; +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); + + // Use default font rather than the style's + QTextLayout::FormatRange fmtRange; + fmtRange.format.setFont(font()); + fmtRange.start = 0; + fmtRange.length = sstr.plainText.length(); + layoutList << fmtRange; + + // Mark URLs + _clickables = ClickableList::fromString(sstr.plainText); + foreach(Clickable click, _clickables) { + if (click.type() == Clickable::Url) { + QTextLayout::FormatRange range; + range.start = click.start(); + range.length = click.length(); + range.format.setForeground(palette().link()); + layoutList << range; + } } - } - _layout.setText(sstr.plainText); - _layout.setAdditionalFormats(layoutList); + _layout.setText(sstr.plainText); + _layout.setAdditionalFormats(layoutList); - layout(); + layout(); - endHoverMode(); + endHoverMode(); } -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::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 = contentsRect().width(); - _layout.beginLayout(); - forever { - QTextLine line = _layout.createLine(); - if(!line.isValid()) - break; - line.setLineWidth(w); - line.setPosition(QPointF(0, h)); - h += line.height(); - } - _layout.endLayout(); +void StyledLabel::layout() +{ + qreal h = 0; + qreal w = contentsRect().width(); + + _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(); + updateSizeHint(); + updateToolTip(); + update(); } -void StyledLabel::paintEvent(QPaintEvent *e) { - QFrame::paintEvent(e); - QPainter painter(this); - qreal y = contentsRect().y() + (contentsRect().height() - _layout.boundingRect().height()) / 2; - _layout.draw(&painter, QPointF(contentsRect().x(), y), _extraLayoutList); +void StyledLabel::paintEvent(QPaintEvent *e) +{ + QFrame::paintEvent(e); + QPainter painter(this); + + qreal y = contentsRect().y() + (contentsRect().height() - _layout.boundingRect().height()) / 2; + _layout.draw(&painter, QPointF(contentsRect().x(), y), _extraLayoutList); } -int StyledLabel::posToCursor(const QPointF &pos) { - if(pos.y() < 0 || pos.y() > height()) - return -1; - for(int l = _layout.lineCount() - 1; l >= 0; l--) { - QTextLine line = _layout.lineAt(l); - if(pos.y() >= line.y()) { - return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter); +int StyledLabel::posToCursor(const QPointF &pos) +{ + if (pos.y() < 0 || pos.y() > height()) + return -1; + + for (int l = _layout.lineCount() - 1; l >= 0; l--) { + QTextLine line = _layout.lineAt(l); + if (pos.y() >= line.y()) { + return line.xToCursor(pos.x(), QTextLine::CursorOnCharacter); + } } - } - return -1; + return -1; } -void StyledLabel::mouseMoveEvent(QMouseEvent *event) { - if(event->buttons() == Qt::NoButton) { - Clickable click = _clickables.atCursorPos(posToCursor(event->posF())); - if(click.isValid()) - setHoverMode(click.start(), click.length()); - else - endHoverMode(); - } -} -void StyledLabel::enterEvent(QEvent *) { - if(resizeMode() == ResizeOnHover) - setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); +void StyledLabel::mouseMoveEvent(QMouseEvent *event) +{ + if (event->buttons() == Qt::NoButton) { + Clickable click = _clickables.atCursorPos(posToCursor(event->posF())); + if (click.isValid()) + setHoverMode(click.start(), click.length()); + else + endHoverMode(); + } } -void StyledLabel::leaveEvent(QEvent *) { - endHoverMode(); - if(resizeMode() == ResizeOnHover) - setWrapMode(QTextOption::NoWrap); -} -void StyledLabel::mousePressEvent(QMouseEvent *event) { - if(event->button() == Qt::LeftButton) { - Clickable click = _clickables.atCursorPos(posToCursor(event->posF())); - if(click.isValid()) - emit clickableActivated(click); - } +void StyledLabel::enterEvent(QEvent *) +{ + if (resizeMode() == ResizeOnHover) + setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); } -void StyledLabel::setHoverMode(int start, int length) { - if(_extraLayoutList.count() >= 1 && _extraLayoutList.first().start == start && _extraLayoutList.first().length == length) - return; - QTextLayout::FormatRange range; - range.start = start; - range.length = length; - range.format.setFontUnderline(true); - _extraLayoutList.clear(); - _extraLayoutList << range; +void StyledLabel::leaveEvent(QEvent *) +{ + endHoverMode(); + if (resizeMode() == ResizeOnHover) + setWrapMode(QTextOption::NoWrap); +} + - setCursor(Qt::PointingHandCursor); - update(); +void StyledLabel::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + Clickable click = _clickables.atCursorPos(posToCursor(event->posF())); + if (click.isValid()) + emit clickableActivated(click); + } } -void StyledLabel::endHoverMode() { - _extraLayoutList.clear(); - setCursor(Qt::ArrowCursor); - update(); + +void StyledLabel::setHoverMode(int start, int length) +{ + if (_extraLayoutList.count() >= 1 && _extraLayoutList.first().start == start && _extraLayoutList.first().length == length) + return; + + QTextLayout::FormatRange range; + range.start = start; + range.length = length; + range.format.setFontUnderline(true); + _extraLayoutList.clear(); + _extraLayoutList << range; + + setCursor(Qt::PointingHandCursor); + update(); } + +void StyledLabel::endHoverMode() +{ + _extraLayoutList.clear(); + setCursor(Qt::ArrowCursor); + update(); +}