Files
ResInsight/ApplicationLibCode/Commands/OsduImportCommands/RiuWellLogImportWizard.cpp
2024-07-08 22:34:35 +02:00

291 lines
11 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- 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 "RiuWellLogImportWizard.h"
#include "RiaFeatureCommandContext.h"
#include "RiaOsduConnector.h"
#include <QAbstractTableModel>
#include <QObject>
#include <QString>
#include <QtNetwork>
#include <QtWidgets>
#include <algorithm>
#include <optional>
#include <vector>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuWellLogImportWizard::RiuWellLogImportWizard( RiaOsduConnector* osduConnector, const QString& wellboreId, QWidget* parent /*= 0*/ )
: QWizard( parent )
{
m_osduConnector = osduConnector;
m_wellboreId = wellboreId;
addPage( new WellLogAuthenticationPage( m_osduConnector, this ) );
addPage( new WellLogSelectionPage( m_osduConnector, this ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuWellLogImportWizard::~RiuWellLogImportWizard()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogImportWizard::downloadWellLogs( const QString& wellboreId )
{
m_osduConnector->requestWellLogsByWellboreId( wellboreId );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogImportWizard::setSelectedWellLogs( const std::vector<QString>& wellboreIds )
{
m_selectedWellLogs = wellboreIds;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<QString> RiuWellLogImportWizard::selectedWellLogs() const
{
return m_selectedWellLogs;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiuWellLogImportWizard::wellboreId() const
{
return m_wellboreId;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RiuWellLogImportWizard::WellLogInfo> RiuWellLogImportWizard::importedWellLogs() const
{
return m_wellLogInfos;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuWellLogImportWizard::addWellLogInfo( RiuWellLogImportWizard::WellLogInfo wellLogInfo )
{
m_wellLogInfos.push_back( wellLogInfo );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
WellLogAuthenticationPage::WellLogAuthenticationPage( RiaOsduConnector* osduConnector, QWidget* parent /*= 0*/ )
: QWizardPage( parent )
, m_osduConnector( osduConnector )
, m_accessOk( false )
{
setTitle( "OSDU - Login" );
QVBoxLayout* layout = new QVBoxLayout;
m_connectionLabel = new QLabel( "Checking OSDU connection. You might need to login." );
layout->addWidget( m_connectionLabel );
QFormLayout* formLayout = new QFormLayout;
layout->addLayout( formLayout );
QLineEdit* serverLineEdit = new QLineEdit( osduConnector->server(), this );
serverLineEdit->setReadOnly( true );
QLineEdit* partitionLineEdit = new QLineEdit( osduConnector->dataPartition(), this );
partitionLineEdit->setReadOnly( true );
formLayout->addRow( "Server:", serverLineEdit );
formLayout->addRow( "Data Partition:", partitionLineEdit );
setLayout( layout );
connect( osduConnector, SIGNAL( tokenReady( const QString& ) ), this, SLOT( accessOk() ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void WellLogAuthenticationPage::initializePage()
{
m_osduConnector->requestToken();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool WellLogAuthenticationPage::isComplete() const
{
return m_accessOk;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void WellLogAuthenticationPage::accessOk()
{
m_connectionLabel->setText( "Connection to OSDU: OK." );
m_accessOk = true;
emit( completeChanged() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
WellLogSelectionPage::WellLogSelectionPage( RiaOsduConnector* osduConnector, QWidget* parent /*= 0*/ )
{
QVBoxLayout* layout = new QVBoxLayout;
setLayout( layout );
QLabel* label = new QLabel( "Select well logs" );
layout->addWidget( label );
QHBoxLayout* filterLayout = new QHBoxLayout;
filterLayout->addWidget( new QLabel( "Filter:", this ) );
QLineEdit* filterLineEdit = new QLineEdit( this );
filterLayout->addWidget( filterLineEdit );
layout->addLayout( filterLayout );
m_tableView = new QTableView( this );
m_tableView->setSelectionBehavior( QAbstractItemView::SelectRows );
m_tableView->setSelectionMode( QAbstractItemView::MultiSelection );
m_tableView->setSortingEnabled( true );
int nameColumn = 2;
m_tableView->sortByColumn( nameColumn, Qt::AscendingOrder );
QHeaderView* header = m_tableView->horizontalHeader();
header->setSectionResizeMode( QHeaderView::Interactive );
header->setStretchLastSection( true );
m_osduWellLogsModel = new OsduWellLogTableModel;
layout->addWidget( m_tableView );
layout->setStretchFactor( m_tableView, 10 );
m_proxyModel = new QSortFilterProxyModel( this );
m_proxyModel->setSourceModel( m_osduWellLogsModel );
m_proxyModel->setFilterKeyColumn( nameColumn );
m_proxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
m_tableView->setModel( m_proxyModel );
m_tableView->setSortingEnabled( true );
QObject::connect( filterLineEdit, &QLineEdit::textChanged, m_proxyModel, &QSortFilterProxyModel::setFilterWildcard );
m_osduConnector = osduConnector;
connect( m_osduConnector, SIGNAL( wellLogsFinished( const QString& ) ), SLOT( wellLogsFinished( const QString& ) ) );
connect( m_tableView->selectionModel(),
SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
SLOT( selectWellLogs( const QItemSelection&, const QItemSelection& ) ) );
connect( m_tableView->selectionModel(),
SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
SIGNAL( completeChanged() ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void WellLogSelectionPage::initializePage()
{
RiuWellLogImportWizard* wiz = dynamic_cast<RiuWellLogImportWizard*>( wizard() );
if ( !wiz ) return;
QString wellboreId = wiz->wellboreId();
wiz->downloadWellLogs( wellboreId );
setButtonText( QWizard::NextButton, "Next" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
WellLogSelectionPage::~WellLogSelectionPage()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void WellLogSelectionPage::wellLogsFinished( const QString& wellboreId )
{
std::vector<OsduWellLog> wellLogs = m_osduConnector->wellLogs( wellboreId );
m_osduWellLogsModel->setOsduWellLogs( wellLogs );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool WellLogSelectionPage::isComplete() const
{
QItemSelectionModel* select = m_tableView->selectionModel();
return !select->selectedRows().empty();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void WellLogSelectionPage::selectWellLogs( const QItemSelection& newSelection, const QItemSelection& oldSelection )
{
if ( !newSelection.indexes().empty() )
{
RiuWellLogImportWizard* wiz = dynamic_cast<RiuWellLogImportWizard*>( wizard() );
std::vector<OsduWellLog> wellLogs = m_osduConnector->wellLogs( wiz->wellboreId() );
auto findWellLogById = []( const std::vector<OsduWellLog>& wellLogs, const QString& wellLogId ) -> std::optional<const OsduWellLog>
{
auto it = std::find_if( wellLogs.begin(), wellLogs.end(), [wellLogId]( const OsduWellLog& w ) { return w.id == wellLogId; } );
if ( it != wellLogs.end() )
return std::optional<const OsduWellLog>( *it );
else
return {};
};
QModelIndexList selection = m_tableView->selectionModel()->selectedRows();
for ( QModelIndex index : selection )
{
int idColumn = 0;
if ( index.column() == idColumn )
{
QString wellLogId = m_proxyModel->data( index.siblingAtColumn( idColumn ) ).toString();
std::optional<const OsduWellLog> wellLog = findWellLogById( wellLogs, wellLogId );
if ( wellLog.has_value() )
{
wiz->addWellLogInfo( { .name = wellLog->name, .wellLog = wellLogId } );
}
}
}
}
}