From e9432d3b6590090961b869fdc260d3ec462ada39 Mon Sep 17 00:00:00 2001 From: pajlada Date: Mon, 29 May 2023 16:37:55 +0200 Subject: [PATCH] Clean up GenericListModel (#4661) Co-authored-by: Daniel Sage --- src/widgets/listview/GenericListModel.cpp | 21 ++++++++++++++++----- src/widgets/listview/GenericListModel.hpp | 16 +++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/widgets/listview/GenericListModel.cpp b/src/widgets/listview/GenericListModel.cpp index 9163b2b31..dc98fb827 100644 --- a/src/widgets/listview/GenericListModel.cpp +++ b/src/widgets/listview/GenericListModel.cpp @@ -2,12 +2,12 @@ namespace chatterino { -GenericListModel::GenericListModel(QWidget *parent) +GenericListModel::GenericListModel(QObject *parent) : QAbstractListModel(parent) { } -int GenericListModel::rowCount(const QModelIndex &parent) const +int GenericListModel::rowCount(const QModelIndex & /*parent*/) const { return this->items_.size(); } @@ -15,12 +15,16 @@ int GenericListModel::rowCount(const QModelIndex &parent) const QVariant GenericListModel::data(const QModelIndex &index, int /* role */) const { if (!index.isValid()) - return QVariant(); + { + return {}; + } if (index.row() >= this->items_.size()) - return QVariant(); + { + return {}; + } - auto item = this->items_[index.row()].get(); + auto *item = this->items_[index.row()].get(); // See https://stackoverflow.com/a/44503822 . return QVariant::fromValue(static_cast(item)); } @@ -37,7 +41,9 @@ void GenericListModel::addItem(std::unique_ptr item) void GenericListModel::clear() { if (this->items_.empty()) + { return; + } // {begin,end}RemoveRows needs to be called to notify attached views this->beginRemoveRows(QModelIndex(), 0, this->items_.size() - 1); @@ -48,4 +54,9 @@ void GenericListModel::clear() this->endRemoveRows(); } +void GenericListModel::reserve(size_t capacity) +{ + this->items_.reserve(capacity); +} + } // namespace chatterino diff --git a/src/widgets/listview/GenericListModel.hpp b/src/widgets/listview/GenericListModel.hpp index 0f2fe2175..10106730b 100644 --- a/src/widgets/listview/GenericListModel.hpp +++ b/src/widgets/listview/GenericListModel.hpp @@ -3,23 +3,24 @@ #include "widgets/listview/GenericListItem.hpp" #include -#include +#include #include +#include namespace chatterino { class GenericListModel : public QAbstractListModel { public: - GenericListModel(QWidget *parent = nullptr); + GenericListModel(QObject *parent = nullptr); /** * @brief Reimplements QAbstractItemModel::rowCount. * - * @return number of items currrently present in this model + * @return number of items currently present in this model */ - int rowCount(const QModelIndex &parent = QModelIndex()) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; /** * @brief Reimplements QAbstractItemModel::data. Currently, the role parameter @@ -30,7 +31,7 @@ public: * * @return GenericListItem * (wrapped as QVariant) at index */ - QVariant data(const QModelIndex &index, int role) const; + QVariant data(const QModelIndex &index, int role) const override; /** * @brief Add an item to this QuickSwitcherModel. It will be displayed in @@ -50,6 +51,11 @@ public: */ void clear(); + /** + * @brief Increases the capacity of the list model. + */ + void reserve(size_t capacity); + private: std::vector> items_; };