core: clearing dive data should clear undo stack

If we clear out our dive data, we also need to clear out the undo stack
because it otherwise will refer to dives that no longer exist.

The cleanest way to do that would be to do it in the same function used
to clear the dive data. Which causes us to call C++ code from C code and
really is a bit of a mess with a circular dependency between our
libraries.

But this does seem better than relying on people to remember to call
into a second function after clearing the data.

Suggested-by: Michael Andreen <michael@andreen.dev>
Suggested-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2022-04-02 14:03:45 -07:00
parent f1c1e0ccee
commit ae12000063
3 changed files with 16 additions and 1 deletions

View file

@ -23,6 +23,9 @@ void init()
void clear() void clear()
{ {
// this can get called from C code even if the Command code was never initialized
// (really only in the case of tests like TestParse)
if (undoStack)
undoStack->clear(); undoStack->clear();
} }
@ -113,3 +116,9 @@ bool placingCommand()
} }
} // namespace Command } // namespace Command
extern "C" {
void command_clear_from_C() {
Command::clear();
}
}

View file

@ -21,6 +21,7 @@
#include "trip.h" #include "trip.h"
bool autogroup = false; bool autogroup = false;
extern void command_clear_from_C();
void set_autogroup(bool value) void set_autogroup(bool value)
{ {
@ -1391,6 +1392,7 @@ void clear_dive_file_data()
clear_git_id(); clear_git_id();
reset_tank_info_table(&tank_info_table); reset_tank_info_table(&tank_info_table);
command_clear_from_C();
/* Inform frontend of reset data. This should reset all the models. */ /* Inform frontend of reset data. This should reset all the models. */
emit_reset_signal(); emit_reset_signal();

View file

@ -64,6 +64,8 @@ function(TEST NAME FILE)
subsurface_backend_shared subsurface_backend_shared
${TEST_SPECIFIC_LIBRARIES} ${TEST_SPECIFIC_LIBRARIES}
subsurface_corelib subsurface_corelib
subsurface_commands
subsurface_corelib # annoying circular references
RESOURCE_LIBRARY RESOURCE_LIBRARY
${QT_TEST_LIBRARIES} ${QT_TEST_LIBRARIES}
${SUBSURFACE_LINK_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES}
@ -99,6 +101,8 @@ add_executable(TestQML testqml.cpp)
target_link_libraries( target_link_libraries(
TestQML TestQML
subsurface_corelib subsurface_corelib
subsurface_commands
subsurface_corelib # annoying circular references
RESOURCE_LIBRARY RESOURCE_LIBRARY
${QT_TEST_LIBRARIES} ${QT_TEST_LIBRARIES}
${SUBSURFACE_LINK_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES}