From e2d9dc2155d3ea23c76643066445cb8f5d53d7ac Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Mon, 29 Apr 2024 22:47:17 +0200 Subject: [PATCH] WIP: import and display well/wellbores. --- .../OsduImportCommands/RiaOsduConnector.cpp | 126 +++- .../OsduImportCommands/RiaOsduConnector.h | 30 +- .../RicWellPathsImportOsduFeature.cpp | 18 +- .../RiuWellImportWizard.cpp | 610 +++--------------- .../OsduImportCommands/RiuWellImportWizard.h | 153 +++-- 5 files changed, 332 insertions(+), 605 deletions(-) diff --git a/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.cpp b/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.cpp index 897aeb25cf4..9272dae6ebc 100644 --- a/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.cpp +++ b/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.cpp @@ -4,16 +4,15 @@ #include "RiaOsduOAuthHttpServerReplyHandler.h" #include +#include #include #include #include #include #include #include -#include - -#include #include +#include #include #include #include @@ -125,9 +124,17 @@ void RiaOsduConnector::requestFieldsByName( const QString& server, const QString params["kind"] = FIELD_KIND; params["limit"] = "10000"; params["query"] = "data.FieldName:IVAR*"; - makeRequest( params, server, dataPartitionId, token ); - connect( m_networkAccessManager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( parseFields( QNetworkReply* ) ) ); + auto reply = makeRequest( params, server, dataPartitionId, token ); + connect( reply, + &QNetworkReply::finished, + [this, reply]() + { + if ( reply->error() == QNetworkReply::NoError ) + { + parseFields( reply ); + } + } ); } //-------------------------------------------------------------------------------------------------- @@ -147,9 +154,25 @@ void RiaOsduConnector::requestWellsByFieldId( const QString& server, const QStri params["kind"] = WELL_KIND; params["limit"] = "10000"; params["query"] = QString( "nested(data.GeoContexts, (FieldID:\"%1\"))" ).arg( fieldId ); - makeRequest( params, server, dataPartitionId, token ); - connect( m_networkAccessManager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( parseWells( QNetworkReply* ) ) ); + auto reply = makeRequest( params, server, dataPartitionId, token ); + connect( reply, + &QNetworkReply::finished, + [this, reply, fieldId]() + { + if ( reply->error() == QNetworkReply::NoError ) + { + parseWells( reply ); + } + } ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiaOsduConnector::requestWellboresByWellId( const QString& wellId ) +{ + requestWellboresByWellId( m_server, m_dataPartitionId, m_token, wellId ); } //-------------------------------------------------------------------------------------------------- @@ -161,9 +184,17 @@ void RiaOsduConnector::requestWellboresByWellId( const QString& server, const QS params["kind"] = WELLBORE_KIND; params["limit"] = "10000"; params["query"] = "data.WellID: \"" + wellId + "\""; - makeRequest( params, server, dataPartitionId, token ); - connect( m_networkAccessManager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( parseWells( QNetworkReply* ) ) ); + auto reply = makeRequest( params, server, dataPartitionId, token ); + connect( reply, + &QNetworkReply::finished, + [this, reply, wellId]() + { + if ( reply->error() == QNetworkReply::NoError ) + { + parseWellbores( reply, wellId ); + } + } ); } //-------------------------------------------------------------------------------------------------- @@ -271,7 +302,7 @@ void RiaOsduConnector::parseFields( QNetworkReply* reply ) QJsonArray resultsArray = jsonObj["results"].toArray(); // Iterate through each element in the "results" array - qDebug() << "Found " << resultsArray.size() << " items."; + // qDebug() << "Found " << resultsArray.size() << " items."; m_fields.clear(); @@ -289,7 +320,7 @@ void RiaOsduConnector::parseFields( QNetworkReply* reply ) m_fields.push_back( OsduField{ id, kind, fieldName } ); } - emit wellsFinished(); + emit fieldsFinished(); } //-------------------------------------------------------------------------------------------------- @@ -310,22 +341,65 @@ void RiaOsduConnector::parseWells( QNetworkReply* reply ) // Access "results" array from the JSON object QJsonArray resultsArray = jsonObj["results"].toArray(); + // Iterate through each element in the "results" array + qDebug() << "Found " << resultsArray.size() << " wells."; + + m_wells.clear(); + foreach ( const QJsonValue& value, resultsArray ) + { + QJsonObject resultObj = value.toObject(); + + // Accessing specific fields from the result object + QString id = resultObj["id"].toString(); + QString kind = resultObj["kind"].toString(); + QString name = resultObj["data"].toObject()["FacilityName"].toString(); + + qDebug() << "Id:" << id << " kind: " << kind << " name: " << name; + qDebug() << resultObj; + m_wells.push_back( OsduWell{ id, kind, name } ); + } + + emit wellsFinished(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiaOsduConnector::parseWellbores( QNetworkReply* reply, const QString& wellId ) +{ + qDebug() << "REQUEST FINISHED. Error? " << ( reply->error() != QNetworkReply::NoError ); + + QByteArray result = reply->readAll(); + + reply->deleteLater(); + + QJsonDocument doc = QJsonDocument::fromJson( result ); + // Extract the JSON object from the QJsonDocument + QJsonObject jsonObj = doc.object(); + + // Access "results" array from the JSON object + QJsonArray resultsArray = jsonObj["results"].toArray(); + // Iterate through each element in the "results" array qDebug() << "Found " << resultsArray.size() << " items."; + + m_wellbores[wellId].clear(); foreach ( const QJsonValue& value, resultsArray ) { QJsonObject resultObj = value.toObject(); // Accessing specific fields from the result object - QString id = resultObj["id"].toString(); - QString kind = resultObj["kind"].toString(); - QString fieldName = resultObj["data"].toObject()["FieldName"].toString(); + QString id = resultObj["id"].toString(); + QString kind = resultObj["kind"].toString(); + QString name = resultObj["data"].toObject()["FacilityName"].toString(); - qDebug() << "Id:" << id << " kind: " << kind << " name: " << fieldName; + qDebug() << "Id:" << id << " kind: " << kind << " name: " << name; qDebug() << resultObj; + + m_wellbores[wellId].push_back( OsduWellbore{ id, kind, name, wellId } ); } - emit finished(); + emit wellboresFinished( wellId ); } //-------------------------------------------------------------------------------------------------- @@ -473,3 +547,23 @@ std::vector RiaOsduConnector::fields() const { return m_fields; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::vector RiaOsduConnector::wells() const +{ + return m_wells; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::vector RiaOsduConnector::wellbores( const QString& wellId ) const +{ + auto it = m_wellbores.find( wellId ); + if ( it != m_wellbores.end() ) + return it->second; + else + return {}; +} diff --git a/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.h b/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.h index cd6b04f1a41..c93246ef96e 100644 --- a/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.h +++ b/ApplicationLibCode/Commands/OsduImportCommands/RiaOsduConnector.h @@ -14,6 +14,21 @@ struct OsduField QString name; }; +struct OsduWell +{ + QString id; + QString kind; + QString name; +}; + +struct OsduWellbore +{ + QString id; + QString kind; + QString name; + QString wellId; +}; + //================================================================================================== /// //================================================================================================== @@ -31,25 +46,30 @@ class RiaOsduConnector : public QObject void requestFieldsByName( const QString& token, const QString& fieldName ); void requestFieldsByName( const QString& fieldName ); - void requestWellsByFieldId( const QString& fieldId ); + void requestWellboresByWellId( const QString& wellId ); QString server() const; QString dataPartition() const; - std::vector fields() const; + std::vector fields() const; + std::vector wells() const; + std::vector wellbores( const QString& wellId ) const; public slots: void requestToken(); void parseFields( QNetworkReply* reply ); void parseWells( QNetworkReply* reply ); + void parseWellbores( QNetworkReply* reply, const QString& wellId ); void parseWellTrajectory( QNetworkReply* reply ); void saveFile( QNetworkReply* reply ); void accessGranted(); signals: void finished(); + void fieldsFinished(); void wellsFinished(); + void wellboresFinished( const QString& wellId ); void tokenReady( const QString& token ); private: @@ -84,6 +104,8 @@ public slots: const QString m_scopes; const QString m_clientId; - QString m_token; - std::vector m_fields; + QString m_token; + std::vector m_fields; + std::vector m_wells; + std::map> m_wellbores; }; diff --git a/ApplicationLibCode/Commands/OsduImportCommands/RicWellPathsImportOsduFeature.cpp b/ApplicationLibCode/Commands/OsduImportCommands/RicWellPathsImportOsduFeature.cpp index 8575f021475..f70a86a51c6 100644 --- a/ApplicationLibCode/Commands/OsduImportCommands/RicWellPathsImportOsduFeature.cpp +++ b/ApplicationLibCode/Commands/OsduImportCommands/RicWellPathsImportOsduFeature.cpp @@ -103,15 +103,15 @@ void RicWellPathsImportSsihubFeature::onActionTriggered( bool isChecked ) if ( QDialog::Accepted == wellImportwizard.exec() ) { - QStringList wellPaths = wellImportwizard.absoluteFilePathsToWellPaths(); - if ( !wellPaths.empty() ) - { - QStringList errorMessages; - app->addWellPathsToModel( wellPaths, &errorMessages ); - app->project()->scheduleCreateDisplayModelAndRedrawAllViews(); - } - - app->setCacheDataObject( "ssihub_username", wellImportwizard.field( "username" ) ); + // QStringList wellPaths = wellImportwizard.absoluteFilePathsToWellPaths(); + // if ( !wellPaths.empty() ) + // { + // QStringList errorMessages; + // app->addWellPathsToModel( wellPaths, &errorMessages ); + // app->project()->scheduleCreateDisplayModelAndRedrawAllViews(); + // } + + // app->setCacheDataObject( "ssihub_username", wellImportwizard.field( "username" ) ); } else { diff --git a/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.cpp b/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.cpp index 43669d5f9d7..53d6133ff25 100644 --- a/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.cpp +++ b/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.cpp @@ -21,8 +21,6 @@ #include "RiaFeatureCommandContext.h" #include "RiaOsduConnector.h" -#include "RimOilFieldEntry.h" -#include "RimOilRegionEntry.h" #include "RimWellPathImport.h" #include "cafCmdFeatureMenuBuilder.h" @@ -47,8 +45,6 @@ #include #include -CAF_PDM_XML_ABSTRACT_SOURCE_INIT( ObjectGroupWithHeaders, "ObjectGroupWithHeaders" ); // Do not use. Abstract class - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -68,10 +64,8 @@ RiuWellImportWizard::RiuWellImportWizard( const QString& downloadFolder, addPage( new AuthenticationPage( m_osduConnector, this ) ); m_fieldSelectionPageId = addPage( new FieldSelectionPage( m_wellPathImportObject, m_osduConnector, this ) ); - m_wellSelectionPageId = addPage( new WellSelectionPage( m_wellPathImportObject, this ) ); + m_wellSelectionPageId = addPage( new WellSelectionPage( m_wellPathImportObject, m_osduConnector, this ) ); m_wellSummaryPageId = addPage( new WellSummaryPage( m_wellPathImportObject, this ) ); - - connect( this, SIGNAL( currentIdChanged( int ) ), SLOT( slotCurrentIdChanged( int ) ) ); } //-------------------------------------------------------------------------------------------------- @@ -91,113 +85,6 @@ void RiuWellImportWizard::downloadFields() m_osduConnector->requestFieldsByName( "IVAR" ); } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiuWellImportWizard::httpFinished() -{ - // if ( m_httpRequestAborted ) - // { - // if ( m_file ) - // { - // m_file->close(); - // m_file->remove(); - // delete m_file; - // m_file = nullptr; - // } - // m_reply->deleteLater(); - // hideProgressDialog(); - // return; - // } - - // m_file->flush(); - // m_file->close(); - - // QVariant redirectionTarget = m_reply->attribute( QNetworkRequest::RedirectionTargetAttribute ); - // if ( m_reply->error() ) - // { - // m_file->remove(); - // QMessageBox::information( this, tr( "HTTP" ), tr( "Download failed: %1." ).arg( m_reply->errorString() ) ); - // } - // else if ( !redirectionTarget.isNull() ) - // { - // QUrl newUrl = m_url.resolved( redirectionTarget.toUrl() ); - // if ( QMessageBox::question( this, tr( "HTTP" ), tr( "Redirect to %1 ?" ).arg( newUrl.toString() ), QMessageBox::Yes | - // QMessageBox::No ) == - // QMessageBox::Yes ) - // { - // m_url = newUrl; - // m_reply->deleteLater(); - // m_file->open( QIODevice::WriteOnly ); - // m_file->resize( 0 ); - // startRequest( m_url ); - // return; - // } - // } - // else - // { - // // m_statusLabel->setText(tr("Downloaded data to %1.").arg(m_destinationFolder)); - // } - - // if ( m_currentDownloadState == DOWNLOAD_WELL_PATH ) - // { - // QString singleWellPathFilePath = "junk"; // m_file->fileName(); - - // QFile file( singleWellPathFilePath ); - // if ( file.open( QFile::ReadOnly ) ) - // { - // QString singleWellPathContent = file.readAll(); - - // // Strip leading and trailing [] - - // if ( singleWellPathContent.indexOf( '{' ) > 0 ) - // { - // singleWellPathContent = singleWellPathContent.right( singleWellPathContent.size() - singleWellPathContent.indexOf( '{' ) - // ); - // } - - // if ( singleWellPathContent[singleWellPathContent.size() - 1] == ']' ) - // { - // singleWellPathContent = singleWellPathContent.left( singleWellPathContent.size() - 1 ); - // } - - // QString wellPathName = getValue( "name", singleWellPathContent ); - // if ( !singleWellPathContent.isEmpty() && !wellPathName.isEmpty() ) - // { - // // Write out the content without leading/trailing [] - // file.close(); - // QFile::remove( singleWellPathFilePath ); - - // if ( file.open( QFile::WriteOnly ) ) - // { - // QTextStream out( &file ); - // out << singleWellPathContent; - // } - // } - - // progressDialog()->setLabelText( QString( "Downloaded well path : %1" ).arg( wellPathName ) ); - // } - - // int newValue = progressDialog()->maximum() - m_wellRequestQueue.size(); - // progressDialog()->setValue( newValue ); - // } - - // // m_reply->deleteLater(); - // // m_reply = nullptr; - // // delete m_file; - // // m_file = nullptr; - - // if ( m_currentDownloadState == DOWNLOAD_WELLS || m_currentDownloadState == DOWNLOAD_WELL_PATH ) - // { - // checkDownloadQueueAndIssueRequests(); - // } - // else if ( m_currentDownloadState == DOWNLOAD_FIELDS ) - // { - // updateFieldsModel(); - // m_currentDownloadState = DOWNLOAD_UNDEFINED; - // } -} - //-------------------------------------------------------------------------------------------------- /// This slot will be called for the first network reply that will need authentication. /// If the authentication is successful, the username/password is cached in the QNetworkAccessManager @@ -212,8 +99,7 @@ void RiuWellImportWizard::slotAuthenticationRequired( QNetworkReply* networkRepl { QMessageBox::information( this, "Authentication failed", - "Failed to authenticate credentials. You will now be directed back to the first " - "wizard page." ); + "Failed to authenticate. You will now be directed back to the first wizard page." ); m_firstTimeRequestingAuthentication = true; restart(); @@ -252,48 +138,6 @@ void RiuWellImportWizard::hideProgressDialog() void RiuWellImportWizard::downloadWells( const QString& fieldId ) { m_osduConnector->requestWellsByFieldId( fieldId ); - - // for ( size_t rIdx = 0; rIdx < m_wellPathImportObject->regions.size(); rIdx++ ) - // { - // RimOilRegionEntry* oilRegion = m_wellPathImportObject->regions[rIdx]; - // if ( oilRegion->selected ) - // { - // for ( size_t fIdx = 0; fIdx < oilRegion->fields.size(); fIdx++ ) - // { - // RimOilFieldEntry* oilField = oilRegion->fields[fIdx]; - // if ( oilField->selected ) - // { - // DownloadEntity urlToFile; - - // QString wellRequest; - // if ( m_wellPathImportObject->utmFilterMode == RimWellPathImport::UTM_FILTER_OFF ) - // { - // wellRequest = QString( "/resinsight/projects/%1/wells" ).arg( oilField->edmId ); - // } - // else - // { - // wellRequest = QString( "/resinsight/projects/%1/" - // "wellsInArea?north=%2&south=%3&east=%4&west=%5&utmzone=32N" ) - // .arg( oilField->edmId ) - // .arg( QString::number( m_wellPathImportObject->north, 'g', 10 ) ) - // .arg( QString::number( m_wellPathImportObject->south, 'g', 10 ) ) - // .arg( QString::number( m_wellPathImportObject->east, 'g', 10 ) ) - // .arg( QString::number( m_wellPathImportObject->west, 'g', 10 ) ); - // } - - // urlToFile.requestUrl = m_webServiceAddress + wellRequest; - // urlToFile.responseFilename = m_destinationFolder + QString( "/wells_%1.json" ).arg( oilField->edmId ); - - // oilField->wellsFilePath = urlToFile.responseFilename; - - // m_wellRequestQueue.push_back( urlToFile ); - // } - // } - // } - // } - - // m_currentDownloadState = DOWNLOAD_WELLS; - // checkDownloadQueueAndIssueRequests(); } //-------------------------------------------------------------------------------------------------- @@ -301,22 +145,6 @@ void RiuWellImportWizard::downloadWells( const QString& fieldId ) //-------------------------------------------------------------------------------------------------- void RiuWellImportWizard::downloadWellPaths() { - // WellSelectionPage* wellSelectionPage = dynamic_cast( page( m_wellSelectionPageId ) ); - // std::vector downloadEntities; - // wellSelectionPage->selectedWellPathEntries( downloadEntities, nullptr ); - - // for ( size_t i = 0; i < downloadEntities.size(); i++ ) - // { - // m_wellRequestQueue.push_back( downloadEntities[i] ); - // } - - // m_currentDownloadState = DOWNLOAD_WELL_PATH; - - // progressDialog()->setMaximum( m_wellRequestQueue.size() ); - // progressDialog()->setValue( 0 ); - // progressDialog()->show(); - - // checkDownloadQueueAndIssueRequests(); } //-------------------------------------------------------------------------------------------------- @@ -329,147 +157,6 @@ void RiuWellImportWizard::resetAuthenticationCount() // m_osduConnector->requestToken(); } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -QStringList RiuWellImportWizard::absoluteFilePathsToWellPaths() const -{ - QStringList filePaths; - - WellSelectionPage* wellSelectionPage = dynamic_cast( page( m_wellSelectionPageId ) ); - std::vector downloadEntities; - wellSelectionPage->selectedWellPathEntries( downloadEntities, nullptr ); - - for ( size_t i = 0; i < downloadEntities.size(); i++ ) - { - if ( caf::Utils::fileExists( downloadEntities[i].responseFilename ) ) - { - filePaths.push_back( downloadEntities[i].responseFilename ); - } - } - - return filePaths; -} - -//-------------------------------------------------------------------------------------------------- -/// Set wells hidden from the field selection page -/// TODO: This can be refactored when UIOrdering for objects is created -//-------------------------------------------------------------------------------------------------- -void RiuWellImportWizard::slotCurrentIdChanged( int currentId ) -{ - bool hideWells = true; - if ( currentId == m_wellSelectionPageId ) hideWells = false; - - // for ( size_t rIdx = 0; rIdx < m_wellPathImportObject->regions.size(); rIdx++ ) - // { - // RimOilRegionEntry* oilRegion = m_wellPathImportObject->regions[rIdx]; - // { - // for ( size_t fIdx = 0; fIdx < oilRegion->fields.size(); fIdx++ ) - // { - // RimOilFieldEntry* oilField = oilRegion->fields[fIdx]; - // oilField->wells.uiCapability()->setUiHidden( hideWells ); - // } - // } - // } - - // Update the editors to propagate the changes to UI - m_wellPathImportObject->updateConnectedEditors(); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiuWellImportWizard::parseWellsResponse( RimOilFieldEntry* oilFieldEntry ) -{ - // QStringList surveyNames; - // QStringList planNames; - - // if ( caf::Utils::fileExists( oilFieldEntry->wellsFilePath ) ) - // { - // QMap jsonMap = ResInsightInternalJson::JsonReader::decodeFile( oilFieldEntry->wellsFilePath ); - // QVariantList variantList = ResInsightInternalJson::JsonReader::getVariantList( jsonMap ); - // for ( const auto& listItem : variantList ) - // { - // QMap rootMap = listItem.toMap(); - - // // if ( m_wellPathImportObject->wellTypeSurvey ) - // // { - // // QMap surveyMap = rootMap["survey"].toMap(); - // // QString name = surveyMap["name"].toString(); - - // // if ( !oilFieldEntry->find( name, RimWellPathEntry::WELL_SURVEY ) ) - // // { - // // QMap linksMap = surveyMap["links"].toMap(); - // // QString requestUrl = m_webServiceAddress + linksMap["entity"].toString(); - // // QString surveyType = surveyMap["surveyType"].toString(); - // // RimWellPathEntry* surveyWellPathEntry = - // // RimWellPathEntry::createWellPathEntry( name, surveyType, requestUrl, m_destinationFolder, - // // RimWellPathEntry::WELL_SURVEY ); - // // oilFieldEntry->wells.push_back( surveyWellPathEntry ); - // // } - - // // surveyNames.push_back( name ); - // // } - - // // if ( m_wellPathImportObject->wellTypePlans ) - // // { - // // QList plansList = rootMap["plans"].toList(); - // // QListIterator planIt( plansList ); - // // while ( planIt.hasNext() ) - // // { - // // QMap planMap = planIt.next().toMap(); - // // QString name = planMap["name"].toString(); - - // // if ( !oilFieldEntry->find( name, RimWellPathEntry::WELL_PLAN ) ) - // // { - // // QMap linksMap = planMap["links"].toMap(); - // // QString requestUrl = m_webServiceAddress + linksMap["entity"].toString(); - // // QString surveyType = planMap["surveyType"].toString(); - // // RimWellPathEntry* surveyWellPathEntry = - // // RimWellPathEntry::createWellPathEntry( name, surveyType, requestUrl, m_destinationFolder, - // // RimWellPathEntry::WELL_PLAN ); - // // oilFieldEntry->wells.push_back( surveyWellPathEntry ); - // // } - - // // planNames.push_back( name ); - // // } - // // } - // } - // } - - // // Delete the well path entries in the model that are not part of the reply from the web service - // std::vector wellsToRemove; - - // for ( size_t i = 0; i < oilFieldEntry->wells.size(); i++ ) - // { - // RimWellPathEntry* wellPathEntry = oilFieldEntry->wells[i]; - // if ( wellPathEntry->wellPathType == RimWellPathEntry::WELL_PLAN ) - // { - // if ( !planNames.contains( wellPathEntry->name ) ) - // { - // wellsToRemove.push_back( wellPathEntry ); - // } - // } - // else - // { - // if ( !surveyNames.contains( wellPathEntry->name ) ) - // { - // wellsToRemove.push_back( wellPathEntry ); - // } - // } - // } - - // for ( size_t i = 0; i < wellsToRemove.size(); i++ ) - // { - // oilFieldEntry->wells.removeChild( wellsToRemove[i] ); - - // delete wellsToRemove[i]; - // } - - // WellSelectionPage* wellSelectionPage = dynamic_cast( page( m_wellSelectionPageId ) ); - // if ( wellSelectionPage ) wellSelectionPage->buildWellTreeView(); -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -561,7 +248,11 @@ FieldSelectionPage::FieldSelectionPage( RimWellPathImport* wellPathImport, RiaOs setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); m_osduConnector = osduConnector; - connect( m_osduConnector, SIGNAL( wellsFinished() ), SLOT( wellsFinished() ) ); + connect( m_osduConnector, SIGNAL( fieldsFinished() ), SLOT( fieldsFinished() ) ); + + connect( m_tableView->selectionModel(), + SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ), + SLOT( selectField( const QItemSelection&, const QItemSelection& ) ) ); connect( m_tableView->selectionModel(), SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ), @@ -580,12 +271,28 @@ void FieldSelectionPage::initializePage() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void FieldSelectionPage::wellsFinished() +void FieldSelectionPage::fieldsFinished() { std::vector fields = m_osduConnector->fields(); m_osduFieldsModel->setOsduFields( fields ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void FieldSelectionPage::selectField( const QItemSelection& newSelection, const QItemSelection& oldSelection ) +{ + qDebug() << "Select field: " << newSelection.indexes().size(); + if ( !newSelection.indexes().empty() ) + { + QModelIndex index = newSelection.indexes()[0]; + int column = 0; + QString fieldId = m_osduFieldsModel->data( index.siblingAtColumn( column ) ).toString(); + RiuWellImportWizard* wiz = dynamic_cast( wizard() ); + wiz->setSelectedFieldId( fieldId ); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -606,7 +313,7 @@ FieldSelectionPage::~FieldSelectionPage() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -WellSelectionPage::WellSelectionPage( RimWellPathImport* wellPathImport, QWidget* parent /*= 0*/ ) +WellSelectionPage::WellSelectionPage( RimWellPathImport* wellPathImport, RiaOsduConnector* osduConnector, QWidget* parent /*= 0*/ ) { QVBoxLayout* layout = new QVBoxLayout; setLayout( layout ); @@ -614,21 +321,22 @@ WellSelectionPage::WellSelectionPage( RimWellPathImport* wellPathImport, QWidget QLabel* label = new QLabel( "Select wells" ); layout->addWidget( label ); - m_wellSelectionTreeView = new caf::PdmUiTreeView( this ); - m_wellSelectionTreeView->treeView()->setContextMenuPolicy( Qt::CustomContextMenu ); - m_wellSelectionTreeView->enableSelectionManagerUpdating( true ); - m_wellSelectionTreeView->treeView()->setSelectionMode( QAbstractItemView::ExtendedSelection ); - - connect( m_wellSelectionTreeView->treeView(), - SIGNAL( customContextMenuRequested( const QPoint& ) ), - SLOT( customMenuRequested( const QPoint& ) ) ); - - layout->addWidget( m_wellSelectionTreeView ); + m_tableView = new QTableView( this ); + m_tableView->setSelectionBehavior( QAbstractItemView::SelectRows ); + m_osduWellboresModel = new OsduWellboreTableModel; + m_tableView->setModel( m_osduWellboresModel ); + layout->addWidget( m_tableView ); + layout->setStretchFactor( m_tableView, 10 ); m_wellPathImportObject = wellPathImport; - m_regionsWithVisibleWells = new ObjectGroupWithHeaders; - m_regionsWithVisibleWells->objects.uiCapability()->setUiHidden( true ); + m_osduConnector = osduConnector; + connect( m_osduConnector, SIGNAL( wellsFinished() ), SLOT( wellsFinished() ) ); + connect( m_osduConnector, SIGNAL( wellboresFinished( const QString& ) ), SLOT( wellboresFinished( const QString& ) ) ); + + connect( m_tableView->selectionModel(), + SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ), + SIGNAL( completeChanged() ) ); } //-------------------------------------------------------------------------------------------------- @@ -639,157 +347,53 @@ void WellSelectionPage::initializePage() RiuWellImportWizard* wiz = dynamic_cast( wizard() ); if ( !wiz ) return; - QString fieldId = "ad214d87-84eb-19da-e053-c918a4889309"; + QString fieldId = wiz->selectedFieldId(); + qDebug() << "FIELD ID: " << fieldId; wiz->downloadWells( fieldId ); - setButtonText( QWizard::NextButton, "Download" ); + setButtonText( QWizard::NextButton, "Import" ); } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -// void WellSelectionPage::buildWellTreeView() -// { -// for ( size_t rIdx = 0; rIdx < m_regionsWithVisibleWells->objects.size(); rIdx++ ) -// { -// caf::PdmObjectGroup* regGroup = dynamic_cast( m_regionsWithVisibleWells->objects[rIdx] ); -// if ( !regGroup ) continue; - -// for ( size_t fIdx = 0; fIdx < regGroup->objects.size(); fIdx++ ) -// { -// caf::PdmObjectGroup* fieldGroup = dynamic_cast( regGroup->objects[fIdx] ); -// if ( !fieldGroup ) continue; - -// // RimWellPathEntry objects are present here, they must be taken out out the container, but not deleted -// // If fieldGroup->objects->deleteObjects is performed, the objects are deleted -// fieldGroup->objects.clear(); -// } -// } - -// // Delete all temporary pdm object groups -// m_regionsWithVisibleWells->objects.deleteChildren(); - -// m_wellSelectionTreeView->setPdmItem( m_regionsWithVisibleWells ); -// m_regionsWithVisibleWells->updateConnectedEditors(); - -// m_wellSelectionTreeView->treeView()->expandAll(); -// } - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- WellSelectionPage::~WellSelectionPage() { - if ( m_wellSelectionTreeView ) - { - m_wellSelectionTreeView->setPdmItem( nullptr ); - } - delete m_regionsWithVisibleWells; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void WellSelectionPage::selectedWellPathEntries( std::vector& downloadEntities, caf::PdmObjectHandle* objHandle ) -{ - if ( objHandle == nullptr ) - { - objHandle = m_regionsWithVisibleWells; - } - - std::vector childFields = objHandle->fields(); - for ( size_t i = 0; i < childFields.size(); i++ ) - { - std::vector childObjects = childFields[i]->children(); - - for ( size_t j = 0; j < childObjects.size(); j++ ) - { - RimWellPathEntry* wellPathEntry = ( dynamic_cast( childObjects[j] ) ); - if ( wellPathEntry ) - { - if ( wellPathEntry->selected && wellPathEntry->isWellPathValid() ) - { - DownloadEntity urlToFile; - - urlToFile.name = wellPathEntry->name; - urlToFile.requestUrl = wellPathEntry->requestUrl; - urlToFile.responseFilename = wellPathEntry->wellPathFilePath; - - downloadEntities.push_back( urlToFile ); - } - } - else - { - selectedWellPathEntries( downloadEntities, childObjects[j] ); - } - } - } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void WellSelectionPage::customMenuRequested( const QPoint& pos ) +void WellSelectionPage::wellsFinished() { - QMenu menu; - - RiaFeatureCommandContextHelper helper( m_wellSelectionTreeView ); - - caf::CmdFeatureMenuBuilder menuBuilder; + std::vector wells = m_osduConnector->wells(); - menuBuilder << "RicToggleItemsOnFeature"; - menuBuilder << "RicToggleItemsOffFeature"; - menuBuilder << "RicToggleItemsFeature"; - menuBuilder << "RicToggleItemsOnOthersOffFeature"; + qDebug() << "WELLS FINISHED: " << wells.size(); - menuBuilder.appendToMenu( &menu ); - - // Qt doc: QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the - // viewport(). Since we might get this signal from different treeViews, we need to map the position accordingly. - QObject* senderObj = sender(); - QTreeView* treeView = dynamic_cast( senderObj ); - if ( treeView ) + for ( auto w : wells ) { - QPoint globalPos = treeView->viewport()->mapToGlobal( pos ); - menu.exec( globalPos ); + m_osduConnector->requestWellboresByWellId( w.id ); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool lessByDescription( const caf::PdmPointer& obj1, const caf::PdmPointer& obj2 ) +void WellSelectionPage::wellboresFinished( const QString& wellId ) { - caf::PdmUiFieldHandle* uiFieldHandle1 = nullptr; - caf::PdmUiFieldHandle* uiFieldHandle2 = nullptr; - - if ( obj1.notNull() && obj1->uiCapability() && obj1->uiCapability()->userDescriptionField() ) - { - uiFieldHandle1 = obj1->uiCapability()->userDescriptionField()->uiCapability(); - } - - if ( obj2.notNull() && obj2->uiCapability() && obj2->uiCapability()->userDescriptionField() ) - { - uiFieldHandle2 = obj2->uiCapability()->userDescriptionField()->uiCapability(); - } - - if ( uiFieldHandle1 && uiFieldHandle2 ) - { - QString string1 = uiFieldHandle1->uiValue().toString(); - QString string2 = uiFieldHandle2->uiValue().toString(); - - return string1 < string2; - } - - return true; + std::vector wellbores = m_osduConnector->wellbores( wellId ); + qDebug() << "Wellbores for " << wellId << ": " << wellbores.size(); + // TODO: change to an append operation + if ( !wellbores.empty() ) m_osduWellboresModel->setOsduWellbores( wellbores ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void WellSelectionPage::sortObjectsByDescription( caf::PdmObjectCollection* objects ) +bool WellSelectionPage::isComplete() const { - std::sort( objects->objects.begin(), objects->objects.end(), lessByDescription ); + QItemSelectionModel* select = m_tableView->selectionModel(); + return select->selectedRows().size() == 1; } //-------------------------------------------------------------------------------------------------- @@ -835,90 +439,62 @@ void WellSummaryPage::initializePage() //-------------------------------------------------------------------------------------------------- void WellSummaryPage::updateSummaryPage() { - m_objectGroup->objects.deleteChildren(); + // m_textEdit->setText( "Summary of imported wells\n\n" ); - m_textEdit->setText( "Summary of imported wells\n\n" ); + // size_t wellPathCount = 0; + // QString errorString; - size_t wellPathCount = 0; - QString errorString; + // RiuWellImportWizard* wiz = dynamic_cast( wizard() ); + // WellSelectionPage* wellSelectionPage = dynamic_cast( wiz->page( wiz->wellSelectionPageId() ) ); + // std::vector downloadEntities; + // wellSelectionPage->selectedWellPathEntries( downloadEntities, nullptr ); - RiuWellImportWizard* wiz = dynamic_cast( wizard() ); - WellSelectionPage* wellSelectionPage = dynamic_cast( wiz->page( wiz->wellSelectionPageId() ) ); - std::vector downloadEntities; - wellSelectionPage->selectedWellPathEntries( downloadEntities, nullptr ); + // for ( size_t i = 0; i < downloadEntities.size(); i++ ) + // { + // if ( caf::Utils::fileExists( downloadEntities[i].responseFilename ) ) + // { + // wellPathCount++; + // } + // else + // { + // errorString += + // QString( "Failed to get file '%1' from well '%2'\n" ).arg( downloadEntities[i].responseFilename ).arg( + // downloadEntities[i].name ); + // } - for ( size_t i = 0; i < downloadEntities.size(); i++ ) - { - if ( caf::Utils::fileExists( downloadEntities[i].responseFilename ) ) - { - wellPathCount++; - } - else - { - errorString += - QString( "Failed to get file '%1' from well '%2'\n" ).arg( downloadEntities[i].responseFilename ).arg( downloadEntities[i].name ); - } - - SummaryPageDownloadEntity* sumPageEntity = new SummaryPageDownloadEntity; - sumPageEntity->name = downloadEntities[i].name; - sumPageEntity->responseFilename = downloadEntities[i].responseFilename; - sumPageEntity->requestUrl = downloadEntities[i].requestUrl; - - m_objectGroup->objects().push_back( sumPageEntity ); - } + // SummaryPageDownloadEntity* sumPageEntity = new SummaryPageDownloadEntity; + // sumPageEntity->name = downloadEntities[i].name; + // sumPageEntity->responseFilename = downloadEntities[i].responseFilename; + // sumPageEntity->requestUrl = downloadEntities[i].requestUrl; - m_textEdit->setText( QString( "Downloaded successfully %1 well paths.\nPlease push 'Import' button to import well " - "paths into ResInsight.\n\n" ) - .arg( wellPathCount ) ); - if ( !errorString.isEmpty() ) - { - m_textEdit->append( "Detected following errors during well path download. See details below." ); - m_textEdit->append( errorString ); - } + // m_objectGroup->objects().push_back( sumPageEntity ); + // } - m_listView->setPdmObject( m_objectGroup ); - m_objectGroup->updateConnectedEditors(); -} + // m_textEdit->setText( QString( "Downloaded successfully %1 well paths.\nPlease push 'Import' button to import well " + // "paths into ResInsight.\n\n" ) + // .arg( wellPathCount ) ); + // if ( !errorString.isEmpty() ) + // { + // m_textEdit->append( "Detected following errors during well path download. See details below." ); + // m_textEdit->append( errorString ); + // } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void WellSummaryPage::slotShowDetails() -{ - if ( m_listView->isHidden() ) - { - m_listView->show(); - } - else - { - m_listView->hide(); - } + // m_listView->setPdmObject( m_objectGroup ); + // m_objectGroup->updateConnectedEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void ObjectGroupWithHeaders::defineObjectEditorAttribute( QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) +void RiuWellImportWizard::setSelectedFieldId( const QString& fieldId ) { - caf::PdmUiTreeViewEditorAttribute* myAttr = dynamic_cast( attribute ); - if ( myAttr ) - { - QStringList colHeaders; - colHeaders << "Wells"; - myAttr->columnHeaders = colHeaders; - } + m_selectedFieldId = fieldId; } -CAF_PDM_SOURCE_INIT( SummaryPageDownloadEntity, "SummaryPageDownloadEntity" ); - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -SummaryPageDownloadEntity::SummaryPageDownloadEntity() +QString RiuWellImportWizard::selectedFieldId() const { - CAF_PDM_InitObject( "SummaryPageDownloadEntity" ); - - CAF_PDM_InitFieldNoDefault( &name, "Name", "" ); - CAF_PDM_InitFieldNoDefault( &requestUrl, "RequestUrl", "" ); - CAF_PDM_InitFieldNoDefault( &responseFilename, "ResponseFilename", "" ); + return m_selectedFieldId; } diff --git a/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.h b/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.h index e8573916021..67a4c46f8d6 100644 --- a/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.h +++ b/ApplicationLibCode/Commands/OsduImportCommands/RiuWellImportWizard.h @@ -59,8 +59,6 @@ class OsduFieldTableModel : public QAbstractTableModel { } - // ~OsduFieldTableModel() override {} - int rowCount( const QModelIndex& parent = QModelIndex() ) const override { Q_UNUSED( parent ); @@ -122,21 +120,95 @@ class OsduFieldTableModel : public QAbstractTableModel void setOsduFields( const std::vector& osduFields ) { - beginInsertRows( QModelIndex(), 0, 0 ); // notify views and proxy models that a line will be inserted - + beginInsertRows( QModelIndex(), 0, 0 ); m_osduFields = osduFields; - // auto topLeft = createIndex( 0, 0 ); - // auto bottomRight = createIndex( m_osduFields.size(), 3 ); - // m_data.prepend( somedata ); // do the modification to the model data endInsertRows(); - - // emit dataChanged( topLeft, bottomRight ); } private: std::vector m_osduFields; }; +class OsduWellboreTableModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + explicit OsduWellboreTableModel( QObject* parent = nullptr ) + : QAbstractTableModel( parent ) + { + } + + int rowCount( const QModelIndex& parent = QModelIndex() ) const override + { + Q_UNUSED( parent ); + return m_osduWellbores.size(); + } + + int columnCount( const QModelIndex& parent = QModelIndex() ) const override + { + Q_UNUSED( parent ); + // Assuming you have three fields: id, kind, and name + return 3; + } + + QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override + { + if ( !index.isValid() ) return QVariant(); + + if ( index.row() >= static_cast( m_osduWellbores.size() ) || index.row() < 0 ) return QVariant(); + + if ( role == Qt::DisplayRole ) + { + const OsduWellbore& field = m_osduWellbores.at( index.row() ); + switch ( index.column() ) + { + case 0: + return field.id; + case 1: + return field.kind; + case 2: + return field.name; + default: + return QVariant(); + } + } + + return QVariant(); + } + + QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override + { + if ( role != Qt::DisplayRole ) return QVariant(); + + if ( orientation == Qt::Horizontal ) + { + switch ( section ) + { + case 0: + return tr( "ID" ); + case 1: + return tr( "Kind" ); + case 2: + return tr( "Name" ); + default: + return QVariant(); + } + } + return QVariant(); + } + + void setOsduWellbores( const std::vector& osduWellbores ) + { + beginInsertRows( QModelIndex(), 0, 0 ); + m_osduWellbores = osduWellbores; + endInsertRows(); + } + +private: + std::vector m_osduWellbores; +}; + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -157,8 +229,6 @@ private slots: bool m_accessOk; }; -class OsduFieldTableModel; - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -173,7 +243,8 @@ class FieldSelectionPage : public QWizardPage void initializePage() override; bool isComplete() const override; private slots: - void wellsFinished(); + void fieldsFinished(); + void selectField( const QItemSelection& newSelection, const QItemSelection& oldSelection ); private: caf::PdmUiPropertyView* m_propertyView; @@ -182,34 +253,6 @@ private slots: OsduFieldTableModel* m_osduFieldsModel; }; -//-------------------------------------------------------------------------------------------------- -/// Container class used to define column headers -//-------------------------------------------------------------------------------------------------- -class ObjectGroupWithHeaders : public caf::PdmObject -{ - CAF_PDM_HEADER_INIT; - -public: - ObjectGroupWithHeaders() - { - CAF_PDM_InitFieldNoDefault( &objects, "PdmObjects", "" ); - - CAF_PDM_InitField( &m_isChecked, "IsChecked", true, "Active" ); - m_isChecked.uiCapability()->setUiHidden( true ); - }; - - void defineObjectEditorAttribute( QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override; - -public: - caf::PdmChildArrayField objects; - -protected: - caf::PdmFieldHandle* objectToggleField() override { return &m_isChecked; } - -protected: - caf::PdmField m_isChecked; -}; - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -244,23 +287,21 @@ class WellSelectionPage : public QWizardPage Q_OBJECT public: - WellSelectionPage( RimWellPathImport* wellPathImport, QWidget* parent = nullptr ); + WellSelectionPage( RimWellPathImport* wellPathImport, RiaOsduConnector* m_osduConnector, QWidget* parent = nullptr ); ~WellSelectionPage() override; void initializePage() override; - - void selectedWellPathEntries( std::vector& downloadEntities, caf::PdmObjectHandle* objHandle ); - -private: - void sortObjectsByDescription( caf::PdmObjectCollection* objects ); + bool isComplete() const override; private slots: - void customMenuRequested( const QPoint& pos ); + void wellboresFinished( const QString& wellId ); + void wellsFinished(); private: - ObjectGroupWithHeaders* m_regionsWithVisibleWells; RimWellPathImport* m_wellPathImportObject; - caf::PdmUiTreeView* m_wellSelectionTreeView; + RiaOsduConnector* m_osduConnector; + QTableView* m_tableView; + OsduWellboreTableModel* m_osduWellboresModel; }; //-------------------------------------------------------------------------------------------------- @@ -277,9 +318,6 @@ class WellSummaryPage : public QWizardPage void updateSummaryPage(); -private slots: - void slotShowDetails(); - private: RimWellPathImport* m_wellPathImportObject; QTextEdit* m_textEdit; @@ -310,25 +348,21 @@ class RiuWellImportWizard : public QWizard QWidget* parent = nullptr ); ~RiuWellImportWizard() override; - QStringList absoluteFilePathsToWellPaths() const; - // Methods used from the wizard pages void resetAuthenticationCount(); + void setSelectedFieldId( const QString& fieldId ); + QString selectedFieldId() const; + public slots: void downloadWellPaths(); void downloadWells( const QString& fieldId ); void downloadFields(); - void httpFinished(); - void slotAuthenticationRequired( QNetworkReply* networkReply, QAuthenticator* authenticator ); int wellSelectionPageId(); -private slots: - void slotCurrentIdChanged( int currentId ); - private: void updateFieldsModel(); void parseWellsResponse( RimOilFieldEntry* oilFieldEntry ); @@ -340,6 +374,7 @@ private slots: private: RiaOsduConnector* m_osduConnector; + QString m_selectedFieldId; QString m_destinationFolder;