- return _contentsItem;
- default:
- return *(ChatItem *)0; // provoke an error
- }
-}
-
-qreal ChatLine::setGeometry(qreal width) {
- if(width != _width)
- prepareGeometryChange();
- QRectF firstColHandleRect = chatScene()->firstColumnHandleRect();
- QRectF secondColHandleRect = chatScene()->secondColumnHandleRect();
-
- _height = _contentsItem.setGeometry(width - secondColHandleRect.right());
- _timestampItem.setGeometry(firstColHandleRect.left(), _height);
- _senderItem.setGeometry(secondColHandleRect.left() - firstColHandleRect.right(), _height);
-
- _senderItem.setPos(firstColHandleRect.right(), 0);
- _contentsItem.setPos(secondColHandleRect.right(), 0);
-
- _width = width;
- return _height;
-}
-
-void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
- if(selected) {
- quint8 sel = (_selection & 0x80) | 0x40 | 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();
+ return &_contentsItem;
+ default:
+ return nullptr;
+ }
+}
+
+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 nullptr;
+}
+
+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(nullptr);
+ }
+ return QGraphicsItem::sceneEvent(event);
+}
+
+void ChatLine::setFirstColumn(const qreal& timestampWidth, 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);
+}
+
+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.
+}
+
+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();
+ }