ResInsight/ApplicationCode/ProjectDataModel/Surfaces/RimSurfaceCollection.cpp
jonjenssen 7cb9a071c1
Surface: Support for folders, copy and drag'n'drop (#6335)
* Enable surface reordering support. Automatically update surface in view ordering based on surface collection ordering

* Remove obsolete code

* Enable drag'n'drop support for surfaces within a surface collection. Still missing the collection update part.

* Bring back code lost in prev. commit

* Add code to accept drops in surface collections. Keep view  in sync.

* Add command for adding additional surface folders.

* Make sure we use the current surface collection as our parent when importing

* Enable name editing.
Make sure we use the correct surface collection when importing/creating surfaces

* More work on getting surface collections working.

* Clean up naming

* Make sure name for surfaceinviewcollection is read only

* Support drawing surfaces from subcollections, too

* Allow deleting subfolders.
Fix legends in view

* Refactor topmost flag for surface collections.

* Fix reload surface to work in all subfolders, too
Add copy surface skeleton. Actual copy operation is still missing

* Add support for copying surfaces

* Remove possibility to choose I and J slice directions for grid case surfaces.

* Fix warnings.

* Make sure we create the surface folder at the correct level

* More warning fix

* Use XML serialization for copy operation

* Fix missing delete

* Fix typo

* Remove unnecessary method.
2020-08-24 11:09:22 +02:00

411 lines
13 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2020- 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 "RimSurfaceCollection.h"
#include "RiaColorTables.h"
#include "RiaLogging.h"
#include "RimFileSurface.h"
#include "RimGridCaseSurface.h"
#include "RimGridView.h"
#include "RimProject.h"
#include "RimSurface.h"
#include "RimSurfaceInView.h"
#include "cafPdmFieldReorderCapability.h"
CAF_PDM_SOURCE_INIT( RimSurfaceCollection, "SurfaceCollection" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceCollection::RimSurfaceCollection()
{
CAF_PDM_InitObject( "Surfaces", ":/ReservoirSurfaces16x16.png", "", "" );
CAF_PDM_InitFieldNoDefault( &m_collectionname, "SurfaceUserDecription", "Name", "", "", "" );
m_collectionname = "Surfaces";
CAF_PDM_InitFieldNoDefault( &m_subcollections, "SubCollections", "Surfaces", "", "", "" );
m_subcollections.uiCapability()->setUiTreeHidden( true );
auto reorderability = caf::PdmFieldReorderCapability::addToField( &m_subcollections );
reorderability->orderChanged.connect( this, &RimSurfaceCollection::orderChanged );
CAF_PDM_InitFieldNoDefault( &m_surfaces, "SurfacesField", "Surfaces", "", "", "" );
m_surfaces.uiCapability()->setUiTreeHidden( true );
setDeletable( true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceCollection::~RimSurfaceCollection()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::setAsTopmostFolder()
{
m_collectionname.uiCapability()->setUiHidden( true );
setDeletable( false );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimSurfaceCollection::collectionname() const
{
return m_collectionname.value();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmFieldHandle* RimSurfaceCollection::userDescriptionField()
{
return &m_collectionname;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::addSurface( RimSurface* surface )
{
m_surfaces.push_back( surface );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurface* RimSurfaceCollection::importSurfacesFromFiles( const QStringList& fileNames )
{
size_t newSurfCount = 0;
size_t existingSurfCount = m_surfaces().size();
QString errorMessages;
std::vector<RimSurface*> surfacesToLoad;
for ( const QString& newFileName : fileNames )
{
RimFileSurface* newSurface = new RimFileSurface;
auto newColor = RiaColorTables::categoryPaletteColors().cycledColor3f( existingSurfCount + newSurfCount );
newSurface->setSurfaceFilePath( newFileName );
newSurface->setColor( newColor );
if ( !newSurface->onLoadData() )
{
delete newSurface;
errorMessages += newFileName + "\n";
}
else
{
this->addSurface( newSurface );
surfacesToLoad.push_back( newSurface );
++newSurfCount;
}
}
if ( !errorMessages.isEmpty() )
{
RiaLogging::warning( "Import Surfaces : Could not import the following files:\n" + errorMessages );
}
this->updateConnectedEditors();
updateViews( surfacesToLoad );
if ( newSurfCount > 0 && !m_surfaces.empty() )
{
return m_surfaces[m_surfaces.size() - 1];
}
else
{
return nullptr;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::reloadSurfaces( std::vector<RimSurface*> surfaces )
{
// ask the surfaces given to reload its data
for ( RimSurface* surface : surfaces )
{
surface->reloadData();
}
this->updateConnectedEditors();
updateViews( surfaces );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurface* RimSurfaceCollection::copySurfaces( std::vector<RimSurface*> surfaces )
{
std::vector<RimSurface*> newsurfaces;
// create a copy of each surface given
for ( RimSurface* surface : surfaces )
{
RimSurface* copy = surface->createCopy();
if ( copy )
{
newsurfaces.push_back( copy );
}
else
{
RiaLogging::warning( "Create Surface Copy: Could not create a copy of the surface " + surface->fullName() );
}
}
RimSurface* retsurf = nullptr;
for ( RimSurface* surface : newsurfaces )
{
m_surfaces.push_back( surface );
retsurf = surface;
}
this->updateConnectedEditors();
return retsurf;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurface* RimSurfaceCollection::addGridCaseSurface( RimCase* sourceCase )
{
auto s = new RimGridCaseSurface;
s->setCase( sourceCase );
int oneBasedSliceIndex = 1;
s->setOneBasedIndex( oneBasedSliceIndex );
s->setUserDescription( "Surface" );
if ( !s->onLoadData() )
{
RiaLogging::warning( "Add Grid Case Surface : Could not create the grid case surface." );
return nullptr;
}
m_surfaces.push_back( s );
this->updateConnectedEditors();
std::vector<RimSurface*> surfacesToRefresh;
surfacesToRefresh.push_back( s );
updateViews( surfacesToRefresh );
return s;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimSurface*> RimSurfaceCollection::surfaces() const
{
return m_surfaces.childObjects();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimSurfaceCollection*> RimSurfaceCollection::subcollections() const
{
return m_subcollections.childObjects();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::loadData()
{
for ( auto surf : m_surfaces )
{
surf->loadDataIfRequired();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::updateViews( const std::vector<RimSurface*>& surfsToReload )
{
RimProject* proj = RimProject::current();
std::vector<Rim3dView*> views;
proj->allViews( views );
// Make sure the tree items are synchronized
for ( auto view : views )
{
auto gridView = dynamic_cast<RimGridView*>( view );
if ( gridView ) gridView->updateSurfacesInViewTreeItems();
}
std::set<RimGridView*> viewsNeedingUpdate;
for ( auto surf : surfsToReload )
{
std::vector<RimSurfaceInView*> surfsInView;
surf->objectsWithReferringPtrFieldsOfType( surfsInView );
for ( auto surfInView : surfsInView )
{
surfInView->clearGeometry();
RimGridView* gridView;
surfInView->firstAncestorOrThisOfType( gridView );
if ( gridView ) viewsNeedingUpdate.insert( gridView );
}
}
// Update the views:
for ( auto view : viewsNeedingUpdate )
{
view->scheduleCreateDisplayModelAndRedraw();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::updateViews()
{
RimProject* proj = RimProject::current();
std::vector<RimGridView*> views;
proj->allVisibleGridViews( views );
// Make sure the tree items are synchronized
for ( auto view : views )
{
view->updateSurfacesInViewTreeItems();
}
for ( auto view : views )
{
view->scheduleCreateDisplayModelAndRedraw();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::onChildDeleted( caf::PdmChildArrayFieldHandle* childArray,
std::vector<caf::PdmObjectHandle*>& referringObjects )
{
updateViews();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::orderChanged( const caf::SignalEmitter* emitter )
{
updateViews();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::removeSurface( RimSurface* surface )
{
m_surfaces.removeChildObject( surface );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurface* RimSurfaceCollection::addSurfacesAtIndex( int position, std::vector<RimSurface*> surfaces )
{
// adjust index for number of folders we have
position = position - static_cast<int>( m_subcollections.size() );
RimSurface* returnSurface = nullptr;
if ( !surfaces.empty() ) returnSurface = surfaces[0];
// insert position at end?
if ( ( position >= static_cast<int>( m_surfaces.size() ) ) || ( position < 0 ) )
{
for ( auto surf : surfaces )
{
m_surfaces.push_back( surf );
}
}
else
{
// build the new surface order
std::vector<RimSurface*> orderedSurfs;
int i = 0;
while ( i < position )
{
orderedSurfs.push_back( m_surfaces[i++] );
}
for ( auto surf : surfaces )
{
orderedSurfs.push_back( surf );
}
int surfcount = static_cast<int>( m_surfaces.size() );
while ( i < surfcount )
{
orderedSurfs.push_back( m_surfaces[i++] );
}
// reset the surface collection and use the new order
m_surfaces.clear();
for ( auto surf : orderedSurfs )
{
m_surfaces.push_back( surf );
}
}
// make sure the views are in sync with the collection order
updateViews();
return returnSurface;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceCollection::addSubCollection( RimSurfaceCollection* subcoll )
{
m_subcollections.push_back( subcoll );
this->updateConnectedEditors();
updateViews();
return;
}