[rkward-cvs] rkward/rkward/plugin rkcomponentproperties.cpp,1.2,1.3 rkcomponentproperties.h,1.2,1.3
Thomas Friedrichsmeier
tfry at users.sourceforge.net
Mon Nov 28 22:14:56 UTC 2005
Update of /cvsroot/rkward/rkward/rkward/plugin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32341
Modified Files:
rkcomponentproperties.cpp rkcomponentproperties.h
Log Message:
More implementation of properties
Index: rkcomponentproperties.cpp
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/plugin/rkcomponentproperties.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** rkcomponentproperties.cpp 27 Nov 2005 16:09:29 -0000 1.2
--- rkcomponentproperties.cpp 28 Nov 2005 22:14:54 -0000 1.3
***************
*** 42,45 ****
--- 42,48 ----
\section RKComponentPropertiesAndComponents Interaction with RKComponents
+ \section TODO TODO
+ How should invalid values be handled? Currently we keep the bad value as the string value, but use a corrected default in
+ the specialized properties (e.g. RKComponentPropertyInt::intValue () always returns something valid). Does this really make sense?
*/
***************
*** 85,89 ****
// fetch current value
! setValue (governor->value (modifier));
}
--- 88,92 ----
// fetch current value
! governorValueChanged (governor);
}
***************
*** 148,152 ****
}
! void RKComponentPropertyBool::setValue (bool new_value) {
RK_TRACE (PLUGIN);
--- 151,155 ----
}
! void RKComponentPropertyBool::setBoolValue (bool new_value) {
RK_TRACE (PLUGIN);
***************
*** 189,191 ****
--- 192,379 ----
}
+ void RKComponentPropertyBool::governorValueChanged (RKComponentPropertyBase *property) {
+ RK_TRACE (PLUGIN);
+
+ if (governor_modifier.isEmpty ()) {
+ if (property->type () == PropertyBool) {
+ internalSetValue (static_cast<RKComponentPropertyBool *>(property)->boolValue ());
+ } else if (property->type () == PropertyInt) {
+ internalSetValue (static_cast<RKComponentPropertyInt *>(property)->intValue () != 0);
+ } else {
+ internalSetValue (property->value (QString::null));
+ }
+ } else {
+ internalSetValue (property->value (governor_modifier));
+ }
+ emit (valueChanged (this));
+ }
+
+
+ ///////////////////////////////////////////// Int //////////////////////////////////////////
+ RKComponentPropertyInt::RKComponentPropertyInt (QObject *parent, bool required, int default_value) : RKComponentPropertyBase (parent, required) {
+ RK_TRACE (PLUGIN);
+
+ validator = new QIntValidator (this); // accepts all ints initially
+ RKComponentPropertyInt::default_value = default_value;
+ internalSetValue (default_value);
+ }
+
+ RKComponentPropertyInt::~RKComponentPropertyInt () {
+ RK_TRACE (PLUGIN);
+ }
+
+ bool RKComponentPropertyInt::setIntValue (int new_value) {
+ RK_TRACE (PLUGIN);
+
+ internalSetValue (new_value);
+ emit (valueChanged (this));
+ return (isValid ());
+ }
+
+ bool RKComponentPropertyInt::setValue (const QString &string) {
+ RK_TRACE (PLUGIN);
+
+ internalSetValue (string);
+ emit (valueChanged (this));
+ return (isValid ());
+ }
+
+ void RKComponentPropertyInt::setMin (int lower) {
+ RK_TRACE (PLUGIN);
+
+ validator->setBottom (lower);
+ if (default_value < lower) {
+ RK_DO (qDebug ("default value in integer property is lower than lower boundary"), PLUGIN, DL_WARNING);
+ default_value = lower;
+ }
+ if (current_value < lower) {
+ setIntValue (lower);
+ }
+ }
+
+ void RKComponentPropertyInt::setMax (int upper) {
+ RK_TRACE (PLUGIN);
+
+ validator->setTop (upper);
+ if (default_value > upper) {
+ RK_DO (qDebug ("default value in integer property is larger than upper boundary"), PLUGIN, DL_WARNING);
+ default_value = upper;
+ }
+ if (current_value > upper) {
+ setIntValue (upper);
+ }
+ }
+
+ int RKComponentPropertyInt::minValue () {
+ RK_TRACE (PLUGIN);
+ RK_ASSERT (validator);
+
+ return (validator->bottom ());
+ }
+
+ int RKComponentPropertyInt::maxValue () {
+ RK_TRACE (PLUGIN);
+ RK_ASSERT (validator);
+
+ return (validator->top ());
+ }
+
+ int RKComponentPropertyInt::intValue () {
+ RK_TRACE (PLUGIN);
+
+ return current_value;
+ }
+
+ QString RKComponentPropertyInt::value (const QString &modifier) {
+ RK_TRACE (PLUGIN);
+
+ if (!modifier.isEmpty ()) {
+ warnModifierNotRecognized (modifier);
+ return QString::null;
+ }
+ return _value;
+ }
+
+ bool RKComponentPropertyInt::isStringValid (const QString &string) {
+ RK_TRACE (PLUGIN);
+
+ int dummy = 0;
+ QString string_copy = string;
+ return (validator->validate (string_copy, dummy) == QValidator::Acceptable);
+ }
+
+ void RKComponentPropertyInt::connectToGovernor (RKComponentPropertyBase *governor, const QString &modifier, bool reconcile_requirements) {
+ RK_TRACE (PLUGIN);
+
+ RK_ASSERT (governor);
+ connect (governor, SIGNAL (valueChanged (RKComponentPropertyBase *)), this, SLOT (governorValueChanged (RKComponentPropertyBase *)));
+ governor_modifier = modifier;
+
+ // reconcile requirements if applicable
+ if (reconcile_requirements && governor_modifier.isEmpty ()) {
+ if (governor->type () == PropertyInt) {
+ RKComponentPropertyInt *igov = static_cast<RKComponentPropertyInt *> (governor); // convenience pointer
+ if (validator->bottom () > igov->minValue ()) {
+ igov->setMin (validator->bottom ());
+ }
+ if (validator->top () < igov->maxValue ()) {
+ igov->setMax (validator->top ());
+ }
+ } else if (governor->type () == PropertyDouble) {
+ RKComponentPropertyDouble *dgov = static_cast<RKComponentPropertyDouble *> (governor); // convenience pointer
+ if (validator->bottom () > (int) dgov->minValue ()) {
+ dgov->setMin (validator->bottom ());
+ }
+ if (validator->top () < (int) dgov->maxValue ()) {
+ dgov->setMax (validator->top ());
+ }
+ }
+ }
+
+ // fetch current value
+ governorValueChanged (governor);
+ }
+
+ void RKComponentPropertyInt::governorValueChanged (RKComponentPropertyBase *property) {
+ RK_TRACE (PLUGIN);
+
+ if (governor_modifier.isEmpty ()) {
+ if (property->type () == PropertyInt) {
+ internalSetValue (static_cast<RKComponentPropertyInt *>(property)->intValue ());
+ } else if (property->type () == PropertyDouble) {
+ internalSetValue ((int) (static_cast<RKComponentPropertyDouble *>(property)->doubleValue ()));
+ } else {
+ internalSetValue (property->value (QString::null));
+ }
+ } else {
+ internalSetValue (property->value (governor_modifier));
+ }
+ emit (valueChanged (this));
+ }
+
+ QIntValidator *RKComponentPropertyInt::getValidator () {
+ RK_TRACE (PLUGIN);
+ RK_ASSERT (validator);
+ return validator;
+ }
+
+ void RKComponentPropertyInt::internalSetValue (int new_value) {
+ current_value = new_value;
+ _value = QString::number (current_value);
+ is_valid = ((new_value >= validator->bottom ()) && (new_value <= validator->top ()));
+ if (!is_valid) current_value = default_value;
+ }
+
+ void RKComponentPropertyInt::internalSetValue (QString new_value) {
+ current_value = new_value.toInt (&is_valid);
+ if (!is_valid) {
+ _value = new_value;
+ current_value = default_value;
+ return;
+ }
+ internalSetValue (current_value); // will check range and prettify _value
+ }
+
+ ///////////////////////////////////////////// Double //////////////////////////////////////////
+
#include "rkcomponentproperties.moc"
Index: rkcomponentproperties.h
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/plugin/rkcomponentproperties.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** rkcomponentproperties.h 27 Nov 2005 16:09:29 -0000 1.2
--- rkcomponentproperties.h 28 Nov 2005 22:14:54 -0000 1.3
***************
*** 85,96 ****
class RKComponentPropertyBool : public RKComponentPropertyBase {
public:
! /** param value_true string value if true/on
! param value_false string value if false/off
! param default value to use, if invalid string value was set */
RKComponentPropertyBool (QObject *parent, bool required, const QString &value_true, const QString &value_false, bool default_state);
/** destructor */
~RKComponentPropertyBool ();
/** sets the bool value. Also takes care of notifying dependent components */
! void setValue (bool new_value);
/** current value as bool */
bool boolValue ();
--- 85,96 ----
class RKComponentPropertyBool : public RKComponentPropertyBase {
public:
! /** @param value_true string value if true/on
! @param value_false string value if false/off
! @param default_state value to use, if invalid string value was set */
RKComponentPropertyBool (QObject *parent, bool required, const QString &value_true, const QString &value_false, bool default_state);
/** destructor */
~RKComponentPropertyBool ();
/** sets the bool value. Also takes care of notifying dependent components */
! void setBoolValue (bool new_value);
/** current value as bool */
bool boolValue ();
***************
*** 101,104 ****
--- 101,108 ----
/** reimplemented from RKComponentPropertyBase to test whether conversion to bool value is possible according to current settings */
bool isStringValid (const QString &string);
+ /** reimplemented from RKComponentPropertyBase to use special handling for bool and int properties (bools are copied directly, int handling: 0->false else true) */
+ void governorValueChanged (RKComponentPropertyBase *property);
+ /** RTTI */
+ int type () { return PropertyBool; };
private:
/** helper function. Sets the value without emitting change signal */
***************
*** 113,119 ****
///////////////////////////////////////////////// Int ////////////////////////////////////////////////////////
! class RKComponentPropertyInt; // min, max
///////////////////////////////////////////////// Double ////////////////////////////////////////////////////////
! class RKComponentPropertyDouble; // min, max
///////////////////////////////////////////////// RObject ////////////////////////////////////////////////////////
--- 117,212 ----
///////////////////////////////////////////////// Int ////////////////////////////////////////////////////////
! #include <qvalidator.h>
!
! class RKComponentPropertyInt : public RKComponentPropertyBase {
! public:
! /** constructor
! @param default_value value to use, if invalid string value was set */
! RKComponentPropertyInt (QObject *parent, bool required, int default_value);
! /** destructor */
! ~RKComponentPropertyInt ();
! /** sets the int value. Also takes care of notifying dependent components */
! bool setIntValue (int new_value);
! /** set lower boundary. Default parameter will effectively remove the boundary. You should call this *before* connecting to any other properties, so limits can be reconciled */
! void setMin (int lower=INT_MIN);
! /** set upper boundary. Default parameter will effectively remove the boundary. You should call this *before* connecting to any other properties, so limits can be reconciled */
! void setMax (int upper=INT_MAX);
! /** return current min value */
! int minValue ();
! /** return current max value */
! int maxValue ();
! /** current value as int */
! int intValue ();
! /** reimplemented from RKComponentPropertyBase. Return current value as a string. In the future, modifier might be used for format. */
! QString value (const QString &modifier=QString::null);
! /** reimplemented from RKComponentPropertyBase to convert to int value according to current settings */
! bool setValue (const QString &string);
! /** reimplemented from RKComponentPropertyBase to test whether conversion to int value is possible according to current settings (is a number, and within limits min and max) */
! bool isStringValid (const QString &string);
! /** reimplemented from RKComponentPropertyBase to actually reconcile requirements with other numeric slots */
! void connectToGovernor (RKComponentPropertyBase *governor, const QString &modifier=QString::null, bool reconcile_requirements=true);
! /** reimplemented from RKComponentPropertyBase to use special handling for int and double properties (ints are copied directly, doubles are rounded) */
! void governorValueChanged (RKComponentPropertyBase *property);
! /** RTTI */
! int type () { return PropertyInt; };
! /** returns a validator for use in lineedits or similar widgets. */
! QIntValidator *getValidator ();
! private:
! /** helper function. Sets the value without emitting change signal */
! void internalSetValue (int new_value);
! /** helper function. Sets the value without emitting change signal */
! void internalSetValue (QString new_value);
! int default_value;
! int current_value;
! /** we could do without the validator, and create the logic on our own. Using Qt's validator, however, a) saves some typing b) allows to provide a validator object in use in lineedits, etc. (see getValidator ()) */
! QIntValidator *validator;
! };
!
///////////////////////////////////////////////// Double ////////////////////////////////////////////////////////
! #include <float.h>
!
! class RKComponentPropertyDouble :public RKComponentPropertyBase {
! public:
! /** constructor
! @param default_value value to use, if invalid string value was set */
! RKComponentPropertyDouble (QObject *parent, bool required, double default_value);
! /** destructor */
! ~RKComponentPropertyDouble ();
! /** sets the int value. Also takes care of notifying dependent components */
! bool setDoubleValue (double new_value);
! /** set lower boundary. Default parameter will effectively remove the boundary. You should call this *before* connecting to any other properties, so limits can be reconciled */
! void setMin (double lower=FLT_MIN);
! /** set upper boundary. Default parameter will effectively remove the boundary. You should call this *before* connecting to any other properties, so limits can be reconciled */
! void setMax (double upper=FLT_MAX);
! /** return current min value */
! double minValue ();
! /** return current max value */
! double maxValue ();
! /** current value as double */
! double doubleValue ();
! /** reimplemented from RKComponentPropertyBase. Return current value as a string. In the future, modifier might be used for format. */
! QString value (const QString &modifier=QString::null);
! /** reimplemented from RKComponentPropertyBase to convert to int value according to current settings */
! bool setValue (const QString &string);
! /** reimplemented from RKComponentPropertyBase to test whether conversion to int value is possible according to current settings (is a number, and within limits min and max) */
! bool isStringValid (const QString &string);
! /** reimplemented from RKComponentPropertyBase to actually reconcile requirements with other numeric slots */
! void connectToGovernor (RKComponentPropertyBase *governor, const QString &modifier=QString::null, bool reconcile_requirements=true);
! /** reimplemented from RKComponentPropertyBase to use special handling for int and double properties (ints and doubles are copied directly) */
! void governorValueChanged (RKComponentPropertyBase *property);
! /** RTTI */
! int type () { return PropertyDouble; };
! /** returns a validator for use in lineedits or similar widgets. */
! QDoubleValidator *getValidator ();
! private:
! /** helper function. Sets the value without emitting change signal */
! void internalSetValue (double new_value);
! /** helper function. Sets the value without emitting change signal */
! void internalSetValue (QString new_value);
! double default_value;
! double current_value;
! /** we could do without the validator, and create the logic on our own. Using Qt's validator, however, a) saves some typing b) allows to provide a validator object in use in lineedits, etc. (see getValidator ()) */
! QDoubleValidator *validator;
! };
///////////////////////////////////////////////// RObject ////////////////////////////////////////////////////////
More information about the rkward-tracker
mailing list