Skip to content

Commit 4ac9775

Browse files
committed
QtPropertyBrowser: Allow controlling whether to show text for bool values
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 <[email protected]>
1 parent 4d83ff3 commit 4ac9775

File tree

6 files changed

+109
-12
lines changed

6 files changed

+109
-12
lines changed

qtpropertybrowser/src/qteditorfactory.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ class QtCheckBoxFactoryPrivate : public EditorFactoryPrivate<QtBoolEdit>
631631
Q_DECLARE_PUBLIC(QtCheckBoxFactory)
632632
public:
633633
void slotPropertyChanged(QtProperty *property, bool value);
634+
void slotTextVisibleChanged(QtProperty *property, bool textVisible);
634635
void slotSetValue(bool value);
635636
};
636637

@@ -648,6 +649,22 @@ void QtCheckBoxFactoryPrivate::slotPropertyChanged(QtProperty *property, bool va
648649
}
649650
}
650651

652+
void QtCheckBoxFactoryPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible)
653+
{
654+
if (!m_createdEditors.contains(property))
655+
return;
656+
657+
QtBoolPropertyManager *manager = q_ptr->propertyManager(property);
658+
if (!manager)
659+
return;
660+
661+
QListIterator<QtBoolEdit *> itEditor(m_createdEditors[property]);
662+
while (itEditor.hasNext()) {
663+
QtBoolEdit *editor = itEditor.next();
664+
editor->setTextVisible(textVisible);
665+
}
666+
}
667+
651668
void QtCheckBoxFactoryPrivate::slotSetValue(bool value)
652669
{
653670
QObject *object = q_ptr->sender();
@@ -702,6 +719,8 @@ void QtCheckBoxFactory::connectPropertyManager(QtBoolPropertyManager *manager)
702719
{
703720
connect(manager, SIGNAL(valueChanged(QtProperty *, bool)),
704721
this, SLOT(slotPropertyChanged(QtProperty *, bool)));
722+
connect(manager, SIGNAL(textVisibleChanged(QtProperty *, bool)),
723+
this, SLOT(slotTextVisibleChanged(QtProperty *, bool)));
705724
}
706725

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

718738
connect(editor, SIGNAL(toggled(bool)), this, SLOT(slotSetValue(bool)));
719739
connect(editor, SIGNAL(destroyed(QObject *)),
@@ -730,6 +750,8 @@ void QtCheckBoxFactory::disconnectPropertyManager(QtBoolPropertyManager *manager
730750
{
731751
disconnect(manager, SIGNAL(valueChanged(QtProperty *, bool)),
732752
this, SLOT(slotPropertyChanged(QtProperty *, bool)));
753+
disconnect(manager, SIGNAL(textVisibleChanged(QtProperty *, bool)),
754+
this, SLOT(slotTextVisibleChanged(QtProperty *, bool)));
733755
}
734756

735757
// QtDoubleSpinBoxFactory

qtpropertybrowser/src/qteditorfactory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCheckBoxFactory : public QtAbstractEditorFac
139139
Q_DECLARE_PRIVATE(QtCheckBoxFactory)
140140
Q_DISABLE_COPY(QtCheckBoxFactory)
141141
Q_PRIVATE_SLOT(d_func(), void slotPropertyChanged(QtProperty *, bool))
142+
Q_PRIVATE_SLOT(d_func(), void slotTextVisibleChanged(QtProperty *, bool))
142143
Q_PRIVATE_SLOT(d_func(), void slotSetValue(bool))
143144
Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *))
144145
};

qtpropertybrowser/src/qtpropertymanager.cpp

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,16 @@ class QtBoolPropertyManagerPrivate
16001600
public:
16011601
QtBoolPropertyManagerPrivate();
16021602

1603-
QMap<const QtProperty *, bool> m_values;
1603+
struct Data
1604+
{
1605+
Data() : val(false), textVisible(true) {}
1606+
bool val;
1607+
bool textVisible;
1608+
};
1609+
1610+
typedef QMap<const QtProperty *, Data> PropertyValueMap;
1611+
PropertyValueMap m_values;
1612+
16041613
const QIcon m_checkedIcon;
16051614
const QIcon m_uncheckedIcon;
16061615
};
@@ -1663,33 +1672,42 @@ QtBoolPropertyManager::~QtBoolPropertyManager()
16631672
*/
16641673
bool QtBoolPropertyManager::value(const QtProperty *property) const
16651674
{
1666-
return d_ptr->m_values.value(property, false);
1675+
return getValue<bool>(d_ptr->m_values, property, false);
1676+
}
1677+
1678+
bool QtBoolPropertyManager::textVisible(const QtProperty *property) const
1679+
{
1680+
return getData<bool>(d_ptr->m_values, &QtBoolPropertyManagerPrivate::Data::textVisible, property, false);
16671681
}
16681682

16691683
/*!
16701684
\reimp
16711685
*/
16721686
QString QtBoolPropertyManager::valueText(const QtProperty *property) const
16731687
{
1674-
const QMap<const QtProperty *, bool>::const_iterator it = d_ptr->m_values.constFind(property);
1688+
const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
16751689
if (it == d_ptr->m_values.constEnd())
16761690
return QString();
16771691

1692+
const QtBoolPropertyManagerPrivate::Data &data = it.value();
1693+
if (!data.textVisible)
1694+
return QString();
1695+
16781696
static const QString trueText = tr("True");
16791697
static const QString falseText = tr("False");
1680-
return it.value() ? trueText : falseText;
1698+
return data.val ? trueText : falseText;
16811699
}
16821700

16831701
/*!
16841702
\reimp
16851703
*/
16861704
QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const
16871705
{
1688-
const QMap<const QtProperty *, bool>::const_iterator it = d_ptr->m_values.constFind(property);
1706+
const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
16891707
if (it == d_ptr->m_values.constEnd())
16901708
return QIcon();
16911709

1692-
return it.value() ? d_ptr->m_checkedIcon : d_ptr->m_uncheckedIcon;
1710+
return it.value().val ? d_ptr->m_checkedIcon : d_ptr->m_uncheckedIcon;
16931711
}
16941712

16951713
/*!
@@ -1701,18 +1719,46 @@ QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const
17011719
*/
17021720
void QtBoolPropertyManager::setValue(QtProperty *property, bool val)
17031721
{
1704-
setSimpleValue<bool, bool, QtBoolPropertyManager>(d_ptr->m_values, this,
1705-
&QtBoolPropertyManager::propertyChanged,
1706-
&QtBoolPropertyManager::valueChanged,
1707-
property, val);
1722+
const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
1723+
if (it == d_ptr->m_values.end())
1724+
return;
1725+
1726+
QtBoolPropertyManagerPrivate::Data data = it.value();
1727+
1728+
if (data.val == val)
1729+
return;
1730+
1731+
data.val = val;
1732+
it.value() = data;
1733+
1734+
emit propertyChanged(property);
1735+
emit valueChanged(property, data.val);
1736+
}
1737+
1738+
void QtBoolPropertyManager::setTextVisible(QtProperty *property, bool textVisible)
1739+
{
1740+
const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
1741+
if (it == d_ptr->m_values.end())
1742+
return;
1743+
1744+
QtBoolPropertyManagerPrivate::Data data = it.value();
1745+
1746+
if (data.textVisible == textVisible)
1747+
return;
1748+
1749+
data.textVisible = textVisible;
1750+
it.value() = data;
1751+
1752+
emit propertyChanged(property);
1753+
emit textVisibleChanged(property, data.textVisible);
17081754
}
17091755

17101756
/*!
17111757
\reimp
17121758
*/
17131759
void QtBoolPropertyManager::initializeProperty(QtProperty *property)
17141760
{
1715-
d_ptr->m_values[property] = false;
1761+
d_ptr->m_values[property] = QtBoolPropertyManagerPrivate::Data();
17161762
}
17171763

17181764
/*!

qtpropertybrowser/src/qtpropertymanager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ class QT_QTPROPERTYBROWSER_EXPORT QtBoolPropertyManager : public QtAbstractPrope
115115
~QtBoolPropertyManager();
116116

117117
bool value(const QtProperty *property) const;
118+
bool textVisible(const QtProperty *property) const;
118119

119120
public Q_SLOTS:
120121
void setValue(QtProperty *property, bool val);
122+
void setTextVisible(QtProperty *property, bool textVisible);
121123
Q_SIGNALS:
122124
void valueChanged(QtProperty *property, bool val);
125+
void textVisibleChanged(QtProperty *property, bool);
123126
protected:
124127
QString valueText(const QtProperty *property) const;
125128
QIcon valueIcon(const QtProperty *property) const;

qtpropertybrowser/src/qtvariantproperty.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class QtVariantPropertyManagerPrivate
344344
void slotFlagChanged(QtProperty *property, int val);
345345
void slotFlagNamesChanged(QtProperty *property, const QStringList &flagNames);
346346
void slotReadOnlyChanged(QtProperty *property, bool readOnly);
347+
void slotTextVisibleChanged(QtProperty *property, bool textVisible);
347348
void slotPropertyInserted(QtProperty *property, QtProperty *parent, QtProperty *after);
348349
void slotPropertyRemoved(QtProperty *property, QtProperty *parent);
349350

@@ -375,6 +376,7 @@ class QtVariantPropertyManagerPrivate
375376
const QString m_regExpAttribute;
376377
const QString m_echoModeAttribute;
377378
const QString m_readOnlyAttribute;
379+
const QString m_textVisibleAttribute;
378380
};
379381

380382
QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() :
@@ -388,7 +390,8 @@ QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() :
388390
m_minimumAttribute(QLatin1String("minimum")),
389391
m_regExpAttribute(QLatin1String("regExp")),
390392
m_echoModeAttribute(QLatin1String("echoMode")),
391-
m_readOnlyAttribute(QLatin1String("readOnly"))
393+
m_readOnlyAttribute(QLatin1String("readOnly")),
394+
m_textVisibleAttribute(QLatin1String("textVisible"))
392395
{
393396
}
394397

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

562+
void QtVariantPropertyManagerPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible)
563+
{
564+
if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0))
565+
emit q_ptr->attributeChanged(varProp, m_textVisibleAttribute, QVariant(textVisible));
566+
}
567+
559568
void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, const QDate &val)
560569
{
561570
valueChanged(property, QVariant(val));
@@ -839,6 +848,10 @@ void QtVariantPropertyManagerPrivate::slotFlagNamesChanged(QtProperty *property,
839848
\o
840849
\o decimals
841850
\o QVariant::Int
851+
\row
852+
\o \c bool
853+
\o textVisible
854+
\o QVariant::Bool
842855
\row
843856
\o QString
844857
\o regExp
@@ -993,9 +1006,12 @@ QtVariantPropertyManager::QtVariantPropertyManager(QObject *parent)
9931006
// BoolPropertyManager
9941007
QtBoolPropertyManager *boolPropertyManager = new QtBoolPropertyManager(this);
9951008
d_ptr->m_typeToPropertyManager[QVariant::Bool] = boolPropertyManager;
1009+
d_ptr->m_typeToAttributeToAttributeType[QVariant::Bool][d_ptr->m_textVisibleAttribute] = QVariant::Bool;
9961010
d_ptr->m_typeToValueType[QVariant::Bool] = QVariant::Bool;
9971011
connect(boolPropertyManager, SIGNAL(valueChanged(QtProperty *, bool)),
9981012
this, SLOT(slotValueChanged(QtProperty *, bool)));
1013+
connect(boolPropertyManager, SIGNAL(textVisibleChanged(QtProperty*, bool)),
1014+
this, SLOT(slotTextVisibleChanged(QtProperty*, bool)));
9991015
// StringPropertyManager
10001016
QtStringPropertyManager *stringPropertyManager = new QtStringPropertyManager(this);
10011017
d_ptr->m_typeToPropertyManager[QVariant::String] = stringPropertyManager;
@@ -1502,6 +1518,10 @@ QVariant QtVariantPropertyManager::attributeValue(const QtProperty *property, co
15021518
if (attribute == d_ptr->m_readOnlyAttribute)
15031519
return doubleManager->isReadOnly(internProp);
15041520
return QVariant();
1521+
} else if (QtBoolPropertyManager *boolManager = qobject_cast<QtBoolPropertyManager *>(manager)) {
1522+
if (attribute == d_ptr->m_textVisibleAttribute)
1523+
return boolManager->textVisible(internProp);
1524+
return QVariant();
15051525
} else if (QtStringPropertyManager *stringManager = qobject_cast<QtStringPropertyManager *>(manager)) {
15061526
if (attribute == d_ptr->m_regExpAttribute)
15071527
return stringManager->regExp(internProp);
@@ -1753,6 +1773,10 @@ void QtVariantPropertyManager::setAttribute(QtProperty *property,
17531773
if (attribute == d_ptr->m_readOnlyAttribute)
17541774
doubleManager->setReadOnly(internProp, qVariantValue<bool>(value));
17551775
return;
1776+
} else if (QtBoolPropertyManager *boolManager = qobject_cast<QtBoolPropertyManager *>(manager)) {
1777+
if (attribute == d_ptr->m_textVisibleAttribute)
1778+
boolManager->setTextVisible(internProp, qVariantValue<bool>(value));
1779+
return;
17561780
} else if (QtStringPropertyManager *stringManager = qobject_cast<QtStringPropertyManager *>(manager)) {
17571781
if (attribute == d_ptr->m_regExpAttribute)
17581782
stringManager->setRegExp(internProp, qVariantValue<QRegExp>(value));

qtpropertybrowser/src/qtvariantproperty.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public Q_SLOTS:
153153
Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, const QCursor &))
154154
Q_PRIVATE_SLOT(d_func(), void slotFlagNamesChanged(QtProperty *, const QStringList &))
155155
Q_PRIVATE_SLOT(d_func(), void slotReadOnlyChanged(QtProperty *, bool))
156+
Q_PRIVATE_SLOT(d_func(), void slotTextVisibleChanged(QtProperty *, bool))
156157
Q_PRIVATE_SLOT(d_func(), void slotPropertyInserted(QtProperty *, QtProperty *, QtProperty *))
157158
Q_PRIVATE_SLOT(d_func(), void slotPropertyRemoved(QtProperty *, QtProperty *))
158159
Q_DECLARE_PRIVATE(QtVariantPropertyManager)

0 commit comments

Comments
 (0)