1 module demo;
2 version(demo){
3 	import qui.qui;
4 	import qui.widgets;
5 	import std.conv : to;
6 	import std.path;
7 	import std.file : thisExePath;
8 	import utils.misc : uinteger, integer, fileToArray;
9 	
10 	void main (){
11 		App appInstance = new App();
12 		appInstance.run();
13 		// not doing this causes an exception
14 		.destroy (appInstance);
15 	}
16 	/// the app
17 	class App{
18 	private:
19 		QTerminal term;
20 		ProgressbarWidget progressBar;
21 		TextLabelWidget label;
22 		EditLineWidget edit;
23 		MemoWidget memo;
24 		LogWidget log;
25 		QLayout hLayout;
26 		SplitterWidget split;
27 	public:
28 		/// constructor
29 		this(){
30 			// construct all widgets
31 			term = new QTerminal(QLayout.Type.Vertical);
32 			progressBar = new ProgressbarWidget();
33 			label = new TextLabelWidget();
34 			edit = new EditLineWidget("EditLineWidget, one line text editor: ");
35 			hLayout = new QLayout(QLayout.Type.Horizontal);
36 			memo = new MemoWidget();
37 			log = new LogWidget();
38 
39 			// prepare the layout in for the Memo and log
40 			hLayout.addWidget([memo, log]);
41 			{
42 				string[] lines = fileToArray(dirName(thisExePath)~dirSeparator~"README.md");
43 				foreach (line; lines){
44 					memo.lines.append(line.to!dstring);
45 				}
46 			}
47 			memo.wantsTab = false;
48 
49 			// put all widgets in the order they are to appear in terminal
50 			term.addWidget([label, edit, progressBar, hLayout]);
51 
52 			// set some properties
53 			label.caption = "this is a label widget. To show single line text. Below is a progress bar:";
54 			progressBar.caption = "this is the progress bar";
55 			progressBar.size.maxHeight = 1;
56 			progressBar.max = 10;
57 			progressBar.progress = 0;
58 
59 			// hide progress bar on Ctrl+O
60 			term.onKeyboardEvent = delegate(QWidget caller, KeyboardEvent key){
61 				log.add(to!dstring("Terminal Keyboard Event: "~key.tostring));
62 				if (key.isCtrlKey){
63 					if (key.key == KeyboardEvent.CtrlKeys.CtrlO){
64 						progressBar.show = !progressBar.show;
65 						if (progressBar.show)
66 							log.add("progressbar shown");
67 						else
68 							log.add("progressbar hidden");
69 					}else if (key.key == KeyboardEvent.CtrlKeys.CtrlP){
70 						progressBar.size.maxHeight = (progressBar.size.maxHeight % 3) + 1;
71 						progressBar.requestResize();
72 						log.add("progressbar resized: "~progressBar.size.maxHeight.to!dstring);
73 					}else if (key.key == KeyboardEvent.CtrlKeys.CtrlL){
74 						log.show = !log.show;
75 						log.add("log "~to!dstring(log.show ? "shown" : "hidden"));
76 					}
77 				}
78 				return false;
79 			};
80 
81 			// and this is how timerEvent can be used
82 			progressBar.onTimerEvent = delegate(QWidget caller, uinteger msecs){
83 				static increasing = true;
84 				ProgressbarWidget owner = cast(ProgressbarWidget)caller;
85 				if (owner.progress >= owner.max){
86 					increasing = false;
87 				}else if (owner.progress == 0){
88 					increasing = true;
89 				}
90 				if (increasing)
91 					owner.progress = owner.progress + 1;
92 				else
93 					owner.progress = owner.progress -1;
94 				return false;
95 			};
96 			memo.onActivateEvent = delegate(QWidget, bool isActive){
97 				log.add(to!dstring("Memo is now "~(isActive?"active":"inactive")));
98 				return false;
99 			};
100 			edit.onActivateEvent = delegate(QWidget, bool isActive){
101 				log.add(to!dstring("EditLine is now "~(isActive?"active":"inactive")));
102 				return false;
103 			};
104 			term.onMouseEvent = delegate(QWidget, MouseEvent mouse){
105 				log.add(to!dstring("Terminal MouseEvent: (" ~ mouse.x.to!string ~ ',' ~ mouse.y.to!string~')'));
106 				return false;
107 			};
108 		}
109 		~this(){
110 			// destroy all widgets
111 			.destroy(term);
112 			.destroy(progressBar);
113 			.destroy(label);
114 			.destroy(edit);
115 			.destroy(memo);
116 			.destroy(log);
117 			.destroy(hLayout);
118 		}
119 		/// run the app
120 		void run(){
121 			term.timerMsecs = 500;
122 			term.run;
123 		}
124 	}
125 }