summaryrefslogtreecommitdiffstats
path: root/src/experimental/qgraphicspathview.cpp
blob: ed1876ccd08e7eb3fda76257cc7e8c2c5fd68bbb (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
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qgraphicspathview.h"
#include "qgraphicslistview_p.h"

#include "qlistselectionmanager.h"
#include "qlistmodelinterface.h"

#include <qdebug.h>
#include <qpainter.h>
#include <qstyle.h>
#include <qstyleoption.h>
//#include <qtimeline.h>
#include <qapplication.h>

QT_BEGIN_NAMESPACE

class QtGraphicsPathViewPrivate : public QtGraphicsListViewPrivate
{
    Q_DECLARE_PUBLIC(QtGraphicsPathView)
public:
    QtGraphicsPathViewPrivate();
    void _q_currentChanged(int current, int previous);

    QPainterPath path;
//    QTimeLine timeline;
};

QtGraphicsPathViewPrivate::QtGraphicsPathViewPrivate()
    : QtGraphicsListViewPrivate()
{
    //path.moveTo(50,0);
    //path.lineTo(50,100);
    
    path.moveTo(0,0);
    path.quadTo(QPointF(100, 25), QPointF(50,50));
    path.quadTo(QPointF(0, 75), QPointF(100,100));
}

void QtGraphicsPathViewPrivate::_q_currentChanged(int current, int previous)
{
    Q_UNUSED(current);
    Q_UNUSED(previous);
    /*
    QTimeLine::Direction direction = current > previous ? QTimeLine::Backward : QTimeLine::Forward;
    if (previous == 0 && current == model->count() - 1)
        direction = QTimeLine::Forward;
    if (current == 0 && previous == model->count() - 1)
        direction = QTimeLine::Backward;
    timeline.stop();
    timeline.setDuration(200);
    timeline.setDirection(direction);
    timeline.start();
    */
}

/*!
  \class QtGraphicsPathView
  \brief A path list view
*/

QtGraphicsPathView::QtGraphicsPathView(QGraphicsWidget *parent, Qt::WindowFlags wFlags)
    : QtGraphicsListView(*(new QtGraphicsPathViewPrivate), Qt::Vertical, parent, wFlags)
{
    //Q_D(QtGraphicsPathView);
    //connect(&d->timeline, SIGNAL(valueChanged(qreal)), this, SLOT(setAnimationProgress(qreal)));
    //connect(&d->timeline, SIGNAL(valueChanged(qreal)), this, SLOT(_q_update()));
    //connect(&d->timeline, SIGNAL(finished()), this, SLOT(clearAnimation()));
    //connect(&d->timeline, SIGNAL(finished()), this, SLOT(update()));
}

QtGraphicsPathView::~QtGraphicsPathView()
{
}

QPainterPath QtGraphicsPathView::path() const
{
    Q_D(const QtGraphicsPathView);
    return d->path;
}

void QtGraphicsPathView::setPath(const QPainterPath &path)
{
    Q_D(QtGraphicsPathView);
    d->path = path;
}

int QtGraphicsPathView::maximumFirstIndex() const
{
    return 0;
}

qreal QtGraphicsPathView::maximumHorizontalOffset() const
{
    return 0;
}

qreal QtGraphicsPathView::maximumVerticalOffset() const
{
    return 0;
}

QRectF QtGraphicsPathView::itemGeometry(int index) const
{
    Q_UNUSED(index);
    // ### FIXME: implementation goes here
    return QRectF();
}

int QtGraphicsPathView::itemAt(const QPointF &position) const
{
    Q_UNUSED(position);
    // ### FIXME: implementation goes here
    return -1;
}

void QtGraphicsPathView::doLayout()
{
    Q_D(QtGraphicsPathView);
    QStyleOptionViewItemV4 option;
    initStyleOption(&option);

    // ### ignores offsets
    const QPainterPath path = d->path;
    const int firstItem = 0; // ###
    const int count = model() ? model()->count() - (firstItem * 2) : 0; // ###
    const int current = qBound(firstItem, /*forward ? d->currentItem() - 1:*/ d->currentItem(), count);
    const int n = (current - (count / 2));

    int index = (n < 0 ? n + count : n);
    int counter = firstItem;

    // scale the path
    const QRectF boundingRect = this->boundingRect(); // ###
    const QRectF pathBoundingRect = path.boundingRect();
    qreal pathWidth = pathBoundingRect.width();
    qreal pathHeight = pathBoundingRect.height();
    qreal horizontalFactor = (pathWidth > 0 ? (boundingRect.width() / pathWidth) : 1);
    qreal verticalFactor = (pathHeight > 0 ?  (boundingRect.height() / pathHeight) : 1);

    // animation
    bool running = false;//d->timeline.state() == QTimeLine::Running;
    bool forward = false;//d->timeline.direction() == QTimeLine::Forward;
    qreal progress = /*animationProgress()*/1 - (running && forward ? 1 : 0);

    while (!path.isEmpty() && counter < count) {
        initStyleOption(&option, index);
        qreal c = qreal(counter) + progress;
        QSizeF size = d->itemSizeHint(&option, index);
        //qreal scale = (qreal(count - qAbs(c - (count / 2))) / qreal(count));
        qreal t = qBound(qreal(0), c / qreal(count), qreal(1));
        QPointF pos = path.pointAtPercent(t);
        pos.rx() *= horizontalFactor;
        pos.ry() *= verticalFactor;
        pos += boundingRect.topLeft();

        // ### FIXME: look up and recycle the items
        QtGraphicsListViewItem *item = 0;
        if (counter >= d->items.count()) {
            item = d->creator->create(index, this);
            d->items.append(item);
        } else {
            item = d->items.at(counter);
            item->setIndex(index); // ###
        }
        item->setGeometry(QRectF(pos, size));

        ++index;
        if (index >= count) // loop
            index = firstItem;
        ++counter;
    }
}

void QtGraphicsPathView::setSelectionManager(QtListSelectionManager *selectionManager)
{
    if (QtListSelectionManager *previous = QtGraphicsListView::selectionManager())
        QObject::disconnect(previous, SIGNAL(currentChanged(int,int)), this, SLOT(_q_currentChanged(int,int)));
    QtGraphicsListView::setSelectionManager(selectionManager);
    if (QtListSelectionManager *current = QtGraphicsListView::selectionManager())
        QObject::connect(current, SIGNAL(currentChanged(int,int)), this, SLOT(_q_currentChanged(int,int)));
}

QT_END_NAMESPACE

#include "moc_qgraphicspathview.cpp"