aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/layout/mlayout_p.cpp
blob: dca7ce64b5c2471dda7bb2731f97e0456b180c0f (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
/***************************************************************************
**
** 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 "mlayout_p.h"

#include "mabstractlayoutpolicy.h"
#include "mbasiclayoutanimation.h"
#include "mlayouthelper_p.h"
#include "mapplication.h"

#include "mwidget_p.h"

#include "mdebug.h"

#include <MScene>

MLayoutPrivate::MLayoutPrivate(MLayout *l) :
    q_ptr(l),
    portraitPolicy(NULL),
    landscapePolicy(NULL),
    current_policy(NULL),
    animation(NULL),
    m_orientation(M::Portrait)
{
    Q_ASSERT(0 != q_ptr);
}

void MLayoutPrivate::registerPolicy(MAbstractLayoutPolicy *policy)
{
    Q_Q(MLayout);
    Q_ASSERT(policy);
    if (policies.contains(policy))
        return;
    policies.append(policy);

    if (!current_policy)
        q->setPolicy(policy);
}

void MLayoutPrivate::unregisterPolicy(MAbstractLayoutPolicy *policy)
{
    Q_Q(MLayout);
    Q_ASSERT(policy);
    if (!policies.contains(policy))
        return;

    if (landscapePolicy == policy)
        q->setLandscapePolicy(NULL);
    if (portraitPolicy == policy)
        q->setPortraitPolicy(NULL);

    policies.removeAll(policy);
    if (current_policy == policy) {
        if (policies.isEmpty())
            q->setPolicy(0);
        else
            q->setPolicy(policies.last());
    }
}

void MLayoutPrivate::setItemGeometry(int index, const QRectF &geometry)
{
    Q_Q(MLayout);
    if (0 > index || q->count() <= index) {
        mWarning("MLayout") << Q_FUNC_INFO << "Attempting to set the geometry of the item at index" << index << "when there are" << q->count() << "items";
        return;
    }

    LayoutItem &item = items[index];
    item.geometry = geometry;

    if (animation && item.item->graphicsItem() && item.item->graphicsItem()->isWidget()) {
        animation->setItemGeometry(index, geometry);
    } else {
        // If no animator or the item is not a widget then do not animate
        Q_ASSERT(!item.toBeDeleted); //It should have been deleted immediately in there's no animator or it is a layout
        //Show item
        if(item.item->graphicsItem())
            showItemNow(item.item->graphicsItem());
        item.item->setGeometry(geometry);
    }
}

QRectF MLayoutPrivate::itemGeometry(int index) const
{
    Q_Q(const MLayout);
    if (0 > index || q->count() <= index) {
        mWarning("MLayout") << Q_FUNC_INFO << "Attempting to set the geometry of the item at index" << index << "when there are" << q->count() << "items";
        return QRectF();
    }

    return items.at(index).geometry;
}
void MLayoutPrivate::itemAnimationFinished(int index)
{
    Q_Q(MLayout);
    if (items.at(index).toBeDeleted)
        q->removeAt(index);  //This will delete the item as well
}

void MLayoutPrivate::animationFinished()
{
    Q_Q(MLayout);
    // check whether any item is now really deleted
    for (int i = items.size() - 1; i >= 0; --i) {
        if (items.at(i).toBeDeleted)
            q->removeAt(i);  //This will delete the item as well
    }
}

void MLayoutPrivate::hideItem(int index)
{
    const LayoutItem &item = items.at(index);
    QGraphicsItem *graphicsItem = item.item->graphicsItem();
    if(!graphicsItem)
        return;

    if (animation && graphicsItem->isWidget())
        animation->hideItem(index);
    else // If no animator, or the item is not a widget, or the widget is not visible, then do not animate
        hideItemNow(graphicsItem);
}

void MLayoutPrivate::setOrientation(M::Orientation orientation)
{
    Q_Q(MLayout);
    if(orientation == m_orientation)
        return;
    m_orientation = orientation;
    if (orientation == M::Portrait) {
        if (portraitPolicy)
            q->setPolicy(portraitPolicy);
    } else if (landscapePolicy) {
        q->setPolicy(landscapePolicy);
    }
    //since the rotation has changed, reread the values from the CSS config
    foreach(MAbstractLayoutPolicy * policy, policies) {
        Q_ASSERT(policy);
        policy->updateStyle();
    }
}
QGraphicsItem *MLayoutPrivate::parentItem() const
{
    Q_Q(const MLayout);

    const QGraphicsLayoutItem *parent = q;
    while (parent && parent->isLayout()) {
        parent = parent->parentLayoutItem();
    }
    return parent ? parent->graphicsItem() : 0;
}
M::Orientation MLayoutPrivate::orientation() const {
    return m_orientation;
}
void MLayoutPrivate::recheckOrientation() {
    //We need to check if the orientation has changed.
    QGraphicsItem *parent = parentItem();
    if(parent) {
        QGraphicsWidget *w;
        if(parent->isWidget()) {
            w = static_cast<QGraphicsWidget*>(parent); //We can't assume it is a MWidget
        } else {
            w = parent->parentWidget();
            if(!w)
                return;
        }
        if(w->scene() && !w->scene()->views().isEmpty()) {
            MWindow *window = qobject_cast<MWindow *>(w->scene()->views().at(0));
            if(window) {
                setOrientation(window->orientation());
            }
        }
    }
}
void MLayoutPrivate::showItemNow(QGraphicsItem *graphicsItem) const
{
    if(!graphicsItem)
        return;
    MWidget *widget = dynamic_cast<MWidget *>(graphicsItem);
    if (widget) {
        widget->d_ptr->layoutHidden = false;
        if (!widget->d_ptr->explicitlyHidden)
            graphicsItem->show(); //Show only if item was not set to invisible by the user
    } else
        graphicsItem->show(); //Show always for non-mwidgets
}
void MLayoutPrivate::removeHiddenFlag(QGraphicsItem *graphicsItem) const
{
    MWidget *widget = dynamic_cast<MWidget *>(graphicsItem);
    if (widget)
        widget->d_ptr->layoutHidden = false;
}
void MLayoutPrivate::hideItemNow(QGraphicsItem *graphicsItem) const
{
    graphicsItem->hide();
    MWidget *widget = dynamic_cast<MWidget *>(graphicsItem);
    if (widget)
        widget->d_ptr->layoutHidden = true;
}