ResInsight/Fwk/VizFwk/LibUtilities/cvfuProperty.cpp
sigurdp bbebebadd5 Added complete VizFwk
Added the complete VizFwk from the ResInsight branch in Perforce as of
changelist 190.
2013-11-01 08:49:42 +01:00

521 lines
15 KiB
C++

//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2011-2013 Ceetron 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 <<http://www.gnu.org/licenses/gpl.html>>
// 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 <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cvfBase.h"
#include "cvfMath.h"
#include "cvfuProperty.h"
namespace cvfu {
//==================================================================================================
///
/// \class cvfu::Property
/// \ingroup Utilities
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Property::Property(const cvf::String& name)
: m_name(name)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const cvf::String& Property::name() const
{
return m_name;
}
//==================================================================================================
///
/// \class cvfu::PropertyBool
/// \ingroup Utilities
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PropertyBool::PropertyBool(const cvf::String& name, bool value)
: Property(name),
m_value(value)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PropertyBool::value() const
{
return m_value;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyBool::setValue(bool val)
{
m_value = val;
}
//==================================================================================================
///
/// \class cvfu::PropertyInt
/// \ingroup Utilities
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PropertyInt::PropertyInt(const cvf::String& name, int value)
: Property(name),
m_min(0),
m_max(-1),
m_guiStep(-1),
m_value(value)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PropertyInt::value() const
{
return m_value;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PropertyInt::setValue(int val)
{
if (m_min <= m_max)
{
if (!cvf::Math::valueInRange(val, m_min, m_max))
{
return false;
}
}
m_value = val;
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyInt::setRange(int min, int max)
{
m_min = min;
m_max = max;
if (m_min <= m_max)
{
m_value = cvf::Math::clamp(m_value, m_min, m_max);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PropertyInt::min() const
{
return m_min;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PropertyInt::max() const
{
return m_max;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyInt::setGuiStep(int step)
{
m_guiStep = step;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PropertyInt::guiStep() const
{
if (m_guiStep > 0)
{
return m_guiStep;
}
else
{
int singleStep = CVF_MAX(1, (m_max - m_min)/100);
return singleStep;
}
}
//==================================================================================================
///
/// \class cvfu::PropertyDouble
/// \ingroup Utilities
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PropertyDouble::PropertyDouble(const cvf::String& name, double value)
: Property(name),
m_min(0),
m_max(-1),
m_guiStep(-1),
m_decimals(2),
m_value(value)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PropertyDouble::value() const
{
return m_value;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PropertyDouble::setValue(double val)
{
if (m_min <= m_max)
{
if (!cvf::Math::valueInRange(val, m_min, m_max))
{
return false;
}
}
m_value = val;
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyDouble::setRange(double min, double max)
{
m_min = min;
m_max = max;
if (m_min <= m_max)
{
m_value = cvf::Math::clamp(m_value, m_min, m_max);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PropertyDouble::min() const
{
return m_min;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PropertyDouble::max() const
{
return m_max;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyDouble::setGuiStep(double step)
{
m_guiStep = step;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PropertyDouble::guiStep() const
{
if (m_guiStep > 0)
{
return m_guiStep;
}
else
{
double singleStep = CVF_MAX(0.0, (m_max - m_min)/100.0);
return singleStep;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyDouble::setDecimals(cvf::uint numDecimals)
{
m_decimals = numDecimals;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::uint PropertyDouble::decimals() const
{
return m_decimals;
}
//==================================================================================================
///
/// \class cvfu::PropertyEnum
/// \ingroup Utilities
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PropertyEnum::PropertyEnum(const cvf::String& name)
: Property(name),
m_currentIndex(0)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::uint PropertyEnum::itemCount() const
{
return static_cast<cvf::uint>(m_itemTexts.size());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyEnum::addItem(const cvf::String& ident, const cvf::String& text)
{
m_itemIds.push_back(ident);
m_itemTexts.push_back(text);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertyEnum::clearAllItems()
{
m_itemIds.clear();
m_itemTexts.clear();
m_currentIndex = 0;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::String PropertyEnum::itemText(cvf::uint index) const
{
return m_itemTexts[index];
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::uint PropertyEnum::currentIndex() const
{
return m_currentIndex;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PropertyEnum::setCurrentIndex(cvf::uint index)
{
if (index >= itemCount())
{
return false;
}
m_currentIndex = index;
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::String PropertyEnum::currentIdent() const
{
if (m_currentIndex < itemCount())
{
return m_itemIds[m_currentIndex];
}
else
{
return "";
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PropertyEnum::setCurrentIdent(cvf::String ident)
{
std::vector<cvf::String>::iterator it = std::find(m_itemIds.begin(), m_itemIds.end(), ident);
if (it == m_itemIds.end())
{
return false;
}
m_currentIndex = static_cast<cvf::uint>(it - m_itemIds.begin());
return true;
}
//==================================================================================================
///
/// \class cvfu::PropertySet
/// \ingroup Utilities
///
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PropertySet::PropertySet()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t PropertySet::count() const
{
return m_properties.size();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PropertySet::add(Property* property)
{
CVF_ASSERT(property);
// Illegal to add a property twice
CVF_ASSERT(std::find(m_properties.begin(), m_properties.end(), property) == m_properties.end());
m_properties.push_back(property);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Property* PropertySet::property(size_t index)
{
return m_properties.at(index);
}
} // namespace cvfu