"quick": update "painted-item" example; tiny revisions;

This commit is contained in:
polos 2017-02-09 12:48:32 +01:00
parent fe648ad3ca
commit ded6a7476d
8 changed files with 115 additions and 17 deletions

View file

@ -37,7 +37,9 @@ public:
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&, const QJSValue& = QJSValue(), const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&,
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
};
class PaintedItem : public QQuickPaintedItem {

View file

@ -37,7 +37,9 @@ public:
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&, const QJSValue& = QJSValue(), const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&,
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
};
class PaintedItem : public QQuickPaintedItem {

View file

@ -8,4 +8,10 @@ which means using QPainter from Lisp (instead of JS in QML).
RUN
===
Please run it from this directory.
Run "painted-item.lisp" from this directory.
NOTE
====
Use QQuickWidget here, because QQuickView would crash (e.g. on Windows).

View file

@ -0,0 +1,52 @@
(defpackage :clock
(:use :common-lisp :eql)
(:export
#:paint))
(in-package :clock)
(defun pen (width color)
(x:let-it (qnew "QPen")
(x:do-with x:it
(|setCapStyle| |Qt.RoundCap|)
(|setWidth| width)
(|setColor| color))))
(defun brush (color)
(x:let-it (qnew "QBrush")
(x:do-with x:it
(|setStyle| |Qt.SolidPattern|)
(|setColor| color))))
(defparameter *pen-clock* (pen 1 "white"))
(defparameter *brush-clock* (brush "white"))
(defparameter *pen-hour-marks* (pen 4 "blue"))
(defparameter *pen-hour* (pen 10 "red"))
(defparameter *pen-minute* (pen 8 "red"))
(defun paint (item p)
(macrolet ((with-save (() &body body)
`(progn (|save| p) ,@body (|restore| p))))
(let* ((size (nthcdr 2 (|contentsBoundingRect| item)))
(scale (/ (apply 'min size) 170)))
(|translate| p (mapcar (lambda (x) (/ x 2)) size))
(|scale| p scale scale))
(|rotate| p -90)
(|setPen| p *pen-clock*)
(|setBrush| p *brush-clock*)
(|drawEllipse| p '(-75 -75 150 150))
(|setPen| p *pen-hour-marks*)
(dotimes (n 12)
(|rotate| p 30)
(|drawLine| p '(55 0 65 0)))
(multiple-value-bind (sec min hour)
(get-decoded-time)
(|setOpacity| p 0.5)
(with-save ()
(|rotate| p (+ (* 30 hour) (/ min 2)))
(|setPen| p *pen-hour*)
(|drawLine| p '(0 0 35 0)))
(with-save ()
(|rotate| p (* 6 min))
(|setPen| p *pen-minute*)
(|drawLine| p '(0 0 64 0))))))

View file

@ -37,7 +37,9 @@ public:
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&, const QJSValue& = QJSValue(), const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&,
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
};
class PaintedItem : public QQuickPaintedItem {

View file

@ -6,22 +6,33 @@
(qrequire :quick)
(require :qml-lisp "qml-lisp")
(require :clock "clock")
(use-package :qml)
(defvar *clock* nil)
;; paint
(defun qml:paint (item painter)
"This function is called on every 'paint' request of QML type 'PaintedItem'."
(|fillRect| painter (|contentsBoundingRect| item)
(qml-get item "fillColor"))) ; QML property
(|setRenderHint| painter |QPainter.Antialiasing|)
(cond ((qeql *clock* item)
(clock:paint item painter))
(t
(|fillRect| painter (|contentsBoundingRect| item)
(qml-get item "fillColor"))))) ; QML property
(defun run ()
(setf qml:*quick-view* (qnew "QQuickWidget")) ; QQuickView would crash on Windows
(setf qml:*quick-view* (qnew "QQuickWidget")) ; QQuickView would crash on Windows
(x:do-with qml:*quick-view*
(|setSource| (|fromLocalFile.QUrl| "qml/painted-item.qml"))
(|setResizeMode| |QQuickView.SizeRootObjectToView|)
(|resize| '(350 350))
(|show|)))
(|resize| '(340 340)))
(setf *clock* (find-quick-item "clock"))
(let ((timer (qnew "QTimer")))
(qconnect timer "timeout()" (lambda () (|update| *clock*))) ; repaint
(|start| timer (* 60 1000))) ; once a minute
(|show| qml:*quick-view*))
(run)

View file

@ -1,20 +1,41 @@
import QtQuick 2.0
import QtQuick 2.2
import EQL5 1.0
Rectangle {
color: "gray"
PaintedItem {
// see QML:PAINT in Lisp
property string fillColor: "red"
x: 10; y: 10; width: 100; height: 100
property string fillColor: "red"
}
PaintedItem {
// see QML:PAINT in Lisp
property string fillColor: "blue"
id: clock
objectName: "clock"
x: 120; y: 10; width: 100; height: 100
SequentialAnimation {
running: true
loops: Animation.Infinite
ScaleAnimator {
target: clock
from: 0.95; to: 1.0
duration: 500
easing.type: Easing.InOutSine
}
ScaleAnimator {
target: clock
from: 1.0; to: 0.95
duration: 500
easing.type: Easing.InOutSine
}
}
}
PaintedItem {
x: 230; y: 10; width: 100; height: 100
property string fillColor: "blue"
}
}

View file

@ -37,7 +37,9 @@ public:
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&, const QJSValue& = QJSValue(), const QJSValue& = QJSValue());
Q_INVOKABLE QVariant apply(const QJSValue&,
const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
};
class PaintedItem : public QQuickPaintedItem {