1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-18 13:12:50 +00:00

Use std::unique_ptr in ProxyItemModel

This commit is contained in:
Evil Eye 2022-08-31 18:49:50 +02:00
parent fd720c0a7b
commit 837183ec79
17 changed files with 40 additions and 59 deletions

View File

@ -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<InventoryItemModel>(MWMechanics::getPlayer());
mModel = model.get();
mSortModel = new SortFilterItemModel(std::move(model));
mSortModel->setFilter(SortFilterItemModel::Filter_OnlyIngredients);
mItemView->setModel (mSortModel);
mItemView->resetScrollBars();

View File

@ -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<CompanionItemModel>(npc);
mModel = model.get();
mSortModel = new SortFilterItemModel(std::move(model));
mFilterEdit->setCaption(std::string());
mItemView->setModel(mSortModel);
mItemView->resetScrollBars();

View File

@ -129,25 +129,26 @@ namespace MWGui
bool loot = mPtr.getClass().isActor() && mPtr.getClass().getCreatureStats(mPtr).isDead();
std::unique_ptr<ItemModel> 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<PickpocketItemModel>(mPtr, std::make_unique<InventoryItemModel>(container),
!mPtr.getClass().getCreatureStats(mPtr).getKnockedDown());
}
else
mModel = new InventoryItemModel(container);
model = std::make_unique<InventoryItemModel>(container);
}
else
{
mModel = new ContainerItemModel(container);
model = std::make_unique<ContainerItemModel>(container);
}
mDisposeCorpseButton->setVisible(loot);
mSortModel = new SortFilterItemModel(mModel);
mModel = model.get();
mSortModel = new SortFilterItemModel(std::move(model));
mItemView->setModel (mSortModel);
mItemView->resetScrollBars();

View File

@ -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<TradeItemModel>(std::make_unique<InventoryItemModel>(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<InventoryItemModel>(player));
model.setSortByType(false);
model.update();
if (model.getItemCount() == 0)

View File

@ -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<ItemModel> sourceModel)
{
if (mSourceModel == sourceModel)
return;
if (mSourceModel)
{
delete mSourceModel;
mSourceModel = nullptr;
}
mSourceModel = sourceModel;
mSourceModel = std::move(sourceModel);
}
void ProxyItemModel::onClose()

View File

@ -1,6 +1,8 @@
#ifndef MWGUI_ITEM_MODEL_H
#define MWGUI_ITEM_MODEL_H
#include <memory>
#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<ItemModel> sourceModel);
ModelIndex mapToSource (ModelIndex index);
ModelIndex mapFromSource (ModelIndex index);
bool usesContainer(const MWWorld::Ptr& container) override;
protected:
ItemModel* mSourceModel;
std::unique_ptr<ItemModel> mSourceModel;
};
}

View File

@ -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<InventoryItemModel>(container));
mItemView->setModel(mSortModel);
mItemView->resetScrollBars();
}

View File

@ -36,7 +36,6 @@ namespace MWGui
private:
ItemView* mItemView;
SortFilterItemModel* mSortModel;
InventoryItemModel* mModel;
void onSelectedItem(int index);

View File

@ -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<ItemModel> 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();

View File

@ -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<ItemModel> sourceModel, bool hideItems = true);
bool allowedToUseItems() const override;
ItemStack getItem (ModelIndex index) override;

View File

@ -47,7 +47,7 @@ void Recharge::onOpen()
{
center();
SortFilterItemModel * model = new SortFilterItemModel(new InventoryItemModel(MWMechanics::getPlayer()));
SortFilterItemModel * model = new SortFilterItemModel(std::make_unique<InventoryItemModel>(MWMechanics::getPlayer()));
model->setFilter(SortFilterItemModel::Filter_OnlyRechargable);
mBox->setModel(model);

View File

@ -46,7 +46,7 @@ void Repair::onOpen()
{
center();
SortFilterItemModel * model = new SortFilterItemModel(new InventoryItemModel(MWMechanics::getPlayer()));
SortFilterItemModel * model = new SortFilterItemModel(std::make_unique<InventoryItemModel>(MWMechanics::getPlayer()));
model->setFilter(SortFilterItemModel::Filter_OnlyRepairable);
mRepairBox->setModel(model);

View File

@ -152,14 +152,12 @@ namespace
namespace MWGui
{
SortFilterItemModel::SortFilterItemModel(ItemModel *sourceModel)
SortFilterItemModel::SortFilterItemModel(std::unique_ptr<ItemModel> sourceModel)
: mCategory(Category_All)
, mFilter(0)
, mSortByType(true)
, mNameFilter("")
, mEffectFilter("")
{
mSourceModel = sourceModel;
mSourceModel = std::move(sourceModel);
}
bool SortFilterItemModel::allowedToUseItems() const

View File

@ -9,7 +9,7 @@ namespace MWGui
class SortFilterItemModel : public ProxyItemModel
{
public:
SortFilterItemModel (ItemModel* sourceModel);
SortFilterItemModel(std::unique_ptr<ItemModel> sourceModel);
void update() override;

View File

@ -10,10 +10,10 @@
namespace MWGui
{
TradeItemModel::TradeItemModel(ItemModel *sourceModel, const MWWorld::Ptr& merchant)
TradeItemModel::TradeItemModel(std::unique_ptr<ItemModel> sourceModel, const MWWorld::Ptr& merchant)
: mMerchant(merchant)
{
mSourceModel = sourceModel;
mSourceModel = std::move(sourceModel);
}
bool TradeItemModel::allowedToUseItems() const

View File

@ -13,7 +13,7 @@ namespace MWGui
class TradeItemModel : public ProxyItemModel
{
public:
TradeItemModel (ItemModel* sourceModel, const MWWorld::Ptr& merchant);
TradeItemModel(std::unique_ptr<ItemModel> sourceModel, const MWWorld::Ptr& merchant);
bool allowedToUseItems() const override;

View File

@ -114,8 +114,9 @@ namespace MWGui
std::vector<MWWorld::Ptr> 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<TradeItemModel>(std::make_unique<ContainerItemModel>(itemSources, worldItems), mPtr);
mTradeModel = tradeModel.get();
mSortModel = new SortFilterItemModel(std::move(tradeModel));
mItemView->setModel (mSortModel);
mItemView->resetScrollBars();