From 7502f167aec63002a1724c165f63ed974bc0c3bf Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 14 May 2017 16:31:59 +0300 Subject: [PATCH] Use QSharedPointer to manage TimelineViews and RoomInfoListItems --- include/RoomList.h | 2 +- include/TimelineView.h | 1 - include/TimelineViewManager.h | 2 +- src/RoomList.cc | 5 +---- src/TimelineView.cc | 6 ------ src/TimelineViewManager.cc | 11 ++++------- 6 files changed, 7 insertions(+), 20 deletions(-) diff --git a/include/RoomList.h b/include/RoomList.h index 8bb962e0..348b1e28 100644 --- a/include/RoomList.h +++ b/include/RoomList.h @@ -60,7 +60,7 @@ private: Ui::RoomList *ui; - QMap rooms_; + QMap> rooms_; QSharedPointer client_; }; diff --git a/include/TimelineView.h b/include/TimelineView.h index 297dffcf..ba5642c3 100644 --- a/include/TimelineView.h +++ b/include/TimelineView.h @@ -73,7 +73,6 @@ public: void addUserTextMessage(const QString &msg, int txn_id); void updatePendingMessage(int txn_id, QString event_id); void scrollDown(); - void clear(); public slots: void sliderRangeChanged(int min, int max); diff --git a/include/TimelineViewManager.h b/include/TimelineViewManager.h index 20f40951..d18e7320 100644 --- a/include/TimelineViewManager.h +++ b/include/TimelineViewManager.h @@ -58,7 +58,7 @@ private slots: private: QString active_room_; - QMap views_; + QMap> views_; QSharedPointer client_; }; diff --git a/src/RoomList.cc b/src/RoomList.cc index a0312113..58053bbd 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -51,9 +51,6 @@ RoomList::~RoomList() void RoomList::clear() { - for (const auto &room : rooms_) - room->deleteLater(); - rooms_.clear(); } @@ -93,7 +90,7 @@ void RoomList::setInitialRooms(const QMap &states) RoomInfoListItem *room_item = new RoomInfoListItem(state, room_id, ui->scrollArea); connect(room_item, &RoomInfoListItem::clicked, this, &RoomList::highlightSelectedRoom); - rooms_.insert(room_id, room_item); + rooms_.insert(room_id, QSharedPointer(room_item)); int pos = ui->scrollVerticalLayout->count() - 1; ui->scrollVerticalLayout->insertWidget(pos, room_item); diff --git a/src/TimelineView.cc b/src/TimelineView.cc index aec519ed..7881aee4 100644 --- a/src/TimelineView.cc +++ b/src/TimelineView.cc @@ -49,12 +49,6 @@ TimelineView::TimelineView(const Timeline &timeline, addEvents(timeline); } -void TimelineView::clear() -{ - for (const auto msg : scroll_layout_->children()) - msg->deleteLater(); -} - void TimelineView::sliderRangeChanged(int min, int max) { Q_UNUSED(min); diff --git a/src/TimelineViewManager.cc b/src/TimelineViewManager.cc index d07e8075..008dc5dc 100644 --- a/src/TimelineViewManager.cc +++ b/src/TimelineViewManager.cc @@ -65,11 +65,8 @@ void TimelineViewManager::clearAll() { NICK_COLORS.clear(); - for (const auto &view : views_) { - view->clear(); - removeWidget(view); - view->deleteLater(); - } + for (auto view : views_) + removeWidget(view.data()); views_.clear(); } @@ -81,7 +78,7 @@ void TimelineViewManager::initialize(const Rooms &rooms) // Create a history view with the room events. TimelineView *view = new TimelineView(it.value().timeline(), client_, it.key()); - views_.insert(it.key(), view); + views_.insert(it.key(), QSharedPointer(view)); // Add the view in the widget stack. addWidget(view); @@ -124,7 +121,7 @@ void TimelineViewManager::setHistoryView(const QString &room_id) auto widget = views_.value(room_id); widget->scrollDown(); - setCurrentWidget(widget); + setCurrentWidget(widget.data()); } QMap TimelineViewManager::NICK_COLORS;