#include "messagefilter.h"
-MessageFilter::MessageFilter(QAbstractItemModel *source, QObject *parent) : QSortFilterProxyModel(parent) {
+MessageFilter::MessageFilter(QAbstractItemModel *source, QObject *parent)
+ : QSortFilterProxyModel(parent)
+{
setSourceModel(source);
}
MessageFilter::MessageFilter(MessageModel *source, const QList<BufferId> &buffers, QObject *parent)
: QSortFilterProxyModel(parent),
- _bufferList(buffers)
+ _validBuffers(buffers.toSet())
{
setSourceModel(source);
}
QString MessageFilter::idString() const {
- if(_bufferList.isEmpty()) return "*";
- QString idstr;
- QStringList bufids;
- foreach(BufferId id, _bufferList) bufids << QString::number(id.toInt());
- bufids.sort();
- foreach(QString id, bufids) idstr += id + '|';
- idstr.chop(1);
- return idstr;
+ if(_validBuffers.isEmpty())
+ return "*";
+
+ QList<BufferId> bufferIds = _validBuffers.toList();;
+ qSort(bufferIds);
+
+ QStringList bufferIdStrings;
+ foreach(BufferId id, bufferIds)
+ bufferIdStrings << QString::number(id.toInt());
+
+ return bufferIdStrings.join("|");
}
bool MessageFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
Q_UNUSED(sourceParent);
- if(_bufferList.isEmpty()) return true;
+ if(_validBuffers.isEmpty())
+ return true;
+
BufferId id = sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::BufferIdRole).value<BufferId>();
- return _bufferList.contains(id);
+ return _validBuffers.contains(id);
}
class MessageFilter : public QSortFilterProxyModel {
Q_OBJECT
- protected:
- MessageFilter(QAbstractItemModel *source, QObject *parent = 0);
-
- public:
- MessageFilter(MessageModel *, const QList<BufferId> &buffers = QList<BufferId>(), QObject *parent = 0);
-
- virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
- virtual QString idString() const;
- inline bool isSingleBufferFilter() const { return _bufferList.count() == 1; }
-
- private:
- QList<BufferId> _bufferList;
+protected:
+ MessageFilter(QAbstractItemModel *source, QObject *parent = 0);
+
+public:
+ MessageFilter(MessageModel *, const QList<BufferId> &buffers = QList<BufferId>(), QObject *parent = 0);
+
+ virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
+ virtual QString idString() const;
+ inline bool isSingleBufferFilter() const { return _validBuffers.count() == 1; }
+
+private:
+ QSet<BufferId> _validBuffers;
};
#endif
_chatViews[id] = chatView;
ui.stackedWidget->addWidget(chatView);
chatView->setFocusProxy(this);
- chatView->setBufferForBacklogFetching(id);
return chatView;
}
#include "networkmodel.h"
ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
- : MessageFilter(model, QList<BufferId>(), parent)
+ : MessageFilter(model, parent)
{
}
_singleBufferScene(false),
_selectingItem(0),
_selectionStart(-1),
- _isSelecting(false),
- _fetchingBacklog(false)
+ _isSelecting(false)
{
MessageFilter *filter = qobject_cast<MessageFilter*>(model);
if(filter) {
setSceneRect(QRectF(0, 0, _width, _height));
emit heightChanged(_height);
}
-
- requestBacklogIfNeeded();
}
void ChatScene::modelReset() {
return result;
}
-void ChatScene::setIsFetchingBacklog(bool fetch) {
- if(!isBacklogFetchingEnabled()) return;
-
- if(!fetch) {
- _fetchingBacklog = false;
- } else {
- _fetchingBacklog = true;
- requestBacklogIfNeeded();
- }
-}
-
-void ChatScene::requestBacklogIfNeeded() {
- const int REQUEST_COUNT = 50;
-
- if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
-
- MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
- if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
- Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
- _lastBacklogOffset = msgId;
- _lastBacklogSize = model()->rowCount();
+void ChatScene::requestBacklog() {
+ static const int REQUEST_COUNT = 50;
+ int backlogSize = model()->rowCount();
+ if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
+ QModelIndex msgIdx = model()->index(0, 0);
+ MsgId msgId = model()->data(msgIdx, ChatLineModel::MsgIdRole).value<MsgId>();
+ BufferId bufferId = model()->data(msgIdx, ChatLineModel::BufferIdRole).value<BufferId>();
+ _lastBacklogSize = backlogSize;
+ Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
}
}
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
-#ifndef _CHATSCENE_H_
-#define _CHATSCENE_H_
+#ifndef CHATSCENE_H_
+#define CHATSCENE_H_
#include <QAbstractItemModel>
#include <QGraphicsScene>
inline QAbstractItemModel *model() const { return _model; }
inline QString idString() const { return _idString; }
- inline bool isFetchingBacklog() const;
- inline bool isBacklogFetchingEnabled() const;
- inline BufferId bufferForBacklogFetching() const;
int sectionByScenePos(int x);
inline int sectionByScenePos(const QPoint &pos) { return sectionByScenePos(pos.x()); }
inline bool isSingleBufferScene() const { return _singleBufferScene; }
void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
void putToClipboard(const QString &);
- void setIsFetchingBacklog(bool);
- inline void setBufferForBacklogFetching(BufferId buffer);
+ void requestBacklog();
signals:
void heightChanged(qreal height);
private:
void updateSelection(const QPointF &pos);
QString selectionToString() const;
- void requestBacklogIfNeeded();
QString _idString;
qreal _width, _height;
int _firstSelectionRow, _lastSelectionRow;
bool _isSelecting;
- bool _fetchingBacklog;
- BufferId _backlogFetchingBuffer;
- MsgId _lastBacklogOffset;
int _lastBacklogSize;
};
-bool ChatScene::isFetchingBacklog() const {
- return _fetchingBacklog;
-}
-
-bool ChatScene::isBacklogFetchingEnabled() const {
- return _backlogFetchingBuffer.isValid();
-}
-
-BufferId ChatScene::bufferForBacklogFetching() const {
- return _backlogFetchingBuffer;
-}
-
-void ChatScene::setBufferForBacklogFetching(BufferId buf) {
- _backlogFetchingBuffer = buf;
-}
-
#endif
filterList.append(bufferId);
MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
init(filter);
-
}
ChatView::ChatView(MessageFilter *filter, QWidget *parent)
connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
setScene(_scene);
- connect(verticalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
- connect(verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
}
if(scrollable) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}
-void ChatView::setBufferForBacklogFetching(BufferId id) {
- scene()->setBufferForBacklogFetching(id);
-}
-
-void ChatView::sliderPressed() {
- verticalScrollbarChanged(verticalScrollBar()->value());
-}
-
-void ChatView::sliderReleased() {
- if(scene()->isFetchingBacklog()) scene()->setIsFetchingBacklog(false);
-}
-
void ChatView::verticalScrollbarChanged(int newPos) {
- Q_UNUSED(newPos);
- if(!scene()->isBacklogFetchingEnabled()) return;
-
QAbstractSlider *vbar = verticalScrollBar();
- if(!vbar)
- return;
- if(vbar->isSliderDown()) {
- /*
- int relativePos = 100;
- if(vbar->maximum() - vbar->minimum() != 0)
- relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
- scene()->setIsFetchingBacklog(relativePos < 20);
- */
- scene()->setIsFetchingBacklog(vbar->value() == vbar->minimum());
+ Q_ASSERT(vbar);
+
+ int relativePos = 100;
+ if(vbar->maximum() - vbar->minimum() != 0)
+ relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
+
+ if(relativePos < 20) {
+ scene()->requestBacklog();
}
}
if(!model || model->rowCount() == 0)
return MsgId();
-
return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
}
public slots:
inline virtual void clear() {}
- void setBufferForBacklogFetching(BufferId buffer);
protected:
virtual void resizeEvent(QResizeEvent *event);
protected slots:
virtual void sceneHeightChanged(qreal height);
virtual void verticalScrollbarChanged(int);
- virtual void sliderPressed();
- virtual void sliderReleased();
private:
void init(MessageFilter *filter);