#2058 Context command. Add 'On - others off' command

This commit is contained in:
Bjørn Erik Jensen 2017-11-20 14:47:22 +01:00
parent ebb726ae0c
commit 36c093d6fb
4 changed files with 207 additions and 6 deletions

View File

@ -9,6 +9,7 @@ ${CEE_CURRENT_LIST_DIR}RicToggleItemsFeatureImpl.h
${CEE_CURRENT_LIST_DIR}RicToggleItemsOnFeature.h
${CEE_CURRENT_LIST_DIR}RicToggleItemsOffFeature.h
${CEE_CURRENT_LIST_DIR}RicToggleItemsFeature.h
${CEE_CURRENT_LIST_DIR}RicToggleItemsOnOthersOffFeature.h
)
set (SOURCE_GROUP_SOURCE_FILES
@ -16,6 +17,7 @@ ${CEE_CURRENT_LIST_DIR}RicToggleItemsFeatureImpl.cpp
${CEE_CURRENT_LIST_DIR}RicToggleItemsOnFeature.cpp
${CEE_CURRENT_LIST_DIR}RicToggleItemsOffFeature.cpp
${CEE_CURRENT_LIST_DIR}RicToggleItemsFeature.cpp
${CEE_CURRENT_LIST_DIR}RicToggleItemsOnOthersOffFeature.cpp
)
list(APPEND CODE_HEADER_FILES

View File

@ -0,0 +1,140 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015- Statoil ASA
// Copyright (C) 2015- Ceetron Solutions AS
//
// 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 "RicToggleItemsOnOthersOffFeature.h"
#include "RicToggleItemsFeatureImpl.h"
#include "cafSelectionManager.h"
#include <QAction>
#include "cafPdmObjectHandle.h"
#include "cafPdmObject.h"
#include "cafPdmUiItem.h"
CAF_CMD_SOURCE_INIT(RicToggleItemsOnOthersOffFeature, "RicToggleItemsOnOthersOffFeature");
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicToggleItemsOnOthersOffFeature::isCommandEnabled()
{
std::vector<caf::PdmObject*> selectedObjects;
caf::SelectionManager::instance()->objectsByType(&selectedObjects);
caf::PdmFieldHandle* commonParent = commonParentForAllSelections(selectedObjects);
std::vector<caf::PdmObjectHandle*> children = childObjects(commonParent);
return commonParent != nullptr
&& children.size() > 0
&& objectToggleField(children.front())
&& children.size() > selectedObjects.size();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicToggleItemsOnOthersOffFeature::onActionTriggered(bool isChecked)
{
std::vector<caf::PdmObject*> selectedObjects;
caf::SelectionManager::instance()->objectsByType(&selectedObjects);
caf::PdmFieldHandle* commonParent = commonParentForAllSelections(selectedObjects);
if (commonParent)
{
for (caf::PdmObjectHandle* child : childObjects(commonParent))
{
caf::PdmField<bool>* field = objectToggleField(child);
if (field)
{
field->setValueWithFieldChanged(false);
}
}
}
// Then toggle on the selected item(s)
for (caf::PdmObject* selectedObject : selectedObjects)
{
caf::PdmField<bool>* field = dynamic_cast<caf::PdmField<bool>*>(selectedObject->objectToggleField());
field->setValueWithFieldChanged(true);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicToggleItemsOnOthersOffFeature::setupActionLook(QAction* actionToSetup)
{
actionToSetup->setText("On - Others Off");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmFieldHandle* RicToggleItemsOnOthersOffFeature::commonParentForAllSelections(const std::vector<caf::PdmObject*>& selectedObjects)
{
caf::PdmFieldHandle* commonParent = nullptr;
for (caf::PdmObject* obj : selectedObjects)
{
caf::PdmFieldHandle* parent = obj->parentField();
if (parent)
{
if (!commonParent)
{
commonParent = parent;
}
else if(parent != commonParent)
{
// Different parents detected
return nullptr;
}
}
}
return commonParent;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<caf::PdmObjectHandle*> RicToggleItemsOnOthersOffFeature::childObjects(caf::PdmFieldHandle* parent)
{
std::vector<caf::PdmObjectHandle*> children;
if (parent)
{
parent->childObjects(&children);
}
return children;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmField<bool>* RicToggleItemsOnOthersOffFeature::objectToggleField(caf::PdmObjectHandle* objectHandle)
{
caf::PdmUiObjectHandle* childUiObject = uiObj(objectHandle);
if (childUiObject && childUiObject->objectToggleField())
{
return dynamic_cast<caf::PdmField<bool>*>(childUiObject->objectToggleField());
}
return nullptr;
}

View File

@ -0,0 +1,49 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015- Statoil ASA
// Copyright (C) 2015- Ceetron Solutions AS
//
// 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"
#include "cafPdmField.h"
namespace caf
{
class PdmFieldHandle;
class PdmObject;
class PdmObjectHandle;
}
//==================================================================================================
///
//==================================================================================================
class RicToggleItemsOnOthersOffFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;
protected:
// Overrides
virtual bool isCommandEnabled() override;
virtual void onActionTriggered( bool isChecked ) override;
virtual void setupActionLook( QAction* actionToSetup ) override;
private:
caf::PdmFieldHandle* commonParentForAllSelections(const std::vector<caf::PdmObject*>& selectedObjects);
std::vector<caf::PdmObjectHandle*> childObjects(caf::PdmFieldHandle* parent);
caf::PdmField<bool>* objectToggleField(caf::PdmObjectHandle* objectHandle);
};

View File

@ -607,13 +607,23 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection()
}
}
if (RicToggleItemsFeatureImpl::isToggleCommandsAvailable())
{
menuBuilder << "Separator";
menuBuilder << "RicToggleItemsOnFeature";
menuBuilder << "RicToggleItemsOffFeature";
menuBuilder << "RicToggleItemsFeature";
bool addSeparator = true;
if (RicToggleItemsFeatureImpl::isToggleCommandsAvailable())
{
menuBuilder << "Separator";
menuBuilder << "RicToggleItemsOnFeature";
menuBuilder << "RicToggleItemsOffFeature";
menuBuilder << "RicToggleItemsFeature";
addSeparator = false;
}
if (addSeparator)
{
menuBuilder.addSeparator();
}
menuBuilder << "RicToggleItemsOnOthersOffFeature";
}
if ( caf::CmdFeatureManager::instance()->getCommandFeature("RicDeleteItemFeature")->canFeatureBeExecuted() )