2017-03-07 06:48:17 -06:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2017- Statoil ASA
|
|
|
|
//
|
|
|
|
// 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 "RicAsciiExportSummaryPlotFeature.h"
|
|
|
|
|
|
|
|
#include "RiaApplication.h"
|
2017-03-08 05:25:32 -06:00
|
|
|
#include "RiaLogging.h"
|
2017-03-07 06:48:17 -06:00
|
|
|
|
|
|
|
#include "RimSummaryPlot.h"
|
|
|
|
|
2017-03-08 05:25:32 -06:00
|
|
|
#include "RiuMainWindow.h"
|
2017-03-07 06:48:17 -06:00
|
|
|
|
2017-03-08 05:25:32 -06:00
|
|
|
#include "cafPdmUiPropertyViewDialog.h"
|
|
|
|
#include "cafProgressInfo.h"
|
2017-03-07 06:48:17 -06:00
|
|
|
#include "cafSelectionManager.h"
|
2017-03-08 05:25:32 -06:00
|
|
|
#include "cvfAssert.h"
|
2017-03-07 06:48:17 -06:00
|
|
|
|
|
|
|
#include <QAction>
|
2017-03-08 05:25:32 -06:00
|
|
|
#include <QDebug>
|
2017-03-08 06:27:17 -06:00
|
|
|
#include <QFileDialog>
|
2017-03-08 05:25:32 -06:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMessageBox>
|
2017-03-07 06:48:17 -06:00
|
|
|
#include <vector>
|
2017-03-10 07:50:04 -06:00
|
|
|
#include "cafUtils.h"
|
2017-03-08 05:25:32 -06:00
|
|
|
|
2017-03-07 06:48:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
CAF_CMD_SOURCE_INIT(RicAsciiExportSummaryPlotFeature, "RicAsciiExportSummaryPlotFeature");
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
bool RicAsciiExportSummaryPlotFeature::isCommandEnabled()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RicAsciiExportSummaryPlotFeature::onActionTriggered(bool isChecked)
|
|
|
|
{
|
2017-03-08 06:27:17 -06:00
|
|
|
RiaApplication* app = RiaApplication::instance();
|
|
|
|
QString projectFolder = app->currentProjectPath();
|
|
|
|
|
2017-03-07 06:48:17 -06:00
|
|
|
RimProject* project = RiaApplication::instance()->project();
|
|
|
|
CVF_ASSERT(project);
|
|
|
|
|
|
|
|
std::vector<RimSummaryPlot*> selectedSummaryPlots;
|
|
|
|
caf::SelectionManager::instance()->objectsByType(&selectedSummaryPlots);
|
2017-03-09 04:14:13 -06:00
|
|
|
QString defaultDir = RiaApplication::instance()->lastUsedDialogDirectoryWithFallback("PLOT_ASCIIEXPORT_DIR", projectFolder);
|
2017-03-07 06:48:17 -06:00
|
|
|
|
2017-03-10 07:50:04 -06:00
|
|
|
|
|
|
|
caf::ProgressInfo pi(selectedSummaryPlots.size(), QString("Exporting to csv"));
|
|
|
|
size_t progress = 0;
|
|
|
|
|
|
|
|
|
|
|
|
bool isOk = false;
|
|
|
|
if (selectedSummaryPlots.size() == 1)
|
|
|
|
{
|
|
|
|
RimSummaryPlot* summaryPlot = selectedSummaryPlots.at(0);
|
|
|
|
QString defaultFileName = defaultDir + "/" + caf::Utils::makeValidFileBasename((summaryPlot->description())) + ".csv";
|
|
|
|
QString fileName = QFileDialog::getSaveFileName(NULL, "Select file for Summary Plot Export", defaultFileName, "All files(*.*)");
|
|
|
|
if (fileName.isEmpty()) return;
|
|
|
|
isOk = writeAsciiExportForSummaryPlots(fileName, summaryPlot);
|
|
|
|
|
|
|
|
progress++;
|
|
|
|
pi.setProgress(progress);
|
|
|
|
}
|
|
|
|
else if (selectedSummaryPlots.size() > 1)
|
|
|
|
{
|
|
|
|
std::vector<QString> fileNames;
|
|
|
|
for (RimSummaryPlot* summaryPlot : selectedSummaryPlots)
|
|
|
|
{
|
|
|
|
QString fileName = caf::Utils::makeValidFileBasename(summaryPlot->description()) + ".csv";
|
|
|
|
fileNames.push_back(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString saveDir;
|
|
|
|
bool writeFiles = caf::Utils::getSaveDirectoryAndCheckOverwriteFiles(defaultDir, fileNames, &saveDir);
|
|
|
|
if (!writeFiles) return;
|
|
|
|
|
|
|
|
RiaLogging::debug(QString("Writing to directory %!").arg(saveDir));
|
|
|
|
for (RimSummaryPlot* summaryPlot : selectedSummaryPlots)
|
|
|
|
{
|
|
|
|
QString fileName = saveDir + "/" + caf::Utils::makeValidFileBasename(summaryPlot->description()) + ".csv";
|
|
|
|
isOk = writeAsciiExportForSummaryPlots(fileName, summaryPlot);
|
|
|
|
progress++;
|
|
|
|
pi.setProgress(progress);
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 06:48:17 -06:00
|
|
|
|
2017-03-08 06:27:17 -06:00
|
|
|
if (!isOk)
|
2017-03-07 06:48:17 -06:00
|
|
|
{
|
2017-03-10 07:50:04 -06:00
|
|
|
QMessageBox::critical(NULL, "File export", "Failed to export summary plots to csv");
|
2017-03-07 06:48:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RicAsciiExportSummaryPlotFeature::setupActionLook(QAction* actionToSetup)
|
|
|
|
{
|
|
|
|
actionToSetup->setText("Export Summary Plot Data");
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2017-03-10 07:50:04 -06:00
|
|
|
bool RicAsciiExportSummaryPlotFeature::writeAsciiExportForSummaryPlots(const QString& fileName, const RimSummaryPlot* summaryPlot)
|
2017-03-07 06:48:17 -06:00
|
|
|
{
|
|
|
|
RiaLogging::info(QString("Writing ascii values for summary plot(s) to file: %1").arg(fileName));
|
|
|
|
|
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTextStream out(&file);
|
2017-03-10 07:50:04 -06:00
|
|
|
out << summaryPlot->description();
|
|
|
|
out << summaryPlot->asciiDataForPlotExport();
|
|
|
|
out << "\n\n";
|
2017-03-08 05:25:32 -06:00
|
|
|
|
|
|
|
RiaLogging::info(QString("Competed writing ascii values for summary plot(s) to file %1").arg(fileName));
|
2017-03-07 06:48:17 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|