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 			// lets change active widget cycling key to something else too
50 			term.setActiveWidgetCycleKey('`'); // this is that key that's usually right above tab, on left of `1`
51 
52 			// put all widgets in the order they are to appear in terminal
53 			term.addWidget([label, edit, progressBar, hLayout]);
54 
55 			// set some properties
56 			label.caption = "this is a label widget. To show single line text. Below is a progress bar:";
57 			progressBar.caption = "this is the progress bar";
58 			progressBar.size.maxHeight = 1;
59 			progressBar.max = 10;
60 			progressBar.progress = 0;
61 
62 			// hide progress bar on Ctrl+O
63 			term.onKeyboardEvent = delegate(QWidget caller, KeyboardEvent key){
64 				log.add(to!dstring("Terminal Keyboard Event; code:"~(cast(uinteger)key.key).to!string~"; char:"~key.tostring));
65 				if (key.isCtrlKey){
66 					if (key.key == KeyboardEvent.CtrlKeys.CtrlO){
67 						progressBar.show = !progressBar.show;
68 						if (progressBar.show)
69 							log.add("progressbar shown");
70 						else
71 							log.add("progressbar hidden");
72 					}else if (key.key == KeyboardEvent.CtrlKeys.CtrlP){
73 						progressBar.size.maxHeight = (progressBar.size.maxHeight % 3) + 1;
74 						progressBar.requestResize();
75 						log.add("progressbar resized: "~progressBar.size.maxHeight.to!dstring);
76 					}else if (key.key == KeyboardEvent.CtrlKeys.CtrlL){
77 						log.show = !log.show;
78 						log.add("log "~to!dstring(log.show ? "shown" : "hidden"));
79 					}else if (key.key == KeyboardEvent.CtrlKeys.CtrlE){
80 						log.add("jump to the EditLine");
81 						term.activateWidget(edit);
82 					}
83 				}
84 				return false;
85 			};
86 
87 			// and this is how timerEvent can be used
88 			progressBar.onTimerEvent = delegate(QWidget caller, uinteger msecs){
89 				static increasing = true;
90 				ProgressbarWidget owner = cast(ProgressbarWidget)caller;
91 				if (owner.progress >= owner.max){
92 					increasing = false;
93 				}else if (owner.progress == 0){
94 					increasing = true;
95 				}
96 				if (increasing)
97 					owner.progress = owner.progress + 1;
98 				else
99 					owner.progress = owner.progress -1;
100 				return false;
101 			};
102 			memo.onActivateEvent = delegate(QWidget, bool isActive){
103 				log.add(to!dstring("Memo is now "~(isActive?"active":"inactive")));
104 				return false;
105 			};
106 			edit.onActivateEvent = delegate(QWidget, bool isActive){
107 				log.add(to!dstring("EditLine is now "~(isActive?"active":"inactive")));
108 				return false;
109 			};
110 			term.onMouseEvent = delegate(QWidget, MouseEvent mouse){
111 				log.add(to!dstring("Terminal MouseEvent: (" ~ mouse.x.to!string ~ ',' ~ mouse.y.to!string~')'));
112 				return false;
113 			};
114 		}
115 		~this(){
116 			// destroy all widgets
117 			.destroy(term);
118 			.destroy(progressBar);
119 			.destroy(label);
120 			.destroy(edit);
121 			.destroy(memo);
122 			.destroy(log);
123 			.destroy(hLayout);
124 		}
125 		/// run the app
126 		void run(){
127 			term.timerMsecs = 500;
128 			term.run;
129 		}
130 	}
131 }