# HG changeset patch
# User insilmaril
# Date 1163684586 0
# Node ID f364b13047bab090948c3e3419a58b97bfbd5a47
# Parent  4bdeec4f279ec449cf2b2367e6730587ee0e40df
Fixed OS X crash when trying to load recent map

diff -r 4bdeec4f279e -r f364b13047ba mainwindow.cpp
--- a/mainwindow.cpp	Thu Nov 16 10:07:12 2006 +0000
+++ b/mainwindow.cpp	Thu Nov 16 13:43:06 2006 +0000
@@ -130,8 +130,6 @@
 		settings.setValue( p,s);
 
 	
-	maxLastMaps=9;
-
 	// Create tab widget which holds the maps
 	tabWidget= new QTabWidget (this);
 	connect( tabWidget, SIGNAL( currentChanged( QWidget * ) ), 
@@ -190,17 +188,6 @@
 	settings.setValue( "/mapeditor/editmode/useFlagGroups",actionSettingsUseFlagGroups->isOn() );
 	settings.setValue( "/export/useHideExport",actionSettingsUseHideExport->isOn() );
 
-	QString s;
-	int maps=lastMaps.count();
-	settings.setValue( "/lastMaps/number",maps );
-	for (int i=1;i<=maps;i++)
-	{
-		s=QString("/lastMaps/map-%1").arg(i);
-		if (!s.isEmpty() && i<=maxLastMaps) 
-			settings.setValue (s, lastMaps.at(i-1));
-	}
-
-
 	// To make the texteditor save its settings, call the destructor
 	delete (textEditor);
 
@@ -1526,45 +1513,33 @@
 	actionFormatBackColor->addTo( canvasContextMenu );
 
 	// Menu for last opened files
-	// Read settings initially
-	QString s;
-	int j=settings.readNumEntry( "/lastMaps/number",0);
-	for (int i=1;i<=j;i++)
+	// Create actions
+	for (int i = 0; i < MaxRecentFiles; ++i) 
 	{
-		s=settings.value(QString("/lastMaps/map-%1").arg(i),"").toString();
-		if (!s.isEmpty() && j<=maxLastMaps) 
-			lastMaps.append(s);
-	}
-	setupLastMapsMenu();
-	connect( fileLastMapsMenu, SIGNAL( triggered(QAction *) ), this, SLOT( fileLoadLast(QAction*) ) );
+        recentFileActs[i] = new QAction(this);
+        recentFileActs[i]->setVisible(false);
+        fileLastMapsMenu->addAction(recentFileActs[i]);
+        connect(recentFileActs[i], SIGNAL(triggered()),
+                this, SLOT(fileLoadRecent()));
+    }
+	setupRecentMapsMenu();
 }
 
-void Main::setupLastMapsMenu()
+void Main::setupRecentMapsMenu()
 {
-	// Remove double entries
-	QStringList::Iterator it=lastMaps.begin();
-	QStringList::Iterator jt;
-	while (it!=lastMaps.end() )
-	{
-		jt=it;
-		++jt;
-		while (jt!=lastMaps.end() )
-		{
-			if (*it == *jt)		
-				jt=lastMaps.remove(jt);
-			else	
-				jt++;
-		}
-		it++;
-	}	
-
-	// Limit length of list to maxLastMaps
-	while ((int)(lastMaps.count()) > maxLastMaps) lastMaps.pop_back();
-	
-	// build Menu from lastMaps string list
-	fileLastMapsMenu->clear();
-	for (it = lastMaps.begin(); it != lastMaps.end(); ++it ) 
-		fileLastMapsMenu->addAction (*it );
+    QStringList files = settings.value("/mainwindow/recentFileList").toStringList();
+
+    int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
+
+    for (int i = 0; i < numRecentFiles; ++i) {
+        //QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
+        QString text = tr("&%1 %2").arg(i + 1).arg(files[i]);
+        recentFileActs[i]->setText(text);
+        recentFileActs[i]->setData(files[i]);
+        recentFileActs[i]->setVisible(true);
+    }
+    for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
+        recentFileActs[j]->setVisible(false);
 }
 
 void Main::hideEvent (QHideEvent * )
@@ -1853,8 +1828,7 @@
 				{
 					// Only append to lastMaps if not loaded from a tmpDir
 					// e.g. imported bookmarks are in a tmpDir
-					lastMaps.prepend(me->getFilePath() );
-					setupLastMapsMenu();
+					addRecentMap(me->getFilePath() );
 				}
 				actionFilePrint->setEnabled (true);
 			}	
@@ -1911,9 +1885,25 @@
 	fileLoad (NewMap);
 }
 
-void Main::fileLoadLast(QAction *a)
+void Main::fileLoadRecent()
 {
-	fileLoad(lastMaps.at(fileLastMapsMenu->actions().indexOf(a)) ,NewMap);
+    QAction *action = qobject_cast<QAction *>(sender());
+    if (action)
+        fileLoad (action->data().toString(), NewMap);
+}
+
+void Main::addRecentMap (const QString &fileName)
+{
+
+    QStringList files = settings.value("/mainwindow/recentFileList").toStringList();
+    files.removeAll(fileName);
+    files.prepend(fileName);
+    while (files.size() > MaxRecentFiles)
+        files.removeLast();
+
+    settings.setValue("/mainwindow/recentFileList", files);
+
+	setupRecentMapsMenu();
 }
 
 void Main::fileSave(const SaveMode &savemode)
@@ -2024,8 +2014,7 @@
 		statusBar()->message( 
 			tr("Saved  %1").arg(me->getFilePath()), 
 			statusbarTime );
-		lastMaps.prepend(me->getFilePath() );
-		setupLastMapsMenu();
+		addRecentMap (me->getFilePath() );
 	} else		
 		statusBar()->message( 
 			tr("Couldn't save ").arg(me->getFilePath()), 
diff -r 4bdeec4f279e -r f364b13047ba mainwindow.h
--- a/mainwindow.h	Thu Nov 16 10:07:12 2006 +0000
+++ b/mainwindow.h	Thu Nov 16 13:43:06 2006 +0000
@@ -39,7 +39,7 @@
     void setupTestActions();
     void setupHelpActions();
     void setupContextMenus();
-	void setupLastMapsMenu();
+	void setupRecentMapsMenu();
 	void hideEvent (QHideEvent * );
 	void showEvent (QShowEvent * );
 	bool reallyWriteDirectory(const QString&);
@@ -53,7 +53,8 @@
     ErrorCode fileLoad(QString ,const LoadMode &);
     void fileLoad(const LoadMode &);
     void fileLoad();
-	void fileLoadLast(QAction *);
+	void fileLoadRecent();
+	void addRecentMap (const QString &);
     void fileSave(const SaveMode & );
     void fileSave();
     void fileSaveAs(const SaveMode &);
@@ -183,8 +184,6 @@
 private:
 	QTabWidget *tabWidget;
 	FindWindow *findWindow;
-	QStringList lastMaps;
-	int maxLastMaps;
 	QProcess *procBrowser;
 
 	QStringList imageTypes;
@@ -196,6 +195,10 @@
 
 	QColor currentColor;
 
+	QMenu *recentFilesMenu;
+	enum { MaxRecentFiles = 9 };
+    QAction *recentFileActs[MaxRecentFiles];
+
 	QAction* actionFileSave;
 	QAction* actionFilePrint;
 	QAction* actionEditUndo;
diff -r 4bdeec4f279e -r f364b13047ba mapeditor.cpp
--- a/mapeditor.cpp	Thu Nov 16 10:07:12 2006 +0000
+++ b/mapeditor.cpp	Thu Nov 16 13:43:06 2006 +0000
@@ -40,11 +40,7 @@
 extern QMenu* branchXLinksContextMenuEdit;
 extern QMenu* branchXLinksContextMenuFollow;
 extern QMenu* floatimageContextMenu;
-extern QMenu* saveImageFormatMenu;
 extern QMenu* canvasContextMenu;
-extern QMenu* lastMapsMenu;
-extern QMenu* importMenu;
-extern QMenu* exportMenu;
 
 
 extern Settings settings;
diff -r 4bdeec4f279e -r f364b13047ba tex/vym.changelog
--- a/tex/vym.changelog	Thu Nov 16 10:07:12 2006 +0000
+++ b/tex/vym.changelog	Thu Nov 16 13:43:06 2006 +0000
@@ -1,3 +1,8 @@
+-------------------------------------------------------------------
+Thu Nov 16 14:41:44 CET 2006 - uwedr
+
+- Bugfix: New recent file handling, fixes crash on Mac OS X 
+
 -------------------------------------------------------------------
 Tue Nov 14 10:00:12 CET 2006 - uwedr
 
diff -r 4bdeec4f279e -r f364b13047ba version.h
--- a/version.h	Thu Nov 16 10:07:12 2006 +0000
+++ b/version.h	Thu Nov 16 13:43:06 2006 +0000
@@ -5,7 +5,7 @@
 
 #define __VYM "VYM"
 #define __VYM_VERSION "1.8.59"
-#define __BUILD_DATE "November 14, 2006"
+#define __BUILD_DATE "November 16, 2006"
 
 
 bool checkVersion(const QString &);