Files
ResInsight/ApplicationLibCode/Commands/WorkflowCommands/RicWorkflowLocationUi.cpp
T
Kristian Bendiksen f9030a3e1f Workflows: Add interactive editor for taskmaestro workflows
Build, copy and edit taskmaestro workflows in ResInsight and save them as
standard workflow.yaml + input.yaml.

- Python helper: catalog, load, save and export-registered subcommands,
  and run --registered for installed workflows
- C++ definition model with pure edit operations, port type checks,
  config field derivation, static validation and undo/redo
- Installed folder listing registered workflows (read-only, runnable,
  can be duplicated)
- Graph editor dock with task palette, drag-to-connect, context menus,
  automatic layout and debounced live validation
- New/Duplicate/Save/Save As/Rescan commands; unsaved edits run from a
  temporary folder and prompt before being discarded
- Update the design document and the user README
2026-10-21 11:19:35 +02:00

141 lines
5.9 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RicWorkflowLocationUi.h"
#include "Workflow/RimWorkflowCollection.h"
#include "Workflow/RimWorkflowDefinitionTools.h"
#include "Riu3DMainWindowTools.h"
#include "cafPdmUiFilePathEditor.h"
#include "cafPdmUiPropertyViewDialog.h"
#include <QDir>
#include <QMessageBox>
CAF_PDM_SOURCE_INIT( RicWorkflowLocationUi, "RicWorkflowLocationUi" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicWorkflowLocationUi::RicWorkflowLocationUi()
{
CAF_PDM_InitObject( "Workflow Location" );
CAF_PDM_InitFieldNoDefault( &m_name, "Name", "Name" );
CAF_PDM_InitFieldNoDefault( &m_folder, "Folder", "Folder" );
m_folder.uiCapability()->setUiEditorTypeName( caf::PdmUiFilePathEditor::uiEditorTypeName() );
m_folder.uiCapability()->setAttribute( caf::PdmUiFilePathEditor::Keys::SELECT_DIRECTORY, true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicWorkflowLocationUi::setName( const QString& name )
{
m_name = name;
if ( !m_folderEditedByUser ) m_folder = defaultFolder( name );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicWorkflowLocationUi::name() const
{
return m_name().trimmed();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicWorkflowLocationUi::folder() const
{
return m_folder().path().trimmed();
}
//--------------------------------------------------------------------------------------------------
/// A folder in the discovery folder named after the workflow
//--------------------------------------------------------------------------------------------------
QString RicWorkflowLocationUi::defaultFolder( const QString& name )
{
return QDir( RimWorkflowCollection::discoveryDirectory() ).absoluteFilePath( RimWorkflowDefinitionTools::identifierFrom( name ) );
}
//--------------------------------------------------------------------------------------------------
/// Ask for a name and a folder. Returns nothing when cancelled. Warns before using a folder that
/// already holds a workflow, or a folder that will not be found by Rescan.
//--------------------------------------------------------------------------------------------------
std::optional<RicWorkflowLocationUi::Location> RicWorkflowLocationUi::askForLocation( const QString& title, const QString& defaultName )
{
RicWorkflowLocationUi ui;
ui.setName( defaultName );
QWidget* parent = Riu3DMainWindowTools::mainWindowWidget();
while ( true )
{
caf::PdmUiPropertyViewDialog dialog( parent, &ui, title, "", QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
dialog.resize( QSize( 500, 150 ) );
if ( dialog.exec() != QDialog::Accepted ) return std::nullopt;
if ( ui.name().isEmpty() || ui.folder().isEmpty() )
{
QMessageBox::warning( parent, title, "Enter both a name and a folder." );
continue;
}
const QDir folder( ui.folder() );
if ( folder.exists( "workflow.yaml" ) )
{
const auto answer =
QMessageBox::question( parent,
title,
QString( "'%1' already contains a workflow. Overwrite it when saving?" ).arg( ui.folder() ) );
if ( answer != QMessageBox::Yes ) continue;
}
const QString discovery = QDir( RimWorkflowCollection::discoveryDirectory() ).absolutePath();
if ( QDir( folder.absolutePath() + "/.." ).absolutePath() != discovery )
{
const auto answer = QMessageBox::question( parent,
title,
QString( "'%1' is not directly inside %2, so the workflow will not be listed "
"after Rescan or a restart. Use it anyway?" )
.arg( ui.folder(), discovery ) );
if ( answer != QMessageBox::Yes ) continue;
}
return Location{ .name = ui.name(), .folder = folder.absolutePath() };
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicWorkflowLocationUi::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue )
{
if ( changedField == &m_name )
{
if ( !m_folderEditedByUser ) m_folder = defaultFolder( m_name() );
}
else if ( changedField == &m_folder )
{
m_folderEditedByUser = true;
}
}