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