Editor: Fix object list update on object removal

directional-lights
Sven Eberhardt 2016-10-15 11:14:50 -04:00
parent 978afbc4a2
commit c44a776090
7 changed files with 28 additions and 15 deletions

View File

@ -355,6 +355,7 @@ bool C4Console::FileRecord()
void C4Console::ClearPointers(C4Object *pObj)
{
EditCursor.ClearPointers(pObj);
C4ConsoleGUI::ClearPointers(pObj);
}
void C4Console::Default()

View File

@ -97,6 +97,7 @@ public:
void ClearGamePointers();
void EnsureDefinitionListInitialized();
void CloseConsoleWindow();
void ClearPointers(class C4Object *obj);
// TODO some qt editor stuff is in state and needs to be public
// Once other editors are removed, C4ConsoleGUI, C4ConsoleQt and C4ConsoleQtState should be reorganized
@ -112,6 +113,7 @@ public:
void OnStartGame() { }
void EnsureDefinitionListInitialized() { }
void CloseConsoleWindow() {}
void ClearPointers(class C4Object *obj) {}
#endif
void SetCursor(Cursor cursor);

View File

@ -19,6 +19,7 @@
#include "C4Include.h"
#include "editor/C4ConsoleQtState.h"
#include "editor/C4ConsoleQtDefinitionListViewer.h"
#include "editor/C4ConsoleQtObjectListViewer.h"
#include "editor/C4Console.h"
#include "editor/C4ConsoleGUI.h"
#include "landscape/C4Texture.h"
@ -372,6 +373,11 @@ void C4ConsoleGUI::CloseConsoleWindow()
if (state && state->window) state->window->close();
}
void C4ConsoleGUI::ClearPointers(class C4Object *obj)
{
if (state && state->object_list_model) state->object_list_model->Invalidate();
}
void C4ToolsDlg::UpdateToolCtrls()
{
// Set selected drawing tool

View File

@ -44,22 +44,23 @@ C4ConsoleQtObjectListModel::~C4ConsoleQtObjectListModel()
void C4ConsoleQtObjectListModel::Invalidate()
{
// Force redraw
// Kill any dead object pointers and force redraw
emit layoutAboutToBeChanged();
QModelIndexList list = this->persistentIndexList();
for (auto idx : list)
{
if (idx.internalPointer())
{
QModelIndex new_index = GetModelIndexByItem(static_cast<C4PropList *>(idx.internalPointer()));
this->changePersistentIndex(idx, new_index);
}
}
QModelIndex topLeft = index(0, 0, QModelIndex());
QModelIndex bottomRight = index(last_row_count, columnCount() - 1, QModelIndex());
emit dataChanged(topLeft, bottomRight);
emit layoutChanged();
}
void C4ConsoleQtObjectListModel::OnItemRemoved(C4PropList *p)
{
QModelIndexList list = this->persistentIndexList();
for (auto idx : list)
if (idx.internalPointer() == p)
this->changePersistentIndex(idx, QModelIndex());
Invalidate();
}
int C4ConsoleQtObjectListModel::rowCount(const QModelIndex & parent) const
{
int result = 0;
@ -69,7 +70,7 @@ int C4ConsoleQtObjectListModel::rowCount(const QModelIndex & parent) const
C4PropList *parent_item = GetItemByModelIndex(parent);
if (!parent_item) return result;
C4Object *obj = parent_item->GetObject();
if (!obj) return result;
if (!obj || !obj->Status) return result;
// Contained objects
for (C4Object *contents : obj->Contents)
if (contents && contents->Status)
@ -192,9 +193,11 @@ QModelIndex C4ConsoleQtObjectListModel::parent(const QModelIndex &index) const
QModelIndex C4ConsoleQtObjectListModel::GetModelIndexByItem(C4PropList *item) const
{
// Deduce position in model list from item pointer
// No position for invalid items
if (!item) return QModelIndex();
C4Object *obj = item->GetObject();
if (obj && !obj->Status) return QModelIndex();
// Default position for Global and Scenario object
C4Object *obj;
int row;
if (item == &::ScriptEngine)
{
@ -204,8 +207,9 @@ QModelIndex C4ConsoleQtObjectListModel::GetModelIndexByItem(C4PropList *item) co
{
row = IDX_Scenario;
}
else if ((obj = item->GetObject()))
else if (obj)
{
// Positions for object items
row = IDX_Objects;
const C4ObjectList *list = &::Objects;
if (obj->Contained) list = &(obj->Contained->Contents);

View File

@ -47,7 +47,6 @@ public:
// Refresh object list on next redraw
void Invalidate();
void OnItemRemoved(C4PropList *p);
QModelIndex GetModelIndexByItem(class C4PropList *item) const;
C4PropList *GetItemByModelIndex(const QModelIndex &index) const;

View File

@ -53,7 +53,7 @@ void C4ObjectListDlg::Update(C4EditCursorSelection &rSelection)
// Could do some crazy fine-grained updates. But updating is cheap enough...
void C4ObjectListDlg::OnObjectRemove(C4ObjectList * pList, C4ObjectLink * pLnk)
{
if (view_model) view_model->OnItemRemoved(pLnk->Obj);
if (view_model) view_model->Invalidate();
}
void C4ObjectListDlg::OnObjectAdded(C4ObjectList * pList, C4ObjectLink * pLnk)

View File

@ -742,6 +742,7 @@ void C4ObjectList::DeleteObjects()
{
C4Object *pObj = First->Obj;
if (pObj->Status) Game.ClearPointers(pObj); // clear pointers to removed objects that weren't deleted (game end or section change)
pObj->Status = C4OS_DELETED;
Remove(pObj);
delete pObj;
}