File: calculator.h

package info (click to toggle)
portabase 2.1%2Bgit20120910-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 8,648 kB
  • ctags: 3,498
  • sloc: cpp: 32,214; sh: 2,868; ansic: 2,320; makefile: 481; python: 121; xml: 23; asm: 10
file content (89 lines) | stat: -rw-r--r-- 2,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * calculator.h (based on kmymoneycalculator.h, by Thomas Baumgart)
 *
 * (c) 2002-2004,2008-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
 * (c) 2000-2002 by Michael Edwardes <mte@users.sourceforge.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/** @file calculator.h
 * Header file for Calculator
 */

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include "pbdialog.h"

class QLabel;
class QAbstractButton;

/**
  * This class implements a simple electronic calculator with the
  * ability of addition, subtraction, multiplication and division
  * and percentage calculation. Memory locations are not available.
  *
  * The first operand and operation can be loaded from an external
  * source to switch from an edit-widget to the calculator widget
  * without the user having to re-type the data. See setInitialValues()
  * for details.
  */
class Calculator : public PBDialog  {
    Q_OBJECT
public:
    Calculator(QWidget* parent = 0);

    const QString result() const;
    void setInitialValue(const QString& value);

protected:
    void keyPressEvent(QKeyEvent* ev);

protected slots:
    void digitClicked(int button);
    void calculationClicked(int button);
    void decimalClicked();
    void plusminusClicked();
    void clearClicked();
    void clearAllClicked();
    void percentClicked();
    void changeDisplay(const QString& str);

private:
    QString operand; /**< The current (second) operand */
    QString m_result; /**< The last result */
    double op1; /**< The numeric representation of the first operand */
    int op; /**< The operation to be performed between the first and the second operand */
    QLabel *display; /**< A pointer to the display area */
    QAbstractButton *buttons[20]; /**< Pointers to the various buttons of the calculator */

    /**
     * The values used for the various keys internally
     */
    enum {
        /* 0-9 are used by digits */
        DECIMAL = 10,
        /*
         * make sure, that PLUS through EQUAL remain in
         * the order they are. Otherwise, check the calculation
         * signal mapper
         */
        PLUS,
        MINUS,
        SLASH,
        STAR,
        EQUAL,
        PLUSMINUS,
        PERCENT,
        CLEAR,
        CLEARALL,
        /* insert new buttons before this line */
        MAX_BUTTONS
    };
};

#endif