git.fiddlerwoaroof.com
Browse code

initial

fiddlerwoaroof authored on 06/08/2014 00:04:17
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,100 @@
1
+#!/usr/bin/sbcl --script
2
+(load "/home/edwlan/.sbclrc")
3
+
4
+(ql:quickload :qt)
5
+
6
+(defpackage jsonview
7
+  (:use #:cl
8
+        #:cl-user
9
+        #:qt))
10
+
11
+(in-package jsonview)
12
+
13
+(named-readtables:in-readtable :qt)
14
+
15
+(defclass json-view-app () 
16
+  ((json-view :accessor json-view)
17
+   (json-model :accessor json-model)
18
+   (json-data :accessor json-data))
19
+  (:metaclass qt-class)
20
+  (:qt-superclass "QWidget"))
21
+
22
+(defmethod initialize-instance :after
23
+  ((instance json-view-app) &key)
24
+  (new instance))
25
+
26
+(defun prepare-row (&rest keys)
27
+  (let ((keys (loop for k in keys collect (if (null k) "NULL" k))))
28
+    (loop for k in keys collect (#_new QStandardItem (format nil "~a" k)))))
29
+
30
+(defun rows-from-alist (alist)
31
+  (loop for (key . value) in alist
32
+        collect (prepare-row (format nil "~a" key) (format nil "~a" value))))
33
+
34
+(defun mktree-helper (key value root n)
35
+ (let
36
+   ((row (if (listp key)
37
+           (prepare-row (car key) (cdr key))
38
+           (prepare-row key)))
39
+    (root (if (listp key)
40
+            (let ((nrow (prepare-row (format nil "List Item ~a" n))))
41
+              (#_appendRow root nrow)
42
+              (car nrow))
43
+            root)))
44
+   (#_appendRow root row)
45
+   (if (listp key)
46
+     (mktree root value)
47
+     (mktree (car row) value))))
48
+
49
+(defun mktree (root alist)
50
+  (let ((counter 0))
51
+    (loop
52
+      for item in alist
53
+      collect (if (listp item)
54
+                (destructuring-bind (key . value) item
55
+                  (cond ((listp value) (mktree-helper key value root (incf counter)))
56
+                        (t (#_appendRow root (prepare-row key value)))))
57
+                (#_appendRow root (prepare-row item))))))
58
+
59
+(defun add-rows (root rows)
60
+  (loop for row in rows do (#_appendRow root row)))
61
+
62
+(defmethod init-ui ((instance json-view-app ))
63
+  (#_setWindowTitle instance "JSON viewer")
64
+
65
+  (setf (json-view instance) (#_new QTreeView instance)
66
+        (json-model instance) (#_new QStandardItemModel instance))
67
+
68
+  (#_setWordWrap (json-view instance) t)
69
+  (let* ((root (#_invisibleRootItem (json-model instance))))
70
+    (mktree root (json-data instance)))
71
+  
72
+  (#_setColumnCount (json-model instance) 2)
73
+  (#_setModel (json-view instance)  (json-model instance))
74
+  (#_expandAll (json-view instance))
75
+  (#_resizeColumnToContents (json-view instance) 0)
76
+
77
+  (let ((grid (#_new QVBoxLayout instance))
78
+        (grid1 (#_new QHBoxLayout instance))
79
+        (quit-button  (#_new QPushButton "&Quit!" instance)))
80
+    (#_addWidget grid (json-view instance))
81
+    (#_addWidget grid1 (#_new QPushButton))
82
+    (#_addWidget grid1 (#_new QPushButton))
83
+    (#_addWidget grid1 quit-button)
84
+    (#_addLayout grid grid1)
85
+    (#_setLayout instance grid)
86
+    
87
+    (connect quit-button "clicked()" *qapplication* "quit()")
88
+    ))
89
+
90
+(make-qapplication)
91
+
92
+(ql:quickload :cl-json)
93
+(ql:quickload :s-http-client)
94
+(let
95
+  ((app (make-instance 'json-view-app))
96
+   (r_json (json:decode-json *standard-input*)))
97
+  (setf (json-data app) r_json)
98
+  (init-ui app)
99
+  (with-main-window
100
+    (window app)))