Files
ResInsight/ApplicationLibCode/ProjectDataModel/Workflow/RimWorkflowCollection.cpp
T

160 lines
6.5 KiB
C++
Raw Normal View History

2026-05-30 10:16:07 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026 Equinor 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 "RimWorkflowCollection.h"
#include "RimWorkflowInstalledCollection.h"
#include "RimWorkflowTaskCatalog.h"
#include "RiaLogging.h"
#include "RiaPreferencesSystem.h"
#include "cafCmdFeatureMenuBuilder.h"
#include "cafPdmUiTreeOrdering.h"
2026-05-30 10:16:07 +02:00
#include <QDir>
#include <QFileInfo>
#include <QJsonArray>
2026-05-30 10:16:07 +02:00
CAF_PDM_SOURCE_INIT( RimWorkflowCollection, "WorkflowCollection" );
2026-10-05 16:12:01 +02:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2026-05-30 10:16:07 +02:00
RimWorkflowCollection::RimWorkflowCollection()
{
CAF_PDM_InitObject( "Workflows", ":/Folder.png" );
CAF_PDM_InitFieldNoDefault( &m_items, "Workflows", "" );
CAF_PDM_InitFieldNoDefault( &m_installed, "Installed", "" );
m_installed = new RimWorkflowInstalledCollection;
2026-05-30 10:16:07 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2026-05-30 10:16:07 +02:00
RimWorkflowCollection::~RimWorkflowCollection() = default;
2026-10-05 16:12:01 +02:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2026-05-30 10:16:07 +02:00
QString RimWorkflowCollection::discoveryDirectory()
{
return QDir::homePath() + "/.taskmaestro/workflows";
}
2026-10-05 16:12:01 +02:00
//--------------------------------------------------------------------------------------------------
/// Reload the task catalog, the workflow folders and the installed workflows. Workflows that
/// have never been saved are kept. This runs the Python helper, so it is not done in the
/// constructor: projects are also created in console mode and by code generation.
2026-10-05 16:12:01 +02:00
//--------------------------------------------------------------------------------------------------
2026-05-30 10:16:07 +02:00
void RimWorkflowCollection::rescanWorkflows()
{
for ( RimWorkflow* workflow : items() )
{
if ( workflow->source() != RimWorkflow::Source::Unsaved ) deleteItem( workflow );
}
m_installed->deleteAllItems();
2026-05-30 10:16:07 +02:00
if ( !RiaPreferencesSystem::current()->isFeatureEnabled( "workflows" ) ) return;
RimWorkflowTaskCatalog::invalidate();
const RimWorkflowTaskCatalog& catalog = RimWorkflowTaskCatalog::instance();
if ( !catalog.isValid() ) RiaLogging::warning( QString( "Workflow task catalog: %1" ).arg( catalog.loadError() ).toStdString() );
for ( const QString& pluginError : catalog.pluginErrors() )
RiaLogging::warning( QString( "Workflow plugin: %1" ).arg( pluginError ).toStdString() );
2026-05-30 10:16:07 +02:00
QDir dir( discoveryDirectory() );
if ( dir.exists() )
2026-05-30 10:16:07 +02:00
{
const QFileInfoList entries = dir.entryInfoList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
for ( const QFileInfo& entry : entries )
{
if ( !QFileInfo( entry.absoluteFilePath() + "/workflow.yaml" ).isFile() ) continue;
auto* workflow = new RimWorkflow;
workflow->setWorkflowDirectory( entry.absoluteFilePath() );
workflow->loadFromDirectory();
addItem( workflow );
}
}
2026-05-30 10:16:07 +02:00
for ( const QJsonValue& entry : catalog.registeredWorkflows() )
{
2026-05-30 10:16:07 +02:00
auto* workflow = new RimWorkflow;
workflow->loadFromRegistered( entry.toObject() );
m_installed->addItem( workflow );
}
uiCapability()->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimWorkflowCollection::hasUnsavedChanges() const
{
for ( RimWorkflow* workflow : items() )
{
if ( workflow->isEditable() && workflow->isDirty() ) return true;
2026-05-30 10:16:07 +02:00
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimWorkflow*> RimWorkflowCollection::allWorkflows() const
{
std::vector<RimWorkflow*> workflows = items();
for ( RimWorkflow* workflow : m_installed->items() )
workflows.push_back( workflow );
return workflows;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimWorkflow* RimWorkflowCollection::findWorkflowByDirectory( const QString& directory ) const
{
const QString wanted = QDir( directory ).absolutePath();
for ( RimWorkflow* workflow : items() )
{
if ( !workflow->workflowDirectory().isEmpty() && QDir( workflow->workflowDirectory() ).absolutePath() == wanted ) return workflow;
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimWorkflowCollection::appendMenuItems( caf::CmdFeatureMenuBuilder& menuBuilder ) const
{
menuBuilder << "RicNewWorkflowFeature";
menuBuilder << "RicRescanWorkflowsFeature";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimWorkflowCollection::defineUiTreeOrdering( caf::PdmUiTreeOrdering& uiTreeOrdering, QString uiConfigName )
{
for ( RimWorkflow* workflow : items() )
uiTreeOrdering.add( workflow );
if ( m_installed->count() > 0 ) uiTreeOrdering.add( m_installed() );
uiTreeOrdering.skipRemainingChildren( true );
2026-05-30 10:16:07 +02:00
}