[rkward-cvs] SF.net SVN: rkward-code:[4738] trunk/rkward/rkward/plugin

tfry at users.sf.net tfry at users.sf.net
Wed May 1 17:56:56 UTC 2013


Revision: 4738
          http://sourceforge.net/p/rkward/code/4738
Author:   tfry
Date:     2013-05-01 17:56:55 +0000 (Wed, 01 May 2013)
Log Message:
-----------
Common minimal base class for RObjects and StringList properties. In preparation for <valueslot>.

Modified Paths:
--------------
    trunk/rkward/rkward/plugin/rkcomponentproperties.cpp
    trunk/rkward/rkward/plugin/rkcomponentproperties.h
    trunk/rkward/rkward/plugin/rkvarslot.cpp

Modified: trunk/rkward/rkward/plugin/rkcomponentproperties.cpp
===================================================================
--- trunk/rkward/rkward/plugin/rkcomponentproperties.cpp	2013-05-01 17:55:53 UTC (rev 4737)
+++ trunk/rkward/rkward/plugin/rkcomponentproperties.cpp	2013-05-01 17:56:55 UTC (rev 4738)
@@ -152,11 +152,65 @@
 }
 
 
+///////////////////////////////////////// AbstractList /////////////////////////////////////
+
+QString RKComponentPropertyAbstractList::sep = "\n";
+RKComponentPropertyAbstractList::RKComponentPropertyAbstractList (QObject* parent, bool required) : RKComponentPropertyBase (parent, required) {
+	RK_TRACE (PLUGIN);
+	setAllowedLength ();
+}
+
+RKComponentPropertyAbstractList::~RKComponentPropertyAbstractList () {
+	RK_TRACE (PLUGIN);
+}
+
+void RKComponentPropertyAbstractList::setAllowedLength ( int min_num_items, int min_num_items_if_any, int max_num_items ) {
+	RK_TRACE (PLUGIN);
+
+	RKComponentPropertyAbstractList::min_num_items = min_num_items;
+	RKComponentPropertyAbstractList::min_num_items_if_any = min_num_items_if_any;
+	RKComponentPropertyAbstractList::max_num_items = max_num_items;
+
+	// TODO: re-validize?
+}
+
+bool RKComponentPropertyAbstractList::checkListLength () {
+	RK_TRACE (PLUGIN);
+
+	if ((min_num_items > 0) || (max_num_items > 0) || (min_num_items_if_any > 0)) {
+		int len = listLength ();
+		if (len < min_num_items) return false;
+		if (len && (len < min_num_items_if_any)) return false;
+		if ((max_num_items > 0) && (len > max_num_items)) return false;
+	}
+	return true;
+}
+
+void RKComponentPropertyAbstractList::reconcileLengthRequirements (RKComponentPropertyAbstractList* governor) {
+	RK_TRACE (PLUGIN);
+
+	if (governor->min_num_items < min_num_items) governor->min_num_items = min_num_items;
+	if (governor->min_num_items_if_any < min_num_items_if_any) governor->min_num_items_if_any = min_num_items_if_any;
+	if (max_num_items && (governor->max_num_items > max_num_items)) governor->max_num_items = max_num_items;
+}
+
+void RKComponentPropertyAbstractList::connectToGovernor (RKComponentPropertyBase* governor, const QString& modifier, bool reconcile_requirements) {
+	RK_TRACE (PLUGIN);
+
+	if (reconcile_requirements && modifier.isEmpty ()) {
+		if ((governor->type () == PropertyStringList) || (governor->type () == PropertyRObjects)) {
+			reconcileLengthRequirements (static_cast<RKComponentPropertyAbstractList*> (governor));
+		}
+	}
+
+	RKComponentPropertyBase::connectToGovernor (governor, modifier, reconcile_requirements);
+}
+
+
 ///////////////////////////////////////// StringList ///////////////////////////////////////
 
-RKComponentPropertyStringList::RKComponentPropertyStringList (QObject *parent, bool required) : RKComponentPropertyBase (parent, required) {
+RKComponentPropertyStringList::RKComponentPropertyStringList (QObject *parent, bool required) : RKComponentPropertyAbstractList (parent, required) {
 	RK_TRACE (PLUGIN);
-	sep = "\n";
 }
 
 RKComponentPropertyStringList::~RKComponentPropertyStringList () {
@@ -457,13 +511,10 @@
 
 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 (reconcile_requirements && modifier.isEmpty ()) {
 		if (governor->type () == PropertyInt) {
 			RKComponentPropertyInt *igov = static_cast<RKComponentPropertyInt *> (governor); 	// convenience pointer
 			if (validator->bottom () > igov->minValue ()) {
@@ -483,8 +534,7 @@
 		}
 	}
 
-	// fetch current value
-	governorValueChanged (governor);
+	RKComponentPropertyBase::connectToGovernor (governor, modifier, reconcile_requirements);
 }
 
 void RKComponentPropertyInt::governorValueChanged (RKComponentPropertyBase *property) {
@@ -616,13 +666,10 @@
 
 void RKComponentPropertyDouble::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 (reconcile_requirements && modifier.isEmpty ()) {
 		if (governor->type () == PropertyInt) {
 			RKComponentPropertyInt *igov = static_cast<RKComponentPropertyInt *> (governor); 	// convenience pointer
 			if (validator->bottom () > igov->minValue ()) {
@@ -642,8 +689,7 @@
 		}
 	}
 
-	// fetch current value
-	governorValueChanged (governor);
+	RKComponentPropertyBase::connectToGovernor (governor, modifier, reconcile_requirements);
 }
 
 void RKComponentPropertyDouble::governorValueChanged (RKComponentPropertyBase *property) {
@@ -702,12 +748,11 @@
 #include "../core/rkmodificationtracker.h"
 #include "../misc/rkobjectlistview.h"
 
-RKComponentPropertyRObjects::RKComponentPropertyRObjects (QObject *parent, bool required) : RKComponentPropertyBase (parent, required), RObjectListener (RObjectListener::Other) {
+RKComponentPropertyRObjects::RKComponentPropertyRObjects (QObject *parent, bool required) : RKComponentPropertyAbstractList (parent, required), RObjectListener (RObjectListener::Other) {
 	RK_TRACE (PLUGIN);
 
 // no initial requirements
-	dims = min_length = max_length = min_num_objects = min_num_objects_if_any = max_num_objects = -1;
-	separator = "\n";
+	dims = min_length = max_length;
 
 	addNotificationType (RObjectListener::ObjectRemoved);
 	addNotificationType (RObjectListener::MetaChanged);
@@ -719,16 +764,6 @@
 	setObjectValue (0);
 }
 
-void RKComponentPropertyRObjects::setListLength (int min_num_objects, int min_num_objects_if_any, int max_num_objects) {
-	RK_TRACE (PLUGIN);
-
-	RKComponentPropertyRObjects::min_num_objects = min_num_objects;
-	RKComponentPropertyRObjects::min_num_objects_if_any = min_num_objects_if_any;
-	RKComponentPropertyRObjects::max_num_objects = max_num_objects;
-
-	validizeAll ();
-}
-
 bool RKComponentPropertyRObjects::addObjectValue (RObject *object) {
 	RK_TRACE (PLUGIN);
 
@@ -921,13 +956,13 @@
 bool RKComponentPropertyRObjects::setValue (const QString &value) {
 	RK_TRACE (PLUGIN);
 
-	return setValue (value.split (separator, QString::SkipEmptyParts));
+	return setValue (value.split (sep, QString::SkipEmptyParts));
 }
 
 bool RKComponentPropertyRObjects::isStringValid (const QString &value) {
 	RK_TRACE (PLUGIN);
 
-	QStringList slist = value.split (separator, QString::SkipEmptyParts);
+	QStringList slist = value.split (sep, QString::SkipEmptyParts);
 
 	for (QStringList::const_iterator it = slist.begin (); it != slist.end (); ++it) {
 		RObject *obj = RObjectList::getObjectList ()->findObject (*it);
@@ -941,13 +976,10 @@
 
 void RKComponentPropertyRObjects::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 (reconcile_requirements && modifier.isEmpty ()) {
 		if (governor->type () == PropertyRObjects) {
 			RKComponentPropertyRObjects *ogov = static_cast<RKComponentPropertyRObjects *> (governor); 	// convenience pointer
 
@@ -968,17 +1000,6 @@
 				}
 			}
 
-			// reconcile number of objects filter
-			if (ogov->min_num_objects < min_num_objects) {
-				ogov->min_num_objects = min_num_objects;
-			}
-			if (ogov->min_num_objects_if_any < min_num_objects_if_any) {
-				ogov->min_num_objects_if_any = min_num_objects_if_any;
-			}
-			if (max_num_objects && (ogov->max_num_objects > max_num_objects)) {
-				ogov->max_num_objects = max_num_objects;
-			}
-
 			// reconcile class filter
 			if (!classes.isEmpty ()) {
 				if (ogov->classes.isEmpty ()) {
@@ -1024,8 +1045,7 @@
 		}
 	}
 
-	// fetch current value
-	governorValueChanged (governor);
+	RKComponentPropertyAbstractList::connectToGovernor (governor, modifier, reconcile_requirements);
 }
 
 void RKComponentPropertyRObjects::governorValueChanged (RKComponentPropertyBase *property) {
@@ -1086,24 +1106,9 @@
 	is_valid = true;	// innocent until proven guilty
 
 	if (!problems.isEmpty ()) is_valid = false;
-	else if ((min_num_objects > 0) || (max_num_objects > 0) || (min_num_objects_if_any > 0)) {
-		int len = object_list.count ();
-		if (len < min_num_objects) is_valid = false;
-		if (len && (len < min_num_objects_if_any)) is_valid = false;
-		if ((max_num_objects > 0) && (len > max_num_objects)) is_valid = false;
-	}
+	else is_valid = checkListLength ();
 }
 
-bool RKComponentPropertyRObjects::atMaxLength () {
-	RK_TRACE (PLUGIN);
-
-	int len = object_list.count ();
-	if (max_num_objects && (len >= max_num_objects)) return true;
-	return false;
-}
-
-
-
 /////////////////////////////////////////// Code ////////////////////////////////////////////////
 
 RKComponentPropertyCode::RKComponentPropertyCode (QObject *parent, bool required) : RKComponentPropertyBase (parent, required) {

Modified: trunk/rkward/rkward/plugin/rkcomponentproperties.h
===================================================================
--- trunk/rkward/rkward/plugin/rkcomponentproperties.h	2013-05-01 17:55:53 UTC (rev 4737)
+++ trunk/rkward/rkward/plugin/rkcomponentproperties.h	2013-05-01 17:56:55 UTC (rev 4738)
@@ -70,10 +70,35 @@
 	QString governor_modifier;
 };
 
+//////////////////////////////////////////////// AbstractList //////////////////////////////////////////////////
+/** Base class for list properties (RKComponentPropertyStringList and RKComponentPropertyRObjects) */
+class RKComponentPropertyAbstractList : public RKComponentPropertyBase {
+public:
+	RKComponentPropertyAbstractList (QObject* parent, bool required);
+	virtual ~RKComponentPropertyAbstractList ();
+/** how many items can this property hold? Use default values (0) to remove constraints
+ at param min_num_items Minimum number of items for this property to be valid
+ at param min_num_items_if_any Some properties may be valid, if they hold either no items at all, or at least a certain number of items
+ at param max_num_items Maximum number of items for this property to be valid */
+	void setAllowedLength (int min_num_items=0, int min_num_items_if_any=0, int max_num_items=0);
+/** @returns true, if the property holds the maximum number of items (or more) */
+	bool atMaxLength () const { return (max_num_items && (max_num_items <= listLength ())); };
+/** reimplemented from RKComponentPropertyBase to actually reconcile requirements with other list properties */
+	void connectToGovernor (RKComponentPropertyBase *governor, const QString &modifier=QString::null, bool reconcile_requirements=true);
+protected:
+	bool checkListLength ();
+	virtual int listLength () const = 0;
+	void reconcileLengthRequirements (RKComponentPropertyAbstractList *governor);
+	static QString sep;
+private:
+	int min_num_items;
+	int min_num_items_if_any;
+	int max_num_items;
+};
 
 /////////////////////////////////////////////// StringList /////////////////////////////////////////////////////
 /** RKComponentProperty, which can handle lists of strings, better */
-class RKComponentPropertyStringList : public RKComponentPropertyBase {
+class RKComponentPropertyStringList : public RKComponentPropertyAbstractList {
 public:
 	RKComponentPropertyStringList (QObject *parent, bool required);
 /** destructor */
@@ -95,9 +120,9 @@
 	void setValues (const QStringList &new_values) { storage = new_values; doChange (); };
 /** reimplemented from RKComponentPropertyBase to use special handling for list properties */
 	void governorValueChanged (RKComponentPropertyBase *property);
+	int listLength () const { return (storage.size ()); };
 private:
 	void doChange () { _value.clear (); emit (valueChanged (this)); };
-	QString sep;	// always "\n". TODO: remove
 	QStringList storage;
 };
 
@@ -251,18 +276,13 @@
 #include "../core/rkmodificationtracker.h"
 
 /** special type of RKComponentProperty, that prepresents one or more RObject (s) */
-class RKComponentPropertyRObjects : public RKComponentPropertyBase, public RObjectListener {
+class RKComponentPropertyRObjects : public RKComponentPropertyAbstractList, public RObjectListener {
 	Q_OBJECT
 public:
 /** constructor */
 	RKComponentPropertyRObjects (QObject *parent, bool required);
 /** destructor */
 	~RKComponentPropertyRObjects ();
-/** how many objects can this property hold? Use default values (0) to remove constraints
- at param min_num_objects Minimum number of objects for this property to be valid
- at param min_num_objects_if_any Some properties may be valid, if they hold either no objects at all, or at least a certain number of objects
- at param max_num_objects Maximum number of objects for this property to be valid */
-	void setListLength (int min_num_objects=0, int min_num_objects_if_any=0, int max_num_objects=0);
 /** add an object value */
 	bool addObjectValue (RObject *object);
 /** Set property to only accept certain classes. If you provide an empty list, all classes will be accepted*/
@@ -303,9 +323,8 @@
 	void connectToGovernor (RKComponentPropertyBase *governor, const QString &modifier=QString::null, bool reconcile_requirements=true);
 /** reimplemented from RKComponentPropertyBase to use special handling for object properties */
 	void governorValueChanged (RKComponentPropertyBase *property);
-/** @returns true, if the property holds the maximum number of items (or more) */
-	bool atMaxLength ();
 	void removeObjectValue (RObject* object) { objectRemoved (object); };
+	int listLength () const { return (object_list.size ()); };
 protected:
 /** remove an object value. reimplemented from RObjectListener::objectRemoved (). This is so we get notified if the object currently selected is removed TODO: is this effectively a duplication of setFromList? */
 	void objectRemoved (RObject *removed);
@@ -326,13 +345,9 @@
 	int dims;
 	int min_length;
 	int max_length;
-	int min_num_objects;
-	int min_num_objects_if_any;
-	int max_num_objects;
 	QStringList classes;
 /** TODO: use a list of enums instead for internal purposes! */
 	QStringList types;
-	QString separator;	// always "\n". TODO: remove
 };
 
 ///////////////////////////////////////////////// Code ////////////////////////////////////////////////////////

Modified: trunk/rkward/rkward/plugin/rkvarslot.cpp
===================================================================
--- trunk/rkward/rkward/plugin/rkvarslot.cpp	2013-05-01 17:55:53 UTC (rev 4737)
+++ trunk/rkward/rkward/plugin/rkvarslot.cpp	2013-05-01 17:56:55 UTC (rev 4738)
@@ -67,10 +67,10 @@
 
 	// find out about options
 	if ((multi = xml->getBoolAttribute (element, "multi", false, DL_INFO))) {
-		available->setListLength (xml->getIntAttribute (element, "min_vars", 1, DL_INFO), xml->getIntAttribute (element, "min_vars_if_any", 1, DL_INFO), xml->getIntAttribute (element, "max_vars", 0, DL_INFO));
+		available->setAllowedLength (xml->getIntAttribute (element, "min_vars", 1, DL_INFO), xml->getIntAttribute (element, "min_vars_if_any", 1, DL_INFO), xml->getIntAttribute (element, "max_vars", 0, DL_INFO));
 		connect (list, SIGNAL (itemSelectionChanged ()), this, SLOT (listSelectionChanged ()));
 	} else {
-		available->setListLength (1, 1, 1);
+		available->setAllowedLength (1, 1, 1);
 
 		// make it look like a line-edit
 		list->header ()->hide ();





More information about the rkward-tracker mailing list