~ubuntu-branches/ubuntu/oneiric/kdeplasma-addons/oneiric

« back to all changes in this revision

Viewing changes to libs/lancelot/layouts/ColumnLayout.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-26 13:35:18 UTC
  • mto: (0.4.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: james.westby@ubuntu.com-20101126133518-remqkgqjafk2a4jc
Tags: upstream-4.5.80
ImportĀ upstreamĀ versionĀ 4.5.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include <QGraphicsScene>
23
23
#include <QGraphicsWidget>
 
24
#include <QPropertyAnimation>
24
25
#include <QList>
 
26
#include <QDebug>
 
27
#include <QPointer>
 
28
 
 
29
#include <Global.h>
 
30
#include <KConfig>
 
31
#include <KConfigGroup>
25
32
 
26
33
#define GOLDEN_SIZE  0.381966011250105  // 1 / (1 + phi); phi = (sqrt(5) + 1) / 2
27
34
 
89
96
public:
90
97
    ColumnLayout * q;
91
98
    QList < QGraphicsWidget * > items;
 
99
    QHash < QGraphicsWidget *, QPointer < QPropertyAnimation > > animators;
 
100
 
92
101
    QGraphicsItem * parentItem;
93
102
    ColumnLayout::ColumnSizer * sizer;
 
103
 
94
104
    int count;
 
105
    bool animate;
95
106
 
96
107
    enum RelayoutType { Clean, Push, Pop, Resize };
97
108
 
98
109
    Private(ColumnLayout * parent)
99
110
        : q(parent), parentItem(NULL),
100
 
          sizer(new GoldenColumnSizer()), count(2) {}
 
111
          sizer(new GoldenColumnSizer()), count(2)
 
112
    {
 
113
        animate = !Global::self()->config("Animation", "disableAnimations", false)
 
114
                && Global::self()->config("Animation", "columnLayoutAnimaiton", true);
 
115
    }
 
116
 
 
117
    QPropertyAnimation * animator(QGraphicsWidget * item, const QByteArray & property)
 
118
    {
 
119
        QPropertyAnimation * result;
 
120
 
 
121
        if (animators.contains(item) && animators[item]) {
 
122
            result = animators[item];
 
123
            result->stop();
 
124
            result->setPropertyName(property);
 
125
 
 
126
        } else {
 
127
            animators[item] = new QPropertyAnimation(item, property);
 
128
            result = animators[item];
 
129
        }
 
130
 
 
131
        return result;
 
132
    }
 
133
 
 
134
    void moveItemTo(QGraphicsWidget * item, QRectF newGeometry)
 
135
    {
 
136
        if (!animate) {
 
137
            item->setGeometry(newGeometry);
 
138
            item->show();
 
139
            item->setOpacity(1);
 
140
            return;
 
141
        }
 
142
 
 
143
        if (newGeometry.width() < 1) {
 
144
            hideItem(item);
 
145
        }
 
146
 
 
147
        if (item->isVisible()) {
 
148
            // Moving the item
 
149
 
 
150
            if (item->geometry().height() == newGeometry.height()) {
 
151
                QPropertyAnimation * animation = animator(item, "geometry");
 
152
                animation->setDuration(300);
 
153
                animation->setStartValue(item->geometry());
 
154
                animation->setEndValue(newGeometry);
 
155
                animation->start();
 
156
 
 
157
            } else {
 
158
                item->setGeometry(newGeometry);
 
159
 
 
160
            }
 
161
 
 
162
        } else {
 
163
            // Showing the item
 
164
 
 
165
            item->setGeometry(newGeometry);
 
166
            item->setOpacity(0);
 
167
            item->show();
 
168
 
 
169
            QPropertyAnimation * animation = animator(item, "opacity");
 
170
            animation->setDuration(300);
 
171
            animation->setStartValue(0);
 
172
            animation->setEndValue(1);
 
173
            animation->start();
 
174
 
 
175
       }
 
176
    }
 
177
 
 
178
    void hideItem(QGraphicsWidget * item)
 
179
    {
 
180
        if (!animate) {
 
181
            item->hide();
 
182
            return;
 
183
        }
 
184
 
 
185
        if (!item->isVisible()) {
 
186
            return;
 
187
        }
 
188
 
 
189
        item->setOpacity(1);
 
190
 
 
191
        QPropertyAnimation * animation = animator(item, "opacity");
 
192
        animation->setDuration(300);
 
193
        animation->setStartValue(1);
 
194
        animation->setEndValue(0);
 
195
        animation->start();
 
196
 
 
197
        //item->connect(animation, SIGNAL(finished()), SLOT(hide()));
 
198
    }
101
199
 
102
200
    void relayout(RelayoutType type = Clean)
103
201
    {
105
203
        if (items.size() == 0) return;
106
204
 
107
205
        int showItems   = qMin(items.size(), count);
108
 
        qreal width     = q->geometry().width();
109
206
        sizer->init(showItems);
110
207
 
111
 
        QRectF newGeometry = q->geometry();
 
208
        qreal left, top, right, bottom;
 
209
        q->getContentsMargins(&left, &top, &right, &bottom);
 
210
 
 
211
        QRectF newGeometry = q->geometry().adjusted(left, top, right, bottom);
 
212
        qreal width = newGeometry.width();
 
213
 
112
214
 
113
215
        int i = 0;
114
216
 
115
217
        foreach (QGraphicsWidget * item, items) {
116
218
            if (items.size() - showItems > i++) {
117
 
                item->setVisible(false);
 
219
                hideItem(item);
 
220
 
118
221
            } else {
119
 
                qreal itemWidth = sizer->size() * width;
120
 
                if (itemWidth != 0) {
121
 
                    newGeometry.setWidth(itemWidth);
122
 
                    item->setGeometry(newGeometry);
123
 
                    item->show();
124
 
 
125
 
                    newGeometry.moveLeft(newGeometry.left() + itemWidth);
126
 
                } else {
127
 
                    item->setVisible(false);
128
 
                }
 
222
                int itemWidth = sizer->size() * width;
 
223
 
 
224
                newGeometry.setWidth(itemWidth);
 
225
 
 
226
                moveItemTo(item, newGeometry);
 
227
 
 
228
                newGeometry.moveLeft(newGeometry.left() + itemWidth);
129
229
            }
130
230
        }
131
231
    }
145
245
    QGraphicsWidget * pop()
146
246
    {
147
247
        QGraphicsWidget * widget = items.takeLast();
 
248
 
 
249
        animators.remove(widget);
 
250
 
148
251
        relayout(Pop);
149
252
        return widget;
150
253
    }