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