aboutsummaryrefslogtreecommitdiff
path: root/src/views/mmenuobjectview.cpp
blob: 3ebf515e95fb61a3de9973f674a6ba00fbae24cd (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/***************************************************************************
**
** 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 "mmenuobjectview.h"

#include <QGraphicsSceneEvent>

#include "mapplicationmenu.h"
#include "mmenuobjectview_p.h"
#include "mbutton.h"
#include "mviewcreator.h"

#include "mpannableviewport.h"
#include "mwidgetaction.h"
#include "mtheme.h"
#include "mscalableimage.h"
#include "mscenemanager.h"
#include "mlayout.h"
#include "mlinearlayoutpolicy.h"
#include <QGraphicsLinearLayout>

MObjectMenuLayoutPolicy::MObjectMenuLayoutPolicy(MLayout *layout) :
    MAbstractLayoutPolicy(layout)
{ }

void MObjectMenuLayoutPolicy::setItemGeometry(QGraphicsLayoutItem *item, const QRectF &geom)
{
    geometries[item] = geom;

    QSizeF maximum = item->effectiveSizeHint(Qt::MaximumSize);
    QSizeF minimum = item->effectiveSizeHint(Qt::MinimumSize);
    QRectF new_geometry = QRectF(geom.topLeft(), geom.size().boundedTo(maximum).expandedTo(minimum));

    QRectF target(contentsArea().topLeft() + new_geometry.topLeft(), new_geometry.size());
    setItemGeometry(item, target);

    updateGeometry();
}

void MObjectMenuLayoutPolicy::removeAt(int index)
{
    QGraphicsLayoutItem *item = itemAt(index);
    if (item) {
        geometries.remove(item);
    }
    MAbstractLayoutPolicy::removeAt(index);
}

void MObjectMenuLayoutPolicy::relayout()
{
    QPointF topLeft = contentsArea().topLeft();

    int i = count();
    while (--i >= 0) {
        QGraphicsLayoutItem *item = itemAt(i);
        QRectF new_geometry = geometries[item];
        QSizeF maximum = item->effectiveSizeHint(Qt::MaximumSize);
        QSizeF minimum = item->effectiveSizeHint(Qt::MinimumSize);
        new_geometry = QRectF(new_geometry.topLeft(), new_geometry.size().boundedTo(maximum).expandedTo(minimum));

        QPointF topLeft = contentsArea().topLeft();
        QRectF target(topLeft + new_geometry.topLeft(), new_geometry.size());
        setItemGeometry(item, target);
    }
}

void MObjectMenuLayoutPolicy::addItem(QGraphicsLayoutItem *item)
{
    MAbstractLayoutPolicy::addItem(item);
}

QSizeF MObjectMenuLayoutPolicy::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
    if (which == Qt::MaximumSize) {
        // maximum size is the QWIDGETSIZE_MAX
        QSizeF new_size = constraint;
        if (new_size.width() < 0)
            new_size.setWidth(QWIDGETSIZE_MAX);
        if (new_size.height() < 0)
            new_size.setHeight(QWIDGETSIZE_MAX);
        return new_size;
    }

    // minimum and preferred size of a layout is the bounding box of the children
    int i = count();
    QRectF boundingBox;
    while (--i >= 0) {
        // iterate through children
        boundingBox = boundingBox.united(itemGeometry(i));
    }

    qreal right, bottom;
    layout()->getContentsMargins(NULL, NULL, &right, &bottom);
    return QSizeF(boundingBox.right() + right - layout()->geometry().left(), boundingBox.bottom() + bottom - layout()->geometry().top());
}



MMenuObjectViewPrivate::MMenuObjectViewPrivate(MApplicationMenu *menu)
    : q_ptr(0), controller(menu), viewport(0), container(0), layout(0),
      linearPolicy(0), customPolicy(0)
{
}

MMenuObjectViewPrivate::~MMenuObjectViewPrivate()
{
    removeEventFilter(controller);
}

void MMenuObjectViewPrivate::refreshLayout()
{
    Q_Q(MMenuObjectView);

    if (controller->sceneManager() == 0)
        return;

    // refresh layout items
    int i;
    for (i = customPolicy->count() - 1; i >= 0; --i) {
        customPolicy->removeAt(i);
        linearPolicy->removeAt(i);
    }

    QList<QAction *> actions = controller->actions();
    const int actionsCount = actions.count();
    for (i = 0; i < actionsCount; ++i) {
        QAction *action = actions[i];
        MAction *mAction = qobject_cast<MAction *>(action);
        if (!mAction) continue;
        if ((mAction->location() & MAction::ObjectMenuLocation) == 0) continue;
        if (!mAction->isVisible()) continue;

        // add to both layouts
        MWidget *widget = widgets.value(mAction);
        if (!widget) {
            addWidget(action);
            widget = widgets.value(mAction);
        }

        if (widget) {
            linearPolicy->addItem(widget);
            customPolicy->addItem(widget);
        }
    }

    const int width = q->size().width();

    // if item count < 4, make it linear
    const int customPolicyCount = customPolicy->count();
    if (customPolicy->count() < 4) {
        for (i = 0; i < customPolicyCount; ++i) {
            QGraphicsLayoutItem *item = customPolicy->itemAt(i);
            QRectF rect(0, 0, 0, 0);
            rect.setLeft(width / 4);
            rect.setTop(i * q->style()->itemHeight());
            rect.setWidth(width / 2);
            rect.setHeight(q->style()->itemHeight());
            customPolicy->setItemGeometry(item, rect);
        }
    } else { // otherwise make it two columns so that if the item count in both columns are not equal, the other is vertically centered
        const bool even_count = (customPolicyCount % 2) == 0 ? true : false;
        int row_count = customPolicyCount / 2;

        if (!even_count)
            row_count++;

        // first column
        for (i = 0; i < row_count; ++i) {
            QGraphicsLayoutItem *item = customPolicy->itemAt(i);
            QRectF rect(0, 0, 0, 0);
            rect.setTop(i * q->style()->itemHeight());
            rect.setWidth(width / 2);
            rect.setHeight(q->style()->itemHeight());
            customPolicy->setItemGeometry(item, rect);
        }

        // second column
        for (i = row_count; i < customPolicyCount; ++i) {
            QGraphicsLayoutItem *item = customPolicy->itemAt(i);

            int row = i - row_count;
            QRectF rect(0, 0, 0, 0);
            rect.setTop(row * q->style()->itemHeight());
            rect.setLeft((width / 2));

            if (!even_count) {
                rect.setTop(rect.top() + (q->style()->itemHeight() / 2));
            }

            rect.setWidth(width / 2);
            rect.setHeight(q->style()->itemHeight());
            customPolicy->setItemGeometry(item, rect);
        }
    }

    QSize visibleSceneSize = controller->sceneManager()->visibleSceneSize(M::Portrait);
    // center the widgets, first the linear layout
    qreal height = visibleSceneSize.height();
    qreal offset = height - ((qreal)linearPolicy->count() * (qreal)q->style()->itemHeight());
    linearPolicy->setContentsMargins(0, offset / 2.0, 0, offset / 2.0);

    visibleSceneSize.transpose();

    // and the custom one
    height = visibleSceneSize.height();
    offset = height - ((qreal)customPolicyCount / 2.0 * (qreal)q->style()->itemHeight());
    customPolicy->setContentsMargins(0, offset / 2.0, 0, offset / 2.0);
}

void MMenuObjectViewPrivate::addWidget(QAction *action)
{
    Q_Q(MMenuObjectView);

    // check whether it is a m action, TODO: Figure out if we need to support QActions
    MAction *mAction = qobject_cast<MAction *>(action);
    if (!mAction)
        return;

    // is this an object menu item?
    if ((mAction->location() & MAction::ObjectMenuLocation) == 0)
        return;

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

        // Check whether a widget was submitted for this action, if not, create button
        MWidgetAction *widgetAction = qobject_cast<MWidgetAction *>(mAction);

        MWidget *widget = NULL;

        if (widgetAction) {
            widget = widgetAction->requestWidget(container);
        } else {
            // create button for this action
            MButton *button = new MButton(mAction->text(), container);
            button->setViewType("objectmenuitem");
            button->setIconID(mAction->iconID());

            QObject::connect(button, SIGNAL(clicked(bool)), controller, SLOT(hideMenu()));
            QObject::connect(button, SIGNAL(clicked(bool)), action, SIGNAL(triggered()));

            // store this separately so we can later on access it
            buttons.insert(mAction, button);
            widget = button;
        }

        if (widget) {
            // store widget so we can easily access it later on if needed
            widgets.insert(mAction, widget);

            // TODO: This could be better
            QSize preferredSize(q->size().width() / 2, q->style()->itemHeight());
            widget->setPreferredSize(preferredSize);
            widget->setMinimumHeight(q->style()->itemHeight());
            widget->setMaximumHeight(q->style()->itemHeight());
            widget->setVisible(true);
            widget->setEnabled(action->isEnabled());

        }
    }
}

void MMenuObjectViewPrivate::removeWidget(QAction *action)
{
    // check whether it is a m action, TODO: Figure out if we need to support QActions
    MAction *mAction = qobject_cast<MAction *>(action);
    if (!mAction)
        return;

    MWidget *widget = widgets.value(mAction);
    if (widget) {
        // Remove the widget from both layouts
        linearPolicy->removeItem(widget);
        customPolicy->removeItem(widget);

        // Check whether we did the widget for this action and clean up
        MButton *button = buttons.value(mAction, NULL);
        buttons.remove(mAction);
        widgets.remove(mAction);

        MWidgetAction *widgetAction = qobject_cast<MWidgetAction *>(mAction);
        if (widgetAction)
            widgetAction->releaseWidget(widget);

        if (button)
            delete button;
    }
}

void MMenuObjectViewPrivate::actionChanged(QAction *action)
{
    MAction *da = qobject_cast<MAction *>(action);

    // check whether we did the button for this action
    if (da) {
        MButton *button = buttons.value(da, NULL);
        if (button) {
            // update button data accordingly
            button->setText(da->text());
            button->setIconID(da->iconID());
            button->setEnabled(da->isEnabled());
        }
    }
}

void MMenuObjectViewPrivate::init()
{
    // create pannable viewport
    viewport = new MPannableViewport(controller);

    // create container widget for pannable viewport
    container = new MWidget;
    viewport->setWidget(container);

    // create layout for controller, make it pannable
    QGraphicsLinearLayout *controllerLayout = new QGraphicsLinearLayout();
    controllerLayout->setContentsMargins(0, 0, 0, 0);
    controllerLayout->addItem(viewport);
    controller->setLayout(controllerLayout);

    // create layout policies for portrait & landscape orientation
    layout = new MLayout(container);
    linearPolicy = new MLinearLayoutPolicy(layout, Qt::Vertical);
    linearPolicy->setContentsMargins(0, 0, 0, 0);
    customPolicy = new MObjectMenuLayoutPolicy(layout);
    customPolicy->setContentsMargins(0, 0, 0, 0);

    controller->installEventFilter(this);
}

bool MMenuObjectViewPrivate::eventFilter(QObject *obj, QEvent *e)
{
    QActionEvent *actionEvent = dynamic_cast<QActionEvent *>(e);

    if (actionEvent) {
        QAction *action = actionEvent->action();
        switch (e->type()) {
        case QEvent::ActionAdded:
            addWidget(action);
            refreshLayout();
            break;
        case QEvent::ActionRemoved:
            removeWidget(action);
            break;
        case QEvent::ActionChanged:
            actionChanged(action);
            break;
        default:
            break;
        }
    }

    return QObject::eventFilter(obj, e);
}


MMenuObjectView::MMenuObjectView(MApplicationMenu *controller) :
    MSceneWindowView(controller),
    d_ptr(new MMenuObjectViewPrivate(controller))
{
    Q_D(MMenuObjectView);
    d->q_ptr = this;
    d->init();
}

MMenuObjectView::MMenuObjectView(MMenuObjectViewPrivate &dd, MApplicationMenu *controller) :
    MSceneWindowView(controller),
    d_ptr(&dd)
{
    Q_D(MMenuObjectView);
    d->q_ptr = this;
    d->init();
}

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

void MMenuObjectView::applyStyle()
{
    Q_D(MMenuObjectView);

    MSceneWindowView::applyStyle();

    d->refreshLayout();

    // Fix item heights, according to style
    QSize preferredSize(size().width() / 2, style()->itemHeight());
    foreach(MWidget * widget, d->widgets) {
        widget->setPreferredSize(preferredSize);
        widget->setMinimumHeight(style()->itemHeight());
        widget->setMaximumHeight(style()->itemHeight());
    }

    // Activate proper policy
    M::Orientation orientation = M::Landscape;
    if (d->controller->sceneManager()) {
        orientation = d->controller->sceneManager()->orientation();
    }
    (orientation == M::Landscape) ?
    d->customPolicy->activate() :
    d->linearPolicy->activate();
}

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

void MMenuObjectView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    MSceneWindowView::drawBackground(painter, option);

    Q_D(const MMenuObjectView);

    if (style()->canvasOpacity() > 0.0) {
        // draw canvas
        painter->setOpacity(d->controller->effectiveOpacity() * style()->canvasOpacity());

        QRectF canvasRect(QPoint(0, 0), size());
        if (style()->canvasImage())
            style()->canvasImage()->draw(canvasRect.toRect(), painter);
        else {
            QColor color = style()->canvasColor();
            painter->fillRect(canvasRect, QBrush(color));
        }
    }
}

M_REGISTER_VIEW_NEW(MMenuObjectView, MApplicationMenu)