summaryrefslogtreecommitdiffstats
path: root/demos/qt3d/photobrowser3d/thumbnailnode.cpp
blob: f80bec8c16477f0b3c9f58f4159e9ec6cd9b264d (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used 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. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 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 the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "thumbnailnode.h"
#include "qglpainter.h"
#include "thumbnaileffect.h"
#include "imagemanager.h"
#include "qatlas.h"
#include "qglrendersequencer.h"
#include "qglpicknode.h"
#include "qlogicalvertex.h"

#include <QMatrix4x4>
#include <QTimer>

ThumbnailNode::ThumbnailNode(QObject *parent)
    : QGLSceneNode(parent)
    , m_thresholdSquared(20.0f * 20.0f)
    , m_defaultMaterial(-1)
    , m_loading(false)
    , m_full(0)
    , m_manager(0)
    , m_lastDistance(ThumbnailNode::Unknown)
{
    setPalette(new QGLMaterialCollection(this));
}

ThumbnailNode::~ThumbnailNode()
{
    delete m_full;
}

void ThumbnailNode::setUrl(const QUrl &url)
{
    m_url = url;
    m_image = ThumbnailableImage();
    m_image.setUrl(m_url);
    QGL::IndexArray inxs = geometry().indices();
    m_image.setIndices(inxs.mid(start(), count()));
}

void ThumbnailNode::setupLoading()
{
    if (!m_loading && !m_url.isEmpty() && m_image.data().isNull())
    {
        m_loading = true;
#ifdef QT_NO_THREADED_FILE_LOAD
        ThumbnailableImage image;
        image.setUrl(m_url);
        QImage im(m_url.toLocalFile());
        if (im.isNull())
            qDebug() << "ThumbnailNode::setupLoading: could not load image:"
                     << m_url.toLocalFile();
        if (im.size().width() > 1024 || im.size().height() > 768)
            im = im.scaled(QSize(1024, 768), Qt::KeepAspectRatio,
                           Qt::SmoothTransformation);
        image.setData(im);
        setImage(image);
#else
        if (m_manager)
            // reconnect the signal we disconnnected in setImage() below
            connect(m_manager, SIGNAL(imageReady(ThumbnailableImage)),
                    this, SLOT(setImage(ThumbnailableImage)));
        emit imageRequired(m_image);
        setMaterialIndex(m_defaultMaterial);
#endif
    }
}

void ThumbnailNode::createFullNode()
{
    m_full = new QGLSceneNode;
    m_full->setPosition(position());
    m_full->setGeometry(geometry());
    m_full->setStart(start());
    m_full->setCount(count());
    m_full->setPalette(palette());
    m_full->setMaterialIndex(m_defaultMaterial);
}

void ThumbnailNode::destroyFullNode()
{
    if (!m_full)
        return;
    QGLMaterial *mat = m_full->material();
    if (m_full->materialIndex() != m_defaultMaterial)
        m_full->palette()->removeMaterial(mat);
    delete m_full;
    m_full = 0;
}

void ThumbnailNode::loadFullImage()
{
    if (!m_full)
        createFullNode();
    Q_CHECK_PTR(m_full);
    // if we have a valid image, and the full node still has the
    // default material, switch to a new material which displays
    // the full size image
    if (!m_image.data().isNull() &&
            m_full->materialIndex() == m_defaultMaterial)
    {
        QGLMaterial *mat = new QGLMaterial;
        QGLTexture2D *tex = new QGLTexture2D;
        tex->setImage(m_image.data());
        mat->setTexture(tex);
        mat->setObjectName(m_image.url().path());
        int ix = palette()->addMaterial(mat);
        m_full->setMaterialIndex(ix);
        mat->setParent(m_full);
    }
}

void ThumbnailNode::drawGeometry(QGLPainter *painter)
{
    QGLSceneNode::drawGeometry(painter);
}

void ThumbnailNode::draw(QGLPainter *painter)
{
    QGLSceneNode *p = qobject_cast<QGLSceneNode*>(parent());
    Q_ASSERT_X(p && p->userEffect() && (!hasEffect()),
               "ThumbnailNode::draw", "Should only inherit parents ThumbnailEffect");

    ThumbnailEffect *effect = static_cast<ThumbnailEffect*>(p->userEffect());
    Q_ASSERT_X(effect && effect->name() == QLatin1String("ThumbnailEffect"),
               "ThumbnailNode::draw", "Can only be drawn with custom ThumbnailEffect");

    if (m_defaultMaterial == -1)
        m_defaultMaterial = materialIndex();

    QMatrix4x4 m = painter->modelViewMatrix().top();
    QVector3D pos = m.map(position());
    qreal magSquared = pos.lengthSquared();

    Distance distance = Unknown;

    if (magSquared > (4.0f * m_thresholdSquared))
        distance = VeryFar;
    else if (magSquared > (2.0f * m_thresholdSquared))
        distance = Far;
    else if (magSquared > m_thresholdSquared)
        distance = Middle;
    else
        distance = Near;

    if (true) // distance != m_lastDistance)
    {
        m_lastDistance = distance;
        m_image.setThumbnailed(m_lastDistance > Near);
        switch (distance)
        {
        case Unknown:
        case Near:
            setupLoading();
            loadFullImage();
            break;
        case Middle:
            setupLoading();
            loadFullImage();
            break;
        case Far:
            setupLoading();
            break;
        case VeryFar:
            destroyFullNode();
            break;
        }
    }

    effect->setThumbnail(m_image.isThumbnailed());
    if (m_image.isThumbnailed() || !m_full)
    {
        QGLSceneNode::draw(painter);
    }
    else
    {
        if (m_image.data().isNull())
            m_full->setMaterialIndex(m_defaultMaterial);
        if (pickNode() && painter->isPicking())
            painter->setObjectPickId(pickNode()->id());
        m_full->draw(painter);
    }

}

void ThumbnailNode::setImage(const ThumbnailableImage &image)
{
    Q_ASSERT(QThread::currentThread() == thread());
    Q_ASSERT(!image.isNull());

    // the manager will be (potentially) loading a number of images, but
    // we only want our one, so just check this is our order
    if (m_url != image.url())
        return;

    // ok we got the right one, stop listening to the manager
    if (sender())
    {
        m_manager = sender();
        m_manager->disconnect(this, SLOT(setImage(ThumbnailableImage)));
    }

    // ok maybe we got what we asked for but in the meantime we decided
    // we did not want it anymore
    if (!m_loading)
        return;

    // the indices we are about to set will index this thumbnail image
    // into the image that its atlas is based on via the texture coords
    // that the atlas is using - those texture coords must be in the
    // same geometry that this node is referencing, so that they will
    // arrive at the vertex shader at the same time - ie they are all
    // matched in the data arrays in the geometry object
    //Q_ASSERT(QAtlas::instance()->geometry() == geometry());

    m_image = image;
    Q_ASSERT(!m_image.data().isNull());

    // configure the placeholder for the actual image size
    // this makes a photo of 1024 x 768 display on approx 3.0 x 2.8 pane
    // add salt to taste
    //QSizeF f = QSizeF(m_image.data().size()) / 600.0f;
    QSizeF f = QSizeF(m_image.data().size());
    f.scale(1.6, 1.2, Qt::KeepAspectRatio);
    QVector3D a(-f.width(), -f.height(), 0.0f);
    QVector3D b(f.width(), -f.height(), 0.0f);
    QVector3D c(f.width(), f.height(), 0.0f);
    QVector3D d(-f.width(), f.height(), 0.0f);
    int k = start();
    Q_ASSERT(count() == 6);
    QGeometryData g = geometry();
    QGL::IndexArray inxs = g.indices();
    g.vertex(inxs.at(k)) = a;
    g.vertex(inxs.at(k+1)) = b;
    g.vertex(inxs.at(k+2)) = c;
    g.vertex(inxs.at(k+5)) = d;

    setMaterialIndex(-1);
    m_loading = false;

    emit nodeChanged();
}