diff --git a/ApplicationCode/ProjectDataModel/Completions/CMakeLists_files.cmake b/ApplicationCode/ProjectDataModel/Completions/CMakeLists_files.cmake index 318f8f01be..f3bfd4f307 100644 --- a/ApplicationCode/ProjectDataModel/Completions/CMakeLists_files.cmake +++ b/ApplicationCode/ProjectDataModel/Completions/CMakeLists_files.cmake @@ -27,6 +27,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RimNonDarcyPerforationParameters.h ${CMAKE_CURRENT_LIST_DIR}/RimWellPathComponentInterface.h ${CMAKE_CURRENT_LIST_DIR}/RimWellPathValve.h ${CMAKE_CURRENT_LIST_DIR}/RimMultipleValveLocations.h +${CMAKE_CURRENT_LIST_DIR}/RimWellPathAicdParameters.h ) @@ -57,6 +58,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RimMswCompletionParameters.cpp ${CMAKE_CURRENT_LIST_DIR}/RimNonDarcyPerforationParameters.cpp ${CMAKE_CURRENT_LIST_DIR}/RimWellPathValve.cpp ${CMAKE_CURRENT_LIST_DIR}/RimMultipleValveLocations.cpp +${CMAKE_CURRENT_LIST_DIR}/RimWellPathAicdParameters.cpp ) list(APPEND CODE_HEADER_FILES diff --git a/ApplicationCode/ProjectDataModel/Completions/RimWellPathAicdParameters.cpp b/ApplicationCode/ProjectDataModel/Completions/RimWellPathAicdParameters.cpp new file mode 100644 index 0000000000..d8015db099 --- /dev/null +++ b/ApplicationCode/ProjectDataModel/Completions/RimWellPathAicdParameters.cpp @@ -0,0 +1,217 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2018 - 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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// +#include "RimWellPathAicdParameters.h" + +#include "RimWellPath.h" + +#include "cafPdmUiDoubleValueEditor.h" +#include "cafPdmUiGroup.h" +#include "cafPdmUiLineEditor.h" + +#include + +CAF_PDM_SOURCE_INIT(RimWellPathAicdParameters, "WellPathAicdParameters"); + +class NumericStringValidator : public QDoubleValidator +{ +public: + NumericStringValidator(const QString& defaultValue) + : m_defaultValue(defaultValue), QDoubleValidator(nullptr) + {} + + void fixup(QString& input) const override + { + input = m_defaultValue; + } + + State validate(QString& input, int& pos) const override + { + if (input == m_defaultValue) + { + return QValidator::Acceptable; + } + + return QDoubleValidator::validate(input, pos); + } +private: + QString m_defaultValue; +}; + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimWellPathAicdParameters::RimWellPathAicdParameters() +{ + // clang-format off + CAF_PDM_InitObject("RimWellPathAicdParameters", "", "", ""); + + CAF_PDM_InitFieldNoDefault(&m_strengthOfAICD, "StrengthAICD", "Strength of AICD", "", "", ""); + CAF_PDM_InitFieldNoDefault(&m_densityOfCalibrationFluid, "DensityCalibrationFluid", "Calibration Fluid Density (kg/m^3)", "", "", ""); + CAF_PDM_InitFieldNoDefault(&m_viscosityOfCalibrationFluid, "ViscosityCalibrationFluid", "Calibration Fluid Viscosity (cP)", "", "", ""); + CAF_PDM_InitFieldNoDefault(&m_volumeFlowRateExponent, "VolumeFlowRateExponent", "Volume Flow Rate Exponent", "", "", ""); + CAF_PDM_InitFieldNoDefault(&m_viscosityFunctionExponent, "ViscosityFunctionExponent", "Viscosity Function Exponent", "", "", ""); + CAF_PDM_InitField(&m_deviceOpen, "DeviceOpen", true, "Device Open?", "", "", ""); + + CAF_PDM_InitField(&m_lengthOfAICD, "LengthOfAICD", 12.0, "Length of AICD (m)", "", "", ""); + m_lengthOfAICD.uiCapability()->setUiEditorTypeName(caf::PdmUiDoubleValueEditor::uiEditorTypeName()); + + CAF_PDM_InitField(&m_criticalWaterInLiquidFractionEmulsions, "CriticalWaterLiquidFractionEmul", QString("1*"), "Critical Water in Liquid Fraction for emulsions", "", "", ""); + CAF_PDM_InitField(&m_emulsionViscosityTransitionRegion, "ViscosityTransitionRegionEmul", QString("1*"), "Emulsion Viscosity Transition Region", "", "", ""); + CAF_PDM_InitField(&m_maxRatioOfEmulsionVisc, "MaxRatioOfEmulsionVisc", QString("1*"), "Max Ratio of Emulsion to Continuous Viscosity", "", "", ""); + CAF_PDM_InitField(&m_maxFlowRate, "MaxFlowRate", QString("1*"), "Max Flow Rate for AICD Device (m^3 / day)", "", "", ""); + CAF_PDM_InitField(&m_exponentOilFractionInDensityMixCalc, "ExponentOilDensity", QString("1*"), "Density Exponent of Oil Fraction", "", "", ""); + CAF_PDM_InitField(&m_exponentWaterFractionInDensityMixCalc, "ExponentWaterDensity", QString("1*"), "Density Exponent of Water Fraction", "", "", ""); + CAF_PDM_InitField(&m_exponentGasFractionInDensityMixCalc, "ExponentGasDensity", QString("1*"), "Density Exponent of Gas Fraction", "", "", ""); + CAF_PDM_InitField(&m_exponentOilFractionInViscosityMixCalc, "ExponentOilViscosity", QString("1*"), "Viscosity Exponent of Oil Fraction", "", "", ""); + CAF_PDM_InitField(&m_exponentWaterFractionInViscosityMixCalc, "ExponentWaterViscosity", QString("1*"), "Viscosity Exponent of Water Fraction", "", "", ""); + CAF_PDM_InitField(&m_exponentGasFractionInViscosityMixCalc, "ExponentGasViscosity", QString("1*"), "Viscosity Exponent of Gas Fraction", "", "", ""); + + std::vector allFields; + this->fields(allFields); + for (caf::PdmFieldHandle* field : allFields) + { + caf::PdmField* stringField = dynamic_cast*>(field); + if (stringField) + { + stringField->uiCapability()->setUiEditorTypeName(caf::PdmUiLineEditor::uiEditorTypeName()); + } + } + + // clang-format on +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimWellPathAicdParameters::~RimWellPathAicdParameters() {} + +//-------------------------------------------------------------------------------------------------- +/// Requires that all the required parameters are set and a proper value +//-------------------------------------------------------------------------------------------------- +bool RimWellPathAicdParameters::isValid() const +{ + for (const caf::PdmField* stringField : stringFieldsWithNoValidDefault()) + { + if (stringField->value().isEmpty()) return false; + bool ok = true; + stringField->value().toDouble(&ok); + if (!ok) return false; + } + return true; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimWellPathAicdParameters::defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) +{ + caf::PdmUiGroup* requiredGroup = uiOrdering.addNewGroup("Required Parameters"); + requiredGroup->add(&m_strengthOfAICD); + requiredGroup->add(&m_densityOfCalibrationFluid); + requiredGroup->add(&m_viscosityOfCalibrationFluid); + requiredGroup->add(&m_volumeFlowRateExponent); + requiredGroup->add(&m_viscosityFunctionExponent); + + caf::PdmUiGroup* additionalGroup = uiOrdering.addNewGroup("Additional Parameters"); + additionalGroup->add(&m_deviceOpen); + additionalGroup->add(&m_lengthOfAICD); + additionalGroup->add(&m_criticalWaterInLiquidFractionEmulsions); + additionalGroup->add(&m_emulsionViscosityTransitionRegion); + additionalGroup->add(&m_maxRatioOfEmulsionVisc); + additionalGroup->add(&m_maxFlowRate); + additionalGroup->add(&m_exponentOilFractionInDensityMixCalc); + additionalGroup->add(&m_exponentWaterFractionInDensityMixCalc); + additionalGroup->add(&m_exponentGasFractionInDensityMixCalc); + additionalGroup->add(&m_exponentOilFractionInViscosityMixCalc); + additionalGroup->add(&m_exponentWaterFractionInViscosityMixCalc); + additionalGroup->add(&m_exponentGasFractionInViscosityMixCalc); + + additionalGroup->setCollapsedByDefault(true); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimWellPathAicdParameters::defineEditorAttribute(const caf::PdmFieldHandle* field, + QString uiConfigName, + caf::PdmUiEditorAttribute* attribute) +{ + const caf::PdmField* stringField = dynamic_cast*>(field); + caf::PdmUiLineEditorAttribute* lineEditorAttr = dynamic_cast(attribute); + if (stringField && lineEditorAttr) + { + if (stringFieldsWithNoValidDefault().count(stringField)) + { + lineEditorAttr->validator = new NumericStringValidator(""); + } + else + { + lineEditorAttr->validator = new NumericStringValidator("1*"); + } + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::set*> RimWellPathAicdParameters::stringFieldsWithNoValidDefault() const +{ + std::set*> fields = {&m_strengthOfAICD, + &m_densityOfCalibrationFluid, + &m_viscosityOfCalibrationFluid, + &m_volumeFlowRateExponent, + &m_viscosityFunctionExponent}; + return fields; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimWellPathAicdParameters::setUnitLabels() +{ + if (isMetric()) + { + m_densityOfCalibrationFluid.uiCapability()->setUiName("Calibration Fluid Density (kg / m ^ 3)"); + m_lengthOfAICD.uiCapability()->setUiName("Length of AICD (m)"); + m_maxFlowRate.uiCapability()->setUiName("Max Flow Rate for AICD Device(m ^ 3 / day)"); + } + else + { + m_densityOfCalibrationFluid.uiCapability()->setUiName("Calibration Fluid Density (lb / ft ^3)"); + m_lengthOfAICD.uiCapability()->setUiName("Length of AICD (ft)"); + m_maxFlowRate.uiCapability()->setUiName("Max Flow Rate for AICD Device(ft ^ 3 / day)"); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimWellPathAicdParameters::isMetric() const +{ + bool metric = false; + RimWellPath* wellPath; + firstAncestorOrThisOfType(wellPath); + if (wellPath) + { + if (wellPath->unitSystem() == RiaEclipseUnitTools::UNITS_METRIC) + { + metric = true; + } + } + return metric; +} diff --git a/ApplicationCode/ProjectDataModel/Completions/RimWellPathAicdParameters.h b/ApplicationCode/ProjectDataModel/Completions/RimWellPathAicdParameters.h new file mode 100644 index 0000000000..f1a86c6201 --- /dev/null +++ b/ApplicationCode/ProjectDataModel/Completions/RimWellPathAicdParameters.h @@ -0,0 +1,64 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2018 - 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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// +#pragma once + +#include "cafPdmBase.h" +#include "cafPdmObject.h" +#include "cafPdmField.h" + +class RimWellPathAicdParameters : public caf::PdmObject +{ + CAF_PDM_HEADER_INIT; +public: + RimWellPathAicdParameters(); + ~RimWellPathAicdParameters(); + bool isValid() const; + +protected: + void defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) override; + void defineEditorAttribute(const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute) override; + +private: + std::set*> stringFieldsWithNoValidDefault() const; + void setUnitLabels(); + bool isMetric() const; +private: + // Required parameters + caf::PdmField m_strengthOfAICD; + caf::PdmField m_densityOfCalibrationFluid; + caf::PdmField m_viscosityOfCalibrationFluid; + caf::PdmField m_volumeFlowRateExponent; + caf::PdmField m_viscosityFunctionExponent; + + // Additional parameters + caf::PdmField m_deviceOpen; + caf::PdmField m_lengthOfAICD; + + caf::PdmField m_criticalWaterInLiquidFractionEmulsions; + caf::PdmField m_emulsionViscosityTransitionRegion; + caf::PdmField m_maxRatioOfEmulsionVisc; + caf::PdmField m_maxFlowRate; + caf::PdmField m_exponentOilFractionInDensityMixCalc; + caf::PdmField m_exponentWaterFractionInDensityMixCalc; + caf::PdmField m_exponentGasFractionInDensityMixCalc; + caf::PdmField m_exponentOilFractionInViscosityMixCalc; + caf::PdmField m_exponentWaterFractionInViscosityMixCalc; + caf::PdmField m_exponentGasFractionInViscosityMixCalc; + +}; + diff --git a/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.cpp b/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.cpp index 35b37a9239..2debc61a1a 100644 --- a/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.cpp +++ b/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.cpp @@ -54,6 +54,11 @@ RimWellPathValve::RimWellPathValve() CAF_PDM_InitField(&m_orificeDiameter, "OrificeDiameter", 8.0, "Orifice Diameter [mm]", "", "", ""); CAF_PDM_InitField(&m_flowCoefficient, "FlowCoefficient", 0.7, "Flow Coefficient", "", "", ""); + + CAF_PDM_InitFieldNoDefault(&m_aicdParameters, "AICDParameters", "AICD Parameters", "", "", ""); + m_aicdParameters = new RimWellPathAicdParameters; + m_aicdParameters.uiCapability()->setUiTreeHidden(true); + m_aicdParameters.uiCapability()->setUiTreeChildrenHidden(true); } //-------------------------------------------------------------------------------------------------- @@ -368,6 +373,11 @@ void RimWellPathValve::defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering group->add(&m_orificeDiameter); group->add(&m_flowCoefficient); } + else + { + caf::PdmUiGroup* group = uiOrdering.addNewGroup("MSW AICD Parameters"); + m_aicdParameters->uiOrdering(uiConfigName, *group); + } uiOrdering.skipRemainingFields(true); } diff --git a/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.h b/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.h index 7e1146b2a9..937e1168df 100644 --- a/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.h +++ b/ApplicationCode/ProjectDataModel/Completions/RimWellPathValve.h @@ -20,6 +20,7 @@ #include "RiaEclipseUnitTools.h" #include "RimCheckableNamedObject.h" +#include "RimWellPathAicdParameters.h" #include "RimWellPathComponentInterface.h" #include "cafPdmObject.h" @@ -82,6 +83,7 @@ private: caf::PdmField m_orificeDiameter; caf::PdmField m_flowCoefficient; + caf::PdmChildField m_aicdParameters; }; diff --git a/Fwk/AppFwk/cafUserInterface/CMakeLists.txt b/Fwk/AppFwk/cafUserInterface/CMakeLists.txt index a7ad48efbc..d865a9cf93 100644 --- a/Fwk/AppFwk/cafUserInterface/CMakeLists.txt +++ b/Fwk/AppFwk/cafUserInterface/CMakeLists.txt @@ -50,6 +50,7 @@ set (MOC_HEADER_FILES cafPdmUiTreeSelectionQModel.h cafPdmUiFormLayoutObjectEditor.h cafPdmUiDoubleValueEditor.h + cafPdmUniqueIdValidator.h ) if ( NOT CMAKE_AUTOMOC ) @@ -152,6 +153,7 @@ set( PROJECT_FILES cafQTreeViewStateSerializer.cpp cafMemoryInspector.h cafMemoryInspector.cpp + cafPdmUniqueIdValidator.cpp ) add_library( ${PROJECT_NAME} diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp index e827cb193d..767950a50e 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp @@ -43,6 +43,7 @@ #include "cafPdmUiDefaultObjectEditor.h" #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" +#include "cafPdmUniqueIdValidator.h" #include "cafSelectionManager.h" #include @@ -56,96 +57,6 @@ #include - - -class PdmUniqueIdValidator : public QValidator -{ -public: - PdmUniqueIdValidator(const std::set& usedIds, bool multipleSelectionOfSameFieldsSelected, const QString& errorMessage, QObject* parent) - : QValidator(parent), - m_usedIds(usedIds), - m_nextValidValue(0), - m_multipleSelectionOfSameFieldsSelected(multipleSelectionOfSameFieldsSelected), - m_errorMessage(errorMessage) - { - computeNextValidId(); - } - - State validate(QString& currentString, int &) const override - { - if (m_multipleSelectionOfSameFieldsSelected) - { - return QValidator::Invalid; - } - - if (currentString.isEmpty()) - { - return QValidator::Intermediate; - } - - bool isValidInteger = false; - int currentValue = currentString.toInt(&isValidInteger); - - if (!isValidInteger) - { - return QValidator::Invalid; - } - - if (currentValue < 0) - { - return QValidator::Invalid; - } - - if (m_usedIds.find(currentValue) != m_usedIds.end()) - { - foreach(QWidget* widget, QApplication::topLevelWidgets()) - { - if (widget->inherits("QMainWindow")) - { - QMainWindow* mainWindow = qobject_cast(widget); - if (mainWindow && mainWindow->statusBar()) - { - mainWindow->statusBar()->showMessage(m_errorMessage, 3000); - } - } - } - - return QValidator::Intermediate; - } - - return QValidator::Acceptable; - } - - void fixup(QString& editorText) const override - { - editorText = QString::number(m_nextValidValue); - } - -private: - int computeNextValidId() - { - if (!m_usedIds.empty()) - { - m_nextValidValue = *m_usedIds.rbegin(); - } - else - { - m_nextValidValue = 1; - } - - return m_nextValidValue; - } - -private: - std::set m_usedIds; - - int m_nextValidValue; - bool m_multipleSelectionOfSameFieldsSelected; - QString m_errorMessage; -}; - - - namespace caf { @@ -209,33 +120,14 @@ void PdmUiLineEditor::configureAndUpdateUi(const QString& uiConfigName) uiObject->editorAttribute(uiField()->fieldHandle(), uiConfigName, &leab); } - if (leab.useRangeValidator) + if (leab.validator) { - m_lineEdit->setValidator(new QIntValidator(leab.minValue, leab.maxValue, this)); + m_lineEdit->setValidator(leab.validator); } m_lineEdit->setAvoidSendingEnterEventToParentWidget(leab.avoidSendingEnterEventToParentWidget); } - { - PdmUiLineEditorAttributeUniqueValues leab; - caf::PdmUiObjectHandle* uiObject = uiObj(uiField()->fieldHandle()->ownerObject()); - if (uiObject) - { - uiObject->editorAttribute(uiField()->fieldHandle(), uiConfigName, &leab); - } - if (leab.usedIds.size() > 0) - { - if (isMultipleFieldsWithSameKeywordSelected(uiField()->fieldHandle())) - { - QMessageBox::information(nullptr, "Invalid operation", "The field you are manipulating is defined to have unique values. A selection of multiple fields is detected. Please select a single item."); - } - - m_lineEdit->setValidator(new PdmUniqueIdValidator(leab.usedIds, isMultipleFieldsWithSameKeywordSelected(uiField()->fieldHandle()), leab.errorMessage, this)); - } - } - - bool fromMenuOnly = true; QList enumNames = uiField()->valueOptions(&fromMenuOnly); CAF_ASSERT(fromMenuOnly); // Not supported diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.h index 6adf7d30d7..555cde5c41 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.h +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.h @@ -43,6 +43,7 @@ #include #include #include +#include #include class QGridLayout; @@ -59,31 +60,11 @@ public: PdmUiLineEditorAttribute() { avoidSendingEnterEventToParentWidget = false; - useRangeValidator = false; - minValue = 0; - maxValue = 0; } public: bool avoidSendingEnterEventToParentWidget; - bool useRangeValidator; - int minValue; - int maxValue; -}; - - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -class PdmUiLineEditorAttributeUniqueValues : public PdmUiEditorAttribute -{ -public: - PdmUiLineEditorAttributeUniqueValues() - {} - -public: - std::set usedIds; - QString errorMessage; + QPointer validator; }; //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUniqueIdValidator.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUniqueIdValidator.cpp new file mode 100644 index 0000000000..8844f1a345 --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUniqueIdValidator.cpp @@ -0,0 +1,132 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library 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. +// +// This library 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. +// +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 Lesser General Public License at <> +// for more details. +// +//################################################################################################## + +#include "cafPdmUniqueIdValidator.h" + +#include +#include +#include + +using namespace caf; + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +PdmUniqueIdValidator::PdmUniqueIdValidator(const std::set& usedIds, + bool multipleSelectionOfSameFieldsSelected, + const QString& errorMessage, + QObject* parent) + : QValidator(parent) + , m_usedIds(usedIds) + , m_nextValidValue(0) + , m_multipleSelectionOfSameFieldsSelected(multipleSelectionOfSameFieldsSelected) + , m_errorMessage(errorMessage) +{ + computeNextValidId(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QValidator::State PdmUniqueIdValidator::validate(QString& currentString, int&) const +{ + if (m_multipleSelectionOfSameFieldsSelected) + { + return QValidator::Invalid; + } + + if (currentString.isEmpty()) + { + return QValidator::Intermediate; + } + + bool isValidInteger = false; + int currentValue = currentString.toInt(&isValidInteger); + + if (!isValidInteger) + { + return QValidator::Invalid; + } + + if (currentValue < 0) + { + return QValidator::Invalid; + } + + if (m_usedIds.find(currentValue) != m_usedIds.end()) + { + foreach (QWidget* widget, QApplication::topLevelWidgets()) + { + if (widget->inherits("QMainWindow")) + { + QMainWindow* mainWindow = qobject_cast(widget); + if (mainWindow && mainWindow->statusBar()) + { + mainWindow->statusBar()->showMessage(m_errorMessage, 3000); + } + } + } + + return QValidator::Intermediate; + } + + return QValidator::Acceptable; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUniqueIdValidator::fixup(QString& editorText) const +{ + editorText = QString::number(m_nextValidValue); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +int PdmUniqueIdValidator::computeNextValidId() +{ + if (!m_usedIds.empty()) + { + m_nextValidValue = *m_usedIds.rbegin(); + } + else + { + m_nextValidValue = 1; + } + + return m_nextValidValue; +} diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUniqueIdValidator.h b/Fwk/AppFwk/cafUserInterface/cafPdmUniqueIdValidator.h new file mode 100644 index 0000000000..07b7b2052f --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUniqueIdValidator.h @@ -0,0 +1,62 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library 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. +// +// This library 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. +// +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 Lesser General Public License at <> +// for more details. +// +//################################################################################################## +#pragma once + +#include +#include + +namespace caf +{ +class PdmUniqueIdValidator : public QValidator +{ +public: + PdmUniqueIdValidator(const std::set& usedIds, bool multipleSelectionOfSameFieldsSelected, const QString& errorMessage, QObject* parent); + + State validate(QString& currentString, int &) const override; + + void fixup(QString& editorText) const override; + +private: + int computeNextValidId(); + +private: + std::set m_usedIds; + + int m_nextValidValue; + bool m_multipleSelectionOfSameFieldsSelected; + QString m_errorMessage; +}; +}