Repository: TrenchBroom/TrenchBroom (2026-03-22T19:27:31Z)

Issue:
Grid size selection does not persist properly when loading new maps
When using `Open` or `Revert Document`, the Grid Size resets to 16 instead of persisting to what was set before, but the drop-down box in the toolbar still shows the old value. Ideally it'd just retain whatever grid size was set.


Discussion:

Patch:
diff --git a/lib/TbUiLib/src/MapWindow.cpp b/lib/TbUiLib/src/MapWindow.cpp
index 6a4b89a139..135e94ba4b 100644
--- a/lib/TbUiLib/src/MapWindow.cpp
+++ b/lib/TbUiLib/src/MapWindow.cpp
@@ -488,6 +488,7 @@ void MapWindow::createToolBar()
     });
 
   m_gridChoice = new QComboBox{};
+  m_gridChoice->setObjectName("MapWindow_GridChoice");
   for (int i = mdl::Grid::MinSize; i <= mdl::Grid::MaxSize; ++i)
   {
     const auto gridSize = mdl::Grid::actualSize(i);
@@ -802,6 +803,7 @@ void MapWindow::documentWasLoaded()
   updateTitle();
   updateActionState();
   updateUndoRedoActions();
+  updateToolBarWidgets();
   updateRecentDocumentsMenu();
   loadLastCompilationProfileName();
 }


Test patch:
diff --git a/lib/TbUiLib/test/CMakeLists.txt b/lib/TbUiLib/test/CMakeLists.txt
index fa3f769eee..85e653aff7 100644
--- a/lib/TbUiLib/test/CMakeLists.txt
+++ b/lib/TbUiLib/test/CMakeLists.txt
@@ -17,6 +17,7 @@ target_sources(TbUiLibTest
     ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_InputEvent.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_LaunchGameEngine.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_MapDocument.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_MapWindow.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_MoveHandleDragTracker.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_QPathUtils.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/src/tst_QPreferenceStore.cpp
diff --git a/lib/TbUiLib/test/src/tst_MapWindow.cpp b/lib/TbUiLib/test/src/tst_MapWindow.cpp
new file mode 100644
index 0000000000..7c1714563b
--- /dev/null
+++ b/lib/TbUiLib/test/src/tst_MapWindow.cpp
@@ -0,0 +1,89 @@
+/*
+ Copyright (C) 2026 Kristian Duske
+
+ This file is part of TrenchBroom.
+
+ TrenchBroom is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ TrenchBroom is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <QApplication>
+#include <QComboBox>
+
+#include "gl/GlManager.h"
+#include "mdl/GameConfigFixture.h"
+#include "mdl/Grid.h"
+#include "mdl/Map.h"
+#include "mdl/MapFormat.h"
+#include "ui/AppControllerFixture.h"
+#include "ui/CatchConfig.h"
+#include "ui/MapDocument.h"
+#include "ui/MapDocumentFixture.h"
+#include "ui/MapWindow.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+namespace tb::ui
+{
+
+TEST_CASE("MapWindow")
+{
+  auto appControllerFixture = AppControllerFixture{};
+  auto& appController = appControllerFixture.appController();
+
+  auto document = MapDocument::createDocument(
+                    appController.environmentConfig(),
+                    mdl::QuakeGameInfo,
+                    mdl::MapFormat::Valve,
+                    vm::bbox3d{8192.0},
+                    appController.taskManager(),
+                    appController.glManager().resourceManager())
+                  | kdl::value();
+
+  auto window = MapWindow{appController, std::move(document)};
+
+  SECTION("load resets grid size dropdown")
+  {
+    auto* gridChoice = window.findChild<QComboBox*>("MapWindow_GridChoice");
+    REQUIRE(gridChoice != nullptr);
+
+    const auto changedGridSize = 5;
+    const auto changedGridIndex = changedGridSize - mdl::Grid::MinSize;
+    const auto path =
+      std::filesystem::current_path() / "fixture/test/ui/MapDocument/emptyValveMap.map";
+
+    window.setGridSize(changedGridSize);
+    QApplication::processEvents();
+
+    REQUIRE(window.document().map().grid().size() == changedGridSize);
+    REQUIRE(gridChoice->currentIndex() == changedGridIndex);
+
+    REQUIRE(window.document().load(
+      appController.environmentConfig(),
+      mdl::QuakeGameInfo,
+      mdl::MapFormat::Unknown,
+      vm::bbox3d{8192.0},
+      path));
+
+    QApplication::processEvents();
+
+    const auto loadedGridSize = window.document().map().grid().size();
+    const auto loadedGridIndex = loadedGridSize - mdl::Grid::MinSize;
+
+    CHECK(loadedGridSize != changedGridSize);
+    CHECK(gridChoice->currentIndex() == loadedGridIndex);
+    CHECK(gridChoice->currentData().toInt() == loadedGridSize);
+  }
+}
+
+} // namespace tb::ui
\ No newline at end of file