Files
ResInsight/ApplicationLibCode/UserInterface/RiuRecentFileActionProvider.cpp
T
Kristian Bendiksen bb0afbf962 #13126 RiaLogging: Extract errorInMessageBox to RiuMessageDialog
Move the QMessageBox-displaying helper out of the logging utility into a
dedicated UI class. RiaLogging.h no longer pulls QMessageBox/QWidget into
the ~270 files that include it. Behavior is preserved: showError displays
the dialog when running interactively (and not under regression tests),
then forwards the text to RiaLogging::error.
2026-05-08 10:11:09 +02:00

180 lines
6.3 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 {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
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 );
}