[go: up one dir, main page]

Skip to content

Commit

Permalink
QtPropertyBrowser: Allow controlling whether to show text for bool va…
Browse files Browse the repository at this point in the history
…lues

This was a rather involved patch, but it avoids the need for using
private API or duplicating QtBoolEdit (which is in a private header).

Apart from adding support for this QtBoolEdit option to the
QtCheckBoxFactory and the QtBoolPropertyManager, the setting can also
be controlled via the 'textVisible' attribute associated with
QtVariantProperty instances.

Change-Id: Ia0c694ca852449c74cbe25429df69413211b45a5
Reviewed-by: Jarek Kobus <jaroslaw.kobus@digia.com>
  • Loading branch information
bjorn committed Jul 8, 2013
1 parent 4d83ff3 commit 4ac9775
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 12 deletions.
22 changes: 22 additions & 0 deletions qtpropertybrowser/src/qteditorfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ class QtCheckBoxFactoryPrivate : public EditorFactoryPrivate<QtBoolEdit>
Q_DECLARE_PUBLIC(QtCheckBoxFactory)
public:
void slotPropertyChanged(QtProperty *property, bool value);
void slotTextVisibleChanged(QtProperty *property, bool textVisible);
void slotSetValue(bool value);
};

Expand All @@ -648,6 +649,22 @@ void QtCheckBoxFactoryPrivate::slotPropertyChanged(QtProperty *property, bool va
}
}

void QtCheckBoxFactoryPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible)
{
if (!m_createdEditors.contains(property))
return;

QtBoolPropertyManager *manager = q_ptr->propertyManager(property);
if (!manager)
return;

QListIterator<QtBoolEdit *> itEditor(m_createdEditors[property]);
while (itEditor.hasNext()) {
QtBoolEdit *editor = itEditor.next();
editor->setTextVisible(textVisible);
}
}

void QtCheckBoxFactoryPrivate::slotSetValue(bool value)
{
QObject *object = q_ptr->sender();
Expand Down Expand Up @@ -702,6 +719,8 @@ void QtCheckBoxFactory::connectPropertyManager(QtBoolPropertyManager *manager)
{
connect(manager, SIGNAL(valueChanged(QtProperty *, bool)),
this, SLOT(slotPropertyChanged(QtProperty *, bool)));
connect(manager, SIGNAL(textVisibleChanged(QtProperty *, bool)),
this, SLOT(slotTextVisibleChanged(QtProperty *, bool)));
}

/*!
Expand All @@ -714,6 +733,7 @@ QWidget *QtCheckBoxFactory::createEditor(QtBoolPropertyManager *manager, QtPrope
{
QtBoolEdit *editor = d_ptr->createEditor(property, parent);
editor->setChecked(manager->value(property));
editor->setTextVisible(manager->textVisible(property));

connect(editor, SIGNAL(toggled(bool)), this, SLOT(slotSetValue(bool)));
connect(editor, SIGNAL(destroyed(QObject *)),
Expand All @@ -730,6 +750,8 @@ void QtCheckBoxFactory::disconnectPropertyManager(QtBoolPropertyManager *manager
{
disconnect(manager, SIGNAL(valueChanged(QtProperty *, bool)),
this, SLOT(slotPropertyChanged(QtProperty *, bool)));
disconnect(manager, SIGNAL(textVisibleChanged(QtProperty *, bool)),
this, SLOT(slotTextVisibleChanged(QtProperty *, bool)));
}

// QtDoubleSpinBoxFactory
Expand Down
1 change: 1 addition & 0 deletions qtpropertybrowser/src/qteditorfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCheckBoxFactory : public QtAbstractEditorFac
Q_DECLARE_PRIVATE(QtCheckBoxFactory)
Q_DISABLE_COPY(QtCheckBoxFactory)
Q_PRIVATE_SLOT(d_func(), void slotPropertyChanged(QtProperty *, bool))
Q_PRIVATE_SLOT(d_func(), void slotTextVisibleChanged(QtProperty *, bool))
Q_PRIVATE_SLOT(d_func(), void slotSetValue(bool))
Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *))
};
Expand Down
68 changes: 57 additions & 11 deletions qtpropertybrowser/src/qtpropertymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,16 @@ class QtBoolPropertyManagerPrivate
public:
QtBoolPropertyManagerPrivate();

QMap<const QtProperty *, bool> m_values;
struct Data
{
Data() : val(false), textVisible(true) {}
bool val;
bool textVisible;
};

typedef QMap<const QtProperty *, Data> PropertyValueMap;
PropertyValueMap m_values;

const QIcon m_checkedIcon;
const QIcon m_uncheckedIcon;
};
Expand Down Expand Up @@ -1663,33 +1672,42 @@ QtBoolPropertyManager::~QtBoolPropertyManager()
*/
bool QtBoolPropertyManager::value(const QtProperty *property) const
{
return d_ptr->m_values.value(property, false);
return getValue<bool>(d_ptr->m_values, property, false);
}

bool QtBoolPropertyManager::textVisible(const QtProperty *property) const
{
return getData<bool>(d_ptr->m_values, &QtBoolPropertyManagerPrivate::Data::textVisible, property, false);
}

/*!
\reimp
*/
QString QtBoolPropertyManager::valueText(const QtProperty *property) const
{
const QMap<const QtProperty *, bool>::const_iterator it = d_ptr->m_values.constFind(property);
const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
if (it == d_ptr->m_values.constEnd())
return QString();

const QtBoolPropertyManagerPrivate::Data &data = it.value();
if (!data.textVisible)
return QString();

static const QString trueText = tr("True");
static const QString falseText = tr("False");
return it.value() ? trueText : falseText;
return data.val ? trueText : falseText;
}

/*!
\reimp
*/
QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const
{
const QMap<const QtProperty *, bool>::const_iterator it = d_ptr->m_values.constFind(property);
const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
if (it == d_ptr->m_values.constEnd())
return QIcon();

return it.value() ? d_ptr->m_checkedIcon : d_ptr->m_uncheckedIcon;
return it.value().val ? d_ptr->m_checkedIcon : d_ptr->m_uncheckedIcon;
}

/*!
Expand All @@ -1701,18 +1719,46 @@ QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const
*/
void QtBoolPropertyManager::setValue(QtProperty *property, bool val)
{
setSimpleValue<bool, bool, QtBoolPropertyManager>(d_ptr->m_values, this,
&QtBoolPropertyManager::propertyChanged,
&QtBoolPropertyManager::valueChanged,
property, val);
const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
if (it == d_ptr->m_values.end())
return;

QtBoolPropertyManagerPrivate::Data data = it.value();

if (data.val == val)
return;

data.val = val;
it.value() = data;

emit propertyChanged(property);
emit valueChanged(property, data.val);
}

void QtBoolPropertyManager::setTextVisible(QtProperty *property, bool textVisible)
{
const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
if (it == d_ptr->m_values.end())
return;

QtBoolPropertyManagerPrivate::Data data = it.value();

if (data.textVisible == textVisible)
return;

data.textVisible = textVisible;
it.value() = data;

emit propertyChanged(property);
emit textVisibleChanged(property, data.textVisible);
}

/*!
\reimp
*/
void QtBoolPropertyManager::initializeProperty(QtProperty *property)
{
d_ptr->m_values[property] = false;
d_ptr->m_values[property] = QtBoolPropertyManagerPrivate::Data();
}

/*!
Expand Down
3 changes: 3 additions & 0 deletions qtpropertybrowser/src/qtpropertymanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ class QT_QTPROPERTYBROWSER_EXPORT QtBoolPropertyManager : public QtAbstractPrope
~QtBoolPropertyManager();

bool value(const QtProperty *property) const;
bool textVisible(const QtProperty *property) const;

public Q_SLOTS:
void setValue(QtProperty *property, bool val);
void setTextVisible(QtProperty *property, bool textVisible);
Q_SIGNALS:
void valueChanged(QtProperty *property, bool val);
void textVisibleChanged(QtProperty *property, bool);
protected:
QString valueText(const QtProperty *property) const;
QIcon valueIcon(const QtProperty *property) const;
Expand Down
26 changes: 25 additions & 1 deletion qtpropertybrowser/src/qtvariantproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ class QtVariantPropertyManagerPrivate
void slotFlagChanged(QtProperty *property, int val);
void slotFlagNamesChanged(QtProperty *property, const QStringList &flagNames);
void slotReadOnlyChanged(QtProperty *property, bool readOnly);
void slotTextVisibleChanged(QtProperty *property, bool textVisible);
void slotPropertyInserted(QtProperty *property, QtProperty *parent, QtProperty *after);
void slotPropertyRemoved(QtProperty *property, QtProperty *parent);

Expand Down Expand Up @@ -375,6 +376,7 @@ class QtVariantPropertyManagerPrivate
const QString m_regExpAttribute;
const QString m_echoModeAttribute;
const QString m_readOnlyAttribute;
const QString m_textVisibleAttribute;
};

QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() :
Expand All @@ -388,7 +390,8 @@ QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() :
m_minimumAttribute(QLatin1String("minimum")),
m_regExpAttribute(QLatin1String("regExp")),
m_echoModeAttribute(QLatin1String("echoMode")),
m_readOnlyAttribute(QLatin1String("readOnly"))
m_readOnlyAttribute(QLatin1String("readOnly")),
m_textVisibleAttribute(QLatin1String("textVisible"))
{
}

Expand Down Expand Up @@ -556,6 +559,12 @@ void QtVariantPropertyManagerPrivate::slotReadOnlyChanged(QtProperty *property,
emit q_ptr->attributeChanged(varProp, m_readOnlyAttribute, QVariant(readOnly));
}

void QtVariantPropertyManagerPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible)
{
if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0))
emit q_ptr->attributeChanged(varProp, m_textVisibleAttribute, QVariant(textVisible));
}

void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, const QDate &val)
{
valueChanged(property, QVariant(val));
Expand Down Expand Up @@ -839,6 +848,10 @@ void QtVariantPropertyManagerPrivate::slotFlagNamesChanged(QtProperty *property,
\o
\o decimals
\o QVariant::Int
\row
\o \c bool
\o textVisible
\o QVariant::Bool
\row
\o QString
\o regExp
Expand Down Expand Up @@ -993,9 +1006,12 @@ QtVariantPropertyManager::QtVariantPropertyManager(QObject *parent)
// BoolPropertyManager
QtBoolPropertyManager *boolPropertyManager = new QtBoolPropertyManager(this);
d_ptr->m_typeToPropertyManager[QVariant::Bool] = boolPropertyManager;
d_ptr->m_typeToAttributeToAttributeType[QVariant::Bool][d_ptr->m_textVisibleAttribute] = QVariant::Bool;
d_ptr->m_typeToValueType[QVariant::Bool] = QVariant::Bool;
connect(boolPropertyManager, SIGNAL(valueChanged(QtProperty *, bool)),
this, SLOT(slotValueChanged(QtProperty *, bool)));
connect(boolPropertyManager, SIGNAL(textVisibleChanged(QtProperty*, bool)),
this, SLOT(slotTextVisibleChanged(QtProperty*, bool)));
// StringPropertyManager
QtStringPropertyManager *stringPropertyManager = new QtStringPropertyManager(this);
d_ptr->m_typeToPropertyManager[QVariant::String] = stringPropertyManager;
Expand Down Expand Up @@ -1502,6 +1518,10 @@ QVariant QtVariantPropertyManager::attributeValue(const QtProperty *property, co
if (attribute == d_ptr->m_readOnlyAttribute)
return doubleManager->isReadOnly(internProp);
return QVariant();
} else if (QtBoolPropertyManager *boolManager = qobject_cast<QtBoolPropertyManager *>(manager)) {
if (attribute == d_ptr->m_textVisibleAttribute)
return boolManager->textVisible(internProp);
return QVariant();
} else if (QtStringPropertyManager *stringManager = qobject_cast<QtStringPropertyManager *>(manager)) {
if (attribute == d_ptr->m_regExpAttribute)
return stringManager->regExp(internProp);
Expand Down Expand Up @@ -1753,6 +1773,10 @@ void QtVariantPropertyManager::setAttribute(QtProperty *property,
if (attribute == d_ptr->m_readOnlyAttribute)
doubleManager->setReadOnly(internProp, qVariantValue<bool>(value));
return;
} else if (QtBoolPropertyManager *boolManager = qobject_cast<QtBoolPropertyManager *>(manager)) {
if (attribute == d_ptr->m_textVisibleAttribute)
boolManager->setTextVisible(internProp, qVariantValue<bool>(value));
return;
} else if (QtStringPropertyManager *stringManager = qobject_cast<QtStringPropertyManager *>(manager)) {
if (attribute == d_ptr->m_regExpAttribute)
stringManager->setRegExp(internProp, qVariantValue<QRegExp>(value));
Expand Down
1 change: 1 addition & 0 deletions qtpropertybrowser/src/qtvariantproperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public Q_SLOTS:
Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, const QCursor &))
Q_PRIVATE_SLOT(d_func(), void slotFlagNamesChanged(QtProperty *, const QStringList &))
Q_PRIVATE_SLOT(d_func(), void slotReadOnlyChanged(QtProperty *, bool))
Q_PRIVATE_SLOT(d_func(), void slotTextVisibleChanged(QtProperty *, bool))
Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty *, QtProperty *, QtProperty *))
Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty *, QtProperty *))
Q_DECLARE_PRIVATE(QtVariantPropertyManager)
Expand Down

0 comments on commit 4ac9775

Please sign in to comment.