2012-05-18 02:45:23 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2014-09-23 08:04:57 -05:00
|
|
|
// Copyright (C) 2011- Statoil ASA
|
|
|
|
// Copyright (C) 2013- Ceetron Solutions AS
|
|
|
|
// Copyright (C) 2011-2012 Ceetron AS
|
2019-04-03 08:04:21 -05:00
|
|
|
//
|
2012-05-18 02:45:23 -05:00
|
|
|
// 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.
|
2019-04-03 08:04:21 -05:00
|
|
|
//
|
2012-05-18 02:45:23 -05:00
|
|
|
// 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.
|
2019-04-03 08:04:21 -05:00
|
|
|
//
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
2012-05-18 02:45:23 -05:00
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "RimScriptCollection.h"
|
2014-07-24 03:11:43 -05:00
|
|
|
|
|
|
|
#include "RimCalcScript.h"
|
|
|
|
#include "RiuMainWindow.h"
|
|
|
|
|
2012-06-26 09:10:41 -05:00
|
|
|
#include "cafPdmUiFilePathEditor.h"
|
2014-07-24 03:11:43 -05:00
|
|
|
#include "cafUtils.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
2012-05-18 02:45:23 -05:00
|
|
|
|
|
|
|
CAF_PDM_SOURCE_INIT(RimScriptCollection, "ScriptLocation");
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
///
|
2012-05-18 02:45:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimScriptCollection::RimScriptCollection()
|
|
|
|
{
|
|
|
|
CAF_PDM_InitObject("ScriptLocation", ":/Folder.png", "", "");
|
|
|
|
|
2019-04-03 08:04:21 -05:00
|
|
|
CAF_PDM_InitFieldNoDefault(&directory, "ScriptDirectory", "Dir", "", "", "");
|
|
|
|
CAF_PDM_InitFieldNoDefault(&calcScripts, "CalcScripts", "", "", "", "");
|
2015-08-11 08:22:49 -05:00
|
|
|
calcScripts.uiCapability()->setUiHidden(true);
|
2019-04-03 08:04:21 -05:00
|
|
|
CAF_PDM_InitFieldNoDefault(&subDirectories, "SubDirectories", "", "", "", "");
|
2015-08-11 08:22:49 -05:00
|
|
|
subDirectories.uiCapability()->setUiHidden(true);
|
2012-06-26 09:10:41 -05:00
|
|
|
|
2019-04-03 08:01:13 -05:00
|
|
|
CAF_PDM_InitField(&m_searchSubFolders, "SearchSubFolders", false, "Add Subfolders", "", "", "");
|
|
|
|
|
2015-08-05 06:27:36 -05:00
|
|
|
directory.uiCapability()->setUiEditorTypeName(caf::PdmUiFilePathEditor::uiEditorTypeName());
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
///
|
2012-05-18 02:45:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
RimScriptCollection::~RimScriptCollection()
|
|
|
|
{
|
2019-04-03 08:04:21 -05:00
|
|
|
calcScripts.deleteAllChildObjects();
|
|
|
|
subDirectories.deleteAllChildObjects();
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
///
|
2012-05-18 02:45:23 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimScriptCollection::readContentFromDisc()
|
|
|
|
{
|
2012-06-26 09:10:41 -05:00
|
|
|
calcScripts.deleteAllChildObjects();
|
2012-05-18 02:45:23 -05:00
|
|
|
|
|
|
|
if (directory().isEmpty())
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < subDirectories.size(); ++i)
|
|
|
|
{
|
|
|
|
if (subDirectories[i]) subDirectories[i]->readContentFromDisc();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-22 07:37:33 -05:00
|
|
|
QDir myDir(this->directory());
|
|
|
|
if (!myDir.isReadable())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
// Build a list of all scripts in the specified directory
|
|
|
|
{
|
2019-07-26 07:54:54 -05:00
|
|
|
QStringList nameFilters; nameFilters << "*.m" << "*.py";
|
|
|
|
QStringList fileList = caf::Utils::getFilesInDirectory(directory, nameFilters, true);
|
2012-05-18 02:45:23 -05:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fileList.size(); i++)
|
|
|
|
{
|
2019-04-03 08:04:21 -05:00
|
|
|
const QString& fileName = fileList.at(i);
|
2012-05-18 02:45:23 -05:00
|
|
|
|
2017-05-11 02:23:13 -05:00
|
|
|
if (caf::Utils::fileExists(fileName))
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
|
|
|
RimCalcScript* calcScript = new RimCalcScript;
|
2019-04-04 14:18:20 -05:00
|
|
|
calcScript->absoluteFileName = fileName;
|
2019-04-03 08:04:21 -05:00
|
|
|
|
2017-05-11 02:23:13 -05:00
|
|
|
QFileInfo fi(fileName);
|
2012-05-18 02:45:23 -05:00
|
|
|
calcScript->setUiName(fi.baseName());
|
|
|
|
|
|
|
|
calcScripts.push_back(calcScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 08:01:13 -05:00
|
|
|
subDirectories.deleteAllChildObjects();
|
|
|
|
|
|
|
|
if (m_searchSubFolders())
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
2019-04-03 08:04:21 -05:00
|
|
|
QDir dir(directory);
|
2013-04-22 07:37:33 -05:00
|
|
|
QFileInfoList fileInfoList = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable);
|
2012-05-18 02:45:23 -05:00
|
|
|
|
|
|
|
QListIterator<QFileInfo> it(fileInfoList);
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
QFileInfo fi = it.next();
|
|
|
|
|
|
|
|
RimScriptCollection* scriptLocation = new RimScriptCollection;
|
2019-04-03 08:04:21 -05:00
|
|
|
scriptLocation->directory = fi.absoluteFilePath();
|
2012-05-18 02:45:23 -05:00
|
|
|
scriptLocation->setUiName(fi.baseName());
|
|
|
|
scriptLocation->readContentFromDisc();
|
2019-04-03 08:04:21 -05:00
|
|
|
|
2012-05-18 02:45:23 -05:00
|
|
|
subDirectories.push_back(scriptLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
void RimScriptCollection::pathsAndSubPaths(QStringList& pathList)
|
|
|
|
{
|
|
|
|
if (!this->directory().isEmpty())
|
|
|
|
{
|
2013-04-22 07:37:33 -05:00
|
|
|
QDir myDir(this->directory());
|
|
|
|
if (myDir.isReadable())
|
|
|
|
{
|
|
|
|
pathList.append(this->directory());
|
|
|
|
}
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
|
2019-04-03 08:04:21 -05:00
|
|
|
for (size_t i = 0; i < this->subDirectories.size(); ++i)
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
2013-04-22 07:37:33 -05:00
|
|
|
if (this->subDirectories[i])
|
|
|
|
{
|
|
|
|
this->subDirectories[i]->pathsAndSubPaths(pathList);
|
|
|
|
}
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
RimScriptCollection* RimScriptCollection::findScriptCollection(const QString& path)
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
|
|
|
if (!this->directory().isEmpty())
|
|
|
|
{
|
|
|
|
QFileInfo otherPath(path);
|
|
|
|
QFileInfo thisPath(directory());
|
|
|
|
if (otherPath == thisPath) return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < this->subDirectories.size(); ++i)
|
|
|
|
{
|
2019-04-03 08:04:21 -05:00
|
|
|
RimScriptCollection* foundColl = nullptr;
|
|
|
|
if (this->subDirectories[i]) foundColl = this->subDirectories[i]->findScriptCollection(path);
|
|
|
|
if (foundColl) return foundColl;
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
|
2018-02-18 11:56:43 -06:00
|
|
|
return nullptr;
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
void RimScriptCollection::fieldChangedByUi(const caf::PdmFieldHandle* changedField,
|
|
|
|
const QVariant& oldValue,
|
|
|
|
const QVariant& newValue)
|
2012-05-18 02:45:23 -05:00
|
|
|
{
|
|
|
|
if (&directory == changedField)
|
|
|
|
{
|
|
|
|
QFileInfo fi(directory);
|
|
|
|
this->setUiName(fi.baseName());
|
|
|
|
this->readContentFromDisc();
|
|
|
|
}
|
2019-04-03 08:01:13 -05:00
|
|
|
else if (&m_searchSubFolders == changedField)
|
|
|
|
{
|
|
|
|
this->readContentFromDisc();
|
|
|
|
}
|
2012-05-18 02:45:23 -05:00
|
|
|
}
|
2012-06-26 09:10:41 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
///
|
2012-06-26 09:10:41 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-04-03 08:04:21 -05:00
|
|
|
void RimScriptCollection::defineEditorAttribute(const caf::PdmFieldHandle* field,
|
|
|
|
QString uiConfigName,
|
|
|
|
caf::PdmUiEditorAttribute* attribute)
|
2012-06-26 09:10:41 -05:00
|
|
|
{
|
|
|
|
if (field == &directory)
|
|
|
|
{
|
2017-04-25 03:31:37 -05:00
|
|
|
caf::PdmUiFilePathEditorAttribute* myAttr = dynamic_cast<caf::PdmUiFilePathEditorAttribute*>(attribute);
|
|
|
|
if (myAttr)
|
|
|
|
{
|
|
|
|
myAttr->m_selectDirectory = true;
|
|
|
|
}
|
2012-06-26 09:10:41 -05:00
|
|
|
}
|
|
|
|
}
|