summaryrefslogtreecommitdiffstats
path: root/examples/animation/piemenu/qgraphicspiemenu.cpp
blob: c91342362885a8d1b9e9de6e2417b3004b5dd278 (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
/****************************************************************************
**
** 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 "qgraphicspiemenu.h"
#include "qgraphicspiemenu_p.h"

#include <QtGui/qaction.h>

QGraphicsPieMenu::QGraphicsPieMenu(QGraphicsItem *parent)
    : QGraphicsWidget(parent), d_ptr(new QGraphicsPieMenuPrivate)
{
    d_ptr->q_ptr = this;
    d_ptr->machine = new QStateMachine();
    d_ptr->popupState = new QState(d_ptr->machine->rootState());
    d_ptr->machine->setInitialState(d_ptr->popupState);
    d_ptr->menuAction = new QAction(this);
}

QGraphicsPieMenu::QGraphicsPieMenu(const QString &title, QGraphicsItem *parent)
    : QGraphicsWidget(parent), d_ptr(new QGraphicsPieMenuPrivate)
{
    d_ptr->q_ptr = this;
    d_ptr->machine = new QStateMachine();
    d_ptr->popupState = new QState(d_ptr->machine->rootState());
    d_ptr->machine->setInitialState(d_ptr->popupState);
    d_ptr->menuAction = new QAction(this);
    setTitle(title);
}

QGraphicsPieMenu::QGraphicsPieMenu(const QIcon &icon, const QString &title, QGraphicsItem *parent)
    : QGraphicsWidget(parent), d_ptr(new QGraphicsPieMenuPrivate)
{
    d_ptr->q_ptr = this;
    d_ptr->machine = new QStateMachine();
    d_ptr->popupState = new QState(d_ptr->machine->rootState());
    d_ptr->machine->setInitialState(d_ptr->popupState);
    d_ptr->menuAction = new QAction(this);
    setIcon(icon);
    setTitle(title);
}

QGraphicsPieMenu::~QGraphicsPieMenu()
{
    delete d_ptr;
}

QAction *QGraphicsPieMenu::addAction(const QString &text)
{
    QAction *action = new QAction(text, this);
    addAction(action);
    return action;
}

QAction *QGraphicsPieMenu::addAction(const QIcon &icon, const QString &text)
{
    QAction *action = new QAction(icon, text, this);
    addAction(action);
    return action;
}

QAction *QGraphicsPieMenu::addAction(const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut)
{
    QAction *action = new QAction(text, this);
    action->setShortcut(shortcut);
    connect(action, SIGNAL(triggered(bool)), receiver, member);
    addAction(action);
    return action;
}

QAction *QGraphicsPieMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut)
{
    QAction *action = new QAction(icon, text, this);
    action->setShortcut(shortcut);
    connect(action, SIGNAL(triggered(bool)), receiver, member);
    addAction(action);
    return action;
}

QAction *QGraphicsPieMenu::addMenu(QGraphicsPieMenu *menu)
{
    QAction *action = menu->menuAction();
    addAction(action);
    return action;
}

QGraphicsPieMenu *QGraphicsPieMenu::addMenu(const QString &title)
{
    QGraphicsPieMenu *menu = new QGraphicsPieMenu(title, this);
    addMenu(menu);
    return menu;
}

QGraphicsPieMenu *QGraphicsPieMenu::addMenu(const QIcon &icon, const QString &title)
{
    QGraphicsPieMenu *menu = new QGraphicsPieMenu(icon, title, this);
    addMenu(menu);
    return menu;
}

QAction *QGraphicsPieMenu::addSeparator()
{
    QAction *action = new QAction(this);
    action->setSeparator(true);
    addAction(action);
    return action;
}

QAction *QGraphicsPieMenu::insertMenu(QAction *before, QGraphicsPieMenu *menu)
{
    QAction *action = menu->menuAction();
    insertAction(before, action);
    return action;
}

QAction *QGraphicsPieMenu::insertSeparator(QAction *before)
{
    QAction *action = new QAction(this);
    action->setSeparator(true);
    insertAction(before, action);
    return action;
}

QAction *QGraphicsPieMenu::menuAction() const
{
    return d_func()->menuAction;
}

bool QGraphicsPieMenu::isEmpty() const
{
    // ### d->actions
    QList<QAction *> actionList = actions();
    bool ret = true;
    for (int i = 0; ret && i < actionList.size(); ++i) {
        const QAction *action = actionList.at(i);
        if (!action->isSeparator() && action->isVisible()) {
            ret = false;
            break;
        }
    }
    return ret;
}

void QGraphicsPieMenu::clear()
{
    // ### d->actions
    QList<QAction *> actionList = actions();
    for(int i = 0; i < actionList.size(); i++) {
        QAction *action = actionList.at(i);
        removeAction(action);
        if (action->parent() == this && action->associatedGraphicsWidgets().isEmpty())
            delete action;
    }
}

void QGraphicsPieMenu::popup(const QPointF &pos)
{
    Q_UNUSED(pos);
    Q_D(QGraphicsPieMenu);
    d->machine->start();
}

QAction *QGraphicsPieMenu::exec()
{
    return exec(pos());
}

QAction *QGraphicsPieMenu::exec(const QPointF &pos)
{
    Q_UNUSED(pos);
    return 0;
}

QAction *QGraphicsPieMenu::exec(QList<QAction *> actions, const QPointF &pos)
{
    QGraphicsPieMenu menu;
    for (QList<QAction *>::ConstIterator it = actions.constBegin(); it != actions.constEnd(); ++it)
        menu.addAction(*it);
    return menu.exec(pos);
}

QString QGraphicsPieMenu::title() const
{
    Q_D(const QGraphicsPieMenu);
    return d->title;
}

void QGraphicsPieMenu::setTitle(const QString &title)
{
    Q_D(QGraphicsPieMenu);
    d->title = title;
    updateGeometry();
}

QIcon QGraphicsPieMenu::icon() const
{
    Q_D(const QGraphicsPieMenu);
    return d->icon;
}

void QGraphicsPieMenu::setIcon(const QIcon &icon)
{
    Q_D(QGraphicsPieMenu);
    d->icon = icon;
    updateGeometry();
}

QSizeF QGraphicsPieMenu::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
    Q_UNUSED(which);
    Q_UNUSED(constraint);
    return QSizeF(1, 1);
}