#1156 Added tristate class and checkbox editor with tristate support

This commit is contained in:
Magne Sjaastad
2017-01-31 16:08:04 +01:00
parent bd732d9564
commit 8c01c23e5e
6 changed files with 368 additions and 0 deletions

View File

@@ -41,6 +41,9 @@ set( PROJECT_FILES
cafNotificationCenter.cpp
cafNotificationCenter.h
cafTristate.cpp
cafTristate.h
)

View File

@@ -0,0 +1,158 @@
#include "cafTristate.h"
#include <QTextStream>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::Tristate::Tristate()
: m_state(State::False)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::Tristate::operator=(const Tristate& other)
{
m_state = other.m_state;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::Tristate::operator=(const State& state)
{
m_state = state;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::Tristate::operator==(const Tristate& other) const
{
return m_state == other.m_state;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::Tristate::operator==(State state) const
{
return m_state == state;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::Tristate::operator!=(const Tristate& other) const
{
return !(m_state == other.m_state);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::Tristate::State caf::Tristate::state() const
{
return m_state;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::Tristate::isTrue() const
{
return m_state == State::True;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::Tristate::isPartiallyTrue() const
{
return m_state == State::PartiallyTrue;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::Tristate::isFalse() const
{
return m_state == State::False;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString caf::Tristate::text() const
{
QString txt;
switch (m_state)
{
case Tristate::State::False:
txt = "False";
break;
case Tristate::State::PartiallyTrue:
txt = "PartiallyTrue";
break;
case Tristate::State::True:
txt = "True";
break;
default:
break;
}
return txt;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::Tristate::setFromText(const QString& valueText)
{
QString lowerCase = valueText.toLower();
if (lowerCase == "false")
{
m_state = State::False;
}
else if (lowerCase == "partiallytrue")
{
m_state = State::PartiallyTrue;
}
else if (lowerCase == "true")
{
m_state = State::True;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void operator >> (QTextStream& str, caf::Tristate& triplet)
{
QString text;
str >> text;
triplet.setFromText(text);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void operator << (QTextStream& str, const caf::Tristate& triplet)
{
str << triplet.text();
}

View File

@@ -0,0 +1,53 @@
#pragma once
#include <QMetaType>
class QTextStream;
namespace caf
{
//==================================================================================================
//==================================================================================================
class Tristate
{
public:
enum class State {False, PartiallyTrue, True};
public:
Tristate();
void operator=(const Tristate& other);
void operator=(const State& state);
bool operator==(const Tristate& other) const;
bool operator==(State state) const;
bool operator!=(const Tristate& other) const;
State state() const;
bool isTrue() const;
bool isPartiallyTrue() const;
bool isFalse() const;
QString text() const;
void setFromText(const QString& valueText);
protected:
State m_state;
};
} // end namespace caf
//==================================================================================================
// Overload of QTextStream for caf::Triplet
//==================================================================================================
void operator >> (QTextStream& str, caf::Tristate& triplet);
void operator << (QTextStream& str, const caf::Tristate& triplet);
Q_DECLARE_METATYPE(caf::Tristate);