-/*
-void ChatItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) {
- qDebug() << (void*)this << "moving" << event->pos();
- if(event->pos().y() < 0) {
- QTextCursor cursor(document());
- //cursor.insertText("foo");
- //cursor.select(QTextCursor::Document);
+int ChatItem::posToCursor(const QPointF &pos) {
+ if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
+ if(pos.y() < 0) return 0;
+ if(!haveLayout()) updateLayout();
+ 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 0;
+}
+
+void ChatItem::clearSelection() {
+ if(_selectionStart >= 0) {
+ QList<QTextLayout::FormatRange> formats = _layout->additionalFormats();
+ formats.removeLast();
+ _layout->setAdditionalFormats(formats);
+ _selectionStart = -1;
+ updateLayout();
+ update();
+ }
+}
+
+void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+ int selectionEnd = posToCursor(event->pos());
+ QList<QTextLayout::FormatRange> formats = _layout->additionalFormats();
+ formats.last().start = qMin(_selectionStart, selectionEnd);
+ formats.last().length = qMax(_selectionStart, selectionEnd) - formats.last().start;
+ _layout->setAdditionalFormats(formats);
+ updateLayout();
+ update();
+}
+
+void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+ if(event->buttons() & Qt::LeftButton) {
+ if(!haveLayout()) updateLayout();
+ chatScene()->setSelectingItem(this);
+ _selectionStart = posToCursor(event->pos());
+ QList<QTextLayout::FormatRange> formats = _layout->additionalFormats();
+ QTextLayout::FormatRange selectFmt;
+ QPalette pal = QApplication::palette();
+ selectFmt.format.setForeground(pal.brush(QPalette::HighlightedText));
+ selectFmt.format.setBackground(pal.brush(QPalette::Highlight));
+ selectFmt.length = 0;
+ formats.append(selectFmt);
+ _layout->setAdditionalFormats(formats);
+ updateLayout();
+ update();
+ event->accept();
+ } else {