From: Manuel Nickschas Date: Fri, 7 Nov 2008 21:21:57 +0000 (+0100) Subject: Some cleanups in ChatScene in preparation to mouse handling revamp X-Git-Tag: 0.4.0~432 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=229b87f259ab1bc2c65f481eb39c25a872080fe7;ds=sidebyside Some cleanups in ChatScene in preparation to mouse handling revamp * We should always use qreal for scene coordinates * Provide a few convenience methods --- diff --git a/src/qtui/chatitem.h b/src/qtui/chatitem.h index cf608cd4..79f8720e 100644 --- a/src/qtui/chatitem.h +++ b/src/qtui/chatitem.h @@ -69,6 +69,8 @@ protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + //virtual bool handleClick(ClickMode mode); + inline QTextLayout *layout() const; virtual QTextLayout::FormatRange selectionFormat() const; diff --git a/src/qtui/chatmonitorview.cpp b/src/qtui/chatmonitorview.cpp index 5ab9df79..d5c37312 100644 --- a/src/qtui/chatmonitorview.cpp +++ b/src/qtui/chatmonitorview.cpp @@ -48,7 +48,7 @@ void ChatMonitorView::contextMenuEvent(QContextMenuEvent *event) { showOwnNicksAction->setCheckable(true); showOwnNicksAction->setChecked(_filter->showOwnMessages()); - if(scene()->sectionByScenePos(event->pos()) == ChatLineModel::SenderColumn) { + if(scene()->columnByScenePos(event->pos()) == ChatLineModel::SenderColumn) { contextMenu.addSeparator(); QAction *showNetworkAction = contextMenu.addAction(tr("Show network name"), this, SLOT(showFieldsChanged(bool))); @@ -66,7 +66,7 @@ void ChatMonitorView::contextMenuEvent(QContextMenuEvent *event) { } void ChatMonitorView::mouseDoubleClickEvent(QMouseEvent *event) { - if(scene()->sectionByScenePos(event->pos()) != ChatLineModel::SenderColumn) { + if(scene()->columnByScenePos(event->pos()) != ChatLineModel::SenderColumn) { ChatView::mouseDoubleClickEvent(event); return; } diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 1ce69f64..ba31ce82 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -104,6 +104,14 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal w ChatScene::~ChatScene() { } +bool ChatScene::containsBuffer(const BufferId &id) const { + MessageFilter *filter = qobject_cast(model()); + if(filter) + return filter->containsBuffer(id); + else + return false; +} + void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) { Q_UNUSED(index); @@ -464,17 +472,9 @@ void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) { } void ChatScene::updateSelection(const QPointF &pos) { - // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since - // it has the full height. From this item, we can then determine the row index and hence the ChatLine. - ChatItem *contentItem = static_cast(itemAt(QPointF(_secondColHandle->sceneRight() + 1, pos.y()))); - if(!contentItem) return; - - int curRow = contentItem->row(); - int curColumn; - if(pos.x() > _secondColHandle->sceneRight()) curColumn = ChatLineModel::ContentsColumn; - else if(pos.x() > _firstColHandlePos) curColumn = ChatLineModel::SenderColumn; - else curColumn = ChatLineModel::TimestampColumn; - + int curRow = rowByScenePos(pos); + if(curRow < 0) return; + int curColumn = (int)columnByScenePos(pos); ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol); if(minColumn != _selectionMinCol) { _selectionMinCol = minColumn; @@ -595,7 +595,7 @@ void ChatScene::requestBacklog() { return; } -int ChatScene::sectionByScenePos(int x) { +ChatLineModel::ColumnType ChatScene::columnByScenePos(qreal x) { if(x < _firstColHandle->x()) return ChatLineModel::TimestampColumn; if(x < _secondColHandle->x()) @@ -604,6 +604,15 @@ int ChatScene::sectionByScenePos(int x) { return ChatLineModel::ContentsColumn; } +int ChatScene::rowByScenePos(qreal y) { + // This is somewhat hacky... we look at the contents item that is at the given y position, since + // it has the full height. From this item, we can then determine the row index and hence the ChatLine. + // ChatItems cover their ChatLine, so we won't get to the latter directly. + ChatItem *contentItem = static_cast(itemAt(QPointF(_secondColHandle->sceneRight() + 1, y))); + if(!contentItem) return -1; + return contentItem->row(); +} + void ChatScene::updateSceneRect(qreal width) { if(_lines.isEmpty()) { updateSceneRect(QRectF(0, 0, width, 0)); diff --git a/src/qtui/chatscene.h b/src/qtui/chatscene.h index 85328df6..b0c80f4d 100644 --- a/src/qtui/chatscene.h +++ b/src/qtui/chatscene.h @@ -24,7 +24,9 @@ #include #include #include +#include +#include "chatlinemodel.h" #include "columnhandleitem.h" #include "messagefilter.h" @@ -54,16 +56,25 @@ public: WebPreviewType }; + enum ClickMode { + NoClick, + SingleClick, + DoubleClick, + TripleClick + }; + ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, QObject *parent); virtual ~ChatScene(); inline QAbstractItemModel *model() const { return _model; } inline QString idString() const { return _idString; } - int sectionByScenePos(int x); - inline int sectionByScenePos(const QPoint &pos) { return sectionByScenePos(pos.x()); } + int rowByScenePos(qreal y); + inline int rowByScenePos(const QPointF &pos) { return rowByScenePos(pos.y()); } + ChatLineModel::ColumnType columnByScenePos(qreal x); + inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) { return columnByScenePos(pos.x()); } inline bool isSingleBufferScene() const { return _singleBufferScene; } - inline bool containsBuffer(const BufferId &id) const; + bool containsBuffer(const BufferId &id) const; inline ChatLine *chatLine(int row) { return (row < _lines.count()) ? _lines[row] : 0; } inline ColumnHandleItem *firstColumnHandle() const { return _firstColHandle; } @@ -100,6 +111,7 @@ protected: virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent); virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent); + //virtual bool handleLeftClick(ClickMode mode); protected slots: void rowsInserted(const QModelIndex &, int, int); @@ -144,6 +156,8 @@ private: bool _showWebPreview; + QTimer _clickTimer; + struct WebPreview { ChatItem *parentItem; QGraphicsItem *previewItem; @@ -156,12 +170,4 @@ private: WebPreview webPreview; }; -bool ChatScene::containsBuffer(const BufferId &id) const { - MessageFilter *filter = qobject_cast(model()); - if(filter) - return filter->containsBuffer(id); - else - return false; -} - #endif diff --git a/src/qtui/chatview.cpp b/src/qtui/chatview.cpp index 9c5a6dc3..53fa024b 100644 --- a/src/qtui/chatview.cpp +++ b/src/qtui/chatview.cpp @@ -61,7 +61,7 @@ void ChatView::init(MessageFilter *filter) { _scrollTimer.setSingleShot(true); connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout())); - _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 2, this); // see below: resizeEvent() + _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent() connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &))); connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal))); connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));