summaryrefslogtreecommitdiffstats
path: root/examples/animation/photobrowser/menu.cpp
blob: d4efe9cc2bc45b469991dae082cec875ee07c704 (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
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the $MODULE$ of the Qt Toolkit.
**
** $TROLLTECH_DUAL_LICENSE$
**
****************************************************************************/

#include "menu.h"

Menu::Menu(QGraphicsItem *parent) : QGraphicsWidget(parent), m_selected(0)
{
    setFlag(QGraphicsItem::ItemIsFocusable);
    m_selection = new QGraphicsRectItem(this);
    QLinearGradient linearGrad(QPointF(0, 0), QPointF(0,50));
    linearGrad.setColorAt(0, QColor(255,255,255,128));
    linearGrad.setColorAt(1, QColor(255,255,255,16));
    m_selection->setBrush(linearGrad);   
    m_selection->setPen(QPen(Qt::transparent));
}

Menu::~Menu()
{
}


MenuAction *Menu::addAction(const QString &text, QObject *receiver, const char* slot)
{
    MenuAction *action = new MenuAction(text, this);
    if (!m_actions.isEmpty()) {
        MenuAction *last = m_actions.last();
        action->setPos(last->pos() + last->boundingRect().bottomLeft());
    }
    m_actions.append(action);
    if (m_selection->boundingRect().width() < action->boundingRect().width())
        m_selection->setRect(action->boundingRect());

    QObject::connect(action, SIGNAL(triggered()), receiver, slot);
    return action;
}

QRectF Menu::boundingRect() const
{
    QRectF res;
    foreach (MenuAction *a, m_actions)
        res |= a->boundingRect();
    return res;
}

void Menu::keyPressEvent (QKeyEvent * event) 
{
    switch (event->key()) {
        case Qt::Key_Escape:
            hide();
            break;
        case Qt::Key_Up:
            m_selected -= 2;
        case Qt::Key_Down:
            if (!m_actions.isEmpty()) {
                m_selected = (m_selected + 1 + m_actions.count()) % m_actions.count();
                QItemAnimation *anim = new QItemAnimation(m_selection, QItemAnimation::Position);
                anim->setEndValue(m_actions.at(m_selected)->pos());
                anim->start(QAbstractAnimation::DeleteWhenStopped);
            }
            break;  
        case Qt::Key_Enter:
        case Qt::Key_Return:
            if (!m_actions.isEmpty()) {
                QItemAnimation *anim = new QItemAnimation(m_selection, QItemAnimation::RotationX);
                anim->setEndValue(m_selection->xRotation() < 180 ? qreal(360) : qreal(0));
                anim->start(QAbstractAnimation::DeleteWhenStopped);
                m_actions.at(m_selected)->trigger();
                hide();
            }            
            break;
        default:
            QGraphicsItem::keyPressEvent(event);
    }
}

void Menu::show()
{
    QItemAnimation *anim = new QItemAnimation(this, QItemAnimation::Opacity);
    anim->setEndValue(qreal(1.));
    anim->start(QAbstractAnimation::DeleteWhenStopped);
    anim = new QItemAnimation(m_selection, QItemAnimation::ScaleFactorX);
    anim->setEndValue(qreal(1));
    anim->start(QAbstractAnimation::DeleteWhenStopped);
    anim = new QItemAnimation(m_selection, QItemAnimation::ScaleFactorY);
    anim->setEndValue(qreal(1));
    anim->start(QAbstractAnimation::DeleteWhenStopped);
    setFocus();
}

void Menu::hide()
{
    QItemAnimation *anim = new QItemAnimation(m_selection, QItemAnimation::ScaleFactorX);
    anim->setEndValue(qreal(.1));
    anim->start(QAbstractAnimation::DeleteWhenStopped);
    anim = new QItemAnimation(m_selection, QItemAnimation::ScaleFactorY);
    anim->setEndValue(qreal(.1));
    anim->start(QAbstractAnimation::DeleteWhenStopped);
    anim = new QItemAnimation(this, QItemAnimation::Opacity);
    anim->setEndValue(qreal(0));
    anim->start(QAbstractAnimation::DeleteWhenStopped);
    parentItem()->setFocus();
}


MenuAction::MenuAction(const QString &text, Menu * parent) 
    : QGraphicsTextItem(text,parent)
{
    QFont f = font();
    f.setPointSize(18);
    setFont(f);
    setDefaultTextColor(Qt::white);
}

void MenuAction::trigger()
{
    emit triggered();
}