summaryrefslogtreecommitdiffstats
path: root/mybudget/src/widgets/timelinemenu.cpp
blob: f958c2828386bafd8b42aa01d3d2436968a1c0ae (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
/****************************************************************************
**
** 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 "timelinemenu.h"

#include <QApplication>
#include <QDate>
#include <QGraphicsLinearLayout>
#include <QGraphicsSceneMouseEvent>

#include "pdata.h"
#include "button.h"
#include "dataresource.h"
#include "pixmaploader.h"
#include "coingraphicwidget.h"


TimelineMenu::TimelineMenu()
    : QGraphicsWidget(),
      contentsWidget(0)
{
    setFlag(QGraphicsItem::ItemHasNoContents);
    setFlag(QGraphicsItem::ItemClipsChildrenToShape);

    QGraphicsPixmapItem *background =
        new QGraphicsPixmapItem(PixmapLoader::pixmap(":/bg.png"), this);
    background->setPos(0, 0);

    setContentsMargins(0, 0, 0, 0);

    // This widget has the contents, that we will scroll on the
    // screen. TimelineMenu will clip it to the correct region.
    contentsWidget = new QGraphicsWidget(this);
    contentsWidget->setContentsMargins(0, 0, 0, 0);

    QGraphicsLinearLayout *l = new QGraphicsLinearLayout(Qt::Horizontal);
    l->setContentsMargins(0, 0, 0, 0);
    l->setSpacing(0);
    contentsWidget->setLayout(l);

    // This variable defines the area of the screen (counting from the
    // bottom) that is going to be used to drag the timeline itself
    draggingHeight = Resource::intValue("Widgets/TimelineMenu.draggingHeight");
    isDragging = false;

    updateMaxValue();

    db = qApp->property("settings").value<PData*>();
    connect(db, SIGNAL(valueAdded(const QString &)),
            SLOT(updateGraph(const QString &)));
    coin = new QPixmap(QLatin1String(":/coin.png"));

    fillWidget(l);
}

TimelineMenu::~TimelineMenu()
{
    delete coin;
}

qreal TimelineMenu::draggableHeight() const
{
    return draggingHeight;
}

void TimelineMenu::updateGraph(const QString &group)
{
    Q_UNUSED(group);
    updateMaxValue();

    for (int i = 0; i < allCoins.size(); i++) {
        CoinGraphicWidget *coins = allCoins.at(i);
        coins->updateValue(i + 1, overallSum, maxValue);
    }
}

void TimelineMenu::updateMaxValue()
{
    PData *db = qApp->property("settings").value<PData*>();
    const QDate &date = QDate::currentDate();

    db->beginGroup(QLatin1String("date"));
    db->beginGroup(QString::number(date.year()));

    const QStringList &keys = db->allKeys();

    qreal sum = 0;
    QList<qreal> values;
    foreach (const QString &key, keys) {
        const qreal value = db->getValue(key);
        sum += value;
        values << value;
    }

    qSort(values.begin(), values.end());
    maxValue = values.size() > 0 ? values.last() : 0;
    db->endGroup();
    db->endGroup();
    overallSum = sum;
}

void TimelineMenu::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->pos().y() <= (size().height() - draggingHeight))
        event->ignore();
    else
        isDragging = true;
}

void TimelineMenu::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (isDragging) {
        qreal x = contentsWidget->x() + event->pos().x() - event->lastPos().x();
        contentsWidget->setX(qBound<qreal>(-contentsWidget->size().width()
                                           + size().width(), x, 0));
    }
}

void TimelineMenu::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    isDragging = false;
}

void TimelineMenu::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    QGraphicsWidget::resizeEvent(event);

    if (contentsWidget)
        contentsWidget->resize(contentsWidget->size().width(), size().height());
}

void TimelineMenu::fillWidget(QGraphicsLinearLayout *l)
{
    Button *button;
    CoinGraphicWidget *coins;
    QGraphicsLinearLayout *vl;

    QFont buttonFont("Nokia Sans");
    buttonFont.setPixelSize(Resource::intValue("Widgets/MonthButton.fontSize"));

    int year = QDate::currentDate().year();

    for (int i = 1; i <= 12; i++) {
        coins = new CoinGraphicWidget(i, overallSum, maxValue, coin);
        allCoins.append(coins);

        button = new Button(PixmapLoader::pixmap(":/btn_timeline_off.png"));
        button->setFont(buttonFont);
        button->setAcceptedMouseButtons(Qt::NoButton);
        button->setText(QDate(year, i, 1).toString("MMM/yy"));
        button->setCacheMode(QGraphicsItem::ItemCoordinateCache);

        vl = new QGraphicsLinearLayout(Qt::Vertical);
        vl->setSpacing(0);
        vl->setContentsMargins(0, 0, 0, 0);
        vl->addItem(coins);
        vl->setAlignment(coins, Qt::AlignHCenter);
        vl->addItem(button);

        l->addItem(vl);
        l->setAlignment(vl, Qt::AlignBottom);
    }
}