aboutsummaryrefslogtreecommitdiff
path: root/src/views/mextendingbackgroundview.cpp
blob: b7d4b02abe945e2d320b430123374ccd973eea92 (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
/***************************************************************************
**
** 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 "mextendingbackgroundview.h"
#include "mextendingbackgroundview_p.h"

#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <MWidgetController>
//#include <MSceneManager>
#include <MApplication>
#include <MApplicationWindow>
#include <MScalableImage>
#include <MViewCreator>

MExtendingBackgroundViewPrivate::MExtendingBackgroundViewPrivate() :
    q_ptr(0), controller(0)
{
}

MExtendingBackgroundViewPrivate::~MExtendingBackgroundViewPrivate()
{
}

void MExtendingBackgroundViewPrivate::init(MWidgetController *controller)
{
    this->controller = controller;
}

QRect MExtendingBackgroundViewPrivate::backgroundGeometry() const
{
    Q_Q(const MExtendingBackgroundView);

    // Calculate background size
    int x = 0;
    int y = 0;
    int width = q->geometry().width();
    int height = q->geometry().height();

    // Get the borders of the scalable background image
    int left = 0, right = 0, top = 0, bottom = 0;
    const MScalableImage *bg = q->style()->backgroundImage();
    if (bg != NULL) {
        bg->borders(&left, &right, &top, &bottom);
    }

    // If the background should extend beyond a border recalculate offset and size
    if (q->style()->extendDirection() == "left") {
        x -= q->geometry().x() + left;
        width += q->geometry().x() + left;
    } else if (q->style()->extendDirection() == "right") {
        width = MApplication::activeWindow()->visibleSceneSize().width() - q->geometry().x() + right;
    } else if (q->style()->extendDirection() == "top") {
        y -= q->geometry().y() + top;
        height += q->geometry().y() + top;
    } else if (q->style()->extendDirection() == "bottom") {
        height = MApplication::activeWindow()->visibleSceneSize().height() - q->geometry().y() + bottom;
    }

    return QRect(x, y, width, height);
}


MExtendingBackgroundView::MExtendingBackgroundView(MWidgetController *controller) :
    MWidgetView(controller),
    d_ptr(new MExtendingBackgroundViewPrivate)
{
    Q_D(MExtendingBackgroundView);
    d->q_ptr = this;
    d->init(controller);
}

MExtendingBackgroundView::MExtendingBackgroundView(MExtendingBackgroundViewPrivate &dd, MWidgetController *controller) :
    MWidgetView(controller),
    d_ptr(&dd)
{
    Q_D(MExtendingBackgroundView);
    d->q_ptr = this;
    d->init(controller);
}

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

void MExtendingBackgroundView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *) const
{
    Q_D(const MExtendingBackgroundView);

    const MScalableImage *bg = style()->backgroundImage();
    if (bg != NULL) {
        // Draw the background
        painter->setOpacity(d->controller->effectiveOpacity());
        bg->draw(d->backgroundGeometry(), painter);
    }
}

void MExtendingBackgroundView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    // Don't pass mouse events through
    event->accept();
}

void MExtendingBackgroundView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    // Don't pass mouse events through
    event->accept();
}

QRectF MExtendingBackgroundView::boundingRect() const
{
    Q_D(const MExtendingBackgroundView);

    return d->backgroundGeometry();
}

M_REGISTER_VIEW_NEW(MExtendingBackgroundView, MWidgetController)