[resolved] signal-slot concept of Qt => Cells

Discussion of Common Lisp
ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: signal-slot concept of Qt for Lisp classes

Post by ramarren » Wed Jan 20, 2010 10:18 am

hewih wrote:does anybody know how to apply rules in Cells on class instances? i can only find examples for class definitions.
In principle a rule should be a part of the definition of a model, and the things that change between instances should be parameters to that rule, which would be slots of the instance. It might be worth to rethink the way you are trying to model your problem domain. That said, it is certainly possible by using initargs, or, if you want to actually change the rule when the instance is live, by parametrizing on a lambda. Note that dependencies are formed dynamically, so you can call arbitrary code and still have it linked. Examples:

Code: Select all

(defmodel starship ()
  ((x :accessor x-of :initform (c-in 0))
   (y :accessor y-of :initform (c-in 0))))

(defparameter *starship* (make-instance 'starship))

(defmodel sidekick-parametrized ()
  ((mothership :accessor mothership-of :initarg :mothership)
   (x-offset :accessor x-offset-of :initform (c-in 0) :initarg :x-offset)
   (y-offset :accessor y-offset-of :initform (c-in 0) :initarg :y-offset)
   (x :reader x-of :initform (c? (+ (x-offset-of self) (x-of (mothership-of self)))))
   (y :reader y-of :initform (c? (+ (y-offset-of self) (y-of (mothership-of self)))))))

(defparameter *sidekick-param* (make-instance 'sidekick-parametrized :mothership (c-in *starship*) :x-offset (c-in 10)))

(defmodel sidekick-instance-rules ()
  ((mothership :accessor mothership-of :initarg :mothership)
   (x :reader x-of :initarg :x)
   (y :reader y-of :initarg :y)))

(defparameter *sidekick-rules*
  (make-instance 'sidekick-instance-rules
                 :mothership *starship*
                 :x (c? (+ (x-of (mothership-of self))
                           10))
                 :y (c? (y-of (mothership-of self)))))

(defmodel sidekick-lambda-rules ()
  ((mothership :accessor mothership-of :initarg :mothership)
   (x-rule :accessor x-rule-of :initarg :x-rule)
   (y-rule :accessor y-rule-of :initarg :y-rule)
   (x :reader x-of :initform (c? (funcall (x-rule-of self) (mothership-of self))))
   (y :reader y-of :initform (c? (funcall (y-rule-of self) (mothership-of self))))))

(defparameter *sidekick-lambda-rules*
  (make-instance 'sidekick-lambda-rules
                 :mothership *starship*
                 :x-rule (c-in (lambda (mothership)
                                 (+ (x-of mothership) 10)))
                 :y-rule (c-in (lambda (mothership)
                                 (y-of mothership)))))

Code: Select all

CL-USER> *starship*
#<STARSHIP {B0D5621}>
CL-USER> (x-of *starship*)
0
CL-USER> (y-of *starship*)
0
CL-USER> (x-of *sidekick-param*)
10
CL-USER> (y-of *sidekick-param*)
0
CL-USER> (x-of *sidekick-rules*)
10
CL-USER> (y-of *sidekick-rules*)
0
CL-USER> (x-of *sidekick-lambda-rules*)
10
CL-USER> (y-of *sidekick-lambda-rules*)
0
CL-USER> (setf (x-of *starship*) 357)
357
CL-USER> (x-of *starship*)
357
CL-USER> (x-of *sidekick-param*)
367
CL-USER> (x-of *sidekick-rules*)
367
CL-USER> (x-of *sidekick-lambda-rules*)
367

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: signal-slot concept of Qt for Lisp classes

Post by gugamilare » Wed Jan 20, 2010 10:23 am

hewih wrote:does anybody know how to apply rules in Cells on class instances? i can only find examples for class definitions.
You can create a slot that holds a function or a list of functions that will be called when some other slot is changed.

hewih
Posts: 30
Joined: Tue Jan 19, 2010 9:36 am

Re: signal-slot concept of Qt for Lisp classes

Post by hewih » Sat Jan 23, 2010 6:24 am

Ramarren, thanks for the exhaustive examples! :)
it still took me a while to get it and i think i'll stick with the parametrized approach.

Post Reply