Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Add sanity check asserts to KisWidgetConnectionUtils

Dmitry Kazakov86325c1c00d7
diff --git a/libs/widgets/KisWidgetConnectionUtils.cpp b/libs/widgets/KisWidgetConnectionUtils.cpp
index ca354d659e..2390d07f91 100644
--- a/libs/widgets/KisWidgetConnectionUtils.cpp
+++ b/libs/widgets/KisWidgetConnectionUtils.cpp
@@ -24,6 +24,14 @@
#include "kis_color_button.h"
#include "kis_file_name_requester.h"
+/**
+ * Sanity check asserts that check the validity of the passed
+ * properties (as c-strings). Sanity check intentionally use
+ * Q_ASSERT to strip this code completely from the release builds.
+ */
+#define SANITY_CHECK_PROPERTY_EXISTS(prop) Q_ASSERT(prop.isValid())
+#define SANITY_CHECK_PROPERTY_EXISTS_AND_WRITABLE(prop) do { Q_ASSERT(prop.isValid()); Q_ASSERT(prop.isWritable()); } while (0)
+
class ConnectButtonStateHelper : public QObject
{
Q_OBJECT
@@ -84,6 +92,7 @@ void connectButtonLikeControl(Button *button, QObject *source, const char *prope
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -125,6 +134,7 @@ void connectControl(QSpinBox *spinBox, QObject *source, const char *property)
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -151,6 +161,7 @@ void connectControl(QSlider *slider, QObject *source, const char *property)
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -177,6 +188,7 @@ void connectControl(QDoubleSpinBox *spinBox, QObject *source, const char *proper
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -227,6 +239,7 @@ void connectControlState(QSpinBox *spinBox, QObject *source, const char *readSta
{
const QMetaObject* meta = source->metaObject();
QMetaProperty readStateProp = meta->property(meta->indexOfProperty(readStateProperty));
+ SANITY_CHECK_PROPERTY_EXISTS(readStateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(readStateProp.hasNotifySignal());
@@ -246,6 +259,7 @@ void connectControlState(QSpinBox *spinBox, QObject *source, const char *readSta
helper->setState(readStateProp.read(source).value<IntSpinBoxState>());
QMetaProperty writeProp = meta->property(meta->indexOfProperty(writeProperty));
+ SANITY_CHECK_PROPERTY_EXISTS_AND_WRITABLE(writeProp);
if (writeProp.isWritable()) {
QObject::connect(spinBox, qOverload<int>(&QSpinBox::valueChanged),
source, [writeProp, source] (int value) { writeProp.write(source, value); });
@@ -280,6 +294,7 @@ void connectControlState(QDoubleSpinBox *spinBox, QObject *source, const char *r
{
const QMetaObject* meta = source->metaObject();
QMetaProperty readStateProp = meta->property(meta->indexOfProperty(readStateProperty));
+ SANITY_CHECK_PROPERTY_EXISTS(readStateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(readStateProp.hasNotifySignal());
@@ -299,6 +314,7 @@ void connectControlState(QDoubleSpinBox *spinBox, QObject *source, const char *r
helper->setState(readStateProp.read(source).value<DoubleSpinBoxState>());
QMetaProperty writeProp = meta->property(meta->indexOfProperty(writeProperty));
+ SANITY_CHECK_PROPERTY_EXISTS_AND_WRITABLE(writeProp);
if (writeProp.isWritable()) {
QObject::connect(spinBox, qOverload<qreal>(&QDoubleSpinBox::valueChanged),
source, [writeProp, source] (qreal value) { writeProp.write(source, value); });
@@ -310,6 +326,7 @@ void connectControl(KisMultipliersDoubleSliderSpinBox *spinBox, QObject *source,
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -380,6 +397,7 @@ void connectControl(QButtonGroup *group, QObject *source, const char *property)
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -409,6 +427,7 @@ void connectControlState(QButtonGroup *group, QObject *source, const char *readS
{
const QMetaObject* meta = source->metaObject();
QMetaProperty readStateProp = meta->property(meta->indexOfProperty(readStateProperty));
+ SANITY_CHECK_PROPERTY_EXISTS(readStateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(readStateProp.hasNotifySignal());
@@ -428,6 +447,8 @@ void connectControlState(QButtonGroup *group, QObject *source, const char *readS
helper->updateState(readStateProp.read(source).value<ButtonGroupState>());
QMetaProperty writeProp = meta->property(meta->indexOfProperty(writeProperty));
+ SANITY_CHECK_PROPERTY_EXISTS_AND_WRITABLE(writeProp);
+
if (writeProp.isWritable()) {
QObject::connect(helper, &ConnectButtonGroupHelper::idClicked,
source, [writeProp, source] (int value) { writeProp.write(source, value); });
@@ -438,7 +459,7 @@ void connectControlState(QAbstractButton *button, QObject *source, const char *r
{
const QMetaObject* meta = source->metaObject();
QMetaProperty readStateProp = meta->property(meta->indexOfProperty(readStatePropertyName));
- Q_ASSERT(readStateProp.isValid());
+ SANITY_CHECK_PROPERTY_EXISTS(readStateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(readStateProp.hasNotifySignal());
@@ -458,7 +479,7 @@ void connectControlState(QAbstractButton *button, QObject *source, const char *r
helper->updateState(readStateProp.read(source).value<CheckBoxState>());
QMetaProperty writeProp = meta->property(meta->indexOfProperty(writePropertyName));
- Q_ASSERT(writeProp.isValid());
+ SANITY_CHECK_PROPERTY_EXISTS_AND_WRITABLE(writeProp);
if (writeProp.isWritable()) {
button->connect(button, &QAbstractButton::toggled,
source, [writeProp, source] (bool value) { writeProp.write(source, value); });
@@ -470,6 +491,7 @@ void connectControlState(QComboBox *button, QObject *source, const char *readSta
{
const QMetaObject* meta = source->metaObject();
QMetaProperty readStateProp = meta->property(meta->indexOfProperty(readStatePropertyName));
+ SANITY_CHECK_PROPERTY_EXISTS(readStateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(readStateProp.hasNotifySignal());
@@ -489,6 +511,7 @@ void connectControlState(QComboBox *button, QObject *source, const char *readSta
helper->updateState(readStateProp.read(source).value<ComboBoxState>());
QMetaProperty writeProp = meta->property(meta->indexOfProperty(writePropertyName));
+ SANITY_CHECK_PROPERTY_EXISTS_AND_WRITABLE(writeProp);
if (writeProp.isWritable()) {
QObject::connect(button, qOverload<int>(&QComboBox::currentIndexChanged),
source, [writeProp, source] (int value) { writeProp.write(source, value); });
@@ -499,6 +522,7 @@ void connectControl(QComboBox *button, QObject *source, const char *property)
{
const QMetaObject* meta = source->metaObject();
QMetaProperty stateProp = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(stateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(stateProp.hasNotifySignal());
@@ -554,6 +578,7 @@ void connectControl(KisSpacingSelectionWidget *widget, QObject *source, const ch
{
const QMetaObject* meta = source->metaObject();
QMetaProperty stateProp = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(stateProp);
KIS_SAFE_ASSERT_RECOVER_RETURN(stateProp.hasNotifySignal());
@@ -582,6 +607,7 @@ void connectControl(KisAngleSelector *widget, QObject *source, const char *prope
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -608,6 +634,7 @@ void connectControl(QLineEdit *widget, QObject *source, const char *property)
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -634,6 +661,7 @@ void connectControl(KisFileNameRequester *widget, QObject *source, const char *p
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -661,6 +689,7 @@ void connectControl(KisColorButton *widget, QObject *source, const char *propert
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
@@ -688,6 +717,10 @@ void connectWidgetVisibleToProperty(QWidget* widget, QObject* source, const char
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
+
+ KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
+
QMetaMethod signal = prop.notifySignal();
const QMetaObject* dstMeta = widget->metaObject();
@@ -703,6 +736,10 @@ void connectWidgetEnabledToProperty(QWidget* widget, QObject* source, const char
{
const QMetaObject* meta = source->metaObject();
QMetaProperty prop = meta->property(meta->indexOfProperty(property));
+ SANITY_CHECK_PROPERTY_EXISTS(prop);
+
+ KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
+
QMetaMethod signal = prop.notifySignal();
const QMetaObject* dstMeta = widget->metaObject();