+#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);
+}
+
+
+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;
+ }
+}
+
+
+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::clearCache()
+{
+ _timestampItem.clearCache();
+ _senderItem.clearCache();
+ _contentsItem.clearCache();
+}
+
+
+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));
+ }
+ else if (event->type() == QEvent::UngrabMouse) {
+ setMouseGrabberItem(0);
+ }
+ return QGraphicsItem::sceneEvent(event);
+}
+
+
+void ChatLine::setFirstColumn(const qreal ×tampWidth, const qreal &senderWidth, const QPointF &senderPos)
+{
+ _timestampItem.setGeometry(timestampWidth, _height);
+ _senderItem.setGeometry(senderWidth, _height);
+ _senderItem.setPos(senderPos);
+}
+
+
+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);
+}
+