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

#include "dataresource.h"
#include "pixmaploader.h"


static const char *optionKeys[] = {
    "food", "car", "house", "books", "clothes", "fun", "health",
    "travel", "overall"
};

HorizontalMenu::HorizontalMenu(bool hasOverallIcon)
    : PixmapWidget(PixmapLoader::pixmap(":/carrousel_bg.png")),
      selectedIndex(0),
      isSliding(false),
      moveThreshold(15)
{
    clipper = new QGraphicsWidget(this);
    clipper->resize(size());
    clipper->setPos(0, 0);
    clipper->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
    clipper->setZValue(2);

    w = new QGraphicsWidget(clipper);
    l = new QGraphicsLinearLayout(Qt::Horizontal);
    w->setLayout(l);

    markSelection = new PixmapWidget(0);
    marker = new QGraphicsRectItem(0, 0, 90, 90, this);

    fillWidget(hasOverallIcon);

    light = new PixmapWidget(Resource::stringValue("Widgets/HorizontalMenu.light"));
    yOffset = Resource::intValue("Widgets/HorizontalMenu.yOffset");
    markOffset = Resource::intValue("Widgets/HorizontalMenu.markOffset");
    lightOffset = Resource::intValue("Widgets/HorizontalMenu.lightOffset");

    light->setParentItem(this);
    light->setZValue(1);

    if (hasOverallIcon)
        w->moveBy(-2000, yOffset); // ###
    else
        w->moveBy(0, yOffset);


    centralizeAnimation = new QPropertyAnimation(w, "x", this);
    centralizeAnimation->setDuration(240);

    // Fade In Animation, from 0 to 1. We use the setDirection method to
    // produce fade out animation.
    fadeAnimation = new QPropertyAnimation(markSelection, "opacity", this);
    fadeAnimation->setStartValue(0.0);
    fadeAnimation->setEndValue(1.0);
    fadeAnimation->setDuration(220);

    // Sequential Animation = Centralize Animation + Fade In Animation
    sequentialAnimation = new QSequentialAnimationGroup(this);
    sequentialAnimation->addAnimation(centralizeAnimation);
    sequentialAnimation->addAnimation(fadeAnimation);

    connect(centralizeAnimation, SIGNAL(stateChanged(QAbstractAnimation::State,
                                                     QAbstractAnimation::State)),
            SLOT(centralizedStarted()));
    connect(centralizeAnimation, SIGNAL(finished()), SLOT(optionSelected()));
}

HorizontalMenu::~HorizontalMenu()
{

}

int HorizontalMenu::count() const
{
    return l->count();
}

void HorizontalMenu::optionSelected()
{
    emit optionChanged(selected());
}

QString HorizontalMenu::selected() const
{
    if (selectedIndex < 0)
        return QString();
    else
        return optionKeys[selectedIndex];
}

void HorizontalMenu::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    PixmapWidget::resizeEvent(event);

    const int w = size().width();
    const int h = size().height();
    const QSizeF &itemSize = l->itemAt(0)->preferredSize();

    clipper->resize(w, h);

    marker->setRect(0, 0, itemSize.width(), h);
    marker->setPos(w / 2 - itemSize.width() / 2, 0);
    markSelection->setGeometry(0, 0, w, markOffset);
    light->setPos(0, lightOffset);

    // ajust selected item
    focusMenuItem(selectedIndex, false);
}

void HorizontalMenu::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    isSliding = false;
    pressedX = event->pos().x();

    // fade out
    fadeAnimation->setDirection(QAbstractAnimation::Backward);
    fadeAnimation->start();
}

void HorizontalMenu::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    const int ex = event->pos().x();

    if (qAbs(ex - pressedX) > moveThreshold)
        isSliding = true;

    if (isSliding) {
        const int mx = marker->pos().x();
        const int mw = marker->rect().width();
        const int fx = w->x() + ex - event->lastPos().x();

        w->setX(qBound<qreal>(mx + mw - w->size().width(), fx, mx));
    }
}

void HorizontalMenu::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    //fade in
    fadeAnimation->setDirection(QAbstractAnimation::Forward);

    if (isSliding) {
        const int index = indexAtPosition(size().width() / 2);
        isSliding = false;
        focusMenuItem(index >= 0 ? index : selectedIndex);
    } else {
        const int index = indexAtPosition(event->pos().x());
        if (index >= 0) {
            selectedIndex = index;
            focusMenuItem(selectedIndex);
        }
    }
}

void HorizontalMenu::fillWidget(bool hasOverallIcon)
{
    markSelection->setParentItem(this);
    markSelection->setCacheMode(NoCache);
    markSelection->setOpacity(0.0);
    markSelection->setPos(0, 0);

    marker->setPen(Qt::NoPen);

    MenuIconWidget *item;

    for (int i = Food; i <= Travel; i++) {
        item = new MenuIconWidget(Option(i));
        l->addItem(item);
        l->setAlignment(item, Qt::AlignCenter);

        selectionPixmaps << QPixmap(PixmapLoader::pixmap(QString(":/carrousel_bg_%1.png")
                                                  .arg(optionKeys[i])));
    }

    if (hasOverallIcon) {
        item = new MenuIconWidget(Overall);
        l->addItem(item);
        l->setAlignment(item, Qt::AlignCenter);

        selectionPixmaps << QPixmap(PixmapLoader::pixmap(":/carrousel_bg_overall.png"));
    }

    l->setContentsMargins(0, 0, 0, 0);
    w->setContentsMargins(0, 0, 0, 0);
    l->setSpacing(item->preferredSize().width() / 3);
}

void HorizontalMenu::centralizedStarted()
{
    markSelection->setPixmap(selectionPixmaps[selectedIndex]);
}

int HorizontalMenu::indexAtPosition(qreal x)
{
    int result = (x - w->x()) / (w->size().width() / l->count());
    return (result < 0 || result >= l->count()) ? -1 : result;
}

void HorizontalMenu::focusMenuItem(int index, bool animated)
{
    const qreal iw = marker->rect().width() + l->spacing();
    const qreal fx = marker->x() - iw * index;

    selectedIndex = index;

    if (!animated) {
        w->setX(fx);
        markSelection->setOpacity(1.0);
        markSelection->setPixmap(selectionPixmaps[selectedIndex]);
        optionSelected();
    } else {
        centralizeAnimation->setStartValue(w->x());
        centralizeAnimation->setEndValue(fx);
        sequentialAnimation->start();
    }
}