mirror of
				https://github.com/OpenMW/openmw.git
				synced 2025-10-25 11:26:37 +00:00 
			
		
		
		
	Use std::unique_ptr in ProxyItemModel
This commit is contained in:
		
							parent
							
								
									fd720c0a7b
								
							
						
					
					
						commit
						837183ec79
					
				
					 17 changed files with 40 additions and 59 deletions
				
			
		|  | @ -245,8 +245,9 @@ namespace MWGui | ||||||
|         mAlchemy->clear(); |         mAlchemy->clear(); | ||||||
|         mAlchemy->setAlchemist (MWMechanics::getPlayer()); |         mAlchemy->setAlchemist (MWMechanics::getPlayer()); | ||||||
| 
 | 
 | ||||||
|         mModel = new InventoryItemModel(MWMechanics::getPlayer()); |         auto model = std::make_unique<InventoryItemModel>(MWMechanics::getPlayer()); | ||||||
|         mSortModel = new SortFilterItemModel(mModel); |         mModel = model.get(); | ||||||
|  |         mSortModel = new SortFilterItemModel(std::move(model)); | ||||||
|         mSortModel->setFilter(SortFilterItemModel::Filter_OnlyIngredients); |         mSortModel->setFilter(SortFilterItemModel::Filter_OnlyIngredients); | ||||||
|         mItemView->setModel (mSortModel); |         mItemView->setModel (mSortModel); | ||||||
|         mItemView->resetScrollBars(); |         mItemView->resetScrollBars(); | ||||||
|  |  | ||||||
|  | @ -123,9 +123,9 @@ void CompanionWindow::setPtr(const MWWorld::Ptr& npc) | ||||||
| { | { | ||||||
|     mPtr = npc; |     mPtr = npc; | ||||||
|     updateEncumbranceBar(); |     updateEncumbranceBar(); | ||||||
| 
 |     auto model = std::make_unique<CompanionItemModel>(npc); | ||||||
|     mModel = new CompanionItemModel(npc); |     mModel = model.get(); | ||||||
|     mSortModel = new SortFilterItemModel(mModel); |     mSortModel = new SortFilterItemModel(std::move(model)); | ||||||
|     mFilterEdit->setCaption(std::string()); |     mFilterEdit->setCaption(std::string()); | ||||||
|     mItemView->setModel(mSortModel); |     mItemView->setModel(mSortModel); | ||||||
|     mItemView->resetScrollBars(); |     mItemView->resetScrollBars(); | ||||||
|  |  | ||||||
|  | @ -129,25 +129,26 @@ namespace MWGui | ||||||
| 
 | 
 | ||||||
|         bool loot = mPtr.getClass().isActor() && mPtr.getClass().getCreatureStats(mPtr).isDead(); |         bool loot = mPtr.getClass().isActor() && mPtr.getClass().getCreatureStats(mPtr).isDead(); | ||||||
| 
 | 
 | ||||||
|  |         std::unique_ptr<ItemModel> model; | ||||||
|         if (mPtr.getClass().hasInventoryStore(mPtr)) |         if (mPtr.getClass().hasInventoryStore(mPtr)) | ||||||
|         { |         { | ||||||
|             if (mPtr.getClass().isNpc() && !loot && !lootAnyway) |             if (mPtr.getClass().isNpc() && !loot && !lootAnyway) | ||||||
|             { |             { | ||||||
|                 // we are stealing stuff
 |                 // 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()); |                                                  !mPtr.getClass().getCreatureStats(mPtr).getKnockedDown()); | ||||||
|             } |             } | ||||||
|             else |             else | ||||||
|                 mModel = new InventoryItemModel(container); |                 model = std::make_unique<InventoryItemModel>(container); | ||||||
|         } |         } | ||||||
|         else |         else | ||||||
|         { |         { | ||||||
|             mModel = new ContainerItemModel(container); |             model = std::make_unique<ContainerItemModel>(container); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         mDisposeCorpseButton->setVisible(loot); |         mDisposeCorpseButton->setVisible(loot); | ||||||
| 
 |         mModel = model.get(); | ||||||
|         mSortModel = new SortFilterItemModel(mModel); |         mSortModel = new SortFilterItemModel(std::move(model)); | ||||||
| 
 | 
 | ||||||
|         mItemView->setModel (mSortModel); |         mItemView->setModel (mSortModel); | ||||||
|         mItemView->resetScrollBars(); |         mItemView->resetScrollBars(); | ||||||
|  |  | ||||||
|  | @ -122,12 +122,13 @@ namespace MWGui | ||||||
|     void InventoryWindow::updatePlayer() |     void InventoryWindow::updatePlayer() | ||||||
|     { |     { | ||||||
|         mPtr = MWBase::Environment::get().getWorld ()->getPlayerPtr(); |         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
 |         if (mSortModel) // reuse existing SortModel when possible to keep previous category/filter settings
 | ||||||
|             mSortModel->setSourceModel(mTradeModel); |             mSortModel->setSourceModel(std::move(tradeModel)); | ||||||
|         else |         else | ||||||
|             mSortModel = new SortFilterItemModel(mTradeModel); |             mSortModel = new SortFilterItemModel(std::move(tradeModel)); | ||||||
| 
 | 
 | ||||||
|         mSortModel->setNameFilter(mFilterEdit->getCaption()); |         mSortModel->setNameFilter(mFilterEdit->getCaption()); | ||||||
| 
 | 
 | ||||||
|  | @ -776,7 +777,7 @@ namespace MWGui | ||||||
| 
 | 
 | ||||||
|         ItemModel::ModelIndex selected = -1; |         ItemModel::ModelIndex selected = -1; | ||||||
|         // not using mSortFilterModel as we only need sorting, not filtering
 |         // 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.setSortByType(false); | ||||||
|         model.update(); |         model.update(); | ||||||
|         if (model.getItemCount() == 0) |         if (model.getItemCount() == 0) | ||||||
|  |  | ||||||
|  | @ -79,17 +79,6 @@ namespace MWGui | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     ProxyItemModel::ProxyItemModel() |  | ||||||
|         : mSourceModel(nullptr) |  | ||||||
|     { |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     ProxyItemModel::~ProxyItemModel() |  | ||||||
|     { |  | ||||||
|         delete mSourceModel; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     bool ProxyItemModel::allowedToUseItems() const |     bool ProxyItemModel::allowedToUseItems() const | ||||||
|     { |     { | ||||||
|         return mSourceModel->allowedToUseItems(); |         return mSourceModel->allowedToUseItems(); | ||||||
|  | @ -134,18 +123,9 @@ namespace MWGui | ||||||
|         return mSourceModel->getIndex(item); |         return mSourceModel->getIndex(item); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void ProxyItemModel::setSourceModel(ItemModel *sourceModel) |     void ProxyItemModel::setSourceModel(std::unique_ptr<ItemModel> sourceModel) | ||||||
|     { |     { | ||||||
|         if (mSourceModel == sourceModel) |         mSourceModel = std::move(sourceModel); | ||||||
|             return; |  | ||||||
| 
 |  | ||||||
|         if (mSourceModel) |  | ||||||
|         { |  | ||||||
|             delete mSourceModel; |  | ||||||
|             mSourceModel = nullptr; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         mSourceModel = sourceModel; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void ProxyItemModel::onClose() |     void ProxyItemModel::onClose() | ||||||
|  |  | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| #ifndef MWGUI_ITEM_MODEL_H | #ifndef MWGUI_ITEM_MODEL_H | ||||||
| #define MWGUI_ITEM_MODEL_H | #define MWGUI_ITEM_MODEL_H | ||||||
| 
 | 
 | ||||||
|  | #include <memory> | ||||||
|  | 
 | ||||||
| #include "../mwworld/ptr.hpp" | #include "../mwworld/ptr.hpp" | ||||||
| 
 | 
 | ||||||
| namespace MWGui | namespace MWGui | ||||||
|  | @ -87,8 +89,8 @@ namespace MWGui | ||||||
|     class ProxyItemModel : public ItemModel |     class ProxyItemModel : public ItemModel | ||||||
|     { |     { | ||||||
|     public: |     public: | ||||||
|         ProxyItemModel(); |         ProxyItemModel() = default; | ||||||
|         virtual ~ProxyItemModel(); |         virtual ~ProxyItemModel() = default; | ||||||
| 
 | 
 | ||||||
|         bool allowedToUseItems() const override; |         bool allowedToUseItems() const override; | ||||||
| 
 | 
 | ||||||
|  | @ -101,14 +103,14 @@ namespace MWGui | ||||||
|         ModelIndex getIndex (const ItemStack &item) override; |         ModelIndex getIndex (const ItemStack &item) override; | ||||||
| 
 | 
 | ||||||
|         /// @note Takes ownership of the passed pointer.
 |         /// @note Takes ownership of the passed pointer.
 | ||||||
|         void setSourceModel(ItemModel* sourceModel); |         void setSourceModel(std::unique_ptr<ItemModel> sourceModel); | ||||||
| 
 | 
 | ||||||
|         ModelIndex mapToSource (ModelIndex index); |         ModelIndex mapToSource (ModelIndex index); | ||||||
|         ModelIndex mapFromSource (ModelIndex index); |         ModelIndex mapFromSource (ModelIndex index); | ||||||
| 
 | 
 | ||||||
|         bool usesContainer(const MWWorld::Ptr& container) override; |         bool usesContainer(const MWWorld::Ptr& container) override; | ||||||
|     protected: |     protected: | ||||||
|         ItemModel* mSourceModel; |         std::unique_ptr<ItemModel> mSourceModel; | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -13,7 +13,6 @@ namespace MWGui | ||||||
|     ItemSelectionDialog::ItemSelectionDialog(const std::string &label) |     ItemSelectionDialog::ItemSelectionDialog(const std::string &label) | ||||||
|         : WindowModal("openmw_itemselection_dialog.layout") |         : WindowModal("openmw_itemselection_dialog.layout") | ||||||
|         , mSortModel(nullptr) |         , mSortModel(nullptr) | ||||||
|         , mModel(nullptr) |  | ||||||
|     { |     { | ||||||
|         getWidget(mItemView, "ItemView"); |         getWidget(mItemView, "ItemView"); | ||||||
|         mItemView->eventItemClicked += MyGUI::newDelegate(this, &ItemSelectionDialog::onSelectedItem); |         mItemView->eventItemClicked += MyGUI::newDelegate(this, &ItemSelectionDialog::onSelectedItem); | ||||||
|  | @ -37,8 +36,7 @@ namespace MWGui | ||||||
| 
 | 
 | ||||||
|     void ItemSelectionDialog::openContainer(const MWWorld::Ptr& container) |     void ItemSelectionDialog::openContainer(const MWWorld::Ptr& container) | ||||||
|     { |     { | ||||||
|         mModel = new InventoryItemModel(container); |         mSortModel = new SortFilterItemModel(std::make_unique<InventoryItemModel>(container)); | ||||||
|         mSortModel = new SortFilterItemModel(mModel); |  | ||||||
|         mItemView->setModel(mSortModel); |         mItemView->setModel(mSortModel); | ||||||
|         mItemView->resetScrollBars(); |         mItemView->resetScrollBars(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -36,7 +36,6 @@ namespace MWGui | ||||||
|     private: |     private: | ||||||
|         ItemView* mItemView; |         ItemView* mItemView; | ||||||
|         SortFilterItemModel* mSortModel; |         SortFilterItemModel* mSortModel; | ||||||
|         InventoryItemModel* mModel; |  | ||||||
| 
 | 
 | ||||||
|         void onSelectedItem(int index); |         void onSelectedItem(int index); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -17,11 +17,11 @@ | ||||||
| namespace MWGui | 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) |         : mActor(actor), mPickpocketDetected(false) | ||||||
|     { |     { | ||||||
|         MWWorld::Ptr player = MWMechanics::getPlayer(); |         MWWorld::Ptr player = MWMechanics::getPlayer(); | ||||||
|         mSourceModel = sourceModel; |         mSourceModel = std::move(sourceModel); | ||||||
|         float chance = player.getClass().getSkill(player, ESM::Skill::Sneak); |         float chance = player.getClass().getSkill(player, ESM::Skill::Sneak); | ||||||
| 
 | 
 | ||||||
|         mSourceModel->update(); |         mSourceModel->update(); | ||||||
|  |  | ||||||
|  | @ -10,7 +10,7 @@ namespace MWGui | ||||||
|     class PickpocketItemModel : public ProxyItemModel |     class PickpocketItemModel : public ProxyItemModel | ||||||
|     { |     { | ||||||
|     public: |     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; |         bool allowedToUseItems() const override; | ||||||
|         ItemStack getItem (ModelIndex index) override; |         ItemStack getItem (ModelIndex index) override; | ||||||
|  |  | ||||||
|  | @ -47,7 +47,7 @@ void Recharge::onOpen() | ||||||
| { | { | ||||||
|     center(); |     center(); | ||||||
| 
 | 
 | ||||||
|     SortFilterItemModel * model = new SortFilterItemModel(new InventoryItemModel(MWMechanics::getPlayer())); |     SortFilterItemModel * model = new SortFilterItemModel(std::make_unique<InventoryItemModel>(MWMechanics::getPlayer())); | ||||||
|     model->setFilter(SortFilterItemModel::Filter_OnlyRechargable); |     model->setFilter(SortFilterItemModel::Filter_OnlyRechargable); | ||||||
|     mBox->setModel(model); |     mBox->setModel(model); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -46,7 +46,7 @@ void Repair::onOpen() | ||||||
| { | { | ||||||
|     center(); |     center(); | ||||||
| 
 | 
 | ||||||
|     SortFilterItemModel * model = new SortFilterItemModel(new InventoryItemModel(MWMechanics::getPlayer())); |     SortFilterItemModel * model = new SortFilterItemModel(std::make_unique<InventoryItemModel>(MWMechanics::getPlayer())); | ||||||
|     model->setFilter(SortFilterItemModel::Filter_OnlyRepairable); |     model->setFilter(SortFilterItemModel::Filter_OnlyRepairable); | ||||||
|     mRepairBox->setModel(model); |     mRepairBox->setModel(model); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -152,14 +152,12 @@ namespace | ||||||
| namespace MWGui | namespace MWGui | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
|     SortFilterItemModel::SortFilterItemModel(ItemModel *sourceModel) |     SortFilterItemModel::SortFilterItemModel(std::unique_ptr<ItemModel> sourceModel) | ||||||
|         : mCategory(Category_All) |         : mCategory(Category_All) | ||||||
|         , mFilter(0) |         , mFilter(0) | ||||||
|         , mSortByType(true) |         , mSortByType(true) | ||||||
|         , mNameFilter("") |  | ||||||
|         , mEffectFilter("") |  | ||||||
|     { |     { | ||||||
|         mSourceModel = sourceModel; |         mSourceModel = std::move(sourceModel); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     bool SortFilterItemModel::allowedToUseItems() const |     bool SortFilterItemModel::allowedToUseItems() const | ||||||
|  |  | ||||||
|  | @ -9,7 +9,7 @@ namespace MWGui | ||||||
|     class SortFilterItemModel : public ProxyItemModel |     class SortFilterItemModel : public ProxyItemModel | ||||||
|     { |     { | ||||||
|     public: |     public: | ||||||
|         SortFilterItemModel (ItemModel* sourceModel); |         SortFilterItemModel(std::unique_ptr<ItemModel> sourceModel); | ||||||
| 
 | 
 | ||||||
|         void update() override; |         void update() override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -10,10 +10,10 @@ | ||||||
| namespace MWGui | namespace MWGui | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
|     TradeItemModel::TradeItemModel(ItemModel *sourceModel, const MWWorld::Ptr& merchant) |     TradeItemModel::TradeItemModel(std::unique_ptr<ItemModel> sourceModel, const MWWorld::Ptr& merchant) | ||||||
|         : mMerchant(merchant) |         : mMerchant(merchant) | ||||||
|     { |     { | ||||||
|         mSourceModel = sourceModel; |         mSourceModel = std::move(sourceModel); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     bool TradeItemModel::allowedToUseItems() const |     bool TradeItemModel::allowedToUseItems() const | ||||||
|  |  | ||||||
|  | @ -13,7 +13,7 @@ namespace MWGui | ||||||
|     class TradeItemModel : public ProxyItemModel |     class TradeItemModel : public ProxyItemModel | ||||||
|     { |     { | ||||||
|     public: |     public: | ||||||
|         TradeItemModel (ItemModel* sourceModel, const MWWorld::Ptr& merchant); |         TradeItemModel(std::unique_ptr<ItemModel> sourceModel, const MWWorld::Ptr& merchant); | ||||||
| 
 | 
 | ||||||
|         bool allowedToUseItems() const override; |         bool allowedToUseItems() const override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -114,8 +114,9 @@ namespace MWGui | ||||||
|         std::vector<MWWorld::Ptr> worldItems; |         std::vector<MWWorld::Ptr> worldItems; | ||||||
|         MWBase::Environment::get().getWorld()->getItemsOwnedBy(actor, worldItems); |         MWBase::Environment::get().getWorld()->getItemsOwnedBy(actor, worldItems); | ||||||
| 
 | 
 | ||||||
|         mTradeModel = new TradeItemModel(new ContainerItemModel(itemSources, worldItems), mPtr); |         auto tradeModel = std::make_unique<TradeItemModel>(std::make_unique<ContainerItemModel>(itemSources, worldItems), mPtr); | ||||||
|         mSortModel = new SortFilterItemModel(mTradeModel); |         mTradeModel = tradeModel.get(); | ||||||
|  |         mSortModel = new SortFilterItemModel(std::move(tradeModel)); | ||||||
|         mItemView->setModel (mSortModel); |         mItemView->setModel (mSortModel); | ||||||
|         mItemView->resetScrollBars(); |         mItemView->resetScrollBars(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue