From 664d2da2bb5f0334802fdff2671b4959ddd1d3e9 Mon Sep 17 00:00:00 2001 From: Federico Montesino Pouzols Date: Thu, 16 Oct 2014 17:46:46 +0100 Subject: [PATCH] recent files menu added into the main app menu, re #10335 --- .../MantidPlot/src/ApplicationWindow.cpp | 100 ++++++++++++++---- .../Mantid/MantidPlot/src/ApplicationWindow.h | 19 +++- 2 files changed, 96 insertions(+), 23 deletions(-) diff --git a/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp b/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp index 9bef2715e1ac..45b97f127fc0 100644 --- a/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp +++ b/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp @@ -551,7 +551,8 @@ void ApplicationWindow::init(bool factorySettings, const QStringList& args) connect(lv, SIGNAL(itemRenamed(Q3ListViewItem *, int, const QString &)), this, SLOT(renameWindow(Q3ListViewItem *, int, const QString &))); - connect(recent, SIGNAL(activated(int)), this, SLOT(openRecentProject(int))); + connect(recentProjectsMenu, SIGNAL(activated(int)), this, SLOT(openRecentProject(int))); + connect(recentFilesMenu, SIGNAL(activated(int)), this, SLOT(openRecentFile(int))); //apply user settings updateAppFonts(); @@ -1155,6 +1156,7 @@ void ApplicationWindow::insertTranslatedStrings() formatToolBar->setWindowTitle(tr("Format")); fileMenu->changeItem(recentMenuID, tr("&Recent Projects")); + fileMenu->changeItem(recentFilesMenuID, tr("R&ecent Files")); translateActionsStrings(); customMenu(activeWindow()); @@ -1166,8 +1168,9 @@ void ApplicationWindow::initMainMenu() fileMenu->setObjectName("fileMenu"); connect(fileMenu, SIGNAL(aboutToShow()), this, SLOT(fileMenuAboutToShow())); - recent = new QMenu(this); newMenu = new QMenu(this); + recentProjectsMenu = new QMenu(this); + recentFilesMenu = new QMenu(this); newMenu->setObjectName("newMenu"); exportPlotMenu = new QMenu(this); exportPlotMenu->setObjectName("exportPlotMenu"); @@ -4461,9 +4464,31 @@ ApplicationWindow* ApplicationWindow::open(const QString& fn, bool factorySettin return app; } +void ApplicationWindow::openRecentFile(int index) +{ + QString fn = recentFilesMenu->text(index); + int pos = fn.find(" ",0); + fn = fn.right(fn.length()-pos-1); + + QFile f(fn); + if (!f.exists()){ + QMessageBox::critical(this, tr("MantidPlot - File Open Error"),//Mantid + tr("The file: %1

does not exist anymore!" + "

It will be removed from the list.").arg(fn)); + + recentFiles.remove(fn); + updateRecentFilesList(fn); + return; + } + + loadDataFileByName(fn); + updateRecentFilesList(fn); + saveSettings(); // save new list of recent files +} + void ApplicationWindow::openRecentProject(int index) { - QString fn = recent->text(index); + QString fn = recentProjectsMenu->text(index); int pos = fn.find(" ",0); fn = fn.right(fn.length()-pos-1); @@ -4502,6 +4527,7 @@ void ApplicationWindow::openRecentProject(int index) } } + ApplicationWindow* ApplicationWindow::openProject(const QString& fn, bool factorySettings, bool newProject) { ApplicationWindow *app = this; @@ -5039,6 +5065,7 @@ void ApplicationWindow::readSettings() show_windows_policy = (ShowWindowsPolicy)settings.value("/ShowWindowsPolicy", ApplicationWindow::ActiveFolder).toInt(); recentProjects = settings.value("/RecentProjects").toStringList(); + recentFiles = settings.value("/RecentFiles").toStringList(); //Follows an ugly hack added by Ion in order to fix Qt4 porting issues //(only needed on Windows due to a Qt bug?) #ifdef Q_OS_WIN @@ -5049,9 +5076,18 @@ void ApplicationWindow::readSettings() if (s.remove(QRegExp("\\s")).isEmpty()) recentProjects = QStringList(); } + + if (!recentFiles.isEmpty() && recentFiles[0].contains("^e")) + recentFiles = recentFiles[0].split("^e", QString::SkipEmptyParts); + else if (recentFiles.count() == 1){ + QString s = recentFiles[0]; + if (s.remove(QRegExp("\\s")).isEmpty()) + recentFiles = QStringList(); + } #endif updateRecentProjectsList(); + updateRecentFilesList(); changeAppStyle(settings.value("/Style", appStyle).toString()); autoSave = settings.value("/AutoSave", false).toBool(); @@ -5513,6 +5549,7 @@ void ApplicationWindow::saveSettings() settings.setValue("/Language", appLanguage); settings.setValue("/ShowWindowsPolicy", show_windows_policy); settings.setValue("/RecentProjects", recentProjects); + settings.setValue("/RecentFiles", recentFiles); settings.setValue("/Style", appStyle); settings.setValue("/AutoSave", autoSave); settings.setValue("/AutoSaveTime", autoSaveTime); @@ -6209,7 +6246,7 @@ void ApplicationWindow::savetoNexusFile() savedatainNexusFormat(wsName,fileName.toStdString()); MantidQt::API::AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(fileName).absoluteDir().path()); - + updateRecentFilesList(fileName); } } @@ -6218,18 +6255,27 @@ void ApplicationWindow::loadDataFile() // Ask user for file QString fn = QFileDialog::getOpenFileName( 0, tr("Mantidplot - Open file to load"), AlgorithmInputHistory::Instance().getPreviousDirectory()); if(fn != "") { - QFileInfo fnInfo(fn); - AlgorithmInputHistory::Instance().setPreviousDirectory(fnInfo.absoluteDir().path()); - if( fnInfo.suffix() == "py") - { // We have a python file, just load it into script window - loadScript( fn, true ); - } - else if(mantidUI) - { // Run Load algorithm on file - QHash params; - params["Filename"] = fn; - mantidUI->showAlgorithmDialog(QString("Load"),params); - } + loadDataFileByName(fn); + } + updateRecentFilesList(fn); + saveSettings(); // save new list of recent files +} + +void ApplicationWindow::loadDataFileByName(QString fn) +{ + QFileInfo fnInfo(fn); + AlgorithmInputHistory::Instance().setPreviousDirectory(fnInfo.absoluteDir().path()); + if( fnInfo.suffix() == "py") + { + // We have a python file, just load it into script window + loadScript( fn, true ); + } + else if(mantidUI) + { + // Run Load algorithm on file + QHash params; + params["Filename"] = fn; + mantidUI->showAlgorithmDialog(QString("Load"),params); } } @@ -9280,7 +9326,9 @@ void ApplicationWindow::fileMenuAboutToShow() openMenu->addAction(actionOpenProj); openMenu->addAction(actionLoadFile); - recentMenuID = fileMenu->insertItem(tr("&Recent Projects"), recent); + recentMenuID = fileMenu->insertItem(tr("&Recent Projects"), recentProjectsMenu); + + recentFilesMenuID = fileMenu->insertItem(tr("R&ecent Files"), recentFilesMenu); fileMenu->insertSeparator(); fileMenu->addAction(actionManageDirs); @@ -14438,10 +14486,24 @@ void ApplicationWindow::updateRecentProjectsList() while ((int)recentProjects.size() > MaxRecentProjects) recentProjects.pop_back(); - recent->clear(); + recentProjectsMenu->clear(); for (int i = 0; i<(int)recentProjects.size(); i++ ) - recent->insertItem("&" + QString::number(i+1) + " " + recentProjects[i]); + recentProjectsMenu->insertItem("&" + QString::number(i+1) + " " + recentProjects[i]); +} + +void ApplicationWindow::updateRecentFilesList(QString fname) +{ + if (!fname.isEmpty()) { + recentFiles.remove(fname); + recentFiles.push_front(fname); + } + while ((int)recentFiles.size() > MaxRecentFiles) + recentFiles.pop_back(); + + recentFilesMenu->clear(); + for (int i = 0; i<(int)recentFiles.size(); i++ ) + recentFilesMenu->insertItem("&" + QString::number(i+1) + " " + recentFiles[i]); } void ApplicationWindow::translateCurveHor() diff --git a/Code/Mantid/MantidPlot/src/ApplicationWindow.h b/Code/Mantid/MantidPlot/src/ApplicationWindow.h index 5de461c6140a..1b3ea9c2f2e3 100644 --- a/Code/Mantid/MantidPlot/src/ApplicationWindow.h +++ b/Code/Mantid/MantidPlot/src/ApplicationWindow.h @@ -213,8 +213,13 @@ public slots: ApplicationWindow* open(const QString& fn, bool factorySettings = false, bool newProject = true); ApplicationWindow* openProject(const QString& fn, bool factorySettings = false, bool newProject = true); ApplicationWindow* importOPJ(const QString& fn, bool factorySettings = false, bool newProject = true); - /// Load mantid data files using generic load algorithm + /// Load mantid data files using generic load algorithm, opening user dialog void loadDataFile(); + /// Load mantid data files (generic load algorithm) + void loadDataFileByName(QString fn); + /// Open from the list of recent files + void openRecentFile(int index); + /** * \brief Create a new project from a data file. * @@ -619,6 +624,7 @@ public slots: Graph3D* openSurfacePlot(ApplicationWindow* app, const QStringList &lst); Graph* openGraph(ApplicationWindow* app, MultiLayer *plot, const QStringList &list); void openRecentProject(int index); + //@} //! \name Table Tools @@ -852,8 +858,12 @@ public slots: void custom3DGrids(int grids); //@} + //! Updates the recent projects list and menu (but doesn't insert anything) void updateRecentProjectsList(); + //! Inserts file name in the list of recent files (if fname not empty) and updates the "recent files" menu + void updateRecentFilesList(QString fname=""); + //! connected to the done(bool) signal of the http object //void receivedVersionFile(bool error); //! called when the user presses the actionCheckUpdates @@ -1279,6 +1289,7 @@ public slots: //! Describes which windows are shown when the folder becomes the current folder ShowWindowsPolicy show_windows_policy; enum {MaxRecentProjects = 10}; + enum {MaxRecentFiles = MaxRecentProjects}; //! File version code used when opening project files (= maj * 100 + min * 10 + patch) int d_file_version; @@ -1327,9 +1338,9 @@ public slots: QColor tableBkgdColor, tableTextColor, tableHeaderColor; QString projectname, columnSeparator, helpFilePath, appLanguage; QString configFilePath, fitPluginsPath, fitModelsPath, asciiDirPath, imagesDirPath, scriptsDirPath; - int ignoredLines, savingTimerId, plot3DResolution, recentMenuID; + int ignoredLines, savingTimerId, plot3DResolution, recentMenuID, recentFilesMenuID; bool renameColumns, strip_spaces, simplify_spaces; - QStringList recentProjects; + QStringList recentProjects, recentFiles; bool saved, showPlot3DProjection, showPlot3DLegend, orthogonal3DPlots, autoscale3DPlots; QStringList plot3DColors, locales; QStringList functions; //user-defined functions; @@ -1413,7 +1424,7 @@ public slots: QWidget* catalogSearch; - QMenu *windowsMenu, *foldersMenu, *view, *graph, *fileMenu, *format, *edit, *recent, *interfaceMenu; + QMenu *windowsMenu, *foldersMenu, *view, *graph, *fileMenu, *format, *edit, *recentProjectsMenu, *recentFilesMenu, *interfaceMenu; QMenu *help, *plot2DMenu, *analysisMenu, *multiPeakMenu, *icat; QMenu *matrixMenu, *plot3DMenu, *plotDataMenu, *tablesDepend, *scriptingMenu;