Browse code

Fixed crash when opening a map without tilesets

In particular this crash would always happen when creating a new map.
The crash was caused by TerrainBrush::mapDocumentChanged, which did not
handle the case where the new map did not have any tilesets correctly.

Thorbjørn Lindeijer authored on 04/06/2012 17:54:16
Showing 1 changed files
... ...
@@ -195,24 +195,27 @@ void TerrainBrush::languageChanged()
195 195
     setShortcut(QKeySequence(tr("T")));
196 196
 }
197 197
 
198
+static Terrain *firstTerrain(MapDocument *mapDocument)
199
+{
200
+    if (!mapDocument)
201
+        return 0;
202
+
203
+    foreach (Tileset *tileset, mapDocument->map()->tilesets())
204
+        if (tileset->terrainCount() > 0)
205
+            return tileset->terrain(0);
206
+
207
+    return 0;
208
+}
209
+
198 210
 void TerrainBrush::mapDocumentChanged(MapDocument *oldDocument,
199
-                                    MapDocument *newDocument)
211
+                                      MapDocument *newDocument)
200 212
 {
201 213
     AbstractTileTool::mapDocumentChanged(oldDocument, newDocument);
202 214
 
203 215
     // Reset the brush, since it probably became invalid
204 216
     brushItem()->setTileRegion(QRegion());
205 217
 
206
-    // if the new document has any terrains defined, choose the first rather than the 'none' terrain
207
-    if (newDocument) {
208
-        Tileset *tileset = newDocument->map()->tilesets().at(0);
209
-        if (tileset && tileset->terrainCount() > 0)
210
-            setTerrain(tileset->terrain(0));
211
-        else
212
-            setTerrain(NULL);
213
-    } else {
214
-        mTerrain = NULL;
215
-    }
218
+    setTerrain(firstTerrain(newDocument));
216 219
 }
217 220
 
218 221
 void TerrainBrush::setTerrain(const Terrain *terrain)