aboutsummaryrefslogtreecommitdiff
path: root/src/views/mobjectmenuview.cpp
blob: 54746addbf303e53a3592f5c79b92576c7689e28 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <QGraphicsSceneMouseEvent>

#include "mobjectmenuview_p.h"
#include "mbutton.h"
#include "mimagewidget.h"
#include "mlabel.h"
#include "mseparator.h"
#include "mviewcreator.h"
#include "mobjectmenu.h"

#include <QGraphicsGridLayout>
#include <QGraphicsLinearLayout>

#include "mwidgetaction.h"
#include "mscenemanager.h"
#include "mapplication.h"
#include "mapplicationwindow.h"
#include "mpannableviewport.h"

MObjectMenuViewPrivate::MObjectMenuViewPrivate() :
      q_ptr(0),
      mainLayout(0),
      actionLayout(0),
      titleLayout(0),
      controller(0),
      titleArea(0),
      titleLabel(0),
      titleIcon(0),
      titleSeparator(0),
      actionViewport(0),
      actionWidget(0)
{
}

MObjectMenuViewPrivate::~MObjectMenuViewPrivate()
{
    if (!titleArea->parentItem() && !titleArea->parentLayoutItem()) {
        delete titleArea;
    }
}

void MObjectMenuViewPrivate::init()
{
    mainLayout = new QGraphicsLinearLayout(Qt::Vertical);
    mainLayout->setContentsMargins(0.0,0.0,0.0,0.0);
    mainLayout->setSpacing(0);
    controller->setLayout(mainLayout);

    //create title bar
    titleLayout = new QGraphicsGridLayout();
    titleLayout->setSpacing(0);
    titleLayout->setContentsMargins(0.0,0.0,0.0,0.0);
    titleArea = new MObjectMenuTitleArea(controller);
    titleArea->setLayout(titleLayout);

    //create and add title icon
    titleIcon = new MImageWidget(titleArea);
    titleLayout->addItem(titleIcon, 0, 0);
    titleLayout->setAlignment(titleIcon, Qt::AlignCenter);

    //create and add title label
    titleLabel = new MLabel("", titleArea);
    titleLabel->setTextElide(true);
    titleLayout->addItem(titleLabel, 0, 1);
    titleLayout->setAlignment(titleLabel, Qt::AlignCenter);

    //create and add title separator
    titleSeparator = new MSeparator(titleArea);
    titleLayout->addItem(titleSeparator, 1, 0, 1, 2);
    titleLayout->setAlignment(titleSeparator, Qt::AlignCenter);

    //setup pannable viewport for the action buttons
    actionLayout = new QGraphicsLinearLayout(Qt::Vertical);
    actionLayout->setSpacing(0);
    actionLayout->setContentsMargins(0.0,0.0,0.0,0.0);
    actionWidget = new QGraphicsWidget;
    actionWidget->setLayout(actionLayout);
    actionViewport = new MPannableViewport(controller);
    actionViewport->setWidget(actionWidget);
    actionViewport->setVerticalPanningPolicy(MPannableWidget::PanningAsNeeded);
    mainLayout->addItem(actionViewport);
}

void MObjectMenuViewPrivate::updateIcon()
{
    Q_Q(MObjectMenuView);

    //setup the icon, first try to load it from filesystem,
    //if it fails load it from theme, if iconId is "" hide
    //the image widget
    if( !q->model()->iconId().isEmpty() ) {
        QPixmap pm(q->model()->iconId());
        if( !pm.isNull() )
            titleIcon->setPixmap(pm);
        else
            titleIcon->setImage(q->model()->iconId());
        titleIcon->show();
        titleLayout->addItem(titleIcon, 0, 0);
        titleLayout->setAlignment(titleIcon, Qt::AlignCenter);
    } else {
        titleIcon->setImage(q->model()->iconId());
        titleIcon->hide();
        titleLayout->removeAt(0);
    }
}

void MObjectMenuViewPrivate::updateTitleAreaVisibility()
{
    Q_Q(MObjectMenuView);

    //hide whole title area if title == "" and icon is not visible
    if( q->model()->title().isEmpty() && q->model()->iconId().isEmpty() ) {
        mainLayout->removeItem(titleArea);
        actionViewport->setStyleName("objectMenuViewportWithoutTitle");
        titleArea->hide();
        if (controller->scene()) {
            //Remove from the scene as otherwise it might distract appear/disappear animation
            controller->scene()->removeItem(titleArea);
        }
    } else if( mainLayout->itemAt(0) != titleArea ) {
        mainLayout->insertItem(0, titleArea);
        actionViewport->setStyleName("objectMenuViewportWithTitle");
        titleArea->show();
    }
}


MObjectMenuView::MObjectMenuView(MObjectMenu *controller) :
    MSceneWindowView(controller),
    d_ptr(new MObjectMenuViewPrivate)
{
    Q_D(MObjectMenuView);
    d->q_ptr = this;
    d->controller = controller;
    d->init();
}

MObjectMenuView::MObjectMenuView(MObjectMenuViewPrivate &dd, MObjectMenu *controller) :
    MSceneWindowView(controller),
    d_ptr(&dd)
{
    Q_D(MObjectMenuView);
    d->q_ptr = this;
    d->controller = controller;
    d->init();
}

MObjectMenuView::~MObjectMenuView()
{
#ifdef HAVE_CONTENTACTION
    Q_D(MObjectMenuView);
    QHash<MAction*, ContentAction::Action>::iterator i = d->contentActions.begin(),
                                                       e = d->contentActions.end();
    for(; i!=e; ++i) {
        actionRemoved(i.key());
        delete i.key();
    }
    d->contentActions.clear();
#endif

    MActionList actions = model()->actions();
    const int count = actions.count();
    for (int i = 0; i < count; ++i) {
        actionRemoved(actions.at(i));
    }

    delete d_ptr;
}

void MObjectMenuView::actionAdded(MAction *action)
{
    Q_D(MObjectMenuView);

    // show only if it is visible
    if (action->isVisible()) {

        // create button for this action
        MButton *button = new MButton(action->iconID(), action->text(), d->actionWidget);

        d->controller->connect(button, SIGNAL(clicked(bool)), SLOT(dismiss()));
        QObject::connect(button, SIGNAL(clicked(bool)), action, SIGNAL(triggered()));

        button->setEnabled(action->isEnabled());
        d->actionLayout->addItem(button);
        int buttonCount = d->actionLayout->count();
        if(buttonCount == 1) {
            // make the only button to use "single button" background
            button->setLayoutPosition(M::DefaultPosition);
        } else if( buttonCount > 1 ) {
            MButton* prev = (MButton*)d->actionLayout->itemAt(buttonCount - 2);
            // we have more than one in layout
            if(buttonCount == 2) {
                prev->setLayoutPosition(M::VerticalTopPosition);
            } else {
                prev->setLayoutPosition(M::VerticalCenterPosition);
            }
            button->setLayoutPosition(M::VerticalBottomPosition);
        }

        d->buttons.insert(action, button);

        //viewport doesnt update its size without this
        d->actionViewport->setWidget(d->actionWidget);
    }
}

void MObjectMenuView::actionRemoved(MAction *action)
{
    Q_D(MObjectMenuView);

    MButton *button = d->buttons.value(action, NULL);
    if (button) {
        d->buttons.remove(action);
        delete button;
    }
}

void MObjectMenuView::actionModified(MAction *action)
{
    Q_D(MObjectMenuView);
    MButton *button = d->buttons.value(action, NULL);
    if (button) {
        if (!action->isVisible()) {
            actionRemoved(action);
        } else {
            // update button data accordingly
            button->setText(action->text());
            button->setIconID(action->iconID());
            button->setEnabled(action->isEnabled());
        }
    } else {
        // there is no button yet, action must've been invisible
        actionAdded(action);
    }
}

void MObjectMenuView::updateData(const QList<const char *> &modifications)
{
    Q_D(MObjectMenuView);

    foreach(const char * member, modifications) {
        // TODO: this could be done without first removing all actions.
        if (member == MObjectMenuModel::Actions) {
            foreach(MAction * action, d->buttons.keys()) {
                actionRemoved(action);
            }

            MActionList actions = model()->actions();
            const int count = actions.count();
            for (int i = 0; i < count; ++i) {
                actionAdded(actions.at(i));
            }
        } else if (member == MObjectMenuModel::ContentURI) {
#ifdef HAVE_CONTENTACTION
            // remove & release the old content uri dependant actions
            QHash<MAction*, ContentAction::Action>::iterator i = d->contentActions.begin(),
                                                               e = d->contentActions.end();
            for(; i!=e; ++i) {
                actionRemoved(i.key());
                delete i.key();
            }
            d->contentActions.clear();

            QList<ContentAction::Action> contentActionList = ContentAction::Action::actions(model()->contentURI());
            foreach(ContentAction::Action contentAction, contentActionList) {
                // TODO: fetch the correct text from contentAction and maybe also an icon
                MAction* action = new MAction(contentAction.name(), this);
                connect(action, SIGNAL(triggered()), SLOT(contentActionTriggered()));
                d->contentActions.insert(action, contentAction);

                actionAdded(action);
            }
#endif
        } else if (member == MObjectMenuModel::Title ) {
            d->titleLabel->setText(model()->title());
            d->updateTitleAreaVisibility();
        } else if (member == MObjectMenuModel::IconId ) {
            d->updateIcon();
            d->updateTitleAreaVisibility();
        }
    }
}

void MObjectMenuView::setupModel()
{
    MSceneWindowView::setupModel();

    Q_D(MObjectMenuView);

#ifdef HAVE_CONTENTACTION
    // remove & release the old content uri dependant actions
    QHash<MAction*, ContentAction::Action>::iterator i = d->contentActions.begin(),
                                                       e = d->contentActions.end();
    for(; i!=e; ++i) {
        actionRemoved(i.key());
        delete i.key();
    }
    d->contentActions.clear();
#endif

    // remove & release the old manually added actions
    foreach(MAction * action, d->buttons.keys()) {
        actionRemoved(action);
    }

    connect(model(), SIGNAL(actionAdded(MAction *)),
            this, SLOT(actionAdded(MAction *)));
    connect(model(), SIGNAL(actionRemoved(MAction *)),
            this, SLOT(actionRemoved(MAction *)));
    connect(model(), SIGNAL(actionModified(MAction *)),
            this, SLOT(actionModified(MAction *)));

    MActionList actions = model()->actions();
    const int count = actions.count();
    for (int i = 0; i < count; ++i) {
        actionAdded(actions.at(i));
    }

#ifdef HAVE_CONTENTACTION
    QList<ContentAction::Action> contentActionList = ContentAction::Action::actions(model()->contentURI());
    foreach(ContentAction::Action contentAction, contentActionList) {
        // TODO: fetch the correct text from contentAction and maybe also an icon
        MAction* action = new MAction(contentAction.name(), this);
        connect(action, SIGNAL(triggered()), SLOT(contentActionTriggered()));
        d->contentActions.insert(action, contentAction);

        actionAdded(action);
    }
#endif
    d->titleArea->setStyleName(style()->titleStyleName());
    d->titleIcon->setStyleName(style()->titleIconStyleName());
    d->titleLabel->setStyleName(style()->titleLabelStyleName());
    d->titleSeparator->setStyleName(style()->titleSeparatorStyleName());

    d->titleLabel->setText(model()->title());
    d->updateIcon();
    d->updateTitleAreaVisibility();
}

void MObjectMenuView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    //Do not pass the events through the menu window
    event->accept();
}

void MObjectMenuView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    Q_D(MObjectMenuView);
    d->controller->dismiss();
}

void MObjectMenuViewPrivate::contentActionTriggered()
{
#ifdef HAVE_CONTENTACTION
    Q_Q(MObjectMenuView);

    MAction* action = qobject_cast<MAction*>(q->sender());
    if(!action)
        return;

    QHash<MAction*, ContentAction::Action>::iterator i = contentActions.find(action);
    if(i == contentActions.end())
        return;

    i.value().trigger();
#endif
}

M_REGISTER_VIEW_NEW(MObjectMenuView, MObjectMenu)
#include "moc_mobjectmenuview.cpp"