Skip to content

Commit

Permalink
recent files menu added into the main app menu, re #10335
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMPouzols committed Oct 16, 2014
1 parent 4c0f9c9 commit 664d2da
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 23 deletions.
100 changes: 81 additions & 19 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -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");
Expand Down Expand Up @@ -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: <b> %1 </b> <p>does not exist anymore!"
"<p>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);

Expand Down Expand Up @@ -4502,6 +4527,7 @@ void ApplicationWindow::openRecentProject(int index)
}
}


ApplicationWindow* ApplicationWindow::openProject(const QString& fn, bool factorySettings, bool newProject)
{
ApplicationWindow *app = this;
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -6209,7 +6246,7 @@ void ApplicationWindow::savetoNexusFile()
savedatainNexusFormat(wsName,fileName.toStdString());

MantidQt::API::AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(fileName).absoluteDir().path());

updateRecentFilesList(fileName);
}
}

Expand All @@ -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<QString,QString> 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<QString,QString> params;
params["Filename"] = fn;
mantidUI->showAlgorithmDialog(QString("Load"),params);
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 15 additions & 4 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 664d2da

Please sign in to comment.