From: Manuel Nickschas Date: Sat, 1 May 2010 15:57:52 +0000 (+0200) Subject: Provide some new accessors for Chat{View|Scene} X-Git-Tag: 0.7-beta1~88 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=57982f5802c957e768ce842888591115694fcbef Provide some new accessors for Chat{View|Scene} This allows accessing the currently visible ChatLines in a view, and a ChatLine by MsgId in a scene. --- diff --git a/src/qtui/chatline.h b/src/qtui/chatline.h index 824d50e9..a7c68e87 100644 --- a/src/qtui/chatline.h +++ b/src/qtui/chatline.h @@ -37,11 +37,14 @@ public: virtual inline QRectF boundingRect () const { return QRectF(0, 0, _width, _height); } - inline int row() { return _row; } + inline QModelIndex index() const { return model()->index(row(), 0); } + inline MsgId msgId() const { return index().data(MessageModel::MsgIdRole).value(); } + inline int row() const { return _row; } inline void setRow(int row) { _row = row; } inline const QAbstractItemModel *model() const { return _model; } inline ChatScene *chatScene() const { return qobject_cast(scene()); } + inline ChatView *chatView() const { return chatScene()->chatView(); } inline qreal width() const { return _width; } inline qreal height() const { return _height; } diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 1c1dd90f..cb5b3f70 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -138,6 +138,33 @@ ColumnHandleItem *ChatScene::secondColumnHandle() const { return _secondColHandle; } +ChatLine *ChatScene::chatLine(MsgId msgId) const { + if(!_lines.count()) + return 0; + + QList::ConstIterator start = _lines.begin(); + QList::ConstIterator end = _lines.end(); + QList::ConstIterator middle; + + int n = int(end - start); + int half; + + while(n > 0) { + half = n >> 1; + middle = start + half; + if((*middle)->msgId() < msgId) { + start = middle + 1; + n -= half + 1; + } else { + n = half; + } + } + if((*start)->msgId() == msgId) + return *start; + + return 0; +} + ChatItem *ChatScene::chatItemAt(const QPointF &scenePos) const { ChatLine *line = qgraphicsitem_cast(itemAt(scenePos)); if(line) diff --git a/src/qtui/chatscene.h b/src/qtui/chatscene.h index f3d62734..3daf861b 100644 --- a/src/qtui/chatscene.h +++ b/src/qtui/chatscene.h @@ -82,7 +82,19 @@ public: ChatView *chatView() const; ChatItem *chatItemAt(const QPointF &pos) const; - inline ChatLine *chatLine(int row) { return (row < _lines.count()) ? _lines[row] : 0; } + inline ChatLine *chatLine(int row) const { return (row < _lines.count()) ? _lines.value(row) : 0; } + inline ChatLine *chatLine(const QModelIndex &index) const { return _lines.value(index.row()); } + + //! Find the ChatLine belonging to a MsgId + /** Searches for the ChatLine belonging to a MsgId. + * Note that this method performs a binary search, hence it has as complexity of O(log n). + * If there is more than one ChatLine for the given ID, the first one will be returned. + * \param msgId The message ID to look for + * \return The ChatLine corresponding to the given MsgId + */ + ChatLine *chatLine(MsgId msgId) const; + + inline ChatLine *lastLine() const { return _lines.count() ? _lines.last() : 0; } inline bool isSingleBufferScene() const { return _singleBufferId.isValid(); } inline BufferId singleBufferId() const { return _singleBufferId; } diff --git a/src/qtui/chatview.cpp b/src/qtui/chatview.cpp index 71c14276..453ac044 100644 --- a/src/qtui/chatview.cpp +++ b/src/qtui/chatview.cpp @@ -32,12 +32,11 @@ #include "qtuistyle.h" #include "clientignorelistmanager.h" +#include "chatline.h" + ChatView::ChatView(BufferId bufferId, QWidget *parent) : QGraphicsView(parent), - AbstractChatView(), - _bufferContainer(0), - _currentScaleFactor(1), - _invalidateFilter(false) + AbstractChatView() { QList filterList; filterList.append(bufferId); @@ -47,15 +46,16 @@ ChatView::ChatView(BufferId bufferId, QWidget *parent) ChatView::ChatView(MessageFilter *filter, QWidget *parent) : QGraphicsView(parent), - AbstractChatView(), - _bufferContainer(0), - _currentScaleFactor(1), - _invalidateFilter(false) + AbstractChatView() { init(filter); } void ChatView::init(MessageFilter *filter) { + _bufferContainer = 0; + _currentScaleFactor = 1; + _invalidateFilter = false; + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); setAlignment(Qt::AlignLeft|Qt::AlignBottom); @@ -202,7 +202,58 @@ MsgId ChatView::lastMsgId() const { if(!model || model->rowCount() == 0) return MsgId(); - return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value(); + return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value(); +} + +MsgId ChatView::lastVisibleMsgId() const { + ChatLine *line = lastVisibleChatLine(); + + if(line) + return line->msgId(); + + return MsgId(); +} + +bool chatLinePtrLessThan(ChatLine *one, ChatLine *other) { + return one->row() < other->row(); +} + +QSet ChatView::visibleChatLines(Qt::ItemSelectionMode mode) const { + QSet result; + foreach(QGraphicsItem *item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) { + ChatLine *line = qgraphicsitem_cast(item); + if(line) + result.insert(line); + } + return result; +} + +QList ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const { + QList result = visibleChatLines(mode).toList(); + qSort(result.begin(), result.end(), chatLinePtrLessThan); + return result; +} + +ChatLine *ChatView::lastVisibleChatLine() const { + if(!scene()) + return 0; + + QAbstractItemModel *model = scene()->model(); + if(!model || model->rowCount() == 0) + return 0; + + int row = -1; + + QSet visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect); + foreach(ChatLine *line, visibleLines) { + if(line->row() > row) + row = line->row(); + } + + if(row >= 0) + return scene()->chatLine(row); + + return 0; } void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) { diff --git a/src/qtui/chatview.h b/src/qtui/chatview.h index 3c2eba13..4fb3661e 100644 --- a/src/qtui/chatview.h +++ b/src/qtui/chatview.h @@ -42,11 +42,31 @@ public: ChatView(BufferId bufferId, QWidget *parent = 0); virtual MsgId lastMsgId() const; + virtual MsgId lastVisibleMsgId() const; inline AbstractBufferContainer *bufferContainer() const { return _bufferContainer; } inline void setBufferContainer(AbstractBufferContainer *c) { _bufferContainer = c; } inline ChatScene *scene() const { return _scene; } + //! Return a set of ChatLines currently visible in the view + /** \param mode How partially visible ChatLines are handled + * \return A set of visible ChatLines + */ + QSet visibleChatLines(Qt::ItemSelectionMode mode = Qt::ContainsItemBoundingRect) const; + + //! Return a sorted list of ChatLines currently visible in the view + /** \param mode How partially visible ChatLines are handled + * \return A list of visible ChatLines sorted by row + * \note If the order of ChatLines does not matter, use visibleChatLines() instead + */ + QList visibleChatLinesSorted(Qt::ItemSelectionMode mode = Qt::ContainsItemBoundingRect) const; + + //! Return the last fully visible ChatLine in this view + /** Using this method more efficient than calling visibleChatLinesSorted() and taking its last element. + * \return The last fully visible ChatLine in the view + */ + ChatLine *lastVisibleChatLine() const; + virtual void addActionsToMenu(QMenu *, const QPointF &pos); virtual bool event(QEvent *event);