#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);

View File

@ -20,6 +20,7 @@ include_directories (
set( QOBJECT_HEADERS
cafPdmUiCheckBoxDelegate.h
cafPdmUiCheckBoxEditor.h
cafPdmUiCheckBoxTristateEditor.h
cafPdmUiColorEditor.h
cafPdmUiComboBoxEditor.h
cafPdmUiDoubleSliderEditor.h
@ -55,6 +56,8 @@ set( PROJECT_FILES
cafPdmUiCheckBoxDelegate.h
cafPdmUiCheckBoxEditor.cpp
cafPdmUiCheckBoxEditor.h
cafPdmUiCheckBoxTristateEditor.cpp
cafPdmUiCheckBoxTristateEditor.h
cafPdmUiColorEditor.cpp
cafPdmUiColorEditor.h
cafPdmUiComboBoxEditor.cpp

View File

@ -0,0 +1,113 @@
#include "cafPdmUiCheckBoxTristateEditor.h"
#include "cafPdmUiDefaultObjectEditor.h"
#include "cafPdmObject.h"
#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiOrdering.h"
#include "cafPdmField.h"
#include "cafFactory.h"
#include "cafTristate.h"
#include <assert.h>
namespace caf
{
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiCheckBoxTristateEditor);
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiCheckBoxTristateEditor::configureAndUpdateUi(const QString& uiConfigName)
{
assert(!m_checkBox.isNull());
assert(!m_label.isNull());
{
QIcon ic = field()->uiIcon(uiConfigName);
if (!ic.isNull())
{
m_label->setPixmap(ic.pixmap(ic.actualSize(QSize(64, 64))));
}
else
{
m_label->setText(field()->uiName(uiConfigName));
}
}
m_label->setEnabled(!field()->isUiReadOnly(uiConfigName));
m_label->setToolTip(field()->uiToolTip(uiConfigName));
m_checkBox->setEnabled(!field()->isUiReadOnly(uiConfigName));
m_checkBox->setToolTip(field()->uiToolTip(uiConfigName));
Tristate state = field()->uiValue().value<Tristate>();
if (state == Tristate::State::True)
{
m_checkBox->setCheckState(Qt::Checked);
}
else if (state == Tristate::State::PartiallyTrue)
{
m_checkBox->setCheckState(Qt::PartiallyChecked);
}
else if (state == Tristate::State::False)
{
m_checkBox->setCheckState(Qt::Unchecked);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiCheckBoxTristateEditor::createEditorWidget(QWidget * parent)
{
m_checkBox = new QCheckBox(parent);
m_checkBox->setTristate(true);
connect(m_checkBox, SIGNAL(clicked(bool)), this, SLOT(slotClicked(bool)));
return m_checkBox;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiCheckBoxTristateEditor::createLabelWidget(QWidget * parent)
{
m_label = new QLabel(parent);
return m_label;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiCheckBoxTristateEditor::slotClicked(bool)
{
Tristate state;
if (m_checkBox->checkState() == Qt::Checked)
{
state = Tristate::State::True;
}
else if (m_checkBox->checkState() == Qt::PartiallyChecked)
{
state = Tristate::State::PartiallyTrue;
}
else if (m_checkBox->checkState() == Qt::Unchecked)
{
state = Tristate::State::False;
}
QVariant v = QVariant::fromValue(state);
this->setValueToField(v);
}
} // end namespace caf

View File

@ -0,0 +1,38 @@
#pragma once
#include "cafPdmUiFieldEditorHandle.h"
#include <QWidget>
#include <QPointer>
#include <QCheckBox>
#include <QLabel>
namespace caf
{
class PdmUiCheckBoxTristateEditor : public PdmUiFieldEditorHandle
{
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
public:
PdmUiCheckBoxTristateEditor() {}
virtual ~PdmUiCheckBoxTristateEditor() {}
protected:
virtual QWidget* createEditorWidget(QWidget* parent);
virtual QWidget* createLabelWidget(QWidget* parent);
virtual void configureAndUpdateUi(const QString& uiConfigName);
protected slots:
void slotClicked(bool);
private:
QPointer<QCheckBox> m_checkBox;
QPointer<QLabel> m_label;
};
} // end namespace caf