Files
ResInsight/ApplicationLibCode/UserInterface/RiuRecentFileActionProvider.cpp
T
Magne Sjaastad 56ff80c8e2 #13360: Integrate project version restore tool into GUI
Add a GUI feature for restoring previous versions of a project file from
its backup database, replacing the need for the external standalone tool.

- RiaProjectBackupTools: add extractVersionsToFolder() to read all stored
  revisions from a project's .rspdb backup database and write each one to
  a timestamped .rsp file in a destination folder.
- RicRestoreProjectVersionsUi: dialog for selecting the project file and
  export folder, defaulting to the currently open project. Adds a Recent
  Project Files dropdown (backed by RiuRecentFileActionProvider) that
  quickly populates the project file and export folder fields.
- RicRestoreProjectVersionsFeature: new command feature wired into the
  File menu as 'Restore Previous Project Versions...', using the
  OpenFolder icon and placed after a separator below Save Project / Save
  Project As.
- RiuRecentFileActionProvider: add recentProjectFiles() to expose the
  list of recently used project files.
- Added '...' suffix to Save Project As, consistent with other
  dialog-launching actions.
2026-10-21 22:23:38 +02:00

198 lines
7.0 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RiaFilePathTools.h"
#include "RiaGuiApplication.h"
#include "RiaLogging.h"
#include "RiaStringListSerializer.h"
#include "RiuMessageDialog.h"
#include <QAction>
#include <QDir>
#include <QFileInfo>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuRecentFileActionProvider::RiuRecentFileActionProvider( int maxActionCount )
: m_maxActionCount( maxActionCount )
{
createActions();
updateActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuRecentFileActionProvider::~RiuRecentFileActionProvider()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::addFileName( const QString& fileName )
{
RiaStringListSerializer stringListSerializer( registryKey() );
stringListSerializer.addString( QDir::fromNativeSeparators( fileName ), m_maxActionCount );
updateActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::removeFileName( const QString& fileName )
{
RiaStringListSerializer stringListSerializer( registryKey() );
stringListSerializer.removeString( QDir::fromNativeSeparators( fileName ) );
updateActions();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiuRecentFileActionProvider::registryKey()
{
return "recentFileList";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiuRecentFileActionProvider::lastUsedProjectFileName()
{
const QStringList files = RiaStringListSerializer( registryKey() ).textStrings();
for ( const QString& file : files )
{
if ( file.endsWith( ".rsp", Qt::CaseInsensitive ) ) return file;
}
return {};
}
//--------------------------------------------------------------------------------------------------
/// Returns all recently used project files, most recently used first.
//--------------------------------------------------------------------------------------------------
QStringList RiuRecentFileActionProvider::recentProjectFiles()
{
QStringList projectFiles;
const QStringList files = RiaStringListSerializer( registryKey() ).textStrings();
for ( const QString& file : files )
{
if ( file.endsWith( ".rsp", Qt::CaseInsensitive ) || file.endsWith( ".rip", Qt::CaseInsensitive ) )
{
projectFiles.push_back( file );
}
}
return projectFiles;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuRecentFileActionProvider::updateActions()
{
RiaStringListSerializer stringListSerializer( registryKey() );
QStringList files = stringListSerializer.textStrings();
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 = RiaFilePathTools::toInternalSeparator( action->data().toString() );
RiaGuiApplication* app = RiaGuiApplication::instance();
if ( RiaApplication::hasValidProjectFileExtension( fileName ) )
{
if ( !app->checkWithUserBeforeClose() ) return;
}
bool loadingSucceded = RiaApplication::instance()->openFile( fileName );
if ( loadingSucceded )
{
addFileName( fileName );
}
else
{
RiuMessageDialog::showError( nullptr, "File open", "Failed to import file located at\n" + fileName );
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 );
}