From bd37d2c94e49e791d2ba44baab4270e030442832 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Mon, 4 Aug 2008 01:43:18 +0200 Subject: [PATCH] Clean up MessageModel on disconnect --- src/client/client.cpp | 1 + src/client/messagemodel.cpp | 16 +++++++++++----- src/client/messagemodel.h | 2 ++ src/qtui/chatscene.cpp | 11 ++++++++++- src/qtui/chatscene.h | 1 + 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index e39435e6..eeb25d58 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -357,6 +357,7 @@ void Client::disconnectFromCore() { _bufferViewManager = 0; } + _messageModel->clear(); _networkModel->clear(); QHash::iterator bufferIter = _buffers.begin(); diff --git a/src/client/messagemodel.cpp b/src/client/messagemodel.cpp index 76104bd2..4032c649 100644 --- a/src/client/messagemodel.cpp +++ b/src/client/messagemodel.cpp @@ -23,14 +23,14 @@ #include "message.h" MessageModel::MessageModel(QObject *parent) : QAbstractItemModel(parent) { - - - + + + } MessageModel::~MessageModel() { - - + + } QVariant MessageModel::data(const QModelIndex &index, int role) const { @@ -66,6 +66,12 @@ void MessageModel::insertMessages(const QList &msglist) { } +void MessageModel::clear() { + reset(); + qDeleteAll(_messageList); + _messageList.clear(); +} + // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg) int MessageModel::indexForId(MsgId id) { if(_messageList.isEmpty() || id <= _messageList[0]->data(0, MsgIdRole).value()) return 0; diff --git a/src/client/messagemodel.h b/src/client/messagemodel.h index f6e2538f..8d7c0ed8 100644 --- a/src/client/messagemodel.h +++ b/src/client/messagemodel.h @@ -66,6 +66,8 @@ class MessageModel : public QAbstractItemModel { void insertMessage(const Message &); void insertMessages(const QList &); + void clear(); + protected: virtual MessageModelItem *createMessageModelItem(const Message &) = 0; diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 8207248e..6dbe1710 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -45,6 +45,7 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &))); connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int))); + connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset())); for(int i = 0; i < model->rowCount(); i++) { ChatLine *line = new ChatLine(model->index(i, 0)); _lines.append(line); @@ -76,7 +77,6 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject ChatScene::~ChatScene() { - } void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) { @@ -106,6 +106,15 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) { } } +void ChatScene::modelReset() { + foreach(ChatLine *line, _lines) { + removeItem(line); + delete line; + } + _lines.clear(); + setSceneRect(QRectF(0, 0, _width, 0)); +} + void ChatScene::setWidth(qreal w) { _width = w; _height = 0; diff --git a/src/qtui/chatscene.h b/src/qtui/chatscene.h index cd71aac8..22dcb479 100644 --- a/src/qtui/chatscene.h +++ b/src/qtui/chatscene.h @@ -62,6 +62,7 @@ class ChatScene : public QGraphicsScene { protected slots: void rowsInserted(const QModelIndex &, int, int); + void modelReset(); private slots: void rectChanged(const QRectF &); -- 2.20.1