#846 Moved recent file management to RiaApplication

This commit is contained in:
Magne Sjaastad
2016-10-05 08:23:07 +02:00
parent 1654ab3c91
commit c8b73dad68
8 changed files with 268 additions and 124 deletions

View File

@@ -256,13 +256,6 @@ void RiuMainWindow::createActions()
m_closeProjectAction = new QAction("&Close Project", this);
for (int i = 0; i < MaxRecentFiles; ++i)
{
m_recentFileActions[i] = new QAction(this);
m_recentFileActions[i]->setVisible(false);
connect(m_recentFileActions[i], SIGNAL(triggered()), this, SLOT(slotOpenRecentFile()));
}
m_exitAction = new QAction("E&xit", this);
connect(m_openProjectAction, SIGNAL(triggered()), SLOT(slotOpenProject()));
@@ -415,12 +408,12 @@ void RiuMainWindow::createMenus()
fileMenu->addAction(m_saveProjectAction);
fileMenu->addAction(m_saveProjectAsAction);
m_recentFilesSeparatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
fileMenu->addAction(m_recentFileActions[i]);
std::vector<QAction*> recentFileActions = RiaApplication::instance()->recentFileActions();
for (auto act : recentFileActions)
{
fileMenu->addAction(act);
}
updateRecentFileActions();
fileMenu->addSeparator();
QMenu* testMenu = fileMenu->addMenu("&Testing");
@@ -850,7 +843,7 @@ void RiuMainWindow::slotImportGeoMechModel()
{
if (app->openOdbCaseFromFile(fileName))
{
addRecentFiles(fileName);
app->addToRecentFiles(fileName);
}
}
}
@@ -873,7 +866,7 @@ void RiuMainWindow::slotOpenProject()
if (app->loadProject(fileName))
{
addRecentFiles(fileName);
app->addToRecentFiles(fileName);
}
}
@@ -887,7 +880,7 @@ void RiuMainWindow::slotOpenLastUsedProject()
if (app->loadProject(fileName))
{
addRecentFiles(fileName);
app->addToRecentFiles(fileName);
}
}
@@ -951,97 +944,6 @@ void RiuMainWindow::slotCloseProject()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMainWindow::slotOpenRecentFile()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
{
QString filename = action->data().toString();
bool loadingSucceded = false;
if (filename.contains(".rsp", Qt::CaseInsensitive) || filename.contains(".rip", Qt::CaseInsensitive) )
{
loadingSucceded = RiaApplication::instance()->loadProject(action->data().toString());
}
else if ( filename.contains(".egrid", Qt::CaseInsensitive) || filename.contains(".grid", Qt::CaseInsensitive) )
{
loadingSucceded = RiaApplication::instance()->openEclipseCaseFromFile(filename);
}
else if (filename.contains(".odb", Qt::CaseInsensitive) )
{
loadingSucceded = RiaApplication::instance()->openOdbCaseFromFile(filename);
}
if (loadingSucceded)
{
addRecentFiles(filename);
}
else
{
removeRecentFiles(filename);
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMainWindow::updateRecentFileActions()
{
QSettings settings;
QStringList files = settings.value("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(QFileInfo(files[i]).fileName());
m_recentFileActions[i]->setText(text);
m_recentFileActions[i]->setData(files[i]);
m_recentFileActions[i]->setToolTip(files[i]);
m_recentFileActions[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
m_recentFileActions[j]->setVisible(false);
m_recentFilesSeparatorAction->setVisible(numRecentFiles > 0);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMainWindow::addRecentFiles(const QString& file)
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(file);
files.prepend(file);
while (files.size() > MaxRecentFiles)
files.removeLast();
settings.setValue("recentFileList", files);
updateRecentFileActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMainWindow::removeRecentFiles(const QString& file)
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(file);
settings.setValue("recentFileList", files);
updateRecentFileActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMdiSubWindow* RiuMainWindow::findMdiSubWindow(QWidget* viewer)
{
QList<QMdiSubWindow*> subws = m_mdiArea->subWindowList();

View File

@@ -110,9 +110,6 @@ public:
void setExpanded(const caf::PdmUiItem* uiItem, bool expanded);
void addRecentFiles(const QString& file);
void removeRecentFiles(const QString& file);
RimMdiWindowGeometry windowGeometryForViewer(QWidget* viewer);
RimMdiWindowGeometry windowGeometryForWidget(QWidget* widget);
@@ -132,8 +129,6 @@ private:
void createToolBars();
void createDockPanels();
void updateRecentFileActions();
void storeTreeViewState();
void restoreTreeViewState();
@@ -152,13 +147,6 @@ private:
QAction* m_closeProjectAction;
QAction* m_exitAction;
// Recent files
enum { MaxRecentFiles = 9 };
QAction* m_recentFilesSeparatorAction;
QMenu* m_recentFilesMenu;
QAction* m_recentFileActions[MaxRecentFiles];
// Edit actions
QAction* m_editPreferences;
QAction* m_newPropertyView;
@@ -224,8 +212,6 @@ private slots:
void slotSaveProjectAs();
void slotCloseProject();
void slotOpenRecentFile();
void slotRefreshFileActions();
// Edit slots

View File

@@ -0,0 +1,167 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) Statoil ASA 2016
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiuRecentFileActionProvider.h"
#include "RiaApplication.h"
#include <QAction>
#include <QSettings>
#include <QFileInfo>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuRecentFileActionProvider::RiuRecentFileActionProvider(int maxActionCount)
: m_maxActionCount(maxActionCount)
{
createActions();
updateActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuRecentFileActionProvider::~RiuRecentFileActionProvider()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::addFileName(const QString& fileName)
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(fileName);
files.prepend(fileName);
while (files.size() > m_maxActionCount)
files.removeLast();
settings.setValue("recentFileList", files);
updateActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::removeFileName(const QString& fileName)
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(fileName);
settings.setValue("recentFileList", files);
updateActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::updateActions()
{
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
int numRecentFiles = qMin(files.size(), m_maxActionCount);
for (int i = 0; i < numRecentFiles; ++i) {
QString text = tr("&%1 %2").arg(i + 1).arg(QFileInfo(files[i]).fileName());
m_recentFileActions[i]->setText(text);
m_recentFileActions[i]->setData(files[i]);
m_recentFileActions[i]->setToolTip(files[i]);
m_recentFileActions[i]->setVisible(true);
}
for (int j = numRecentFiles; j < m_maxActionCount; ++j)
m_recentFileActions[j]->setVisible(false);
m_separatorAction->setVisible(numRecentFiles > 0);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<QAction*> RiuRecentFileActionProvider::actions() const
{
std::vector<QAction*> actionItems;
actionItems.push_back(m_separatorAction);
for (auto act : m_recentFileActions)
{
actionItems.push_back(act);
}
return actionItems;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::slotOpenRecentFile()
{
QAction* action = qobject_cast<QAction *>(sender());
if (action)
{
QString filename = action->data().toString();
bool loadingSucceded = false;
if (filename.contains(".rsp", Qt::CaseInsensitive) || filename.contains(".rip", Qt::CaseInsensitive))
{
loadingSucceded = RiaApplication::instance()->loadProject(action->data().toString());
}
else if (filename.contains(".egrid", Qt::CaseInsensitive) || filename.contains(".grid", Qt::CaseInsensitive))
{
loadingSucceded = RiaApplication::instance()->openEclipseCaseFromFile(filename);
}
else if (filename.contains(".odb", Qt::CaseInsensitive))
{
loadingSucceded = RiaApplication::instance()->openOdbCaseFromFile(filename);
}
if (loadingSucceded)
{
addFileName(filename);
}
else
{
removeFileName(filename);
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::createActions()
{
for (int i = 0; i < m_maxActionCount; ++i)
{
QAction* act = new QAction(this);
act->setVisible(false);
connect(act, SIGNAL(triggered()), this, SLOT(slotOpenRecentFile()));
m_recentFileActions.push_back(act);
}
m_separatorAction = new QAction(this);
m_separatorAction->setSeparator(true);
}

View File

@@ -0,0 +1,59 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) Statoil ASA 2016
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <QObject>
#include <QString>
#include <vector>
class QAction;
//==================================================================================================
//
//
//
//==================================================================================================
class RiuRecentFileActionProvider : public QObject
{
Q_OBJECT
public:
RiuRecentFileActionProvider(int maxActionCount = 9);
~RiuRecentFileActionProvider();
void addFileName(const QString& fileName);
std::vector<QAction*> actions() const;
private slots:
void slotOpenRecentFile();
private:
void createActions();
void updateActions();
void removeFileName(const QString& fileName);
private:
int m_maxActionCount;
std::vector<QAction*> m_recentFileActions;
QAction* m_separatorAction;
};