From 858b005567de791c3bbcb5547308d263275de85e Mon Sep 17 00:00:00 2001 From: TheSpiritXIII Date: Wed, 17 Jun 2015 08:40:14 -0400 Subject: [PATCH] Circles: Fix memory leaks and extraneous saving --- src/core/core.cpp | 1 - src/core/core.h | 2 - src/persistence/settings.cpp | 2 - src/widget/categorywidget.cpp | 37 +++++++++------- src/widget/categorywidget.h | 5 ++- src/widget/circlewidget.cpp | 8 ++-- src/widget/friendlistwidget.cpp | 78 ++++++++++++++++++++++----------- src/widget/friendlistwidget.h | 1 + src/widget/friendwidget.cpp | 1 + ui/window/statusPanel.css | 1 + 10 files changed, 85 insertions(+), 51 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index da42f6c2f..1d8a4f567 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -364,7 +364,6 @@ void Core::start() if (isReady()) GUI::setEnabled(true); - process(); // starts its own timer } diff --git a/src/core/core.h b/src/core/core.h index e324e8db1..d8d1eec9a 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -157,8 +157,6 @@ signals: void connected(); void disconnected(); - void blockingClearContacts(); - void friendRequestReceived(const QString& userId, const QString& message); void friendMessageReceived(uint32_t friendId, const QString& message, bool isAction); diff --git a/src/persistence/settings.cpp b/src/persistence/settings.cpp index 992f453fe..a8ab02ef0 100644 --- a/src/persistence/settings.cpp +++ b/src/persistence/settings.cpp @@ -1312,7 +1312,6 @@ void Settings::setFriendCircleID(const ToxId &id, int circleID) fp.circleID = circleID; friendLst[key] = fp; } - savePersonal(); } QDate Settings::getFriendActivity(const ToxId &id) const @@ -1426,7 +1425,6 @@ bool Settings::getCircleExpanded(int id) const void Settings::setCircleExpanded(int id, bool expanded) { circleLst[id].expanded = expanded; - savePersonal(); } int Settings::removeCircle(int id) diff --git a/src/widget/categorywidget.cpp b/src/widget/categorywidget.cpp index 8085096e9..f5765ffea 100644 --- a/src/widget/categorywidget.cpp +++ b/src/widget/categorywidget.cpp @@ -26,6 +26,8 @@ #include #include +#include + void emitChatroomWidget(QLayout* layout, int index) { GenericChatroomWidget* chatWidget = dynamic_cast(layout->itemAt(index)->widget()); @@ -68,7 +70,7 @@ CategoryWidget::CategoryWidget(QWidget* parent) onCompactChanged(isCompact()); - setExpanded(true); + setExpanded(true, false); updateStatus(); } @@ -77,38 +79,41 @@ bool CategoryWidget::isExpanded() const return expanded; } -void CategoryWidget::setExpanded(bool isExpanded) +void CategoryWidget::setExpanded(bool isExpanded, bool save) { expanded = isExpanded; + setMouseTracking(true); listWidget->setVisible(isExpanded); - listWidget->move(1000, 1000); + if (isExpanded) - { statusPic.setPixmap(QPixmap(":/ui/chatArea/scrollBarDownArrow.svg")); - } else - { statusPic.setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg")); - } // The listWidget will recieve a enterEvent for some reason if now visible. // Using the following, we prevent that. - hide(); - show(); + QApplication::processEvents(QEventLoop::ExcludeSocketNotifiers); + container->hide(); + container->show(); - // However, the above also removes the hover, so we need to reenable it. - if (underMouse()) - setContainerAttribute(Qt::WA_UnderMouse, true); // Simulate hover. - - onExpand(); + if (save) + onExpand(); } -void CategoryWidget::setName(const QString &name) +void CategoryWidget::leaveEvent(QEvent *event) +{ + event->ignore(); +} + +void CategoryWidget::setName(const QString &name, bool save) { nameLabel->setText(name); + if (isCompact()) nameLabel->minimizeMaximumWidth(); - onSetName(); + + if (save) + onSetName(); } void CategoryWidget::editName() diff --git a/src/widget/categorywidget.h b/src/widget/categorywidget.h index 9c7eb38e9..ff91a40f8 100644 --- a/src/widget/categorywidget.h +++ b/src/widget/categorywidget.h @@ -36,8 +36,8 @@ public: CategoryWidget(QWidget* parent = 0); bool isExpanded() const; - void setExpanded(bool isExpanded); - void setName(const QString &name); + void setExpanded(bool isExpanded, bool save = true); + void setName(const QString &name, bool save = true); void addFriendWidget(FriendWidget* w, Status s); void removeFriendWidget(FriendWidget* w, Status s); @@ -52,6 +52,7 @@ public slots: void onCompactChanged(bool compact); protected: + virtual void leaveEvent(QEvent* event) final override; virtual void mouseReleaseEvent(QMouseEvent* event) final override; void editName(); diff --git a/src/widget/circlewidget.cpp b/src/widget/circlewidget.cpp index f9a49da9b..5320d6da9 100644 --- a/src/widget/circlewidget.cpp +++ b/src/widget/circlewidget.cpp @@ -39,7 +39,7 @@ CircleWidget::CircleWidget(FriendListWidget* parent, int id) : CategoryWidget(parent) , id(id) { - setName(Settings::getInstance().getCircleName(id)); + setName(Settings::getInstance().getCircleName(id), false); circleList[id] = this; connect(nameLabel, &CroppingLabel::editFinished, [this](const QString &newName) @@ -54,7 +54,7 @@ CircleWidget::CircleWidget(FriendListWidget* parent, int id) nameLabel->minimizeMaximumWidth(); }); - setExpanded(Settings::getInstance().getCircleExpanded(id)); + setExpanded(Settings::getInstance().getCircleExpanded(id), false); updateStatus(); } @@ -118,7 +118,7 @@ void CircleWidget::dropEvent(QDropEvent* event) { if (event->mimeData()->hasFormat("friend")) { - setExpanded(true); + setExpanded(true, false); int friendId = event->mimeData()->data("friend").toInt(); Friend* f = FriendList::findFriend(friendId); @@ -131,6 +131,7 @@ void CircleWidget::dropEvent(QDropEvent* event) CircleWidget* circleWidget = getFromID(Settings::getInstance().getFriendCircleID(f->getToxId())); addFriendWidget(widget, f->getStatus()); + Settings::getInstance().savePersonal(); if (circleWidget != nullptr) { @@ -150,6 +151,7 @@ void CircleWidget::onSetName() void CircleWidget::onExpand() { Settings::getInstance().setCircleExpanded(id, isExpanded()); + Settings::getInstance().savePersonal(); } void CircleWidget::onAddFriendWidget(FriendWidget* w) diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index d08d6af9d..e022bb629 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -99,19 +99,11 @@ Time getTime(const QDate date) return LongAgo; } -int toIndex(Time time) -{ - if (time < ThisMonth) - return time; - - if (!last7DaysWasLastMonth()) - return time - 1; - - return time; -} - FriendListWidget::FriendListWidget(Widget* parent, bool groupsOnTop) : QWidget(parent) + // Prevent Valgrind from complaining. We're changing this to Name here. + // Must be Activity for change to take effect. + , mode(Activity) , groupsOnTop(groupsOnTop) { listLayout = new FriendListLayout(); @@ -121,6 +113,10 @@ FriendListWidget::FriendListWidget(Widget* parent, bool groupsOnTop) groupLayout.getLayout()->setSpacing(0); groupLayout.getLayout()->setMargin(0); + // Prevent QLayout's add child warning before setting the mode. + listLayout->removeItem(listLayout->getLayoutOnline()); + listLayout->removeItem(listLayout->getLayoutOffline()); + setMode(Name); onGroupchatPositionChanged(groupsOnTop); @@ -128,6 +124,31 @@ FriendListWidget::FriendListWidget(Widget* parent, bool groupsOnTop) setAcceptDrops(true); } +FriendListWidget::~FriendListWidget() +{ + if (activityLayout != nullptr) + { + QLayoutItem* item; + while ((item = activityLayout->takeAt(0)) != nullptr) + { + delete item->widget(); + delete item; + } + delete activityLayout; + } + + if (circleLayout != nullptr) + { + QLayoutItem* item; + while ((item = circleLayout->getLayout()->takeAt(0)) != nullptr) + { + delete item->widget(); + delete item; + } + delete circleLayout; + } +} + void FriendListWidget::setMode(Mode mode) { if (this->mode == mode) @@ -160,13 +181,16 @@ void FriendListWidget::setMode(Mode mode) if (activityLayout != nullptr) { - while (activityLayout->count() > 0) - delete activityLayout->takeAt(0)->widget(); + QLayoutItem* item; + while ((item = activityLayout->takeAt(0)) != nullptr) + { + delete item->widget(); + delete item; + } + delete activityLayout; + activityLayout = nullptr; } - delete activityLayout; - activityLayout = nullptr; - reDraw(); } else if (mode == Activity) @@ -174,6 +198,7 @@ void FriendListWidget::setMode(Mode mode) activityLayout = new QVBoxLayout(); CategoryWidget* categoryToday = new CategoryWidget(this); + categoryToday->setObjectName("Todddd"); categoryToday->setName(tr("Today", "Category for sorting friends by activity")); activityLayout->addWidget(categoryToday); @@ -241,21 +266,24 @@ void FriendListWidget::setMode(Mode mode) categoryWidget->setVisible(categoryWidget->hasChatrooms()); } - if (circleLayout != nullptr) - { - while (circleLayout->getLayout()->count() > 0) - delete circleLayout->getLayout()->takeAt(0)->widget(); - } - listLayout->removeItem(listLayout->getLayoutOnline()); listLayout->removeItem(listLayout->getLayoutOffline()); listLayout->removeItem(circleLayout->getLayout()); listLayout->insertLayout(1, activityLayout); - reDraw(); + if (circleLayout != nullptr) + { + QLayoutItem* item; + while ((item = circleLayout->getLayout()->takeAt(0)) != nullptr) + { + delete item->widget(); + delete item; + } + delete circleLayout; + circleLayout = nullptr; + } - delete circleLayout; - circleLayout = nullptr; + reDraw(); } } diff --git a/src/widget/friendlistwidget.h b/src/widget/friendlistwidget.h index e62e21f9a..f890fa912 100644 --- a/src/widget/friendlistwidget.h +++ b/src/widget/friendlistwidget.h @@ -45,6 +45,7 @@ public: }; explicit FriendListWidget(Widget* parent, bool groupsOnTop = true); + ~FriendListWidget(); void setMode(Mode mode); Mode getMode() const; diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index 182668679..6e8c7dd8c 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -198,6 +198,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event) circle->addFriendWidget(this, FriendList::findFriend(friendId)->getStatus()); circle->setExpanded(true); Widget::getInstance()->searchCircle(circle); + Settings::getInstance().savePersonal(); } else Settings::getInstance().setFriendCircleID(id, circleActions[selectedItem]); diff --git a/ui/window/statusPanel.css b/ui/window/statusPanel.css index 9619e6d5b..6af24a53e 100644 --- a/ui/window/statusPanel.css +++ b/ui/window/statusPanel.css @@ -18,6 +18,7 @@ QToolButton { QToolButton:pressed { background-color: @themeMediumDark; border-radius: 4px; + color: white; } QToolButton::menu-indicator {