summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/kmymoneycalculator.h
blob: 4eef50182b4b4435f95fe0f1657a9a7957ca32fb (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
                          kmymoneycalculator.h  -  description
                             -------------------
    begin                : Sat Oct 19 2002
    copyright            : (C) 2000-2002 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef KMYMONEYCALCULATOR_H
#define KMYMONEYCALCULATOR_H

// ----------------------------------------------------------------------------
// TQt Includes

#include <tqwidget.h>
#include <tqframe.h>
#include <tqlayout.h>
#include <tqgrid.h>
#include <tqlcdnumber.h>
#include <tqlineedit.h>
#include <tqlabel.h>

// ----------------------------------------------------------------------------
// TDE Includes

#include <kpushbutton.h>

// ----------------------------------------------------------------------------
// Project Includes

/**
  *@author Thomas Baumgart
  */

/**
  * 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 having the user to re-type the data. See setInitialValues()
  * for details.
  */
class kMyMoneyCalculator : public TQFrame  {
   TQ_OBJECT
  
public:
  kMyMoneyCalculator(TQWidget* parent = 0, const char *name = 0);
  ~kMyMoneyCalculator();

  /**
    * This methods is used to extract the result of the last
    * calculation. The fractional part is separated from the
    * integral part by the character setup using setComma().
    *
    * @return TQString representing the result of the
    *         last operation
    */
  const TQString result(void) const;

  /**
    * This method is used to set the character to be used
    * as the separator between the integer and fractional part
    * of an operand. Upon creation of the object, m_comma is
    * set to the current locale setting of KDE's decimalSymbol.
    *
    * @param ch TQChar representing the character to be used
    */
  void setComma(const TQChar ch) { m_comma = ch; };

  /**
    * This method is used to preset the first operand and start
    * execution of an operation. This method is currently used
    * by kMyMoneyEdit. If @p ev is 0, then no operation will be
    * started.
    *
    * @param value reference to TQString representing the operands value
    * @param ev    pointer to TQKeyEvent representing the operation's key
    */
  void setInitialValues(const TQString& value, TQKeyEvent* ev);

signals:
  /**
    * This signal is emitted, when a new result is available
    */
  void signalResultAvailable();

protected:
  void keyPressEvent(TQKeyEvent* ev);

  /**
    * This method is used to transform a double into a TQString
    * and removing any trailing 0's and decimal seperators
    *
    * @param val reference to double value to be converted
    * @return TQString object containing the converted value
    */
  TQString normalizeString(const double& val);

protected slots:
  /**
    * This method appends the digit represented by the parameter
    * to the current operand
    *
    * @param button integer value of the digit to be added in the
    *               range [0..9]
    */
  void digitClicked(int button);

  /**
    * This methods starts the operation contained in the parameter
    *
    * @param button The TQt::Keycode for the button pressed or clicked
    */
  void calculationClicked(int button);

  /**
    * This method appends a period (comma) to initialize the fractional
    * part of an operand. The period is only appended once.
    */
  void commaClicked(void);

  /**
    * This method reverses the sign of the current operand
    */
  void plusminusClicked(void);

  /**
    * This method clears the current operand
    */
  void clearClicked(void);

  /**
    * This method clears all registers
    */
  void clearAllClicked(void);

  /**
    * This method executes the percent operation
    */
  void percentClicked(void);

  /**
    * This method updates the display of the calculator with
    * the text passed as argument
    *
    * @param str reference to TQString containing the new display contents
    */
  void changeDisplay(const TQString& str);

private:
  /**
    * This member variable stores the current (second) operand
    */
  TQString operand;

  /**
    * This member variable stores the last result
    */
  TQString m_result;

  /**
    * This member variable stores the representation of the
    * character to be used to separate the integer and fractional
    * part of numbers. The internal representation is always a period.
    */
  TQChar m_comma;

  /**
    * The numeric representation of a stacked first operand
    */
  double op0;

  /**
    * The numeric representation of the first operand
    */
  double op1;

  /**
    * This member stores the operation to be performed between
    * the first and the second operand.
    */
  int op;

  /**
   * This member stores a pending addition operation
   */
  int stackedOp;

  /**
    * This member stores a pointer to the display area
    */
  TQLabel *display;

  /**
    * This member array stores the pointers to the various
    * buttons of the calculator. It is setup during the
    * constructor of this object
    */
  KPushButton *buttons[20];

  /**
    * This enumeration type stores the values used for the
    * various keys internally
    */
  enum {
    /* 0-9 are used by digits */
    COMMA = 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
  };

  /**
    * This flag signals, if the operand should be replaced upon
    * a digit key pressure. Defaults to false and will be set, if
    * setInitialValues() is called without an operation.
    */
  bool m_clearOperandOnDigit;
};

#endif