Skip to content

Commit

Permalink
no conflict in recent files menu, re #10335
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMPouzols committed Oct 16, 2014
2 parents 868e167 + 664d2da commit 82955d5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 23 deletions.
99 changes: 80 additions & 19 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Expand Up @@ -558,7 +558,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 @@ -1210,6 +1211,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 @@ -1221,8 +1223,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 @@ -4513,9 +4516,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 @@ -4891,6 +4916,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 @@ -4901,9 +4927,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 @@ -5365,6 +5400,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 @@ -6057,7 +6093,7 @@ void ApplicationWindow::savetoNexusFile()
savedatainNexusFormat(wsName,fileName.toStdString());

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

updateRecentFilesList(fileName);
}
}

Expand All @@ -6066,18 +6102,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 @@ -9072,7 +9117,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 @@ -13385,10 +13432,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
22 changes: 18 additions & 4 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Expand Up @@ -196,8 +196,13 @@ public slots:
ApplicationWindow* openProject(const QString& fn, const int fileVersion);
void openProjectFolder(std::string lines, const int fileVersion, bool isTopLevel = false);
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 @@ -591,6 +596,8 @@ public slots:

void openRecentProject(int index);

//@}

//! \name Table Tools
//@{
void sortSelection();
Expand Down Expand Up @@ -822,8 +829,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 @@ -1259,6 +1270,9 @@ private 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;

QColor workspaceColor, panelsColor, panelsTextColor;
QString appStyle, workingDir;
Expand Down Expand Up @@ -1305,9 +1319,9 @@ private 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 @@ -1379,7 +1393,7 @@ private 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;
QMenu *tableMenu, *fillMenu, *normMenu, *newMenu, *exportPlotMenu, *smoothMenu, *filterMenu, *decayMenu,*saveMenu,*openMenu, *toolbarsMenu;
Expand Down

0 comments on commit 82955d5

Please sign in to comment.