X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fchatitem.cpp;h=14fd3addb049f37bcca246496f950a9dc78f2291;hp=5d1bcb7a6063b93dfc41110bc93e3e408b48c1ea;hb=f5f53f6c963f376b3003582f22314d744d157229;hpb=ae374ec02b44f508d207d2e69d800f26b34e0d9d diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index 5d1bcb7a..14fd3add 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -30,12 +31,20 @@ #include "chatlinemodel.h" #include "qtui.h" -ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) { - _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value().at(0).second); - _layout = 0; - _lines = 0; - _selectionStart = -1; - _selectionMode = NoSelection; +ChatItem::ChatItem(int col, QAbstractItemModel *model, QGraphicsItem *parent) + : QGraphicsItem(parent), + _fontMetrics(0), + _col(col), + _lines(0), + _layout(0), + _selectionMode(NoSelection), + _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); } ChatItem::~ChatItem() { @@ -43,11 +52,12 @@ ChatItem::~ChatItem() { } QVariant ChatItem::data(int role) const { - if(!_index.isValid()) { - qWarning() << "ChatItem::data(): Model index is invalid!" << _index; + QModelIndex index = model()->index(row(), column()); + if(!index.isValid()) { + qWarning() << "ChatItem::data(): model index is invalid!" << index; return QVariant(); } - return _index.data(role); + return model()->data(index, role); } qreal ChatItem::setWidth(qreal w) { @@ -138,11 +148,11 @@ void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q_UNUSED(option); Q_UNUSED(widget); if(!haveLayout()) updateLayout(); painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work - if(_selectionMode == FullSelection) { - painter->save(); - painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight)); - painter->restore(); - } // TODO: add selection format here + //if(_selectionMode == FullSelection) { + //painter->save(); + //painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight)); + //painter->restore(); + //} QVector formats; if(_selectionMode != NoSelection) { QTextLayout::FormatRange selectFmt; @@ -174,20 +184,36 @@ qint16 ChatItem::posToCursor(const QPointF &pos) { } void ChatItem::setFullSelection() { - _selectionMode = FullSelection; - update(); + if(_selectionMode != FullSelection) { + _selectionMode = FullSelection; + update(); + } } void ChatItem::clearSelection() { - _selectionMode = NoSelection; + if(_selectionMode != NoSelection) { + _selectionMode = NoSelection; + update(); + } +} + +void ChatItem::continueSelecting(const QPointF &pos) { + _selectionMode = PartialSelection; + _selectionEnd = posToCursor(pos); update(); } void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if(event->buttons() & Qt::LeftButton) { - chatScene()->setSelectingItem(this); // removes earlier selection if exists - _selectionStart = _selectionEnd = posToCursor(event->pos()); - _selectionMode = PartialSelection; + if(_selectionMode == NoSelection) { + chatScene()->setSelectingItem(this); // removes earlier selection if exists + _selectionStart = _selectionEnd = posToCursor(event->pos()); + _selectionMode = PartialSelection; + } else { + chatScene()->setSelectingItem(0); + _selectionMode = NoSelection; + update(); + } event->accept(); } else { event->ignore(); @@ -196,12 +222,14 @@ void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if(contains(event->pos())) { - _selectionEnd = posToCursor(event->pos()); - update(); + qint16 end = posToCursor(event->pos()); + if(end != _selectionEnd) { + _selectionEnd = end; + update(); + } } else { setFullSelection(); - ungrabMouse(); - chatScene()->startGlobalSelection(this); + chatScene()->startGlobalSelection(this, event->pos()); } } @@ -217,6 +245,41 @@ void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { } } +void ChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { + // FIXME dirty and fast hack to make http:// urls klickable + + QRegExp regex("\\b([hf]t{1,2}ps?://[^\\s]+)\\b"); + QString str = data(ChatLineModel::DisplayRole).toString(); + int idx = posToCursor(event->pos()); + int mi = 0; + do { + mi = regex.indexIn(str, mi); + if(mi < 0) break; + if(idx >= mi && idx < mi + regex.matchedLength()) { + QDesktopServices::openUrl(QUrl(regex.capturedTexts()[1])); + break; + } + mi += regex.matchedLength(); + } while(mi >= 0); + event->accept(); +} + +void ChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { + //qDebug() << (void*)this << "entering"; + +} + +void ChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { + //qDebug() << (void*)this << "leaving"; + +} + +void ChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { + //qDebug() << (void*)this << event->pos(); + +} + + /*************************************************************************************************/ ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {