summaryrefslogtreecommitdiffstats
path: root/examples/photoAlbum/main.cpp
blob: 3f47cb53c5dbf7e17f5c893cdf821de4530d5d7e (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
/****************************************************************************
**
** 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 <QtGui>
#include <qgraphicslistview.h>
#include <qgraphicsgridview.h>
#include <qlistdefaultmodel.h>
#include <qlistselectionmanager.h>
#include <qkineticlistcontroller.h>
#include <qpropertyanimation.h>

class Photo : public QtGraphicsListViewItem
{
public:
    Photo(int index, QtGraphicsListView *view);
    QSizeF sizeHint(int index, const QStyleOptionViewItemV4 *option, Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
};

Photo::Photo(int index, QtGraphicsListView *view)
    : QtGraphicsListViewItem(index, view)
{
}

QSizeF Photo::sizeHint(int index, const QStyleOptionViewItemV4 *option, Qt::SizeHint which, const QSizeF &constraint) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    Q_UNUSED(constraint);
    switch (which) {
        case Qt::MinimumSize:
            return QSizeF(1, 1);
        case Qt::PreferredSize:
            return QSizeF(640, 480).boundedTo(constraint);
        case Qt::MaximumSize:
            return constraint;
        default:
            break;
    }
    return QSize();
}

void Photo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    const QPixmap pixmap = qvariant_cast<QPixmap>(data().value(Qt::DecorationRole));
    const QSizeF bounded = pixmap.size().boundedTo(option->rect.size());
    QSizeF scaled = pixmap.size();
    scaled.scale(bounded, Qt::KeepAspectRatio);
    const QRectF aligned = QStyle::alignedRect(option->direction, Qt::AlignCenter, scaled.toSize(), option->rect);
    painter->drawPixmap(aligned, pixmap, pixmap.rect());
}

class Switcher : public QObject
{
    Q_OBJECT
public:
    Switcher(QtGraphicsGridView *grid, QtGraphicsListView *list, QObject *parent = 0)
        : QObject(parent), grid(grid), list(list), photo(0) { animation.setPropertyName("geometry"); }
    ~Switcher() {}

public slots:
    void gridItemClicked(int index) {
        grid->hide();
        list->setHorizontalOffset(index * list->size().width()); // ### ensure visible
        list->show();
        // ### start animation
    }
    void listItemClicked(int index) {
        Q_UNUSED(index);
        list->hide();
        grid->setHorizontalOffset(0);
        grid->show();
        // ### start animation
    }

private:
    QtGraphicsGridView *grid;
    QtGraphicsListView *list;
    Photo *photo;
    QPropertyAnimation animation;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QGraphicsView widget;
    widget.setScene(new QGraphicsScene(&widget));
    widget.resize(QSize(640, 480));
    widget.scene()->setSceneRect(0, 0, 640, 480);

    // data

    QtListDefaultModel *model = new QtListDefaultModel(&widget);

    QtListDefaultItem *item = new QtListDefaultItem;
    item->setData(QPixmap(":images/go.jpg"), Qt::DecorationRole);
    model->appendItem(item);

    item = new QtListDefaultItem;
    item->setData(QPixmap(":images/contrast.jpg"), Qt::DecorationRole);
    model->appendItem(item);

    item = new QtListDefaultItem;
    item->setData(QPixmap(":images/nightfall.jpg"), Qt::DecorationRole);
    model->appendItem(item);

    item = new QtListDefaultItem;
    item->setData(QPixmap(":images/flower.jpg"), Qt::DecorationRole);
    model->appendItem(item);

    item = new QtListDefaultItem;
    item->setData(QPixmap(":images/beach.jpg"), Qt::DecorationRole);
    model->appendItem(item);

    // selections

    QtListSelectionManager *selections = new QtListSelectionManager(&widget);

    // views and controllers

    QtGraphicsListView *listView = new QtGraphicsListView(Qt::Horizontal);
    QtKineticListController *listController = new QtKineticListController(&widget);

    listView->setItemCreator(new QtGraphicsListViewItemCreator<Photo>());
    listView->setGeometry(0, 0, 640, 480);
    listView->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);

    listController->setView(listView);
    listController->setModel(model);
    listController->setSelectionManager(selections);
    listController->setHorizontalOvershootEnabled(true);
    listController->setCenterOnItemEnabled(true);

    QtGraphicsGridView *gridView = new QtGraphicsGridView(4, Qt::Vertical);
    QtKineticListController *gridController = new QtKineticListController(&widget);

    gridView->setItemCreator(new QtGraphicsListViewItemCreator<Photo>());
    gridView->setGeometry(0, 0, 640, 480);
    //gridView->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); // ###
    gridView->hide();

    gridController->setView(gridView);
    gridController->setModel(model);
    gridController->setSelectionManager(selections);
    gridController->setVerticalOvershootEnabled(true);
    
    Switcher switcher(gridView, listView);
    QObject::connect(listController, SIGNAL(itemClicked(int, Qt::MouseButton)),
                     &switcher, SLOT(listItemClicked(int)));
    QObject::connect(gridController, SIGNAL(itemClicked(int, Qt::MouseButton)),
                     &switcher, SLOT(gridItemClicked(int)));

    widget.setBackgroundBrush(Qt::black);
    widget.scene()->addItem(listView);
    widget.scene()->addItem(gridView);
    widget.show();

    return app.exec();
}

#include "main.moc"