~mutse-young/ubuntu-docviewer-app/trunk

« back to all changes in this revision

Viewing changes to src/plugin/poppler-qml-plugin/verticalview.h

  • Committer: Stefano Verzegnassi
  • Date: 2015-01-30 19:20:39 UTC
  • mto: (63.3.3 30-new-header-style)
  • mto: This revision was merged to the branch mainline in revision 69.
  • Revision ID: stefano92.100@gmail.com-20150130192039-4hxxt4enfv4t3evz
Add VerticalView class in popplerqmlplugin (but not use it for now)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013-2015 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#ifndef VERTICALVIEW_H
 
18
#define VERTICALVIEW_H
 
19
 
 
20
#include <private/qquickitemchangelistener_p.h>
 
21
#include <private/qquickflickable_p.h>
 
22
 
 
23
class QAbstractItemModel;
 
24
class QQmlChangeSet;
 
25
class QQmlDelegateModel;
 
26
 
 
27
 
 
28
/**
 
29
    Note for users of this class
 
30
 
 
31
    VerticalView already loads delegates async when appropiate so if
 
32
    your delegate uses a Loader you should not enable the asynchronous feature since
 
33
    that will need to introduce sizing problems
 
34
 
 
35
    With the double async it may happen what while we are scrolling down
 
36
    we reach to a point where given the size of the just created delegate with loader not yet loaded (which will be very close to 0)
 
37
    we are already "at the end" of the list, but then a few milliseconds later the loader finishes loading and we could
 
38
    have kept scrolling. This is specially visible at the end of the list where you realize
 
39
    that scrolling ended a bit before the end of the list but the speed of the flicking was good
 
40
    to reach the end
 
41
 
 
42
    By not having the second async we get a better sizing when the delegate is created and things work better
 
43
*/
 
44
 
 
45
class VerticalView : public QQuickFlickable, public QQuickItemChangeListener
 
46
{
 
47
    Q_OBJECT
 
48
    Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
 
49
    Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
 
50
    Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
 
51
    Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
 
52
    Q_PROPERTY(int count READ count NOTIFY countChanged)
 
53
    Q_PROPERTY(int currentPageIndex READ currentPageIndex NOTIFY currentPageIndexChanged)
 
54
    Q_PROPERTY(QQuickItem *currentPageItem READ currentPageItem NOTIFY currentPageItemChanged)
 
55
 
 
56
public:
 
57
    VerticalView();
 
58
    ~VerticalView();
 
59
 
 
60
    QAbstractItemModel *model() const;
 
61
    void setModel(QAbstractItemModel *model);
 
62
 
 
63
    QQmlComponent *delegate() const;
 
64
    void setDelegate(QQmlComponent *delegate);
 
65
 
 
66
    int cacheBuffer() const;
 
67
    void setCacheBuffer(int cacheBuffer);
 
68
 
 
69
    qreal spacing() const;
 
70
    void setSpacing(qreal spacing);
 
71
 
 
72
    int count() const;
 
73
 
 
74
    int currentPageIndex() const;
 
75
    QQuickItem *currentPageItem() const;
 
76
 
 
77
    Q_INVOKABLE void positionAtBeginning();
 
78
    Q_INVOKABLE void positionAtIndex(int index);
 
79
    Q_INVOKABLE void positionAtEnd();
 
80
 
 
81
    Q_INVOKABLE QQuickItem *itemAt(int modelIndex) const;
 
82
 
 
83
Q_SIGNALS:
 
84
    void modelChanged();
 
85
    void delegateChanged();
 
86
    void cacheBufferChanged();
 
87
    void spacingChanged();
 
88
    void countChanged();
 
89
    void currentPageIndexChanged();
 
90
    void currentPageItemChanged();
 
91
 
 
92
protected:
 
93
    void componentComplete() override;
 
94
    void viewportMoved(Qt::Orientations orient) override;
 
95
    qreal minYExtent() const override;
 
96
    void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override;
 
97
    void updatePolish() override;
 
98
 
 
99
private Q_SLOTS:
 
100
    void _q_itemCreated(int modelIndex, QObject *object);
 
101
    void _q_heightChanged();
 
102
    void _q_modelUpdated(const QQmlChangeSet &changeSet, bool reset);
 
103
    void _q_updateCurrentPageIndex();
 
104
 
 
105
private:
 
106
    class ListItem
 
107
    {
 
108
        public:
 
109
            qreal height() const;
 
110
 
 
111
            qreal y() const;
 
112
            void setY(qreal newY);
 
113
 
 
114
            bool culled() const;
 
115
            void setCulled(bool culled);
 
116
 
 
117
            QQuickItem *m_item;
 
118
    };
 
119
 
 
120
    void createDelegateModel();
 
121
 
 
122
    void layout();
 
123
    void refill();
 
124
    bool addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous);
 
125
    bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo);
 
126
    ListItem *createItem(int modelIndex, bool asynchronous);
 
127
 
 
128
    void adjustMinYExtent();
 
129
    ListItem *itemAtIndex(int modelIndex) const; // Returns the item at modelIndex if has been created
 
130
    void releaseItem(ListItem *item);
 
131
    void reallyReleaseItem(ListItem *item);
 
132
    void updateWatchedRoles();
 
133
 
 
134
    QQmlDelegateModel *m_delegateModel;
 
135
 
 
136
    // Index we are waiting because we requested it asynchronously
 
137
    int m_asyncRequestedIndex;
 
138
 
 
139
    // Used to only give a warning once if the delegate does not return objects
 
140
    bool m_delegateValidated;
 
141
 
 
142
    // Visible indexes, [0] is m_firstValidIndex, [0+1] is m_firstValidIndex +1, ...
 
143
    QList<ListItem *> m_visibleItems;
 
144
    int m_firstVisibleIndex;
 
145
 
 
146
    int m_currentPageIndex;
 
147
 
 
148
    qreal m_minYExtent;
 
149
 
 
150
    // If any of the heights has changed
 
151
    // or new items have been added/removed
 
152
    bool m_contentHeightDirty;
 
153
 
 
154
    qreal m_previousContentY;
 
155
 
 
156
    bool m_inLayout;
 
157
 
 
158
    int m_cacheBuffer;
 
159
    qreal m_spacing;
 
160
 
 
161
    // Qt 5.0 doesn't like releasing the items just after itemCreated
 
162
    // so we delay the releasing until the next updatePolish
 
163
    QList<ListItem *> m_itemsToRelease;
 
164
};
 
165
 
 
166
 
 
167
#endif // VERTICALVIEW_H