X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fchatitem.cpp;h=ac2558ecddc7444eff7dd46c4e4ee319cb564273;hp=28b496ea75314eef5df86113d02f01b39484e069;hb=429f2aad1e22ba8410f4ea63471fcfc9887c55aa;hpb=ba934ceb1bfe30d01d5fb5c072c3197f8080be04 diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index 28b496ea..ac2558ec 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -32,22 +32,20 @@ #include "qtui.h" #include "qtuistyle.h" -ChatItem::ChatItem(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGraphicsItem *parent) +ChatItem::ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : QGraphicsItem(parent), - _fontMetrics(0), + _data(0), + _boundingRect(0, 0, width, height), _selectionMode(NoSelection), - _selectionStart(-1), - _layout(0) + _selectionStart(-1) { - Q_ASSERT(model); - QModelIndex index = model->index(row(), col); - _fontMetrics = QtUi::style()->fontMetrics(model->data(index, ChatLineModel::FormatRole).value().at(0).second); setAcceptHoverEvents(true); setZValue(20); + setPos(pos); } ChatItem::~ChatItem() { - delete _layout; + delete _data; } QVariant ChatItem::data(int role) const { @@ -59,20 +57,6 @@ QVariant ChatItem::data(int role) const { return model()->data(index, role); } -qreal ChatItem::setGeometry(qreal w, qreal h) { - if(w == _boundingRect.width()) return _boundingRect.height(); - prepareGeometryChange(); - _boundingRect.setWidth(w); - if(h < 0) h = computeHeight(); - _boundingRect.setHeight(h); - if(haveLayout()) updateLayout(); - return h; -} - -qreal ChatItem::computeHeight() { - return fontMetrics()->lineSpacing(); // only contents can be multi-line -} - QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignment alignment) { QTextLayout *layout = new QTextLayout(data(MessageModel::DisplayRole).toString()); @@ -88,28 +72,30 @@ QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignmen } void ChatItem::updateLayout() { - if(!haveLayout()) - setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft)); - - layout()->beginLayout(); - QTextLine line = layout()->createLine(); + if(!privateData()) { + setPrivateData(new ChatItemPrivate(createLayout())); + } + QTextLayout *layout_ = layout(); + layout_->beginLayout(); + QTextLine line = layout_->createLine(); if(line.isValid()) { line.setLineWidth(width()); line.setPosition(QPointF(0,0)); } - layout()->endLayout(); + layout_->endLayout(); } void ChatItem::clearLayout() { - delete _layout; - _layout = 0; + delete _data; + _data = 0; } // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data // This is a deliberate trade-off. (-> selectFmt creation, data() call) void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); - if(!haveLayout()) updateLayout(); + if(!hasLayout()) + updateLayout(); painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work //if(_selectionMode == FullSelection) { //painter->save(); @@ -131,12 +117,29 @@ void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, formats.append(selectFmt); } layout()->draw(painter, QPointF(0,0), formats, boundingRect()); + + // Debuging Stuff + // uncomment the following lines to draw the bounding rect and the row number in alternating colors +// if(row() % 2) +// painter->setPen(Qt::red); +// else +// painter->setPen(Qt::blue); +// QString rowString = QString::number(row()); +// QRect rowRect = painter->fontMetrics().boundingRect(rowString); +// QPointF topPoint = _boundingRect.topLeft(); +// topPoint.ry() += rowRect.height(); +// painter->drawText(topPoint, rowString); +// QPointF bottomPoint = _boundingRect.bottomRight(); +// bottomPoint.rx() -= rowRect.width(); +// painter->drawText(bottomPoint, rowString); +// painter->drawRect(_boundingRect.adjusted(0, 0, -1, -1)); } qint16 ChatItem::posToCursor(const QPointF &pos) { if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length(); if(pos.y() < 0) return 0; - if(!haveLayout()) updateLayout(); + if(!hasLayout()) + updateLayout(); for(int l = layout()->lineCount() - 1; l >= 0; l--) { QTextLine line = layout()->lineAt(l); if(pos.y() >= line.y()) { @@ -178,17 +181,21 @@ QList ChatItem::findWords(const QString &searchWord, Qt::CaseSensitivity searchIdx = plainText.indexOf(searchWord, searchIdx + 1, caseSensitive); } - if(!haveLayout()) + bool hadLayout = hasLayout(); + if(!hadLayout) updateLayout(); foreach(int idx, indexList) { QTextLine line = layout()->lineForTextPosition(idx); qreal x = line.cursorToX(idx); qreal width = line.cursorToX(idx + searchWord.count()) - x; - qreal height = fontMetrics()->lineSpacing(); + qreal height = line.height(); qreal y = height * line.lineNumber(); resultList << QRectF(x, y, width, height); } + + if(!hadLayout) + clearLayout(); return resultList; } @@ -235,51 +242,43 @@ void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { } } -/*************************************************************************************************/ - -/*************************************************************************************************/ - -void SenderChatItem::updateLayout() { - if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignRight)); - ChatItem::updateLayout(); -} - -/*************************************************************************************************/ +// ************************************************************ +// SenderChatItem +// ************************************************************ -ContentsChatItem::ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent), - _layoutData(0) +// ************************************************************ +// ContentsChatItem +// ************************************************************ +ContentsChatItem::ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent) + : ChatItem(0, 0, pos, parent) { + const QAbstractItemModel *model_ = model(); + QModelIndex index = model_->index(row(), column()); + _fontMetrics = QtUi::style()->fontMetrics(model_->data(index, ChatLineModel::FormatRole).value().at(0).second); + setGeometryByWidth(width); } -ContentsChatItem::~ContentsChatItem() { - delete _layoutData; -} - -qreal ContentsChatItem::computeHeight() { - int lines = 1; - WrapColumnFinder finder(this); - while(finder.nextWrapColumn() > 0) lines++; - return lines * fontMetrics()->lineSpacing(); -} - -void ContentsChatItem::setLayout(QTextLayout *layout) { - if(!_layoutData) { - _layoutData = new LayoutData; - _layoutData->clickables = findClickables(); - } else { - delete _layoutData->layout; +qreal ContentsChatItem::setGeometryByWidth(qreal w) { + if(w != width()) { + setWidth(w); + // compute height + int lines = 1; + WrapColumnFinder finder(this); + while(finder.nextWrapColumn() > 0) + lines++; + setHeight(lines * fontMetrics()->lineSpacing()); } - _layoutData->layout = layout; -} - -void ContentsChatItem::clearLayout() { - delete _layoutData; - _layoutData = 0; + return height(); } void ContentsChatItem::updateLayout() { - if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere)); + if(!privateData()) { + ContentsChatItemPrivate *data = new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), + findClickables()); + // data->clickables = findClickables(); + setPrivateData(data); + } // Now layout ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value(); @@ -367,8 +366,8 @@ QList ContentsChatItem::findClickables() { QVector ContentsChatItem::additionalFormats() const { // mark a clickable if hovered upon QVector fmt; - if(layoutData()->currentClickable.isValid()) { - Clickable click = layoutData()->currentClickable; + if(privateData()->currentClickable.isValid()) { + Clickable click = privateData()->currentClickable; QTextLayout::FormatRange f; f.start = click.start; f.length = click.length; @@ -379,22 +378,22 @@ QVector ContentsChatItem::additionalFormats() const { } void ContentsChatItem::endHoverMode() { - if(layoutData()->currentClickable.isValid()) { + if(privateData()->currentClickable.isValid()) { setCursor(Qt::ArrowCursor); - layoutData()->currentClickable = Clickable(); + privateData()->currentClickable = Clickable(); update(); } } void ContentsChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - layoutData()->hasDragged = false; + privateData()->hasDragged = false; ChatItem::mousePressEvent(event); } void ContentsChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if(!event->buttons() && !layoutData()->hasDragged) { + if(!event->buttons() && !privateData()->hasDragged) { // got a click - Clickable click = layoutData()->currentClickable; + Clickable click = privateData()->currentClickable; if(click.isValid()) { QString str = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length); switch(click.type) { @@ -416,9 +415,9 @@ void ContentsChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { // mouse move events always mean we're not hovering anymore... endHoverMode(); // also, check if we have dragged the mouse - if(!layoutData()->hasDragged && event->buttons() & Qt::LeftButton + if(!privateData()->hasDragged && event->buttons() & Qt::LeftButton && (event->buttonDownScreenPos(Qt::LeftButton) - event->screenPos()).manhattanLength() >= QApplication::startDragDistance()) - layoutData()->hasDragged = true; + privateData()->hasDragged = true; ChatItem::mouseMoveEvent(event); } @@ -430,8 +429,8 @@ void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { bool onClickable = false; qint16 idx = posToCursor(event->pos()); - for(int i = 0; i < layoutData()->clickables.count(); i++) { - Clickable click = layoutData()->clickables.at(i); + for(int i = 0; i < privateData()->clickables.count(); i++) { + Clickable click = privateData()->clickables.at(i); if(idx >= click.start && idx < click.start + click.length) { if(click.type == Clickable::Url) onClickable = true; @@ -441,7 +440,7 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { } if(onClickable) { setCursor(Qt::PointingHandCursor); - layoutData()->currentClickable = click; + privateData()->currentClickable = click; update(); break; } @@ -458,9 +457,11 @@ ContentsChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) layout(0), wrapList(item->data(ChatLineModel::WrapListRole).value()), wordidx(0), + lineCount(0), + choppedTrailing(0), lastwrapcol(0), lastwrappos(0), - w(0) + width(0) { } @@ -469,33 +470,35 @@ ContentsChatItem::WrapColumnFinder::~WrapColumnFinder() { } qint16 ContentsChatItem::WrapColumnFinder::nextWrapColumn() { - while(wordidx < wrapList.count()) { - w += wrapList.at(wordidx).width; - if(w >= item->width()) { - if(lastwrapcol >= wrapList.at(wordidx).start) { - // first word, and it doesn't fit - if(!line.isValid()) { - layout = item->createLayout(QTextOption::NoWrap); - layout->beginLayout(); - line = layout->createLine(); - line.setLineWidth(item->width()); - layout->endLayout(); - } - int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter); - qreal x = line.cursorToX(idx, QTextLine::Trailing); - w = w - wrapList.at(wordidx).width - (x - lastwrappos); - lastwrappos = x; - lastwrapcol = idx; - return idx; + if(wordidx >= wrapList.count()) + return -1; + + lineCount++; + qreal targetWidth = lineCount * item->width() + choppedTrailing; + + qint16 start = wordidx; + qint16 end = wrapList.count() - 1; + + // check if the whole line fits + if(wrapList.at(end).endX <= targetWidth || start == end) + return -1; + + while(true) { + if(start == end) { + wordidx = start; + if(wordidx > 0) { + const ChatLineModel::Word &prevWord = wrapList.at(wordidx - 1); + choppedTrailing += prevWord.trailing - (targetWidth - prevWord.endX); } - // not the first word, so just wrap before this - lastwrapcol = wrapList.at(wordidx).start; - lastwrappos = lastwrappos + w - wrapList.at(wordidx).width; - w = 0; - return lastwrapcol; + return wrapList.at(wordidx).start; + } + qint16 pivot = (end + start) / 2; + if(wrapList.at(pivot).endX > targetWidth && wordidx != pivot) { + end = pivot; + } else { + start = pivot + 1; } - w += wrapList.at(wordidx).trailing; - wordidx++; } return -1; } +