mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#6476 AppFwk : Add support for moving selected items to top list
This commit is contained in:
parent
27230ed308
commit
1642370928
@ -6,6 +6,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsOffFeature.h
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsFeature.h
|
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsFeature.h
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsOnOthersOffFeature.h
|
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsOnOthersOffFeature.h
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RicCollapseSiblingsFeature.h
|
${CMAKE_CURRENT_LIST_DIR}/RicCollapseSiblingsFeature.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/RicMoveItemsToTopFeature.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set (SOURCE_GROUP_SOURCE_FILES
|
set (SOURCE_GROUP_SOURCE_FILES
|
||||||
@ -15,6 +16,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsOffFeature.cpp
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsFeature.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsFeature.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsOnOthersOffFeature.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RicToggleItemsOnOthersOffFeature.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RicCollapseSiblingsFeature.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RicCollapseSiblingsFeature.cpp
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/RicMoveItemsToTopFeature.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
list(APPEND COMMAND_CODE_HEADER_FILES
|
list(APPEND COMMAND_CODE_HEADER_FILES
|
||||||
|
@ -0,0 +1,157 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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 "RicMoveItemsToTopFeature.h"
|
||||||
|
|
||||||
|
#include "RiaGuiApplication.h"
|
||||||
|
#include "RicToggleItemsFeatureImpl.h"
|
||||||
|
#include "RiuMainWindow.h"
|
||||||
|
|
||||||
|
#include "cafPdmFieldReorderCapability.h"
|
||||||
|
#include "cafPdmObject.h"
|
||||||
|
#include "cafPdmObjectHandle.h"
|
||||||
|
#include "cafPdmPtrArrayFieldHandle.h"
|
||||||
|
#include "cafPdmUiObjectHandle.h"
|
||||||
|
#include "cafPdmUiTreeView.h"
|
||||||
|
#include "cafSelectionManager.h"
|
||||||
|
|
||||||
|
#include <QAction>
|
||||||
|
|
||||||
|
CAF_CMD_SOURCE_INIT( RicMoveItemsToTopFeature, "RicMoveItemsToTopFeature" );
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
bool RicMoveItemsToTopFeature::isCommandEnabled()
|
||||||
|
{
|
||||||
|
using namespace caf;
|
||||||
|
|
||||||
|
std::vector<caf::PdmUiItem*> selectedItems;
|
||||||
|
caf::SelectionManager::instance()->selectedItems( selectedItems );
|
||||||
|
|
||||||
|
if ( !selectedItems.empty() )
|
||||||
|
{
|
||||||
|
auto pdmObject = RicMoveItemsToTopFeature::objectHandleFromUiItem( selectedItems[0] );
|
||||||
|
auto reorderability = PdmFieldReorderCapability::reorderCapabilityOfParentContainer( pdmObject );
|
||||||
|
if ( reorderability )
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void RicMoveItemsToTopFeature::onActionTriggered( bool isChecked )
|
||||||
|
{
|
||||||
|
using namespace caf;
|
||||||
|
|
||||||
|
std::vector<caf::PdmUiItem*> selectedItems;
|
||||||
|
caf::SelectionManager::instance()->selectedItems( selectedItems );
|
||||||
|
|
||||||
|
if ( !selectedItems.empty() )
|
||||||
|
{
|
||||||
|
auto pdmObject = RicMoveItemsToTopFeature::objectHandleFromUiItem( selectedItems[0] );
|
||||||
|
auto reorderability = PdmFieldReorderCapability::reorderCapabilityOfParentContainer( pdmObject );
|
||||||
|
|
||||||
|
if ( reorderability )
|
||||||
|
{
|
||||||
|
PdmPtrArrayFieldHandle* arrayField = dynamic_cast<PdmPtrArrayFieldHandle*>( pdmObject->parentField() );
|
||||||
|
CAF_ASSERT( arrayField );
|
||||||
|
|
||||||
|
bool anyObjectMoved = false;
|
||||||
|
|
||||||
|
std::vector<caf::PdmUiItem*> reverseOrder( selectedItems );
|
||||||
|
std::reverse( reverseOrder.begin(), reverseOrder.end() );
|
||||||
|
for ( auto uiItem : reverseOrder )
|
||||||
|
{
|
||||||
|
auto currentObject = dynamic_cast<PdmObject*>( uiItem );
|
||||||
|
if ( currentObject )
|
||||||
|
{
|
||||||
|
size_t indexToMove = arrayField->size() + 1;
|
||||||
|
for ( auto i = 0; i < arrayField->size(); i++ )
|
||||||
|
{
|
||||||
|
if ( arrayField->at( i ) == currentObject )
|
||||||
|
{
|
||||||
|
indexToMove = i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( reorderability->canItemBeMovedUp( indexToMove ) )
|
||||||
|
{
|
||||||
|
reorderability->moveItemToTop( indexToMove );
|
||||||
|
|
||||||
|
anyObjectMoved = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( anyObjectMoved )
|
||||||
|
{
|
||||||
|
std::vector<const caf::PdmUiItem*> constSelectedItems;
|
||||||
|
for ( auto s : selectedItems )
|
||||||
|
{
|
||||||
|
constSelectedItems.push_back( s );
|
||||||
|
}
|
||||||
|
|
||||||
|
caf::PdmUiTreeView* uiTreeView = RiaGuiApplication::activeMainWindow()->projectTreeView();
|
||||||
|
|
||||||
|
if ( !constSelectedItems.empty() )
|
||||||
|
{
|
||||||
|
QModelIndex itemIndex = uiTreeView->findModelIndex( constSelectedItems[0] );
|
||||||
|
QModelIndex parentIndex = itemIndex.parent();
|
||||||
|
uiTreeView->updateSubTree( parentIndex );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore selection highlight after reordering
|
||||||
|
uiTreeView->selectItems( constSelectedItems );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void RicMoveItemsToTopFeature::setupActionLook( QAction* actionToSetup )
|
||||||
|
{
|
||||||
|
actionToSetup->setText( "Move to Top" );
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
caf::PdmObjectHandle* RicMoveItemsToTopFeature::objectHandleFromUiItem( caf::PdmUiItem* uiItem )
|
||||||
|
{
|
||||||
|
using namespace caf;
|
||||||
|
|
||||||
|
PdmUiObjectHandle* uiObjectHandle = dynamic_cast<PdmUiObjectHandle*>( uiItem );
|
||||||
|
|
||||||
|
if ( uiObjectHandle )
|
||||||
|
{
|
||||||
|
PdmObjectHandle* pdmObject = uiObjectHandle->objectHandle();
|
||||||
|
|
||||||
|
return pdmObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "cafCmdFeature.h"
|
||||||
|
|
||||||
|
namespace caf
|
||||||
|
{
|
||||||
|
class PdmObjectHandle;
|
||||||
|
class PdmUiItem;
|
||||||
|
} // namespace caf
|
||||||
|
|
||||||
|
//==================================================================================================
|
||||||
|
///
|
||||||
|
//==================================================================================================
|
||||||
|
class RicMoveItemsToTopFeature : public caf::CmdFeature
|
||||||
|
{
|
||||||
|
CAF_CMD_HEADER_INIT;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool isCommandEnabled() override;
|
||||||
|
void onActionTriggered( bool isChecked ) override;
|
||||||
|
void setupActionLook( QAction* actionToSetup ) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static caf::PdmObjectHandle* objectHandleFromUiItem( caf::PdmUiItem* uiItem );
|
||||||
|
};
|
@ -1147,6 +1147,7 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection()
|
|||||||
menuBuilder << "RicToggleItemsOnFeature";
|
menuBuilder << "RicToggleItemsOnFeature";
|
||||||
menuBuilder << "RicToggleItemsOffFeature";
|
menuBuilder << "RicToggleItemsOffFeature";
|
||||||
menuBuilder << "RicToggleItemsFeature";
|
menuBuilder << "RicToggleItemsFeature";
|
||||||
|
|
||||||
addSeparator = false;
|
addSeparator = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1164,6 +1165,8 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection()
|
|||||||
{
|
{
|
||||||
menuBuilder << "RicCollapseSiblingsFeature";
|
menuBuilder << "RicCollapseSiblingsFeature";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
menuBuilder << "RicMoveItemsToTopFeature";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( caf::CmdFeatureManager::instance()->getCommandFeature( "RicDeleteItemFeature" )->canFeatureBeExecuted() )
|
if ( caf::CmdFeatureManager::instance()->getCommandFeature( "RicDeleteItemFeature" )->canFeatureBeExecuted() )
|
||||||
|
@ -33,7 +33,9 @@
|
|||||||
// for more details.
|
// for more details.
|
||||||
//
|
//
|
||||||
//##################################################################################################
|
//##################################################################################################
|
||||||
|
|
||||||
#include "cafPdmFieldReorderCapability.h"
|
#include "cafPdmFieldReorderCapability.h"
|
||||||
|
#include "cafPdmObjectHandle.h"
|
||||||
|
|
||||||
#include "cafAssert.h"
|
#include "cafAssert.h"
|
||||||
|
|
||||||
@ -141,3 +143,49 @@ void PdmFieldReorderCapability::onMoveItemDown( const SignalEmitter* emitter, si
|
|||||||
{
|
{
|
||||||
moveItemDown( index );
|
moveItemDown( index );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
void PdmFieldReorderCapability::onMoveItemToTop( const SignalEmitter* emitter, size_t index )
|
||||||
|
{
|
||||||
|
moveItemToTop( index );
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
bool PdmFieldReorderCapability::moveItemToTop( size_t index )
|
||||||
|
{
|
||||||
|
if ( canItemBeMovedUp( index ) )
|
||||||
|
{
|
||||||
|
PdmObjectHandle* itemToShift = m_field->at( index );
|
||||||
|
if ( itemToShift )
|
||||||
|
{
|
||||||
|
int newIndex = 0;
|
||||||
|
m_field->erase( index );
|
||||||
|
m_field->insertAt( newIndex, itemToShift );
|
||||||
|
orderChanged.send();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
PdmFieldReorderCapability* PdmFieldReorderCapability::reorderCapabilityOfParentContainer( const PdmObjectHandle* pdmObject )
|
||||||
|
{
|
||||||
|
if ( pdmObject )
|
||||||
|
{
|
||||||
|
PdmPtrArrayFieldHandle* arrayField = dynamic_cast<PdmPtrArrayFieldHandle*>( pdmObject->parentField() );
|
||||||
|
if ( arrayField )
|
||||||
|
{
|
||||||
|
PdmFieldReorderCapability* reorderability = arrayField->capability<PdmFieldReorderCapability>();
|
||||||
|
return reorderability;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
@ -41,6 +41,8 @@
|
|||||||
|
|
||||||
namespace caf
|
namespace caf
|
||||||
{
|
{
|
||||||
|
class PdmObjectHandle;
|
||||||
|
|
||||||
class PdmFieldReorderCapability : public PdmFieldCapability, public SignalEmitter, public SignalObserver
|
class PdmFieldReorderCapability : public PdmFieldCapability, public SignalEmitter, public SignalObserver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -52,6 +54,7 @@ public:
|
|||||||
bool canItemBeMovedUp( size_t index ) const;
|
bool canItemBeMovedUp( size_t index ) const;
|
||||||
bool canItemBeMovedDown( size_t index ) const;
|
bool canItemBeMovedDown( size_t index ) const;
|
||||||
|
|
||||||
|
bool moveItemToTop( size_t index );
|
||||||
bool moveItemUp( size_t index );
|
bool moveItemUp( size_t index );
|
||||||
bool moveItemDown( size_t index );
|
bool moveItemDown( size_t index );
|
||||||
|
|
||||||
@ -68,6 +71,9 @@ public:
|
|||||||
}
|
}
|
||||||
static bool fieldIsReorderable( PdmPtrArrayFieldHandle* field );
|
static bool fieldIsReorderable( PdmPtrArrayFieldHandle* field );
|
||||||
|
|
||||||
|
static PdmFieldReorderCapability* reorderCapabilityOfParentContainer( const PdmObjectHandle* pdmObject );
|
||||||
|
|
||||||
|
void onMoveItemToTop( const SignalEmitter* emitter, size_t index );
|
||||||
void onMoveItemUp( const SignalEmitter* emitter, size_t index );
|
void onMoveItemUp( const SignalEmitter* emitter, size_t index );
|
||||||
void onMoveItemDown( const SignalEmitter* emitter, size_t index );
|
void onMoveItemDown( const SignalEmitter* emitter, size_t index );
|
||||||
|
|
||||||
|
@ -502,45 +502,43 @@ void PdmUiTreeViewEditor::updateItemDelegateForSubTree( const QModelIndex& model
|
|||||||
PdmObjectHandle* pdmObject = uiObjectHandle->objectHandle();
|
PdmObjectHandle* pdmObject = uiObjectHandle->objectHandle();
|
||||||
if ( pdmObject )
|
if ( pdmObject )
|
||||||
{
|
{
|
||||||
PdmPtrArrayFieldHandle* arrayField = dynamic_cast<PdmPtrArrayFieldHandle*>( pdmObject->parentField() );
|
PdmFieldReorderCapability* reorderability =
|
||||||
if ( arrayField )
|
PdmFieldReorderCapability::reorderCapabilityOfParentContainer( pdmObject );
|
||||||
|
|
||||||
|
std::vector<PdmUiItem*> selection;
|
||||||
|
selectedUiItems( selection );
|
||||||
|
|
||||||
|
if ( reorderability && index.row() >= 0 && selection.size() == 1u && selection.front() == uiItem )
|
||||||
{
|
{
|
||||||
PdmFieldReorderCapability* reorderability = arrayField->capability<PdmFieldReorderCapability>();
|
size_t indexInParent = static_cast<size_t>( index.row() );
|
||||||
std::vector<PdmUiItem*> selection;
|
|
||||||
selectedUiItems( selection );
|
|
||||||
|
|
||||||
if ( reorderability && index.row() >= 0 && selection.size() == 1u && selection.front() == uiItem )
|
|
||||||
{
|
{
|
||||||
size_t indexInParent = static_cast<size_t>( index.row() );
|
auto tag = PdmUiTreeViewItemAttribute::Tag::create();
|
||||||
|
tag->icon = caf::IconProvider( ":/caf/Up16x16.png" );
|
||||||
|
tag->selectedOnly = true;
|
||||||
|
if ( reorderability->canItemBeMovedUp( indexInParent ) )
|
||||||
{
|
{
|
||||||
auto tag = PdmUiTreeViewItemAttribute::Tag::create();
|
tag->clicked.connect( reorderability, &PdmFieldReorderCapability::onMoveItemUp );
|
||||||
tag->icon = caf::IconProvider( ":/caf/Up16x16.png" );
|
}
|
||||||
tag->selectedOnly = true;
|
else
|
||||||
if ( reorderability->canItemBeMovedUp( indexInParent ) )
|
{
|
||||||
{
|
tag->icon.setActive( false );
|
||||||
tag->clicked.connect( reorderability, &PdmFieldReorderCapability::onMoveItemUp );
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tag->icon.setActive( false );
|
|
||||||
}
|
|
||||||
|
|
||||||
m_delegate->addTag( index, std::move( tag ) );
|
m_delegate->addTag( index, std::move( tag ) );
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
auto tag = PdmUiTreeViewItemAttribute::Tag::create();
|
||||||
|
tag->icon = IconProvider( ":/caf/Down16x16.png" );
|
||||||
|
tag->selectedOnly = true;
|
||||||
|
if ( reorderability->canItemBeMovedDown( indexInParent ) )
|
||||||
{
|
{
|
||||||
auto tag = PdmUiTreeViewItemAttribute::Tag::create();
|
tag->clicked.connect( reorderability, &PdmFieldReorderCapability::onMoveItemDown );
|
||||||
tag->icon = IconProvider( ":/caf/Down16x16.png" );
|
|
||||||
tag->selectedOnly = true;
|
|
||||||
if ( reorderability->canItemBeMovedDown( indexInParent ) )
|
|
||||||
{
|
|
||||||
tag->clicked.connect( reorderability, &PdmFieldReorderCapability::onMoveItemDown );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tag->icon.setActive( false );
|
|
||||||
}
|
|
||||||
m_delegate->addTag( index, std::move( tag ) );
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tag->icon.setActive( false );
|
||||||
|
}
|
||||||
|
m_delegate->addTag( index, std::move( tag ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user