summaryrefslogtreecommitdiffstats
path: root/mybudget/src/calculator/calculatorscreen.cpp
blob: 177413725808c7b7b3fa2a646d655c800fef97af (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: qt-info@nokia.com
**
** This software, including documentation, is protected by copyright
** controlled by Nokia Corporation.  You may use this software in
** accordance with the terms and conditions contained in the Qt Phone
** Demo License Agreement.
**
****************************************************************************/

#include <QGraphicsLinearLayout>

#include "calculatorscreen.h"

#include "pdata.h"
#include "label.h"
#include "button.h"
#include "titlebar.h"
#include "dataresource.h"
#include "calculatorengine.h"


CalculatorScreen::CalculatorScreen()
  : Page()
{
    setContentsMargins(0, 0, 0, 0);

    mainLayout = new QGraphicsLinearLayout(Qt::Vertical);
    mainLayout->setSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    setLayout(mainLayout);

    m_engine = new CalculatorEngine;
    m_engine->connect(&_digitMapper, SIGNAL(mapped(int)), SLOT(pressDigit(int)));
    m_engine->connect(&_operatorMapper, SIGNAL(mapped(int)), SLOT(pressOperator(int)));

    TitleBar *title = new TitleBar;
    mainLayout->addItem(title);

    createCarousel();
    createDisplay();
    createKeyboard();

    connect(title, SIGNAL(closeClicked()), qApp, SLOT(quit()));

    connect(m_engine, SIGNAL(displayChanged(QString)),
            SLOT(handleDisplayChange(QString)));
}

CalculatorScreen::~CalculatorScreen()
{
    delete m_engine;
}

void CalculatorScreen::setDisplayText(const QString &text)
{
    display->setText(text);
}

QString CalculatorScreen::optionSelected()
{
    return _menu->selected();
}

void CalculatorScreen::createDisplay()
{
    QFont nokiaFont("Nokia Sans");
    nokiaFont.setPixelSize(Resource::intValue("Calculator/Screen.fontSize"));

    display = new Label;
    display->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
    display->setFont(nokiaFont);
    setDisplayText("0.00");

    PixmapWidget *image = \
        new PixmapWidget(PixmapLoader::pixmap(":/calculator_display.png"));
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(image);

    layout->addItem(display);

    mainLayout->addItem(image);
}

void CalculatorScreen::createCarousel()
{
    _menu = new HorizontalMenu();
    _menu->setZValue(1);
    mainLayout->addItem(_menu);
}

void CalculatorScreen::createKeyboard()
{
    QStringList buttons;

    // Line: 7 8 9 /
    buttons << "7" << "8" << "9" << "/";
    mainLayout->addItem(createKeyboardLine(buttons));
    buttons.clear();

    // Line: 4 5 6 -
    buttons << "4" << "5" << "6" << "-";
    mainLayout->addItem(createKeyboardLine(buttons));
    buttons.clear();

    // Line: 1 2 3 +
    buttons << "1" << "2" << "3" << "+";
    mainLayout->addItem(createKeyboardLine(buttons));
    buttons.clear();

    // Line: . 0 X =
    buttons << "." << "0" << "x" << "=";
    mainLayout->addItem(createKeyboardLine(buttons));
    buttons.clear();

    // Line: Clear Submit
    buttons << "CLEAR" << "SUBMIT";
    mainLayout->addItem(createKeyboardLine(buttons));
    buttons.clear();
}

QGraphicsLinearLayout *CalculatorScreen::createKeyboardLine(const QStringList &items)
{
    static QString operations = "+-x/.=";

    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);

    foreach (const QString &item, items) {
        Button *button;

        QFont font("Nokia Sans");
        font.setPixelSize(Resource::intValue("Calculator/Button.fontSize"));

        if (operations.contains(item)) {
            button = new Button(PixmapLoader::pixmap(":/bt_02_off.png"),
                                PixmapLoader::pixmap(":/bt_02_on.png"));

            if (item == ".")
                m_engine->connect(button, SIGNAL(clicked()), SLOT(pressDot()));
            else if (item == "=")
                m_engine->connect(button, SIGNAL(clicked()), SLOT(pressEqual()));
            else
                connectButton(&_operatorMapper, button, operations.indexOf(item));
        } else if (item == "SUBMIT" || item == "CLEAR") {
            button = new Button(PixmapLoader::pixmap(":/bt_03_off.png"),
                                PixmapLoader::pixmap(":/bt_03_on.png"));

            if (item == "CLEAR")
                m_engine->connect(button, SIGNAL(clicked()), SLOT(pressClearAll()));
            else {
                connect(button, SIGNAL(clicked()), SLOT(handleSubmit()));
                //Submit also cleans the display
                m_engine->connect(button, SIGNAL(clicked()), SLOT(pressClearAll()));
            }

            font.setPixelSize(Resource::intValue("Calculator/Action.fontSize"));
        } else {
            // digit
            button = new Button(PixmapLoader::pixmap(":/bt_01_off.png"),
                                PixmapLoader::pixmap(":/bt_01_on.png"));
            connectButton(&_digitMapper, button, item.toInt());
        }

        button->setText(item);
        button->setFont(font);

        layout->addItem(button);
    }
    return layout;
}

void CalculatorScreen::connectButton(QSignalMapper *mapper, QGraphicsLayoutItem *item,
                                     int mapping)
{
    QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
    mapper->setMapping(widget, mapping);
    mapper->connect(widget, SIGNAL(clicked()), SLOT(map()));
}

QRectF CalculatorScreen::flickableRect() const
{
    int offset = _menu->y() + _menu->size().height();
    return boundingRect().adjusted(0, offset, 0, 0);
}

void CalculatorScreen::handleDisplayChange(const QString &display)
{
    setDisplayText(display);
}

void CalculatorScreen::handleSubmit()
{
    const qreal value = m_engine->currentValue();

    if (value > 0) {
        const QString &category = optionSelected();
        PData *db = qApp->property("settings").value<PData*>();

        db->addValue(category, m_engine->currentValue());
    }
}