1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00

Use std::unique_ptr in ItemView

This commit is contained in:
Evil Eye 2022-08-31 19:03:45 +02:00
parent 837183ec79
commit 59fab4c9e2
8 changed files with 28 additions and 32 deletions

View File

@ -247,12 +247,13 @@ namespace MWGui
auto model = std::make_unique<InventoryItemModel>(MWMechanics::getPlayer()); auto model = std::make_unique<InventoryItemModel>(MWMechanics::getPlayer());
mModel = model.get(); mModel = model.get();
mSortModel = new SortFilterItemModel(std::move(model)); auto sortModel = std::make_unique<SortFilterItemModel>(std::move(model));
mSortModel = sortModel.get();
mSortModel->setFilter(SortFilterItemModel::Filter_OnlyIngredients); mSortModel->setFilter(SortFilterItemModel::Filter_OnlyIngredients);
mItemView->setModel (mSortModel); mItemView->setModel(std::move(sortModel));
mItemView->resetScrollBars(); mItemView->resetScrollBars();
mNameEdit->setCaption(""); mNameEdit->setCaption({});
mBrewCountEdit->setValue(1); mBrewCountEdit->setValue(1);
int index = 0; int index = 0;

View File

@ -125,9 +125,10 @@ void CompanionWindow::setPtr(const MWWorld::Ptr& npc)
updateEncumbranceBar(); updateEncumbranceBar();
auto model = std::make_unique<CompanionItemModel>(npc); auto model = std::make_unique<CompanionItemModel>(npc);
mModel = model.get(); mModel = model.get();
mSortModel = new SortFilterItemModel(std::move(model)); auto sortModel = std::make_unique<SortFilterItemModel>(std::move(model));
mFilterEdit->setCaption(std::string()); mSortModel = sortModel.get();
mItemView->setModel(mSortModel); mFilterEdit->setCaption({});
mItemView->setModel(std::move(sortModel));
mItemView->resetScrollBars(); mItemView->resetScrollBars();
setTitle(npc.getClass().getName(npc)); setTitle(npc.getClass().getName(npc));

View File

@ -148,9 +148,10 @@ namespace MWGui
mDisposeCorpseButton->setVisible(loot); mDisposeCorpseButton->setVisible(loot);
mModel = model.get(); mModel = model.get();
mSortModel = new SortFilterItemModel(std::move(model)); auto sortModel = std::make_unique<SortFilterItemModel>(std::move(model));
mSortModel = sortModel.get();
mItemView->setModel (mSortModel); mItemView->setModel(std::move(sortModel));
mItemView->resetScrollBars(); mItemView->resetScrollBars();
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mCloseButton); MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mCloseButton);

View File

@ -128,12 +128,14 @@ namespace MWGui
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(std::move(tradeModel)); mSortModel->setSourceModel(std::move(tradeModel));
else else
mSortModel = new SortFilterItemModel(std::move(tradeModel)); {
auto sortModel = std::make_unique<SortFilterItemModel>(std::move(tradeModel));
mSortModel = sortModel.get();
mItemView->setModel(std::move(sortModel));
}
mSortModel->setNameFilter(mFilterEdit->getCaption()); mSortModel->setNameFilter(mFilterEdit->getCaption());
mItemView->setModel(mSortModel);
mFilterAll->setStateSelected(true); mFilterAll->setStateSelected(true);
mFilterWeapon->setStateSelected(false); mFilterWeapon->setStateSelected(false);
mFilterApparel->setStateSelected(false); mFilterApparel->setStateSelected(false);

View File

@ -36,8 +36,9 @@ namespace MWGui
void ItemSelectionDialog::openContainer(const MWWorld::Ptr& container) void ItemSelectionDialog::openContainer(const MWWorld::Ptr& container)
{ {
mSortModel = new SortFilterItemModel(std::make_unique<InventoryItemModel>(container)); auto sortModel = std::make_unique<SortFilterItemModel>(std::make_unique<InventoryItemModel>(container));
mItemView->setModel(mSortModel); mSortModel = sortModel.get();
mItemView->setModel(std::move(sortModel));
mItemView->resetScrollBars(); mItemView->resetScrollBars();
} }

View File

@ -16,23 +16,13 @@ namespace MWGui
{ {
ItemView::ItemView() ItemView::ItemView()
: mModel(nullptr) : mScrollView(nullptr)
, mScrollView(nullptr)
{ {
} }
ItemView::~ItemView() void ItemView::setModel(std::unique_ptr<ItemModel> model)
{ {
delete mModel; mModel = std::move(model);
}
void ItemView::setModel(ItemModel *model)
{
if (mModel == model)
return;
delete mModel;
mModel = model;
update(); update();
} }
@ -114,7 +104,7 @@ void ItemView::update()
ItemWidget* itemWidget = dragArea->createWidget<ItemWidget>("MW_ItemIcon", ItemWidget* itemWidget = dragArea->createWidget<ItemWidget>("MW_ItemIcon",
MyGUI::IntCoord(0, 0, 42, 42), MyGUI::Align::Default); MyGUI::IntCoord(0, 0, 42, 42), MyGUI::Align::Default);
itemWidget->setUserString("ToolTipType", "ItemModelIndex"); itemWidget->setUserString("ToolTipType", "ItemModelIndex");
itemWidget->setUserData(std::make_pair(i, mModel)); itemWidget->setUserData(std::make_pair(i, mModel.get()));
ItemWidget::ItemState state = ItemWidget::None; ItemWidget::ItemState state = ItemWidget::None;
if (item.mType == ItemStack::Type_Barter) if (item.mType == ItemStack::Type_Barter)
state = ItemWidget::Barter; state = ItemWidget::Barter;

View File

@ -13,13 +13,12 @@ namespace MWGui
MYGUI_RTTI_DERIVED(ItemView) MYGUI_RTTI_DERIVED(ItemView)
public: public:
ItemView(); ItemView();
~ItemView() override;
/// Register needed components with MyGUI's factory manager /// Register needed components with MyGUI's factory manager
static void registerComponents (); static void registerComponents ();
/// Takes ownership of \a model /// Takes ownership of \a model
void setModel (ItemModel* model); void setModel(std::unique_ptr<ItemModel> model);
typedef MyGUI::delegates::CMultiDelegate1<ItemModel::ModelIndex> EventHandle_ModelIndex; typedef MyGUI::delegates::CMultiDelegate1<ItemModel::ModelIndex> EventHandle_ModelIndex;
typedef MyGUI::delegates::CMultiDelegate0 EventHandle_Void; typedef MyGUI::delegates::CMultiDelegate0 EventHandle_Void;
@ -44,7 +43,7 @@ namespace MWGui
void onSelectedBackground (MyGUI::Widget* sender); void onSelectedBackground (MyGUI::Widget* sender);
void onMouseWheelMoved(MyGUI::Widget* _sender, int _rel); void onMouseWheelMoved(MyGUI::Widget* _sender, int _rel);
ItemModel* mModel; std::unique_ptr<ItemModel> mModel;
MyGUI::ScrollView* mScrollView; MyGUI::ScrollView* mScrollView;
}; };

View File

@ -116,8 +116,9 @@ namespace MWGui
auto tradeModel = std::make_unique<TradeItemModel>(std::make_unique<ContainerItemModel>(itemSources, worldItems), mPtr); auto tradeModel = std::make_unique<TradeItemModel>(std::make_unique<ContainerItemModel>(itemSources, worldItems), mPtr);
mTradeModel = tradeModel.get(); mTradeModel = tradeModel.get();
mSortModel = new SortFilterItemModel(std::move(tradeModel)); auto sortModel = std::make_unique<SortFilterItemModel>(std::move(tradeModel));
mItemView->setModel (mSortModel); mSortModel = sortModel.get();
mItemView->setModel(std::move(sortModel));
mItemView->resetScrollBars(); mItemView->resetScrollBars();
updateLabels(); updateLabels();