initial import --> to head
src/mandelbrot/mandelbrotwidget.lisp
Sat Apr 3 19:19:09 CEST 2010 Tobias Rautenkranz <tobias@rautenkranz.ch>
* Replace mudballs with ASDF and support the modular Smoke.
Sun May 24 23:29:56 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* Work around null pointer for references bug
Fri Apr 17 17:30:05 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* Use C++ style overload resolution.
Tue Apr 14 16:32:06 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* License
Sun Apr 12 15:34:59 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* Use overload by argument count for :cxx generic functions
Wed Apr 8 22:56:48 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* make kmandelbrotui.rc validate with kpartgui.dtd
Fri Apr 3 00:16:06 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* initial import
--- old-kde.examples/src/mandelbrot/mandelbrotwidget.lisp 1970-01-01 01:00:00.000000000 +0100
+++ new-kde.examples/src/mandelbrot/mandelbrotwidget.lisp 2014-10-30 08:41:55.000000000 +0100
@@ -0,0 +1,171 @@
+;;; Copyright (C) 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
+;;;
+;;; This program is free software: you can redistribute it and/or modify
+;;; it under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation, either version 3 of the License, or
+;;; (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+(in-package :cl-smoke.kde.examples)
+
+(defun zoom-in-factor ()
+ 0.8)
+
+(defun zoom-out-factor ()
+ (/ (zoom-in-factor)))
+
+(defclass mandelbrotwidget (qt:widget)
+ ((pixmap :accessor pixmap :initform nil)
+ (pixmap-scale :accessor pixmap-scale)
+ (pixmap-offset :accessor pixmap-offset)
+ (center :accessor center :initform #C(-0.5d0 0d0))
+ (scale :accessor scale :initform 0.007d0)
+ (update-pixmap-slot :accessor update-pixmap-slot)
+ (render :accessor render-thread)
+ (last-drag-pos :accessor last-drag-pos :initform nil))
+ (:metaclass cxx:class))
+
+(defun update-pixmap (mandelbrotwidget image scale)
+ (when (null (last-drag-pos mandelbrotwidget))
+ (setf (pixmap mandelbrotwidget)
+ (qt:pixmap.from-image image))
+ (setf (pixmap-offset mandelbrotwidget) #c(0 0))
+ (setf (pixmap-scale mandelbrotwidget) scale)
+ (cxx:update mandelbrotwidget)))
+
+(defparameter *render-control* (make-instance 'render-control))
+(defun setup-slot (mandelbrotwidget)
+ (setf (update-pixmap-slot mandelbrotwidget)
+ (qt:make-slot #'(lambda (image scale)
+ (update-pixmap mandelbrotwidget image scale))
+ (mapcar #'find-class
+ '(qt:image double-float))))
+ (qt:connect (done-signal *render-control*)
+ (update-pixmap-slot mandelbrotwidget)
+ qt:+queued-connection+))
+
+(defun draw-rendering (painter widget)
+ (cxx:set-pen painter #xFFFFFF) ;; FIXME make qt:+white+ work
+ (cxx:draw-text painter (cxx:rect widget)
+ (value qt:+align-center+)
+ "Rendering initial image, please wait..."))
+
+(defun complex-floor (complex)
+ "Applys FLOOR to the real and imaginary part of COMPLEX."
+ (complex (floor (realpart complex)
+ (imagpart complex))))
+
+(defun draw-scaled-pixmap (painter widget)
+ (cxx:save painter) ;; FIXME make with- macro
+ (let* ((scale-factor (/ (pixmap-scale widget)
+ (scale widget)))
+ (pixmap-size (complex (cxx:width (pixmap widget))
+ (cxx:height (pixmap widget))))
+ (new-size (* scale-factor pixmap-size))
+ (new-center (+ (pixmap-offset widget) (/ (- pixmap-size new-size) 2))))
+ (cxx:translate painter (realpart new-center) (imagpart new-center))
+ (cxx:scale painter scale-factor scale-factor)
+ (let ((exposed (cxx:adjusted (cxx:map-rect (cxx:inverted (cxx:matrix painter))
+ (cxx:rect widget))
+ -1 -1 1 1)))
+ (cxx:draw-pixmap painter exposed (pixmap widget) exposed)))
+ (cxx:restore painter))
+
+
+(defun draw-pixmap (painter widget)
+ (if (= (scale widget) (pixmap-scale widget))
+ (cxx:draw-pixmap painter
+ (floor (realpart (pixmap-offset widget)))
+ (floor (imagpart (pixmap-offset widget)))
+ (pixmap widget))
+ (draw-scaled-pixmap painter widget)))
+
+(defmethod cxx:paint-event ((widget mandelbrotwidget) event)
+ (declare (ignore event))
+ (qt:with-painter (painter widget)
+ (cxx:fill-rect painter (cxx:rect widget)
+ #x000) ;; FIXME qt:+black+
+ (if (null (pixmap widget))
+ (draw-rendering painter widget)
+ (draw-pixmap painter widget))))
+
+(defmethod cxx:resize-event ((widget mandelbrotwidget) event)
+ (declare (ignore event))
+ (when (slot-boundp widget 'render)
+ (setf (stop-p *render-control*) t)
+ (join-thread (slot-value widget 'render)))
+ (queue-image widget))
+
+(defmethod cxx:key-press-event ((widget mandelbrotwidget) event)
+ (enum-cases (cxx:key event)
+ (qt:+key-up+
+ (scroll widget #C(0d0 -5d0)))
+ (qt:+key-down+
+ (scroll widget #C(0d0 5d0)))
+ (qt:+key-left+
+ (scroll widget #C(-5d0 0d0)))
+ (qt:+key-right+
+ (scroll widget #C(5d0 0d0)))
+ (qt:+key-plus+
+ (zoom widget (zoom-in-factor)))
+ (qt:+key-minus+
+ (zoom widget (zoom-out-factor)))))
+
+(defmethod cxx:mouse-press-event ((widget mandelbrotwidget) event)
+ (when (enum= (cxx:button event) qt:+left-button+)
+ (setf (last-drag-pos widget) (complex (cxx:x event)
+ (cxx:y event)))))
+
+(defmethod cxx:wheel-event ((widget mandelbrotwidget) event)
+ (zoom widget (expt (zoom-in-factor) (/ (cxx:delta event) (* 8 15.0)))))
+
+(defmethod cxx:mouse-move-event ((widget mandelbrotwidget) event)
+ (when (and (logand (cxx:buttons event) (value qt:+left-button+))
+ (last-drag-pos widget))
+ (incf (pixmap-offset widget)
+ (- (complex (cxx:x event)
+ (cxx:y event))
+ (last-drag-pos widget)))
+ (setf (last-drag-pos widget) (complex (cxx:x event)
+ (cxx:y event)))
+ (cxx:update widget)))
+
+(defmethod cxx:mouse-release-event ((widget mandelbrotwidget) event)
+ (when (enum= (cxx:button event) qt:+left-button+)
+ (incf (pixmap-offset widget)
+ (- (complex (cxx:x event)
+ (cxx:y event))
+ (last-drag-pos widget)))
+ (setf (last-drag-pos widget) nil)
+ (let ((pixmap-size (complex (cxx:width (pixmap widget))
+ (cxx:height (pixmap widget))))
+ (size (complex (cxx:width widget)
+ (cxx:height widget))))
+ (scroll widget (- (/ (- size pixmap-size) 2) (pixmap-offset widget))))))
+
+(defun queue-image (widget)
+ (setf (stop-p *render-control*) nil)
+ ;; FIXME do not creating a new thread on every invokation
+ (setf (render-thread widget)
+ (make-thread (make-render-function (center widget)
+ (scale widget)
+ (cxx:width widget)
+ (cxx:height widget)
+ *render-control*))))
+
+(defun zoom (widget factor)
+ (setf (scale widget) (* (scale widget) factor))
+ (cxx:update widget)
+ (queue-image widget))
+
+(defun scroll (widget delta)
+ (incf (center widget) (* (scale widget) delta))
+ (cxx:update widget)
+ (queue-image widget))