Browse code
Adding a sample qt tip-calc
Ed L authored on 22/10/2015 20:43:39
Showing 1 changed files
Showing 1 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,85 @@ |
1 |
+(ql:quickload :qtools) |
|
2 |
+(ql:quickload :cells) |
|
3 |
+(ql:quickload :qtcore) |
|
4 |
+(ql:quickload :qtgui) |
|
5 |
+ |
|
6 |
+(cl:defpackage :tipcalc.models |
|
7 |
+ (:use :cl :cells) |
|
8 |
+ (:export |
|
9 |
+ service-rating tip-calc tax-calc item bill meal-expense-calculator |
|
10 |
+ rating rate cost tip tax kind item subtotal subtotal total)) |
|
11 |
+ |
|
12 |
+(in-package :tipcalc.models) |
|
13 |
+ |
|
14 |
+(defmodel service-rating () |
|
15 |
+ ((rating :cell t :accessor rating :initarg :rating :initform (c-in :normal)) |
|
16 |
+ (rate :cell t :accessor rate :initform (c? (case (rating self) |
|
17 |
+ (:normal 0.18) |
|
18 |
+ (:excellent 0.25) |
|
19 |
+ (:poor 0.15)))))) |
|
20 |
+ |
|
21 |
+(defmodel tip-calc () |
|
22 |
+ ((cost :cell t :accessor cost :initarg :cost :initform (c-in 0)) |
|
23 |
+ (rate :cell t :accessor rate :initarg :rate :initform (c-in 0.18)) |
|
24 |
+ (tip :cell t :accessor tip :initform (c? (* (cost self) |
|
25 |
+ (rate self)))))) |
|
26 |
+ |
|
27 |
+(defmodel tax-calc () |
|
28 |
+ ((cost :cell t :accessor cost :initarg :cost :initform (c-in 0)) |
|
29 |
+ (rate :cell t :accessor rate :initarg :rate :initform (c-in 0.18)) |
|
30 |
+ (tax :cell t :accessor tax :initform (c? (* (cost self) |
|
31 |
+ (rate self)))))) |
|
32 |
+ |
|
33 |
+(defmodel item () |
|
34 |
+ ((kind :cell t :accessor kind :initarg :kind :initform (c-in :food)) |
|
35 |
+ (cost :cell t :accessor cost :initarg :cost :initform (c-in 0)))) |
|
36 |
+ |
|
37 |
+(defmodel bill () |
|
38 |
+ ((items :accessor items :initarg :items :initform (c-in nil)) |
|
39 |
+ (cost :accessor cost :initform (c? (apply #'+ |
|
40 |
+ (loop for item in (items self) |
|
41 |
+ collect (cost item))))))) |
|
42 |
+ |
|
43 |
+(defmodel meal-expense-calculator () |
|
44 |
+ ((subtotal :accessor subtotal :initarg :subtotal :initform (c-in 0)) |
|
45 |
+ (tax :accessor tax :initarg :tax :initform (c-in 0.08)) |
|
46 |
+ (tip :accessor tip :initarg :tip :initform (c-in 0)) |
|
47 |
+ (total :accessor total :initform (c? (+ (subtotal self) |
|
48 |
+ (* (tax-rate self) |
|
49 |
+ (subtotal self)) |
|
50 |
+ (tip self)))))) |
|
51 |
+ |
|
52 |
+(defpackage tipcalc |
|
53 |
+ (:use :cl+qt :tipcalc.models)) |
|
54 |
+(in-package :tipcalc) |
|
55 |
+(in-readtable :qtools) |
|
56 |
+ |
|
57 |
+(define-widget main-window (QWidget) ()) |
|
58 |
+ |
|
59 |
+(define-subwidget (main-window cost-widg) (q+:make-qlineedit main-window) |
|
60 |
+ (setf (q+:placeholder-text cost-widg) "Meal Cost?")) |
|
61 |
+ |
|
62 |
+(define-subwidget (main-window rate-widg) (q+:make-qlineedit main-window) |
|
63 |
+ (setf (q+:placeholder-text rate-widg) "Tip Rate?")) |
|
64 |
+ |
|
65 |
+(define-subwidget (main-window tip-widg) (q+:make-qlineedit main-window) |
|
66 |
+ (setf (q+:placeholder-text tip-widg) "Tip . . .")) |
|
67 |
+ |
|
68 |
+(define-subwidget (main-window layout) (q+:make-qhboxlayout main-window) |
|
69 |
+ (q+:add-widget layout cost-widg) |
|
70 |
+ (q+:add-widget layout rate-widg) |
|
71 |
+ (q+:add-widget layout tip-widg)) |
|
72 |
+ |
|
73 |
+(defparameter *tip-calc* (make-instance 'tip-calc)) |
|
74 |
+(cells:defobserver tip ((self tip-calc)) |
|
75 |
+ ; I want this to run: |
|
76 |
+ ; EITHER When the old value is not bound |
|
77 |
+ ; OR When the old value is not equal to the new value |
|
78 |
+ (when (or (not old-value-boundp) (/= new-value old-value)) |
|
79 |
+ (setf (q+:text) |
|
80 |
+ (format nil "~a" new-value)))) |
|
81 |
+ |
|
82 |
+ |
|
83 |
+(defun main () |
|
84 |
+ (with-main-window (window (make-instance 'main-window)))) |
|
85 |
+ |