Seamly2D
Code documentation
calculator.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include <QtWidgets>
52 #include <QtGlobal>
53 
54 #include <cmath>
55 
56 #include "button.h"
57 #include "calculator.h"
58 
59 //! [0]
61  : QWidget(parent)
62  , sumInMemory(0.0)
63  , sumSoFar(0.0)
64  , factorSoFar(0.0)
65  , pendingAdditiveOperator(QString())
66  , pendingMultiplicativeOperator(QString())
67  , waitingForOperand(true)
68  , display(new QLineEdit("0"))
69  , digitButtons()
70 {
71 //! [0]
72 //! [1]
73 //! [1] //! [2]
74  display->setReadOnly(true);
75  display->setAlignment(Qt::AlignRight);
76  display->setMaxLength(15);
77 
78  QFont font = display->font();
79  font.setPointSize(font.pointSize() + 8);
80  display->setFont(font);
81 //! [2]
82 
83 //! [4]
84  for (int i = 0; i < NumDigitButtons; ++i) {
85  digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
86  }
87 
88  Button *pointButton = createButton(tr("."), SLOT(pointClicked()));
89  Button *changeSignButton = createButton(tr("\302\261"), SLOT(changeSignClicked()));
90 
91  Button *backspaceButton = createButton(tr("Backspace"), SLOT(backspaceClicked()));
92  Button *clearButton = createButton(tr("Clear"), SLOT(clear()));
93  Button *clearAllButton = createButton(tr("Clear All"), SLOT(clearAll()));
94 
95  Button *clearMemoryButton = createButton(tr("MC"), SLOT(clearMemory()));
96  Button *readMemoryButton = createButton(tr("MR"), SLOT(readMemory()));
97  Button *setMemoryButton = createButton(tr("MS"), SLOT(setMemory()));
98  Button *addToMemoryButton = createButton(tr("M+"), SLOT(addToMemory()));
99 
100  Button *divisionButton = createButton(tr("\303\267"), SLOT(multiplicativeOperatorClicked()));
101  Button *timesButton = createButton(tr("\303\227"), SLOT(multiplicativeOperatorClicked()));
102  Button *minusButton = createButton(tr("-"), SLOT(additiveOperatorClicked()));
103  Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
104 
105  Button *squareRootButton = createButton(tr("Sqrt"), SLOT(unaryOperatorClicked()));
106  Button *powerButton = createButton(tr("x\302\262"), SLOT(unaryOperatorClicked()));
107  Button *reciprocalButton = createButton(tr("1/x"), SLOT(unaryOperatorClicked()));
108  Button *equalButton = createButton(tr("="), SLOT(equalClicked()));
109 //! [4]
110 
111 //! [5]
112  QGridLayout *mainLayout = new QGridLayout;
113 //! [5] //! [6]
114  mainLayout->setSizeConstraint(QLayout::SetFixedSize);
115  mainLayout->addWidget(display, 0, 0, 1, 6);
116  mainLayout->addWidget(backspaceButton, 1, 0, 1, 2);
117  mainLayout->addWidget(clearButton, 1, 2, 1, 2);
118  mainLayout->addWidget(clearAllButton, 1, 4, 1, 2);
119 
120  mainLayout->addWidget(clearMemoryButton, 2, 0);
121  mainLayout->addWidget(readMemoryButton, 3, 0);
122  mainLayout->addWidget(setMemoryButton, 4, 0);
123  mainLayout->addWidget(addToMemoryButton, 5, 0);
124 
125  for (int i = 1; i < NumDigitButtons; ++i) {
126  int row = ((9 - i) / 3) + 2;
127  int column = ((i - 1) % 3) + 1;
128  mainLayout->addWidget(digitButtons[i], row, column);
129  }
130 
131  mainLayout->addWidget(digitButtons[0], 5, 1);
132  mainLayout->addWidget(pointButton, 5, 2);
133  mainLayout->addWidget(changeSignButton, 5, 3);
134 
135  mainLayout->addWidget(divisionButton, 2, 4);
136  mainLayout->addWidget(timesButton, 3, 4);
137  mainLayout->addWidget(minusButton, 4, 4);
138  mainLayout->addWidget(plusButton, 5, 4);
139 
140  mainLayout->addWidget(squareRootButton, 2, 5);
141  mainLayout->addWidget(powerButton, 3, 5);
142  mainLayout->addWidget(reciprocalButton, 4, 5);
143  mainLayout->addWidget(equalButton, 5, 5);
144  setLayout(mainLayout);
145 
146  setWindowTitle(tr("Calculator"));
147 }
148 //! [6]
149 //---------------------------------------------------------------------------------------------------------------------
151 {
152 }
153 
154 //! [7]
156 {
157  Button *clickedButton = qobject_cast<Button *>(sender());
158  int digitValue = clickedButton->text().toInt();
159  if (display->text() == "0" && digitValue == 0 )
160  return;
161 
162  if (waitingForOperand) {
163  display->clear();
164  waitingForOperand = false;
165  }
166  display->setText(display->text() + QString::number(digitValue));
167 }
168 //! [7]
169 
170 //! [8]
172 //! [8] //! [9]
173 {
174  Button *clickedButton = qobject_cast<Button *>(sender());
175  QString clickedOperator = clickedButton->text();
176  double operand = display->text().toDouble();
177  double result = 0.0;
178 
179  if (clickedOperator == tr("Sqrt")) {
180  if (operand < 0.0) {
181  abortOperation();
182  return;
183  }
184  result = std::sqrt(operand);
185  } else if (clickedOperator == tr("x\302\262")) {
186  result = std::pow(operand, 2.0);
187  } else if (clickedOperator == tr("1/x")) {
188  if (qFuzzyIsNull(operand))
189  {
190  abortOperation();
191  return;
192  }
193  result = 1.0 / operand;
194  }
195  display->setText(QString::number(result));
196  waitingForOperand = true;
197 }
198 //! [9]
199 
200 //! [10]
202 //! [10] //! [11]
203 {
204  Button *clickedButton = qobject_cast<Button *>(sender());
205  QString clickedOperator = clickedButton->text();
206  double operand = display->text().toDouble();
207 
208 //! [11] //! [12]
209  if (!pendingMultiplicativeOperator.isEmpty()) {
210 //! [12] //! [13]
211  if (!calculate(operand, pendingMultiplicativeOperator)) {
212  abortOperation();
213  return;
214  }
215  display->setText(QString::number(factorSoFar));
216  operand = factorSoFar;
217  factorSoFar = 0.0;
219  }
220 
221 //! [13] //! [14]
222  if (!pendingAdditiveOperator.isEmpty()) {
223 //! [14] //! [15]
224  if (!calculate(operand, pendingAdditiveOperator)) {
225  abortOperation();
226  return;
227  }
228  display->setText(QString::number(sumSoFar));
229  } else {
230  sumSoFar = operand;
231  }
232 
233 //! [15] //! [16]
234  pendingAdditiveOperator = clickedOperator;
235 //! [16] //! [17]
236  waitingForOperand = true;
237 }
238 //! [17]
239 
240 //! [18]
242 {
243  Button *clickedButton = qobject_cast<Button *>(sender());
244  QString clickedOperator = clickedButton->text();
245  double operand = display->text().toDouble();
246 
247  if (!pendingMultiplicativeOperator.isEmpty()) {
248  if (!calculate(operand, pendingMultiplicativeOperator)) {
249  abortOperation();
250  return;
251  }
252  display->setText(QString::number(factorSoFar));
253  } else {
254  factorSoFar = operand;
255  }
256 
257  pendingMultiplicativeOperator = clickedOperator;
258  waitingForOperand = true;
259 }
260 //! [18]
261 
262 //! [20]
264 {
265  double operand = display->text().toDouble();
266 
267  if (!pendingMultiplicativeOperator.isEmpty()) {
268  if (!calculate(operand, pendingMultiplicativeOperator)) {
269  abortOperation();
270  return;
271  }
272  operand = factorSoFar;
273  factorSoFar = 0.0;
275  }
276  if (!pendingAdditiveOperator.isEmpty()) {
277  if (!calculate(operand, pendingAdditiveOperator)) {
278  abortOperation();
279  return;
280  }
281  pendingAdditiveOperator.clear();
282  } else {
283  sumSoFar = operand;
284  }
285 
286  display->setText(QString::number(sumSoFar));
287  sumSoFar = 0.0;
288  waitingForOperand = true;
289 }
290 //! [20]
291 
292 //! [22]
294 {
295  if (waitingForOperand)
296  display->setText("0");
297  if (!display->text().contains('.'))
298  display->setText(display->text() + tr("."));
299  waitingForOperand = false;
300 }
301 //! [22]
302 
303 //! [24]
305 {
306  QString text = display->text();
307  double value = text.toDouble();
308 
309  if (value > 0.0) {
310  text.prepend(tr("-"));
311  } else if (value < 0.0) {
312  text.remove(0, 1);
313  }
314  display->setText(text);
315 }
316 //! [24]
317 
318 //! [26]
320 {
321  if (waitingForOperand)
322  return;
323 
324  QString text = display->text();
325  text.chop(1);
326  if (text.isEmpty()) {
327  text = "0";
328  waitingForOperand = true;
329  }
330  display->setText(text);
331 }
332 //! [26]
333 
334 //! [28]
336 {
337  if (waitingForOperand)
338  return;
339 
340  display->setText("0");
341  waitingForOperand = true;
342 }
343 //! [28]
344 
345 //! [30]
347 {
348  sumSoFar = 0.0;
349  factorSoFar = 0.0;
350  pendingAdditiveOperator.clear();
352  display->setText("0");
353  waitingForOperand = true;
354 }
355 //! [30]
356 
357 //! [32]
359 {
360  sumInMemory = 0.0;
361 }
362 
364 {
365  display->setText(QString::number(sumInMemory));
366  waitingForOperand = true;
367 }
368 
370 {
371  equalClicked();
372  sumInMemory = display->text().toDouble();
373 }
374 
376 {
377  equalClicked();
378  sumInMemory += display->text().toDouble();
379 }
380 //! [32]
381 //! [34]
382 Button *CalculatorUtil::createButton(const QString &text, const char *member)
383 {
384  Button *button = new Button(text);
385  connect(button, SIGNAL(clicked()), this, member);
386  return button;
387 }
388 //! [34]
389 
390 //! [36]
392 {
393  clearAll();
394  display->setText(tr("####"));
395 }
396 //! [36]
397 
398 //! [38]
399 bool CalculatorUtil::calculate(double rightOperand, const QString &pendingOperator)
400 {
401  if (pendingOperator == tr("+")) {
402  sumSoFar += rightOperand;
403  } else if (pendingOperator == tr("-")) {
404  sumSoFar -= rightOperand;
405  } else if (pendingOperator == tr("\303\227")) {
406  factorSoFar *= rightOperand;
407  } else if (pendingOperator == tr("\303\267")) {
408  if (qFuzzyIsNull(rightOperand))
409  return false;
410  factorSoFar /= rightOperand;
411  }
412  return true;
413 }
414 //! [38]
[0]
Definition: button.h:58
double sumInMemory
[2]
Definition: calculator.h:100
void clearMemory()
[30]
Definition: calculator.cpp:358
bool calculate(double rightOperand, const QString &pendingOperator)
[36]
Definition: calculator.cpp:399
void backspaceClicked()
[24]
Definition: calculator.cpp:319
void pointClicked()
[20]
Definition: calculator.cpp:293
void clearAll()
[28]
Definition: calculator.cpp:346
void unaryOperatorClicked()
[7]
Definition: calculator.cpp:171
QString pendingMultiplicativeOperator
[6] //! [7]
Definition: calculator.h:108
void addToMemory()
Definition: calculator.cpp:375
void clear()
[26]
Definition: calculator.cpp:335
CalculatorUtil(QWidget *parent=nullptr)
[0]
Definition: calculator.cpp:60
void changeSignClicked()
[22]
Definition: calculator.cpp:304
virtual ~CalculatorUtil()
[6]
Definition: calculator.cpp:150
bool waitingForOperand
[7] //! [8]
Definition: calculator.h:110
double sumSoFar
[3] //! [4]
Definition: calculator.h:102
void additiveOperatorClicked()
[9]
Definition: calculator.cpp:201
Button * createButton(const QString &text, const char *member)
[0]
Definition: calculator.cpp:382
Button * digitButtons[NumDigitButtons]
Definition: calculator.h:118
QString pendingAdditiveOperator
[5] //! [6]
Definition: calculator.h:106
void digitClicked()
[7]
Definition: calculator.cpp:155
QLineEdit * display
[8]
Definition: calculator.h:114
void multiplicativeOperatorClicked()
[17]
Definition: calculator.cpp:241
void abortOperation()
[34]
Definition: calculator.cpp:391
double factorSoFar
[4] //! [5]
Definition: calculator.h:104
void equalClicked()
[18]
Definition: calculator.cpp:263