summaryrefslogtreecommitdiffstats
path: root/weather/src/contentlist.h
blob: 9ec1268d6643f528840e91de98b4d21413faff70 (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: qt-info@nokia.com
**
** This software, including documentation, is protected by copyright
** controlled by Nokia Corporation.  You may use this software in
** accordance with the terms and conditions contained in the Qt Phone
** Demo License Agreement.
**
****************************************************************************/

#ifndef CONTENTLIST_H
#define CONTENTLIST_H

#include <QList>
#include <QObject>
#include <QGraphicsObject>
#include <QAbstractAnimation>
#include <QPointer>

#define ITEM_TOP_PROPERTY_NAME  "top"

class ContentListItem : public QObject, public QGraphicsItem
{
    Q_OBJECT
    Q_PROPERTY(qreal top READ getTop WRITE setTop);
    Q_INTERFACES(QGraphicsItem);
public:
    ContentListItem(QGraphicsItem *parent = 0);
    virtual qreal contentHeight() const = 0;
    QRectF boundingRect() const;

    virtual QAbstractAnimation *getShowAnimation() = 0;
    virtual QAbstractAnimation *getHideAnimation() = 0;

protected:
    void updateGeometry();
    QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
private:
    friend class ContentList;
    QRectF m_geometry;
    bool m_geometryReady;
    qreal getTop() { return pos().y(); }
    void setTop(qreal top) { setPos(pos().x(), top); }

};

class ContentListActivity;
class RemoveActivity;
class InsertActivity;

class ContentList : public QObject, public QGraphicsItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsItem);
public:
    ContentList(QGraphicsItem *parent = 0);
    ContentList(QList<ContentListItem*> items, QGraphicsItem *parent = 0);
    ~ContentList();

    bool insertItem(int idx, ContentListItem*);
    bool addItem(ContentListItem *item) { return insertItem(m_items.count(), item); }
    bool removeItem(int idx);
    bool removeItem(ContentListItem* item) { return removeItem(getItemIndex(item)); }
    bool moveItem(int from, int to);
    bool moveItem(ContentListItem* item, int to) { return moveItem(getItemIndex(item), to); }
    void appendItems(QList<ContentListItem*> items);

    int itemCount() const { return m_items.count(); }
    ContentListItem* getItem(int idx) { return m_items.at(idx); }
    int getItemIndex(ContentListItem* item) { return m_items.indexOf(item); }

    bool busy() { return m_queue.count() > 0; }
    void addActivity(ContentListActivity *);

    void updateItems();

    QRectF boundingRect() const;
    qreal width() const;
    void setWidth(qreal width);

signals:
    void newContentItem(QGraphicsItem *item);
    void contentItemRemoved(QGraphicsItem *item);

protected:
    virtual QAbstractAnimation *getInsertAnimation(int idx, qreal height) = 0;
    virtual QAbstractAnimation *getRemoveAnimation(int idx) = 0;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

    QList<ContentListItem*> getItems() { return m_items; }

    typedef bool (*ContentListItemCompare)(const ContentListItem* s1, const ContentListItem* s2);
    void sortItems(ContentListItemCompare compare);

private:
    friend class ContentListActivity;
    friend class RemoveActivity;
    friend class InsertActivity;
    friend class SortActivity;
    friend class AppendItemsActivity;
    QList<ContentListItem*> m_items;
    QList<ContentListActivity*> m_queue;

    QRectF m_boundingRect;

    void updateGeometry();

    void checkQueue();
    void activityEnd();
    void doRemoveItem(int idx, bool notify);
    void doInsertItem(int idx, ContentListItem* item, bool notify);
    void doAppendItems(QList<ContentListItem*> items, bool notify);

};

class ContentListActivity : public QObject
{
    Q_OBJECT
public:
    ContentListActivity(ContentList &list) : m_list(list), m_insertPos(0) {}
    virtual bool run() = 0;
    virtual bool active();

protected:
    ContentList &m_list;
    void addActivity(ContentListActivity*);

protected slots:
    void activityEnd() { m_list.activityEnd(); }

private:
    int m_insertPos;

};

class SignalActivity : public ContentListActivity
{
    Q_OBJECT
public:
    SignalActivity(ContentList &list) : ContentListActivity(list) {}
    bool run();

signals:
    void notify();
};

class SortActivity : public ContentListActivity
{
    Q_OBJECT
public:
    SortActivity(ContentList &list, ContentList::ContentListItemCompare compare)
        : ContentListActivity(list), m_compare(compare) {}
    bool run();

private:
    ContentList::ContentListItemCompare m_compare;
};

class AppendItemsActivity : public ContentListActivity
{
    Q_OBJECT
public:
    AppendItemsActivity(ContentList &list, QList<ContentListItem*> items)
        : ContentListActivity(list), m_items(items) {}
    bool run();

private:
    QList<ContentListItem*> m_items;
};

class AnimationActivity : public ContentListActivity
{
    Q_OBJECT
public:
    AnimationActivity(QAbstractAnimation *animation, ContentList &list);
    ~AnimationActivity();
    bool run();
    bool active() { return m_animation && m_animation->state() != QAbstractAnimation::Stopped; }

private slots:
    void animationEnd();

private:
    QAbstractAnimation *m_animation;

};

class RemoveActivity : public ContentListActivity
{
    Q_OBJECT
public:
    RemoveActivity(int idx, bool destroyItem, bool notify, ContentList &list);
    bool run();
    bool active() { return m_active; }

private slots:
    void hideEnd();

private:
    int m_idx;
    bool m_destroyItem;
    ContentListItem *m_item;
    bool m_active;
    const bool m_notify;

};

class InsertActivity : public ContentListActivity
{
    Q_OBJECT
public:
    InsertActivity(int idx, ContentListItem* item, bool notify, ContentList &list);
    bool run();
    bool active() { return m_active; }

private slots:
    void showItem();

private:
    int m_idx;
    QPointer<ContentListItem> m_item;
    bool m_active;
    const bool m_notify;

};

class MoveActivity : public ContentListActivity
{
public:
    MoveActivity(int from, int to, ContentList &list);
    bool run();

private:
     int m_from;
     int m_to;

};

#endif // CONTENTLIST_H