ResInsight/ApplicationCode/UserInterface/RiuRecentFileActionProvider.cpp
Gaute Lindkvist 57b33b0d4c
First implementation of Headless (#4392)
* Revert "#4377 Octave : Use RiaLogging for error messages instead of QErrorMessage "

This reverts commit f758a8edb2.

* Revert "#4380 Preferences : Changing scene font size when geo mech view is open causes crash"

This reverts commit df62a41397.

* Revert "#4379 Documentation : Update command line parser for import of summary files"

This reverts commit d0b5357ed4.

* Unfinished WIP

* Builds but crashes

* Refactored code now builds and runs

* ResInsight can now run the unittests headless

* Can run some command files successfully

* Build on Linux

* Extra headless hack header

* Moved PdmUiItem hack to cpp file

* Fix headless crash in RimWellAllocationPlot

* Handle error gracefully if ExportSnapshots command is executed from console

* Add caf::QIconProvider and remove some hacks

* Also made the greying out of disabled icons work for a couple of cases where it didn't.

* Linux build fix

* #4380 Reimplement fix df62a41397 by @magnesj on top of Headless code changes

* #4379 Reintroduce kode from d0b5357ed4 by @magnesj

* #4377 Restore f758a8edb2 in new Headless code
2019-05-06 10:36:05 +02:00

165 lines
5.6 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 "RiaGuiApplication.h"
#include "RiaFilePathTools.h"
#include <QAction>
#include <QFileInfo>
#include <QMessageBox>
#include <QSettings>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
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 = RiaFilePathTools::toInternalSeparator(action->data().toString());
RiaGuiApplication* app = RiaGuiApplication::instance();
if (RiaApplication::hasValidProjectFileExtension(fileName))
{
if (!app->askUserToSaveModifiedProject()) return;
}
bool loadingSucceded = RiaApplication::instance()->openFile(fileName);
if (loadingSucceded)
{
addFileName(fileName);
}
else
{
QMessageBox::warning(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);
}