1 module app;
2 
3 import core.runtime; 
4 import std.conv;
5 
6 import qte5;
7 
8 QLCDNumber lcd;
9 const strPink   = "background: pink";
10 
11 int result;
12 string resultRegister, numberRegister;
13 string operationSign;
14 
15 extern(C)
16 {
17 	void updateLCD(string number)
18 	{
19 		numberRegister ~= number;
20 		lcd.display(to!int(numberRegister));
21 		lcd.update;
22 	}
23 
24 	void setOperationSign(string sign)
25 	{
26 		resultRegister = numberRegister;
27 		operationSign = sign;
28 		numberRegister = "";
29 		result = 0;
30 	}
31 
32 	void onButton0(void* button)
33 	{
34 		updateLCD("0");
35 	}
36 
37 	void onButton1(void* button)
38 	{
39 		updateLCD("1");
40 	}
41 
42 	void onButton2(void* button)
43 	{
44 		updateLCD("2");
45 	}
46 
47 	void onButton3(void* button)
48 	{
49 		updateLCD("3");
50 	}
51 
52 	void onButton4(void* button)
53 	{
54 		updateLCD("4");
55 	}
56 	
57 	void onButton5(void* button)
58 	{
59 		updateLCD("5");
60 	}
61 	
62 	void onButton6(void* button)
63 	{
64 		updateLCD("6");
65 	}
66 
67 	void onButton7(void* button)
68 	{
69 		updateLCD("7");
70 	}
71 	
72 	void onButton8(void* button)
73 	{
74 		updateLCD("8");
75 	}
76 	
77 	void onButton9(void* button)
78 	{
79 		updateLCD("9");
80 	}
81 
82 	void onAddButton(void* button)
83 	{
84 		setOperationSign("+");
85 	}
86 
87 	void onSubtractButton(void* button)
88 	{
89 		setOperationSign("-");
90 	}
91 
92 	void onMultiplyButton(void* button)
93 	{
94 		setOperationSign("*");
95 	}
96 
97 	void onDivideButton(void* button)
98 	{
99 		setOperationSign("/");
100 	}
101 
102 	void onClearButton(void* button)
103 	{
104 		numberRegister = "0";
105 		lcd.display(0);
106 		lcd.update;
107 	}
108 
109 	void onSignButton(void* button)
110 	{
111 		numberRegister = "-" ~ numberRegister;
112 		lcd.display(to!int(numberRegister));
113 		lcd.update;
114 	}
115 
116 	void onEqualButton(void* button)
117 	{
118 		switch (operationSign)
119 		{
120 			case "+":
121 				result = to!int(resultRegister) + to!int(numberRegister);
122 				numberRegister = to!string(result);
123 				break;
124 			case "-":
125 				result = to!int(resultRegister) - to!int(numberRegister);
126 				numberRegister = to!string(result);
127 				break;
128 			case "*":
129 				result = to!int(resultRegister) * to!int(numberRegister);
130 				numberRegister = to!string(result);
131 				break;
132 			case "/":
133 				result = to!int(resultRegister) / to!int(numberRegister);
134 				numberRegister = to!string(result);
135 				break;
136 			default:
137 				numberRegister = resultRegister;
138 				break;
139 		}
140 
141 		lcd.display(result);
142 		lcd.update;
143 	}
144 }
145 
146 alias WindowType = QtE.WindowType;
147 enum WHITE = "background : white";
148 
149 class MainForm : QWidget 
150 {
151 	QVBoxLayout verticalSizer, verticalSizer1, buttonGroup5;
152 	QHBoxLayout horizontalSizer, buttonGroup1, buttonGroup2, buttonGroup3, buttonGroup4;
153 	QPushButton button0, button1, button2, button3,
154 		button4, button5, button6, 
155 		button7, button8, button9,
156 		sign, clear, 
157 		add, subtract, multiply,divide, equal;
158 
159 	this(QWidget parent, WindowType windowType) 
160 	{
161 		super(parent, windowType); 
162 		resize(300, 400); 
163 		setWindowTitle("QtE Calculator");
164 		setStyleSheet(strPink);
165 
166 		lcd = new QLCDNumber(this);
167 		lcd.setMode(QLCDNumber.Mode.Dec);
168 		lcd.setStyleSheet("background: lightgreen; color : gray");
169 		lcd.display(0);
170 		
171 		verticalSizer = new QVBoxLayout(null);
172 		verticalSizer1 = new QVBoxLayout(null);
173 		horizontalSizer = new QHBoxLayout(null);
174 
175 		with (buttonGroup1 = new QHBoxLayout(null))
176 		{
177 			sign = new QPushButton("+/-", this);
178 			button0 = new QPushButton("0", this);
179 			clear = new QPushButton("C", this);
180 
181 			QAction action0 = new QAction(null, &onButton0, null);
182 			connects(button0, "clicked()", action0, "Slot()");
183 
184 			QAction actionSign = new QAction(null, &onSignButton, null);
185 			connects(sign, "clicked()", actionSign, "Slot()");
186 
187 			QAction actionClear = new QAction(null, &onClearButton, null);
188 			connects(clear, "clicked()", actionClear, "Slot()");
189 			
190 			addWidget(sign);
191 			addWidget(button0);
192 			addWidget(clear);
193 		}
194 
195 		with (buttonGroup2 = new QHBoxLayout(null))
196 		{
197 			button1 = new QPushButton("1", this);
198 			button2 = new QPushButton("2", this);
199 			button3 = new QPushButton("3", this);
200 
201 			QAction action1 = new QAction(null, &onButton1, null);
202 			connects(button1, "clicked()", action1, "Slot()");
203 
204 			QAction action2 = new QAction(null, &onButton2, null);
205 			connects(button2, "clicked()", action2, "Slot()");
206 
207 			QAction action3 = new QAction(null, &onButton3, null);
208 			connects(button3, "clicked()", action3, "Slot()");
209 
210 			addWidget(button1);
211 			addWidget(button2);
212 			addWidget(button3);
213 		}
214 
215 		with (buttonGroup3 = new QHBoxLayout(null))
216 		{
217 			button4 = new QPushButton("4", this);
218 			button5 = new QPushButton("5", this);
219 			button6 = new QPushButton("6", this);
220 
221 			QAction action4 = new QAction(null, &onButton4, null);
222 			connects(button4, "clicked()", action4, "Slot()");
223 			
224 			QAction action5 = new QAction(null, &onButton5, null);
225 			connects(button5, "clicked()", action5, "Slot()");
226 			
227 			QAction action6 = new QAction(null, &onButton6, null);
228 			connects(button6, "clicked()", action6, "Slot()");
229 			
230 			addWidget(button4);
231 			addWidget(button5);
232 			addWidget(button6);
233 		}
234 
235 		with (buttonGroup4 = new QHBoxLayout(null))
236 		{
237 			button7 = new QPushButton("7", this);
238 			button8 = new QPushButton("8", this);
239 			button9 = new QPushButton("9", this);
240 
241 			QAction action7 = new QAction(null, &onButton7, null);
242 			connects(button7, "clicked()", action7, "Slot()");
243 			
244 			QAction action8 = new QAction(null, &onButton8, null);
245 			connects(button8, "clicked()", action8, "Slot()");
246 			
247 			QAction action9 = new QAction(null, &onButton9, null);
248 			connects(button9, "clicked()", action9, "Slot()");
249 			
250 			addWidget(button7);
251 			addWidget(button8);
252 			addWidget(button9);
253 		}
254 
255 		with (buttonGroup5 = new QVBoxLayout(null))
256 		{
257 			add = new QPushButton("+", this);
258 			subtract = new QPushButton("-", this);
259 			multiply = new QPushButton("*", this);
260 			divide = new QPushButton("/", this);
261 
262 			QAction actionAdd = new QAction(null, &onAddButton, null);
263 			connects(add, "clicked()", actionAdd, "Slot()");
264 
265 			QAction actionSubtract = new QAction(null, &onSubtractButton, null);
266 			connects(subtract, "clicked()", actionSubtract, "Slot()");
267 			
268 			QAction actionMultiply = new QAction(null, &onMultiplyButton, null);
269 			connects(multiply, "clicked()", actionMultiply, "Slot()");
270 
271 			QAction actionDivide = new QAction(null, &onDivideButton, null);
272 			connects(divide, "clicked()", actionDivide, "Slot()");
273 			
274 			addWidget(add);
275 			addWidget(subtract);
276 			addWidget(multiply);
277 			addWidget(divide);
278 		}
279 
280 		equal = new QPushButton("=", this);
281 
282 		QAction actionEqual = new QAction(null, &onEqualButton, null);
283 		connects(equal, "clicked()", actionEqual, "Slot()");
284 
285 		verticalSizer1
286 			.addLayout(buttonGroup4)
287 				.addLayout(buttonGroup3)
288 				.addLayout(buttonGroup2)
289 				.addLayout(buttonGroup1);
290 
291 		horizontalSizer
292 			.addLayout(verticalSizer1)
293 				.addLayout(buttonGroup5);
294 
295 		
296 		verticalSizer
297 			.addWidget(lcd)
298 				.addLayout(horizontalSizer)
299 				.addWidget(equal);
300 		
301 		setLayout(verticalSizer);
302 	}
303 }
304 
305 
306 int main(string[] args) 
307 {
308 	alias normalWindow = QtE.WindowType.Window;
309 
310 	if (LoadQt(dll.QtE5Widgets, true)) 
311 	{
312 		return 1;
313 	}
314 
315 	QApplication app = new QApplication(&Runtime.cArgs.argc, Runtime.cArgs.argv, 1);
316 
317 	MainForm mainForm = new MainForm(null, normalWindow);
318 
319 	mainForm
320 		.show
321 			.saveThis(&mainForm);
322 
323 	return app.exec;
324 }