/***************************************************************************
- * Copyright (C) 2005-07 by The Quassel IRC Development Team *
+ * Copyright (C) 2005-2015 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) any later version. *
+ * (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 *
* 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. *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
+#include <QDateTime>
+#include <QGraphicsSceneMouseEvent>
+#include <QString>
+#include <QtGui>
+
+#include "bufferinfo.h"
+#include "buffersyncer.h"
+#include "client.h"
+#include "chatitem.h"
#include "chatline.h"
+#include "chatview.h"
+#include "columnhandleitem.h"
+#include "messagemodel.h"
+#include "networkmodel.h"
#include "qtui.h"
+#include "qtuisettings.h"
+#include "qtuistyle.h"
+
+ChatLine::ChatLine(int row, QAbstractItemModel *model,
+ const qreal &width,
+ const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
+ const QPointF &senderPos, const QPointF &contentsPos,
+ QGraphicsItem *parent)
+ : QGraphicsItem(parent),
+ _row(row), // needs to be set before the items
+ _model(model),
+ _contentsItem(contentsPos, contentsWidth, this),
+ _senderItem(QRectF(senderPos, QSizeF(senderWidth, _contentsItem.height())), this),
+ _timestampItem(QRectF(0, 0, timestampWidth, _contentsItem.height()), this),
+ _width(width),
+ _height(_contentsItem.height()),
+ _selection(0),
+ _mouseGrabberItem(0),
+ _hoverItem(0)
+{
+ Q_ASSERT(model);
+ QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
+ setZValue(0);
+ setAcceptHoverEvents(true);
+ setHighlighted(index.data(MessageModel::FlagsRole).toInt() & Message::Highlight);
+}
+
+
+ChatLine::~ChatLine()
+{
+ if (chatView())
+ chatView()->setHasCache(this, false);
+}
+
-//!\brief Construct a ChatLine object from a message.
-/**
- * \param m The message to be layouted and rendered
- * \param net The network name
- * \param buf The buffer name
- */
-ChatLine::ChatLine(Message m) {
- hght = 0;
- //networkName = m.buffer.network();
- //bufferName = m.buffer.buffer();
- msg = m;
- selectionMode = None;
- formatMsg(msg);
+ChatItem *ChatLine::item(ChatLineModel::ColumnType column)
+{
+ switch (column) {
+ case ChatLineModel::TimestampColumn:
+ return &_timestampItem;
+ case ChatLineModel::SenderColumn:
+ return &_senderItem;
+ case ChatLineModel::ContentsColumn:
+ return &_contentsItem;
+ default:
+ return 0;
+ }
}
-ChatLine::~ChatLine() {
+ChatItem *ChatLine::itemAt(const QPointF &pos)
+{
+ if (_contentsItem.boundingRect().contains(pos))
+ return &_contentsItem;
+ if (_senderItem.boundingRect().contains(pos))
+ return &_senderItem;
+ if (_timestampItem.boundingRect().contains(pos))
+ return &_timestampItem;
+ return 0;
}
-void ChatLine::formatMsg(Message msg) {
- QTextOption tsOption, senderOption, textOption;
- styledTimeStamp = QtUi::style()->styleString(msg.formattedTimeStamp());
- styledSender = QtUi::style()->styleString(msg.formattedSender());
- styledText = QtUi::style()->styleString(msg.formattedText());
- precomputeLine();
+
+void ChatLine::clearCache()
+{
+ _timestampItem.clearCache();
+ _senderItem.clearCache();
+ _contentsItem.clearCache();
}
-// This function is almost obsolete, since with the new style engine, we already get a list of formats...
-// We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice.
-// Also, the additional format is ignored for now, which means that you won't see a selection...
-// FIXME TODO
-QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledString &fs, QTextLayout::FormatRange additional) {
- QList<FormatRange> ranges;
-
- foreach(QTextLayout::FormatRange f, fs.formats) {
- FormatRange range;
- range.start = f.start;
- range.length = f.length;
- range.format = f.format;
- QFontMetrics metrics(range.format.font());
- range.height = metrics.lineSpacing();
- ranges.append(range);
- }
- /*
- QList<QTextLayout::FormatRange> formats = fs.formats;
- formats.append(additional);
- int cur = -1;
- FormatRange range, lastrange;
- for(int i = 0; i < fs.text.length(); i++) {
- QTextCharFormat format;
- foreach(QTextLayout::FormatRange f, formats) {
- if(i >= f.start && i < f.start + f.length) format.merge(f.format);
+
+void ChatLine::setMouseGrabberItem(ChatItem *item)
+{
+ _mouseGrabberItem = item;
+}
+
+
+bool ChatLine::sceneEvent(QEvent *event)
+{
+ if (event->type() == QEvent::GrabMouse) {
+ // get mouse cursor pos relative to us
+ ChatView *view = chatScene()->chatView();
+ QPointF linePos = mapFromScene(view->mapToScene(view->mapFromGlobal(QCursor::pos())));
+ setMouseGrabberItem(itemAt(linePos));
}
- if(cur < 0) {
- range.start = 0; range.length = 1; range.format= format;
- cur = 0;
- } else {
- if(format == range.format) range.length++;
- else {
- QFontMetrics metrics(range.format.font());
- range.height = metrics.lineSpacing();
- ranges.append(range);
- range.start = i; range.length = 1; range.format = format;
- cur++;
- }
+ else if (event->type() == QEvent::UngrabMouse) {
+ setMouseGrabberItem(0);
}
- }
- if(cur >= 0) {
- QFontMetrics metrics(range.format.font());
- range.height = metrics.lineSpacing();
- ranges.append(range);
- }
- */
- return ranges;
+ return QGraphicsItem::sceneEvent(event);
}
-void ChatLine::setSelection(SelectionMode mode, int start, int end) {
- selectionMode = mode;
- //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
- QPalette pal = QApplication::palette();
- QTextLayout::FormatRange tsSel, senderSel, textSel;
- switch (mode) {
- case None:
- tsFormat = calcFormatRanges(styledTimeStamp);
- senderFormat = calcFormatRanges(styledSender);
- textFormat = calcFormatRanges(styledText);
- break;
- case Partial:
- selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
- textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
- textSel.format.setBackground(pal.brush(QPalette::Highlight));
- textSel.start = selectionStart;
- textSel.length = selectionEnd - selectionStart;
- //textFormat.append(textSel);
- textFormat = calcFormatRanges(styledText, textSel);
- foreach(FormatRange fr, textFormat);
- break;
- case Full:
- tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
- tsSel.format.setBackground(pal.brush(QPalette::Highlight));
- tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
- tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
- senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
- senderSel.format.setBackground(pal.brush(QPalette::Highlight));
- senderSel.start = 0; senderSel.length = styledSender.text.length();
- senderFormat = calcFormatRanges(styledSender, senderSel);
- textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
- textSel.format.setBackground(pal.brush(QPalette::Highlight));
- textSel.start = 0; textSel.length = styledText.text.length();
- textFormat = calcFormatRanges(styledText, textSel);
- break;
- }
+
+void ChatLine::setFirstColumn(const qreal ×tampWidth, const qreal &senderWidth, const QPointF &senderPos)
+{
+ _timestampItem.setGeometry(timestampWidth, _height);
+ _senderItem.setGeometry(senderWidth, _height);
+ _senderItem.setPos(senderPos);
}
-uint ChatLine::msgId() const {
- return msg.buffer().uid();
+
+void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth, const QPointF &contentsPos, qreal &linePos)
+{
+ // linepos is the *bottom* position for the line
+ qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
+ linePos -= height;
+ bool needGeometryChange = (height != _height);
+
+ _timestampItem.setHeight(height);
+ _senderItem.setGeometry(senderWidth, height);
+ _contentsItem.setPos(contentsPos);
+
+ if (needGeometryChange)
+ prepareGeometryChange();
+
+ _height = height;
+
+ setPos(0, linePos);
}
-BufferInfo ChatLine::bufferInfo() const {
- return msg.buffer();
+
+void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos)
+{
+ // linepos is the *bottom* position for the line
+ qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
+ linePos -= height;
+ bool needGeometryChange = (height != _height || width != _width);
+
+ if (height != _height) {
+ _timestampItem.setHeight(height);
+ _senderItem.setHeight(height);
+ }
+
+ if (needGeometryChange) {
+ prepareGeometryChange();
+ _height = height;
+ _width = width;
+ }
+
+ setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
}
-QDateTime ChatLine::timeStamp() const {
- return msg.timeStamp();
+
+void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn)
+{
+ if (selected) {
+ quint8 sel = (_selection & Highlighted) | Selected | minColumn;
+ if (sel != _selection) {
+ _selection = sel;
+ for (int i = 0; i < minColumn; i++)
+ item((ChatLineModel::ColumnType)i)->clearSelection();
+ for (int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
+ item((ChatLineModel::ColumnType)i)->setFullSelection();
+ update();
+ }
+ }
+ else {
+ quint8 sel = _selection & Highlighted;
+ if (sel != _selection) {
+ _selection = sel;
+ for (int i = 0; i <= ChatLineModel::ContentsColumn; i++)
+ item((ChatLineModel::ColumnType)i)->clearSelection();
+ update();
+ }
+ }
}
-QString ChatLine::sender() const {
- return styledSender.text;
+
+void ChatLine::setHighlighted(bool highlighted)
+{
+ if (highlighted) _selection |= Highlighted;
+ else _selection &= ~Highlighted;
+ update();
+}
+
+
+void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+ Q_UNUSED(option);
+ Q_UNUSED(widget);
+
+ const QAbstractItemModel *model_ = model();
+ QModelIndex myIdx = model_->index(row(), 0);
+ Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
+ UiStyle::MessageLabel label = (UiStyle::MessageLabel)myIdx.data(ChatLineModel::MsgLabelRole).toInt();
+
+ QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label);
+ if (msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
+ painter->fillRect(boundingRect(), msgFmt.background());
+ }
+
+ if (_selection & Selected) {
+ QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::Selected);
+ if (selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
+ qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask))->pos().x();
+ QRectF selectRect(left, 0, width() - left, height());
+ painter->fillRect(selectRect, selFmt.background());
+ }
+ }
+
+ // draw chatitems
+ // the items draw themselves at the correct position
+ timestampItem()->paint(painter, option, widget);
+ senderItem()->paint(painter, option, widget);
+ contentsItem()->paint(painter, option, widget);
}
-QString ChatLine::text() const {
- return styledText.text;
+
+// We need to dispatch all mouse-related events to the appropriate (mouse grabbing) ChatItem
+
+ChatItem *ChatLine::mouseEventTargetItem(const QPointF &pos)
+{
+ if (mouseGrabberItem())
+ return mouseGrabberItem();
+ return itemAt(pos);
}
-bool ChatLine::isUrl(int c) const {
- if(c < 0 || c >= charUrlIdx.count()) return false;;
- return charUrlIdx[c] >= 0;
+
+void ChatLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+ ChatItem *item = mouseEventTargetItem(event->pos());
+ if (item)
+ item->mouseMoveEvent(event);
}
-QUrl ChatLine::getUrl(int c) const {
- if(c < 0 || c >= charUrlIdx.count()) return QUrl();
- int i = charUrlIdx[c];
- if(i >= 0) return styledText.urls[i].url;
- else return QUrl();
+
+void ChatLine::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+ ChatItem *item = mouseEventTargetItem(event->pos());
+ if (item)
+ item->mousePressEvent(event);
}
-//!\brief Return the cursor position for the given coordinate pos.
-/**
- * \param pos The position relative to the ChatLine
- * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
- */
-int ChatLine::posToCursor(QPointF pos) {
- if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
- qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
- if(pos.x() < textStart) return -1;
- int x = (int)(pos.x() - textStart);
- for(int l = lineLayouts.count() - 1; l >=0; l--) {
- LineLayout line = lineLayouts[l];
- if(pos.y() >= line.y) {
- int offset = charPos[line.start]; x += offset;
- for(int i = line.start + line.length - 1; i >= line.start; i--) {
- if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
- }
- return line.start;
- }
- }
- return 0;
+
+void ChatLine::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
+{
+ ChatItem *item = mouseEventTargetItem(event->pos());
+ if (item)
+ item->mouseReleaseEvent(event);
}
-void ChatLine::precomputeLine() {
- tsFormat = calcFormatRanges(styledTimeStamp);
- senderFormat = calcFormatRanges(styledSender);
- textFormat = calcFormatRanges(styledText);
-
- minHeight = 0;
- foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
- foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
-
- words.clear();
- charPos.resize(styledText.text.length() + 1);
- charHeights.resize(styledText.text.length());
- charUrlIdx.fill(-1, styledText.text.length());
- for(int i = 0; i < styledText.urls.count(); i++) {
- QtUiStyle::UrlInfo url = styledText.urls[i];
- for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
- }
- if(!textFormat.count()) return;
- int idx = 0; int cnt = 0; int w = 0;
- QFontMetrics metrics(textFormat[0].format.font());
- Word wr;
- wr.start = -1; wr.trailing = -1;
- for(int i = 0; i < styledText.text.length(); ) {
- charPos[i] = w; charHeights[i] = textFormat[idx].height;
- w += metrics.charWidth(styledText.text, i);
- if(!styledText.text[i].isSpace()) {
- if(wr.trailing >= 0) {
- // new word after space
- words.append(wr);
- wr.start = -1;
- }
- if(wr.start < 0) {
- wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
- } else {
- wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
- }
- } else {
- if(wr.start < 0) {
- wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
- } else {
- wr.trailing++;
- }
- }
- if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
- cnt = 0; idx++;
- Q_ASSERT(idx < textFormat.count());
- metrics = QFontMetrics(textFormat[idx].format.font());
+
+void ChatLine::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
+{
+ ChatItem *item = mouseEventTargetItem(event->pos());
+ if (item && !_hoverItem) {
+ _hoverItem = item;
+ item->hoverEnterEvent(event);
}
- }
- charPos[styledText.text.length()] = w;
- if(wr.start >= 0) words.append(wr);
}
-qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
- tsWidth = tsw; senderWidth = senderw; textWidth = textw;
- if(textw <= 0) return minHeight;
- lineLayouts.clear(); LineLayout line;
- int h = 0;
- int offset = 0; int numWords = 0;
- line.y = 0;
- line.start = 0;
- line.height = minHeight; // first line needs room for ts and sender
- for(uint i = 0; i < (uint)words.count(); i++) {
- int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
- if(lastpos - offset <= textw) {
- line.height = qMax(line.height, words[i].height);
- line.length = words[i].start + words[i].length - line.start;
- numWords++;
- } else {
- // we need to wrap!
- if(numWords > 0) {
- // ok, we had some words before, so store the layout and start a new line
- h += line.height;
- line.length = words[i-1].start + words[i-1].length - line.start;
- lineLayouts.append(line);
- line.y += line.height;
- line.start = words[i].start;
- line.height = words[i].height;
- offset = charPos[words[i].start];
- }
- numWords = 1;
- // check if the word fits into the current line
- if(lastpos - offset <= textw) {
- line.length = words[i].length;
- } else {
- // we need to break a word in the middle
- int border = (int)textw + offset; // save some additions
- line.start = words[i].start;
- line.length = 1;
- line.height = charHeights[line.start];
- int j = line.start + 1;
- for(int l = 1; l < words[i].length; j++, l++) {
- if(charPos[j+1] < border) {
- line.length++;
- line.height = qMax(line.height, charHeights[j]);
- continue;
- } else {
- h += line.height;
- lineLayouts.append(line);
- line.y += line.height;
- line.start = j;
- line.height = charHeights[j];
- line.length = 1;
- offset = charPos[j];
- border = (int)textw + offset;
- }
- }
- }
+
+void ChatLine::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
+{
+ if (_hoverItem) {
+ _hoverItem->hoverLeaveEvent(event);
+ _hoverItem = 0;
}
- }
- h += line.height;
- if(numWords > 0) {
- lineLayouts.append(line);
- }
- hght = h;
- return hght;
}
-//!\brief Draw ChatLine on the given QPainter at the given position.
-void ChatLine::draw(QPainter *p, const QPointF &pos) {
- QPalette pal = QApplication::palette();
-
- if(selectionMode == Full) {
- p->setPen(Qt::NoPen);
- p->setBrush(pal.brush(QPalette::Highlight));
- p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
- } else if(selectionMode == Partial) {
-
- } /*
- p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
- tsLayout.draw(p, pos, tsFormat);
- p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
- senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
- p->setClipping(false);
- textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
- */
- //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
- //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
- p->setBackgroundMode(Qt::OpaqueMode);
- QPointF tp = pos;
- QRectF rect(pos, QSizeF(tsWidth, minHeight));
- QRectF brect;
- foreach(FormatRange fr, tsFormat) {
- p->setFont(fr.format.font());
- p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
- p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
- rect.setLeft(brect.right());
- }
- rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
- for(int i = senderFormat.count() - 1; i >= 0; i--) {
- FormatRange fr = senderFormat[i];
- p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
- p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
- rect.setRight(brect.left());
- }
- QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
- qreal h = 0; int l = 0;
- rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
- int offset = 0;
- foreach(FormatRange fr, textFormat) {
- if(l >= lineLayouts.count()) break;
- p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
- int start, end, frend, llend;
- do {
- frend = fr.start + fr.length;
- if(frend <= lineLayouts[l].start) break;
- llend = lineLayouts[l].start + lineLayouts[l].length;
- start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
- rect.setLeft(tpos.x() + charPos[start] - offset);
- p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
- if(llend <= end) {
- h += lineLayouts[l].height;
- l++;
- if(l < lineLayouts.count()) {
- rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
- offset = charPos[lineLayouts[l].start];
- }
- }
- } while(end < frend && l < lineLayouts.count());
- }
+
+void ChatLine::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
+{
+ ChatItem *item = mouseEventTargetItem(event->pos());
+ if (item)
+ item->hoverMoveEvent(event);
}