From 6bcf1b56fe4e898eeb931e5378ea9d467fad87ba Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Thu, 13 Aug 2020 12:00:53 +0200 Subject: [PATCH] Disallow dropping curves onto other curves * Dropping curves between other curves works much better with this change. --- .../RicWellLogPlotTrackFeatureImpl.cpp | 34 +-- .../RicWellLogPlotTrackFeatureImpl.h | 4 +- .../ProjectDataModel/RimMultiPlot.cpp | 14 +- .../ProjectDataModel/RimMultiPlot.h | 2 +- ApplicationCode/UserInterface/RiuDragDrop.cpp | 251 ++++-------------- ApplicationCode/UserInterface/RiuDragDrop.h | 19 +- 6 files changed, 85 insertions(+), 239 deletions(-) diff --git a/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.cpp b/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.cpp index 6b2f09f9ea..c940cdaa7e 100644 --- a/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.cpp +++ b/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.cpp @@ -35,10 +35,8 @@ //-------------------------------------------------------------------------------------------------- void RicWellLogPlotTrackFeatureImpl::moveCurvesToWellLogPlotTrack( RimWellLogTrack* destTrack, const std::vector& curves, - RimWellLogCurve* curveToInsertBeforeOrAfter, - int insertAtPosition ) + int insertAtPosition ) { - CVF_ASSERT( insertAtPosition >= 0 ); CVF_ASSERT( destTrack ); std::set srcTracks; @@ -62,8 +60,15 @@ void RicWellLogPlotTrackFeatureImpl::moveCurvesToWellLogPlotTrack( RimWellLogTra for ( size_t cIdx = 0; cIdx < curves.size(); cIdx++ ) { - size_t position = (size_t)insertAtPosition + cIdx; - destTrack->insertCurve( curves[cIdx], position ); + if ( insertAtPosition >= 0 ) + { + size_t position = (size_t)insertAtPosition + cIdx; + destTrack->insertCurve( curves[cIdx], position ); + } + else + { + destTrack->addCurve( curves[cIdx] ); + } } for ( auto track : srcTracks ) @@ -91,8 +96,7 @@ void RicWellLogPlotTrackFeatureImpl::moveCurvesToWellLogPlotTrack( RimWellLogTra //-------------------------------------------------------------------------------------------------- void RicWellLogPlotTrackFeatureImpl::moveTracksToWellLogPlot( RimWellLogPlot* wellLogPlot, const std::vector& tracksToMove, - RimWellLogTrack* trackToInsertBeforeOrAfter, - bool isSwapOperation ) + int insertAtPosition ) { CVF_ASSERT( wellLogPlot ); @@ -108,15 +112,15 @@ void RicWellLogPlotTrackFeatureImpl::moveTracksToWellLogPlot( RimWellLogPlot* } } - size_t insertionStartIndex = 0; - if ( trackToInsertBeforeOrAfter ) - { - insertionStartIndex = wellLogPlot->plotIndex( trackToInsertBeforeOrAfter ); - if ( !isSwapOperation ) insertionStartIndex += 1; - } - for ( size_t tIdx = 0; tIdx < tracksToMove.size(); tIdx++ ) { - wellLogPlot->insertPlot( tracksToMove[tIdx], insertionStartIndex + tIdx ); + if ( insertAtPosition >= 0 ) + { + wellLogPlot->insertPlot( tracksToMove[tIdx], (size_t)insertAtPosition + tIdx ); + } + else + { + wellLogPlot->addPlot( tracksToMove[tIdx] ); + } } } diff --git a/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.h b/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.h index a5b1bc503d..32699f3db0 100644 --- a/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.h +++ b/ApplicationCode/Commands/WellLogCommands/RicWellLogPlotTrackFeatureImpl.h @@ -33,10 +33,8 @@ class RicWellLogPlotTrackFeatureImpl public: static void moveCurvesToWellLogPlotTrack( RimWellLogTrack* dstTrack, const std::vector& curves, - RimWellLogCurve* curveToInsertBeforeOrAfter, int insertAtPosition ); static void moveTracksToWellLogPlot( RimWellLogPlot* wellLogPlot, const std::vector& tracks, - RimWellLogTrack* trackToInsertBeforeOrAfter, - bool isSwapOperation ); + int insertAtPosition ); }; diff --git a/ApplicationCode/ProjectDataModel/RimMultiPlot.cpp b/ApplicationCode/ProjectDataModel/RimMultiPlot.cpp index f938e6eedd..224359ed27 100644 --- a/ApplicationCode/ProjectDataModel/RimMultiPlot.cpp +++ b/ApplicationCode/ProjectDataModel/RimMultiPlot.cpp @@ -229,7 +229,7 @@ void RimMultiPlot::removePlot( RimPlot* plot ) //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RimMultiPlot::movePlotsToThis( const std::vector& plotsToMove, RimPlot* plotToInsertAfter ) +void RimMultiPlot::movePlotsToThis( const std::vector& plotsToMove, int insertAtPosition ) { for ( size_t tIdx = 0; tIdx < plotsToMove.size(); tIdx++ ) { @@ -245,12 +245,16 @@ void RimMultiPlot::movePlotsToThis( const std::vector& plotsToMove, Ri } } - size_t insertionStartIndex = 0; - if ( plotToInsertAfter ) insertionStartIndex = this->plotIndex( plotToInsertAfter ) + 1; - for ( size_t tIdx = 0; tIdx < plotsToMove.size(); tIdx++ ) { - this->insertPlot( plotsToMove[tIdx], insertionStartIndex + tIdx ); + if ( insertAtPosition >= 0 ) + { + this->insertPlot( plotsToMove[tIdx], (size_t)insertAtPosition + tIdx ); + } + else + { + this->addPlot( plotsToMove[tIdx] ); + } } this->updateLayout(); diff --git a/ApplicationCode/ProjectDataModel/RimMultiPlot.h b/ApplicationCode/ProjectDataModel/RimMultiPlot.h index cbb70570c7..7e32245410 100644 --- a/ApplicationCode/ProjectDataModel/RimMultiPlot.h +++ b/ApplicationCode/ProjectDataModel/RimMultiPlot.h @@ -72,7 +72,7 @@ public: void addPlot( RimPlot* plot ); void insertPlot( RimPlot* plot, size_t index ); void removePlot( RimPlot* plot ); - void movePlotsToThis( const std::vector& plots, RimPlot* plotToInsertAfter ); + void movePlotsToThis( const std::vector& plots, int insertAtPosition ); size_t plotCount() const; size_t plotIndex( const RimPlot* plot ) const; diff --git a/ApplicationCode/UserInterface/RiuDragDrop.cpp b/ApplicationCode/UserInterface/RiuDragDrop.cpp index 2367d5fac3..51ad5c847a 100644 --- a/ApplicationCode/UserInterface/RiuDragDrop.cpp +++ b/ApplicationCode/UserInterface/RiuDragDrop.cpp @@ -118,6 +118,18 @@ public: return typedObjectsGetter.typedObjects().size() > 0; } + static std::vector typedAncestorsFromGroup( const caf::PdmObjectGroup& objectGroup ) + { + RiuTypedPdmObjects typedObjectsGetter( objectGroup ); + return typedObjectsGetter.typedAncestors(); + } + + static std::vector typedAncestorsFromGroup( const std::vector>& objectHandles ) + { + RiuTypedPdmObjects typedObjectsGetter( objectHandles ); + return typedObjectsGetter.typedAncestors(); + } + private: std::vector typedObjects() { @@ -134,6 +146,22 @@ private: return typedObjectsVec; } + std::vector typedAncestors() + { + std::vector typedAncestorsVec; + for ( size_t i = 0; i < m_objects.size(); i++ ) + { + T* typedAncestor = nullptr; + m_objects[i]->firstAncestorOfType( typedAncestor ); + if ( typedAncestor ) + { + typedAncestorsVec.push_back( typedAncestor ); + } + } + + return typedAncestorsVec; + } + private: std::vector> m_objects; }; @@ -203,7 +231,7 @@ Qt::ItemFlags RiuDragDrop::flags( const QModelIndex& index ) const if ( m_dragItems.empty() ) return itemflags; - if ( dynamic_cast( uiItem ) || dynamic_cast( uiItem ) ) + if ( dynamic_cast( uiItem ) ) { if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) { @@ -216,7 +244,14 @@ Qt::ItemFlags RiuDragDrop::flags( const QModelIndex& index ) const { if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) { - itemflags |= Qt::ItemIsDropEnabled; + auto plotParents = RiuTypedPdmObjects::typedAncestorsFromGroup( m_dragItems ); + bool draggedOntoSameParent = index.row() == -1 && plotParents.size() == 1u && + plotParents.front() == uiItem; + + if ( !draggedOntoSameParent ) + { + itemflags |= Qt::ItemIsDropEnabled; + } } } else if ( dynamic_cast( uiItem ) ) @@ -231,42 +266,23 @@ Qt::ItemFlags RiuDragDrop::flags( const QModelIndex& index ) const { if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) { - itemflags |= Qt::ItemIsDropEnabled; - } - else if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) - { - itemflags |= Qt::ItemIsDropEnabled; + auto trackParents = RiuTypedPdmObjects::typedAncestorsFromGroup( m_dragItems ); + bool draggedOntoSameParent = index.row() == -1 && trackParents.size() == 1u && + trackParents.front() == uiItem; + + if ( !draggedOntoSameParent ) + { + itemflags |= Qt::ItemIsDropEnabled; + } } } - else if ( dynamic_cast( uiItem ) ) - { - if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) - { - itemflags |= Qt::ItemIsDropEnabled; - } - } - else if ( dynamic_cast( uiItem ) || dynamic_cast( uiItem ) || - dynamic_cast( uiItem ) ) + else if ( dynamic_cast( uiItem ) || dynamic_cast( uiItem ) ) { if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) { itemflags |= Qt::ItemIsDropEnabled; } } - else if ( dynamic_cast( uiItem ) ) - { - if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) - { - itemflags |= Qt::ItemIsDropEnabled; - } - } - else if ( dynamic_cast( uiItem ) ) - { - if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) - { - itemflags |= Qt::ItemIsDropEnabled; - } - } else if ( dynamic_cast( uiItem ) ) { if ( RiuTypedPdmObjects::containsTypedObjects( m_dragItems ) ) @@ -304,22 +320,6 @@ Qt::ItemFlags RiuDragDrop::flags( const QModelIndex& index ) const return itemflags; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RiuDragDrop::isSwapOperation( int targetRow, const QModelIndexList& dragIndices, const QModelIndex& dropTargetIndex ) -{ - if ( dragIndices.size() == 1u && targetRow == -1 ) // targetRow >= 0 means a drop between two other rows. - { - QModelIndex dragIndex = dragIndices.front(); - bool sharesParent = dropTargetIndex.parent() == dragIndex.parent(); - bool targetIsJustAboveOrBelow = dropTargetIndex.row() >= 0 && dragIndex.row() >= 0 && - std::abs( dragIndex.row() - dropTargetIndex.row() ) == 1; - return sharesParent && targetIsJustAboveOrBelow; - } - return false; -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -356,48 +356,18 @@ bool RiuDragDrop::dropMimeData( const QMimeData* data, Qt::DropAction action, in return handleGridCaseGroupDrop( action, draggedObjects, gridCaseGroup ); } - RimWellLogCurve* wellLogPlotCurve; - dropTarget->firstAncestorOrThisOfType( wellLogPlotCurve ); - if ( wellLogPlotCurve ) - { - return handleWellLogPlotCurveDrop( action, - draggedObjects, - wellLogPlotCurve, - row, - isSwapOperation( row, myMimeData->indexes(), dropTargetIndex ) ); - } - RimWellLogTrack* wellLogPlotTrack; dropTarget->firstAncestorOrThisOfType( wellLogPlotTrack ); if ( wellLogPlotTrack ) { - return handleWellLogPlotTrackDrop( action, - draggedObjects, - wellLogPlotTrack, - row, - isSwapOperation( row, myMimeData->indexes(), dropTargetIndex ) ); + return handleWellLogPlotTrackDrop( action, draggedObjects, wellLogPlotTrack, row ); } RimWellLogPlot* wellLogPlot; dropTarget->firstAncestorOrThisOfType( wellLogPlot ); if ( wellLogPlot ) { - return handleWellLogPlotDrop( action, - draggedObjects, - wellLogPlot, - row, - isSwapOperation( row, myMimeData->indexes(), dropTargetIndex ) ); - } - - RimSummaryCurve* summaryCurve; - dropTarget->firstAncestorOrThisOfType( summaryCurve ); - if ( summaryCurve ) - { - return handleSummaryCurveDrop( action, - draggedObjects, - summaryCurve, - row, - isSwapOperation( row, myMimeData->indexes(), dropTargetIndex ) ); + return handleWellLogPlotDrop( action, draggedObjects, wellLogPlot, row ); } RimSummaryPlot* summaryPlot; @@ -512,52 +482,7 @@ bool RiuDragDrop::handleMultiPlotDrop( Qt::DropAction action, { if ( action == Qt::MoveAction ) { - RimPlot* insertAfter = nullptr; - if ( insertAtPosition > 0 ) - { - auto visibleTracks = multiPlot->visiblePlots(); - if ( !visibleTracks.empty() ) - { - int insertAfterPosition = std::min( insertAtPosition - 1, (int)visibleTracks.size() - 1 ); - insertAfter = dynamic_cast( visibleTracks[insertAfterPosition] ); - } - } - multiPlot->movePlotsToThis( plots, insertAfter ); - return true; - } - } - - return false; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RiuDragDrop::handleWellLogPlotCurveDrop( Qt::DropAction action, - caf::PdmObjectGroup& draggedObjects, - RimWellLogCurve* curveDropTarget, - int insertAtPosition, - bool isSwapOperation ) -{ - std::vector wellLogPlotCurves = - RiuTypedPdmObjects::typedObjectsFromGroup( draggedObjects ); - if ( wellLogPlotCurves.size() > 0 ) - { - if ( action == Qt::MoveAction ) - { - RimWellLogTrack* wellLogPlotTrack; - curveDropTarget->firstAncestorOrThisOfTypeAsserted( wellLogPlotTrack ); - - if ( insertAtPosition == -1 ) - { - insertAtPosition = (int)wellLogPlotTrack->curveIndex( curveDropTarget ); - if ( !isSwapOperation ) insertAtPosition += 1; - } - - RicWellLogPlotTrackFeatureImpl::moveCurvesToWellLogPlotTrack( wellLogPlotTrack, - wellLogPlotCurves, - curveDropTarget, - insertAtPosition ); + multiPlot->movePlotsToThis( plots, insertAtPosition ); return true; } } @@ -571,8 +496,7 @@ bool RiuDragDrop::handleWellLogPlotCurveDrop( Qt::DropAction action, bool RiuDragDrop::handleWellLogPlotTrackDrop( Qt::DropAction action, caf::PdmObjectGroup& draggedObjects, RimWellLogTrack* trackTarget, - int insertAtPosition, - bool isSwapOperation ) + int insertAtPosition ) { std::vector wellLogFileChannels = RiuTypedPdmObjects::typedObjectsFromGroup( draggedObjects ); @@ -591,38 +515,11 @@ bool RiuDragDrop::handleWellLogPlotTrackDrop( Qt::DropAction action, { if ( action == Qt::MoveAction ) { - RimWellLogCurve* insertBeforeOrAfter = nullptr; - auto visibleCurves = trackTarget->visibleCurves(); - if ( insertAtPosition == -1 ) - { - insertAtPosition = (int)visibleCurves.size(); - if ( !visibleCurves.empty() ) insertBeforeOrAfter = visibleCurves.back(); - } - else if ( insertAtPosition < (int)visibleCurves.size() ) - { - insertBeforeOrAfter = visibleCurves[insertAtPosition]; - } - - RicWellLogPlotTrackFeatureImpl::moveCurvesToWellLogPlotTrack( trackTarget, - wellLogPlotCurves, - insertBeforeOrAfter, - insertAtPosition ); + RicWellLogPlotTrackFeatureImpl::moveCurvesToWellLogPlotTrack( trackTarget, wellLogPlotCurves, insertAtPosition ); return true; } } - std::vector wellLogPlotTracks = - RiuTypedPdmObjects::typedObjectsFromGroup( draggedObjects ); - if ( wellLogPlotTracks.size() > 0 ) - { - if ( action == Qt::MoveAction ) - { - RimWellLogPlot* wellLogPlot; - trackTarget->firstAncestorOrThisOfType( wellLogPlot ); - return handleWellLogPlotDrop( action, draggedObjects, wellLogPlot, insertAtPosition, isSwapOperation ); - } - } - return false; } @@ -632,8 +529,7 @@ bool RiuDragDrop::handleWellLogPlotTrackDrop( Qt::DropAction action, bool RiuDragDrop::handleWellLogPlotDrop( Qt::DropAction action, caf::PdmObjectGroup& draggedObjects, RimWellLogPlot* wellLogPlotTarget, - int insertAtPosition, - bool isSwapOperation ) + int insertAtPosition ) { std::vector wellLogPlotTracks = RiuTypedPdmObjects::typedObjectsFromGroup( draggedObjects ); @@ -641,20 +537,7 @@ bool RiuDragDrop::handleWellLogPlotDrop( Qt::DropAction action, { if ( action == Qt::MoveAction ) { - RimWellLogTrack* insertAfter = nullptr; - if ( insertAtPosition > 0 ) - { - auto visibleTracks = wellLogPlotTarget->visiblePlots(); - if ( !visibleTracks.empty() ) - { - int insertAfterPosition = std::min( insertAtPosition - 1, (int)visibleTracks.size() - 1 ); - insertAfter = dynamic_cast( visibleTracks[insertAfterPosition] ); - } - } - RicWellLogPlotTrackFeatureImpl::moveTracksToWellLogPlot( wellLogPlotTarget, - wellLogPlotTracks, - insertAfter, - isSwapOperation ); + RicWellLogPlotTrackFeatureImpl::moveTracksToWellLogPlot( wellLogPlotTarget, wellLogPlotTracks, insertAtPosition ); return true; } } @@ -662,34 +545,6 @@ bool RiuDragDrop::handleWellLogPlotDrop( Qt::DropAction action, return false; } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -bool RiuDragDrop::handleSummaryCurveDrop( Qt::DropAction action, - caf::PdmObjectGroup& objectGroup, - RimSummaryCurve* summaryCurveTarget, - int insertAtPosition, - bool isSwapOperation ) -{ - std::vector summaryCurves = RiuTypedPdmObjects::typedObjectsFromGroup( objectGroup ); - if ( summaryCurves.size() > 0 ) - { - if ( action == Qt::MoveAction ) - { - RimSummaryCurveCollection* summaryCurveCollection; - summaryCurveTarget->firstAncestorOrThisOfType( summaryCurveCollection ); - - RimSummaryCurveCollection::moveCurvesToCollection( summaryCurveCollection, - summaryCurves, - summaryCurveTarget, - insertAtPosition, - isSwapOperation ); - return true; - } - } - return false; -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/UserInterface/RiuDragDrop.h b/ApplicationCode/UserInterface/RiuDragDrop.h index 235d48adc8..2858281cb7 100644 --- a/ApplicationCode/UserInterface/RiuDragDrop.h +++ b/ApplicationCode/UserInterface/RiuDragDrop.h @@ -64,29 +64,15 @@ private: caf::PdmObjectGroup& objectGroup, RimIdenticalGridCaseGroup* gridCaseGroup ); - bool handleWellLogPlotCurveDrop( Qt::DropAction action, - caf::PdmObjectGroup& objectGroup, - RimWellLogCurve* wellLogPlotCurve, - int insertAtPosition, - bool isSwapOperation = false ); - bool handleWellLogPlotTrackDrop( Qt::DropAction action, caf::PdmObjectGroup& objectGroup, RimWellLogTrack* wellLogPlotTrack, - int insertAtPosition, - bool isSwapOperation = false ); + int insertAtPosition ); bool handleWellLogPlotDrop( Qt::DropAction action, caf::PdmObjectGroup& objectGroup, RimWellLogPlot* wellLogPlot, - int insertAtPosition, - bool isSwapOperation = false ); - - bool handleSummaryCurveDrop( Qt::DropAction action, - caf::PdmObjectGroup& objectGroup, - RimSummaryCurve* summaryCurveTarget, - int insertAtPosition, - bool isSwapOperation = false ); + int insertAtPosition ); bool handleSummaryPlotDrop( Qt::DropAction action, caf::PdmObjectGroup& objectGroup, @@ -106,7 +92,6 @@ private: static void objectGroupFromModelIndexes( caf::PdmObjectGroup* objectGroup, const QModelIndexList& indexes ); static std::vector> objectHandlesFromSelection(); - static bool isSwapOperation( int targetRow, const QModelIndexList& dragIndices, const QModelIndex& dropTargetIndex ); private: mutable std::vector> m_dragItems;