diff --git a/apps/openmw/mwgui/alchemywindow.cpp b/apps/openmw/mwgui/alchemywindow.cpp index 557cb3df4a..48d1a609fc 100644 --- a/apps/openmw/mwgui/alchemywindow.cpp +++ b/apps/openmw/mwgui/alchemywindow.cpp @@ -245,8 +245,9 @@ namespace MWGui mAlchemy->clear(); mAlchemy->setAlchemist (MWMechanics::getPlayer()); - mModel = new InventoryItemModel(MWMechanics::getPlayer()); - mSortModel = new SortFilterItemModel(mModel); + auto model = std::make_unique(MWMechanics::getPlayer()); + mModel = model.get(); + mSortModel = new SortFilterItemModel(std::move(model)); mSortModel->setFilter(SortFilterItemModel::Filter_OnlyIngredients); mItemView->setModel (mSortModel); mItemView->resetScrollBars(); diff --git a/apps/openmw/mwgui/companionwindow.cpp b/apps/openmw/mwgui/companionwindow.cpp index 45c5eb9f83..a8e545f8fd 100644 --- a/apps/openmw/mwgui/companionwindow.cpp +++ b/apps/openmw/mwgui/companionwindow.cpp @@ -123,9 +123,9 @@ void CompanionWindow::setPtr(const MWWorld::Ptr& npc) { mPtr = npc; updateEncumbranceBar(); - - mModel = new CompanionItemModel(npc); - mSortModel = new SortFilterItemModel(mModel); + auto model = std::make_unique(npc); + mModel = model.get(); + mSortModel = new SortFilterItemModel(std::move(model)); mFilterEdit->setCaption(std::string()); mItemView->setModel(mSortModel); mItemView->resetScrollBars(); diff --git a/apps/openmw/mwgui/container.cpp b/apps/openmw/mwgui/container.cpp index 8478fc46e0..4f48f70636 100644 --- a/apps/openmw/mwgui/container.cpp +++ b/apps/openmw/mwgui/container.cpp @@ -129,25 +129,26 @@ namespace MWGui bool loot = mPtr.getClass().isActor() && mPtr.getClass().getCreatureStats(mPtr).isDead(); + std::unique_ptr model; if (mPtr.getClass().hasInventoryStore(mPtr)) { if (mPtr.getClass().isNpc() && !loot && !lootAnyway) { // we are stealing stuff - mModel = new PickpocketItemModel(mPtr, new InventoryItemModel(container), + model = std::make_unique(mPtr, std::make_unique(container), !mPtr.getClass().getCreatureStats(mPtr).getKnockedDown()); } else - mModel = new InventoryItemModel(container); + model = std::make_unique(container); } else { - mModel = new ContainerItemModel(container); + model = std::make_unique(container); } mDisposeCorpseButton->setVisible(loot); - - mSortModel = new SortFilterItemModel(mModel); + mModel = model.get(); + mSortModel = new SortFilterItemModel(std::move(model)); mItemView->setModel (mSortModel); mItemView->resetScrollBars(); diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index 1dfaa0d047..297bfb04b1 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -122,12 +122,13 @@ namespace MWGui void InventoryWindow::updatePlayer() { mPtr = MWBase::Environment::get().getWorld ()->getPlayerPtr(); - mTradeModel = new TradeItemModel(new InventoryItemModel(mPtr), MWWorld::Ptr()); + auto tradeModel = std::make_unique(std::make_unique(mPtr), MWWorld::Ptr()); + mTradeModel = tradeModel.get(); if (mSortModel) // reuse existing SortModel when possible to keep previous category/filter settings - mSortModel->setSourceModel(mTradeModel); + mSortModel->setSourceModel(std::move(tradeModel)); else - mSortModel = new SortFilterItemModel(mTradeModel); + mSortModel = new SortFilterItemModel(std::move(tradeModel)); mSortModel->setNameFilter(mFilterEdit->getCaption()); @@ -776,7 +777,7 @@ namespace MWGui ItemModel::ModelIndex selected = -1; // not using mSortFilterModel as we only need sorting, not filtering - SortFilterItemModel model(new InventoryItemModel(player)); + SortFilterItemModel model(std::make_unique(player)); model.setSortByType(false); model.update(); if (model.getItemCount() == 0) diff --git a/apps/openmw/mwgui/itemmodel.cpp b/apps/openmw/mwgui/itemmodel.cpp index 165e9ba2aa..55b2994199 100644 --- a/apps/openmw/mwgui/itemmodel.cpp +++ b/apps/openmw/mwgui/itemmodel.cpp @@ -79,17 +79,6 @@ namespace MWGui return true; } - - ProxyItemModel::ProxyItemModel() - : mSourceModel(nullptr) - { - } - - ProxyItemModel::~ProxyItemModel() - { - delete mSourceModel; - } - bool ProxyItemModel::allowedToUseItems() const { return mSourceModel->allowedToUseItems(); @@ -134,18 +123,9 @@ namespace MWGui return mSourceModel->getIndex(item); } - void ProxyItemModel::setSourceModel(ItemModel *sourceModel) + void ProxyItemModel::setSourceModel(std::unique_ptr sourceModel) { - if (mSourceModel == sourceModel) - return; - - if (mSourceModel) - { - delete mSourceModel; - mSourceModel = nullptr; - } - - mSourceModel = sourceModel; + mSourceModel = std::move(sourceModel); } void ProxyItemModel::onClose() diff --git a/apps/openmw/mwgui/itemmodel.hpp b/apps/openmw/mwgui/itemmodel.hpp index d538a040db..9ddcaffc50 100644 --- a/apps/openmw/mwgui/itemmodel.hpp +++ b/apps/openmw/mwgui/itemmodel.hpp @@ -1,6 +1,8 @@ #ifndef MWGUI_ITEM_MODEL_H #define MWGUI_ITEM_MODEL_H +#include + #include "../mwworld/ptr.hpp" namespace MWGui @@ -87,8 +89,8 @@ namespace MWGui class ProxyItemModel : public ItemModel { public: - ProxyItemModel(); - virtual ~ProxyItemModel(); + ProxyItemModel() = default; + virtual ~ProxyItemModel() = default; bool allowedToUseItems() const override; @@ -101,14 +103,14 @@ namespace MWGui ModelIndex getIndex (const ItemStack &item) override; /// @note Takes ownership of the passed pointer. - void setSourceModel(ItemModel* sourceModel); + void setSourceModel(std::unique_ptr sourceModel); ModelIndex mapToSource (ModelIndex index); ModelIndex mapFromSource (ModelIndex index); bool usesContainer(const MWWorld::Ptr& container) override; protected: - ItemModel* mSourceModel; + std::unique_ptr mSourceModel; }; } diff --git a/apps/openmw/mwgui/itemselection.cpp b/apps/openmw/mwgui/itemselection.cpp index 23f1398eaa..4674e9bf5e 100644 --- a/apps/openmw/mwgui/itemselection.cpp +++ b/apps/openmw/mwgui/itemselection.cpp @@ -13,7 +13,6 @@ namespace MWGui ItemSelectionDialog::ItemSelectionDialog(const std::string &label) : WindowModal("openmw_itemselection_dialog.layout") , mSortModel(nullptr) - , mModel(nullptr) { getWidget(mItemView, "ItemView"); mItemView->eventItemClicked += MyGUI::newDelegate(this, &ItemSelectionDialog::onSelectedItem); @@ -37,8 +36,7 @@ namespace MWGui void ItemSelectionDialog::openContainer(const MWWorld::Ptr& container) { - mModel = new InventoryItemModel(container); - mSortModel = new SortFilterItemModel(mModel); + mSortModel = new SortFilterItemModel(std::make_unique(container)); mItemView->setModel(mSortModel); mItemView->resetScrollBars(); } diff --git a/apps/openmw/mwgui/itemselection.hpp b/apps/openmw/mwgui/itemselection.hpp index 6132bac7af..33b06c08cf 100644 --- a/apps/openmw/mwgui/itemselection.hpp +++ b/apps/openmw/mwgui/itemselection.hpp @@ -36,7 +36,6 @@ namespace MWGui private: ItemView* mItemView; SortFilterItemModel* mSortModel; - InventoryItemModel* mModel; void onSelectedItem(int index); diff --git a/apps/openmw/mwgui/pickpocketitemmodel.cpp b/apps/openmw/mwgui/pickpocketitemmodel.cpp index 976485c23a..aca1b7392b 100644 --- a/apps/openmw/mwgui/pickpocketitemmodel.cpp +++ b/apps/openmw/mwgui/pickpocketitemmodel.cpp @@ -17,11 +17,11 @@ namespace MWGui { - PickpocketItemModel::PickpocketItemModel(const MWWorld::Ptr& actor, ItemModel *sourceModel, bool hideItems) + PickpocketItemModel::PickpocketItemModel(const MWWorld::Ptr& actor, std::unique_ptr sourceModel, bool hideItems) : mActor(actor), mPickpocketDetected(false) { MWWorld::Ptr player = MWMechanics::getPlayer(); - mSourceModel = sourceModel; + mSourceModel = std::move(sourceModel); float chance = player.getClass().getSkill(player, ESM::Skill::Sneak); mSourceModel->update(); diff --git a/apps/openmw/mwgui/pickpocketitemmodel.hpp b/apps/openmw/mwgui/pickpocketitemmodel.hpp index e28af73d78..ec18f13609 100644 --- a/apps/openmw/mwgui/pickpocketitemmodel.hpp +++ b/apps/openmw/mwgui/pickpocketitemmodel.hpp @@ -10,7 +10,7 @@ namespace MWGui class PickpocketItemModel : public ProxyItemModel { public: - PickpocketItemModel (const MWWorld::Ptr& thief, ItemModel* sourceModel, bool hideItems=true); + PickpocketItemModel(const MWWorld::Ptr& thief, std::unique_ptr sourceModel, bool hideItems = true); bool allowedToUseItems() const override; ItemStack getItem (ModelIndex index) override; diff --git a/apps/openmw/mwgui/recharge.cpp b/apps/openmw/mwgui/recharge.cpp index 4c6a80540d..cbebb0cc68 100644 --- a/apps/openmw/mwgui/recharge.cpp +++ b/apps/openmw/mwgui/recharge.cpp @@ -47,7 +47,7 @@ void Recharge::onOpen() { center(); - SortFilterItemModel * model = new SortFilterItemModel(new InventoryItemModel(MWMechanics::getPlayer())); + SortFilterItemModel * model = new SortFilterItemModel(std::make_unique(MWMechanics::getPlayer())); model->setFilter(SortFilterItemModel::Filter_OnlyRechargable); mBox->setModel(model); diff --git a/apps/openmw/mwgui/repair.cpp b/apps/openmw/mwgui/repair.cpp index c3d26ee6f0..9917e03f9b 100644 --- a/apps/openmw/mwgui/repair.cpp +++ b/apps/openmw/mwgui/repair.cpp @@ -46,7 +46,7 @@ void Repair::onOpen() { center(); - SortFilterItemModel * model = new SortFilterItemModel(new InventoryItemModel(MWMechanics::getPlayer())); + SortFilterItemModel * model = new SortFilterItemModel(std::make_unique(MWMechanics::getPlayer())); model->setFilter(SortFilterItemModel::Filter_OnlyRepairable); mRepairBox->setModel(model); diff --git a/apps/openmw/mwgui/sortfilteritemmodel.cpp b/apps/openmw/mwgui/sortfilteritemmodel.cpp index 650257736f..09ecdb54e5 100644 --- a/apps/openmw/mwgui/sortfilteritemmodel.cpp +++ b/apps/openmw/mwgui/sortfilteritemmodel.cpp @@ -152,14 +152,12 @@ namespace namespace MWGui { - SortFilterItemModel::SortFilterItemModel(ItemModel *sourceModel) + SortFilterItemModel::SortFilterItemModel(std::unique_ptr sourceModel) : mCategory(Category_All) , mFilter(0) , mSortByType(true) - , mNameFilter("") - , mEffectFilter("") { - mSourceModel = sourceModel; + mSourceModel = std::move(sourceModel); } bool SortFilterItemModel::allowedToUseItems() const diff --git a/apps/openmw/mwgui/sortfilteritemmodel.hpp b/apps/openmw/mwgui/sortfilteritemmodel.hpp index 64a01f71bd..2640737a89 100644 --- a/apps/openmw/mwgui/sortfilteritemmodel.hpp +++ b/apps/openmw/mwgui/sortfilteritemmodel.hpp @@ -9,7 +9,7 @@ namespace MWGui class SortFilterItemModel : public ProxyItemModel { public: - SortFilterItemModel (ItemModel* sourceModel); + SortFilterItemModel(std::unique_ptr sourceModel); void update() override; diff --git a/apps/openmw/mwgui/tradeitemmodel.cpp b/apps/openmw/mwgui/tradeitemmodel.cpp index 667832b05d..22973ee91a 100644 --- a/apps/openmw/mwgui/tradeitemmodel.cpp +++ b/apps/openmw/mwgui/tradeitemmodel.cpp @@ -10,10 +10,10 @@ namespace MWGui { - TradeItemModel::TradeItemModel(ItemModel *sourceModel, const MWWorld::Ptr& merchant) + TradeItemModel::TradeItemModel(std::unique_ptr sourceModel, const MWWorld::Ptr& merchant) : mMerchant(merchant) { - mSourceModel = sourceModel; + mSourceModel = std::move(sourceModel); } bool TradeItemModel::allowedToUseItems() const diff --git a/apps/openmw/mwgui/tradeitemmodel.hpp b/apps/openmw/mwgui/tradeitemmodel.hpp index 53b616aeda..5a22bab8d3 100644 --- a/apps/openmw/mwgui/tradeitemmodel.hpp +++ b/apps/openmw/mwgui/tradeitemmodel.hpp @@ -13,7 +13,7 @@ namespace MWGui class TradeItemModel : public ProxyItemModel { public: - TradeItemModel (ItemModel* sourceModel, const MWWorld::Ptr& merchant); + TradeItemModel(std::unique_ptr sourceModel, const MWWorld::Ptr& merchant); bool allowedToUseItems() const override; diff --git a/apps/openmw/mwgui/tradewindow.cpp b/apps/openmw/mwgui/tradewindow.cpp index 7fe7cc2877..5832bab830 100644 --- a/apps/openmw/mwgui/tradewindow.cpp +++ b/apps/openmw/mwgui/tradewindow.cpp @@ -114,8 +114,9 @@ namespace MWGui std::vector worldItems; MWBase::Environment::get().getWorld()->getItemsOwnedBy(actor, worldItems); - mTradeModel = new TradeItemModel(new ContainerItemModel(itemSources, worldItems), mPtr); - mSortModel = new SortFilterItemModel(mTradeModel); + auto tradeModel = std::make_unique(std::make_unique(itemSources, worldItems), mPtr); + mTradeModel = tradeModel.get(); + mSortModel = new SortFilterItemModel(std::move(tradeModel)); mItemView->setModel (mSortModel); mItemView->resetScrollBars();