better integration with QML Repeater and 'objectName' of respective children in there

This commit is contained in:
polos 2021-05-18 20:25:25 +02:00
parent 44689ec14e
commit 8494a71fae
2 changed files with 26 additions and 7 deletions

View file

@ -122,6 +122,7 @@
(:export
#:*quick-view*
#:*root*
#:*root-item*
#:*caller*
#:children
#:file-to-url

View file

@ -10,6 +10,7 @@
(defvar *quick-view* nil)
(defvar *caller* nil)
(defvar *root* nil)
(defvar *root-item* nil) ; see note in 'find-quick-item'
(defun string-to-symbol (name)
(let ((upper (string-upcase name))
@ -76,13 +77,30 @@
(when *quick-view*
(|rootContext| *quick-view*)))
(defun find-quick-item (object-name)
"Finds the first QQuickItem matching OBJECT-NAME."
(let ((root (root-item)))
(unless (qnull root)
(if (string= (|objectName| root) object-name)
(root-item)
(qt-object-? (qfind-child root object-name))))))
(defun find-quick-item (object-name &optional (parent *root-item*))
"Finds the first QQuickItem matching OBJECT-NAME. Optionally pass a QQuickItem as parent, which defaults to *ROOT-ITEM*, which is defined but NIL, and meant to be used as special variable (in case of multiple items with the same OBJECT-NAME, e.g. in QML 'Repeater')."
;;
;; when to use *ROOT-ITEM*
;;
;; say you have a Repeater QML item with multiple instances of the same
;; QQuickItem. The children of those QQuickItems all have the same object
;; names, respectively. In order to access those child items, we need to
;; search in the specific item of the Repeater.
;;
;; So, we locally bind *ROOT-ITEM* in order to find a specific child item
;; inside the Repeater:
;;
;; (let ((qml:*root-item* (q! |itemAt| ui:*repeater* 0)))
;; ;; everything we do here will only affect children of the first item
;; ;: in ui:*repeater* (see index 0 above)
;; (q< |text| ui:*edit*)
;;
(unless parent
(setf parent (root-item)))
(unless (qnull parent)
(if (string= (|objectName| parent) object-name)
parent
(qt-object-? (qfind-child parent object-name)))))
(defun quick-item (item/name)
(cond ((stringp item/name)