ctkBooleanMapper.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkBooleanMapper_h
  15. #define __ctkBooleanMapper_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QVariant>
  19. // CTK includes
  20. #include "ctkCoreExport.h"
  21. class ctkBooleanMapperPrivate;
  22. //---------------------------------------------------------------------------
  23. /// \ingroup Core
  24. /// QCheckBox* checkBox = new QCheckBox;
  25. /// ctkBooleanMapper* inverter =
  26. /// new ctkBooleanMapper("checked", SIGNAL("toggled(bool)"), checkBox);
  27. /// inverter->setComplementValue(true);
  28. /// // -> checkBox->checked() == false
  29. /// inverter->setValue(false);
  30. /// // -> checkBox->checked() == false
  31. class CTK_CORE_EXPORT ctkBooleanMapper : public QObject
  32. {
  33. Q_OBJECT
  34. /// This property contains the name of the object mapped property.
  35. Q_PROPERTY(QByteArray propertyName READ propertyName)
  36. /// This property holds the mapped property.
  37. /// It is the value of the mapped object property
  38. Q_PROPERTY(bool value READ value WRITE setValue NOTIFY complementChanged STORED false);
  39. /// This property is the complement of the mapped property.
  40. /// false if \a value is true and true if \a value is false
  41. Q_PROPERTY(bool complement READ complement WRITE setComplement NOTIFY complementChanged STORED false)
  42. Q_PROPERTY(int valueAsInt READ valueAsInt WRITE setValueAsInt NOTIFY valueAsIntChanged STORED false )
  43. Q_PROPERTY(QString valueAsString READ valueAsString WRITE setValueAsString NOTIFY valueAsStringChanged STORED false )
  44. /// 1 by default
  45. Q_PROPERTY(QVariant trueValue READ trueValue WRITE setTrueValue )
  46. /// 0 by default
  47. Q_PROPERTY(QVariant falseValue READ falseValue WRITE setFalseValue )
  48. public:
  49. /// Map the property \a property of the object.
  50. /// The mapper becomes a child of \a object and will be destructed when
  51. /// \a object is destructed.
  52. /// property and object must be valid and non empty. If signal is 0,
  53. /// \a valueChanged(bool) and \a complementChanged(bool) won't be fired.
  54. ctkBooleanMapper(QObject* targetObject, const QByteArray& propertyName, const char* signal);
  55. virtual ~ctkBooleanMapper();
  56. QByteArray propertyName()const;
  57. /// The mapped object (the mapper parent)
  58. QObject* targetObject()const;
  59. bool value()const;
  60. bool complement()const;
  61. int valueAsInt()const;
  62. QString valueAsString()const;
  63. QVariant trueValue()const;
  64. QVariant falseValue()const;
  65. void setTrueValue(const QVariant& value);
  66. void setFalseValue(const QVariant& value);
  67. public Q_SLOTS:
  68. void setValue(bool value);
  69. void setComplement(bool complement);
  70. void setValueAsInt(int value);
  71. void setValueAsString(const QString& value);
  72. void toggle();
  73. Q_SIGNALS:
  74. void valueChanged(bool value);
  75. void complementChanged(bool complement);
  76. void valueAsIntChanged(int value);
  77. void valueAsStringChanged(const QString& value);
  78. protected Q_SLOTS:
  79. void emitValueChanged();
  80. void emitValueAsChanged();
  81. protected:
  82. QScopedPointer<ctkBooleanMapperPrivate> d_ptr;
  83. private:
  84. Q_DECLARE_PRIVATE(ctkBooleanMapper);
  85. Q_DISABLE_COPY(ctkBooleanMapper);
  86. };
  87. #endif