aboutsummaryrefslogtreecommitdiff
path: root/demos/widgetsgallery/spinnerpage.cpp
blob: 8fddf6d71f1bf994a2de307969ca2e07838b120d (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
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libdui.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it 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.
**
****************************************************************************/

#include "spinnerpage.h"
#include "utils.h"
#include <QTimer>
#include <DuiButton>
#include <DuiLabel>
#include <DuiTheme>
#include <DuiLocale>
#include <DuiAction>
#include <DuiLinearLayoutPolicy>
#include <DuiGridLayoutPolicy>
#include <DuiFlowLayoutPolicy>
#include <DuiProgressIndicator>
#include <DuiDialog>
#include <DuiContainer>
#include <QGraphicsLinearLayout>
#include <QStringListModel>
#include <DuiImageWidget>
#include <QDir>
#include <QDebug>

static const int ImageSize = 64;

SpinnerPage::SpinnerPage() :
    TemplatePage(),
    container(NULL),
    header(NULL),
    description(NULL),
    view(Unknown)
{
    gid = TemplatePage::IndicatorsAndNotifications;
}

SpinnerPage::~SpinnerPage()
{
}

QString SpinnerPage::timedemoTitle()
{
    return "Spinner";
}

void SpinnerPage::createContent()
{
    TemplatePage::createContent();

    //% "In container main area"
    DuiAction *action = new DuiAction(qtTrId("xx_spinner_page_container_main_area"), this);
    action->setLocation(DuiAction::ApplicationMenuLocation);
    connect(action, SIGNAL(triggered()), this, SLOT(inContainerMainArea()));
    addAction(action);

    //% "In container header"
    action = new DuiAction(qtTrId("xx_spinner_page_container_header"), this);
    action->setLocation(DuiAction::ApplicationMenuLocation);
    connect(action, SIGNAL(triggered()), this, SLOT(inContainerHeader()));
    addAction(action);

    //% "In view menu"
    action = new DuiAction(qtTrId("xx_spinner_page_menu"), this);
    action->setLocation(DuiAction::ApplicationMenuLocation);
    connect(action, SIGNAL(triggered()), this, SLOT(inViewmenu()));
    addAction(action);

    //% "In dialog"
    action = new DuiAction(qtTrId("xx_spinner_page_dialog"), this);
    action->setLocation(DuiAction::ApplicationMenuLocation);
    connect(action, SIGNAL(triggered()), this, SLOT(inDialog()));
    addAction(action);

    inContainerMainArea();

    retranslateUi();
}

void SpinnerPage::retranslateUi()
{
    //% "Spinner"
    setTitle(qtTrId("xx_spinner_page_title"));
    if (!isContentCreated())
        return;

    //% "Spinner is used to indicate that a progress is ongoing "
    //% "and the duration is unknown.\n\n"
    //% "Spinners can also be used in situations when duration is known, "
    //% "but available space on screen does not permit using a Progress bar.\n\n"
    //% "See the menu for examples of different use cases."
    infoLabel->setText("<a></a>" + qtTrId("xx_spinner_page_info_label"));
}

void SpinnerPage::inContainerMainArea()
{
    reset();
    view = ContainerMainArea;

    header = new DuiLabel(centralWidget());
    //% "From: John Doe\nDate: Today\nSubject: This is funny stuff!"
    header->setText("<a></a>" + qtTrId("xx_spinner_page_email_header"));
    containerPolicy->addItem(header);

    container = new DuiContainer(centralWidget());
    container->setHeaderVisible(false);
    containerPolicy->addItem(container);

    DuiProgressIndicator *spinner = new DuiProgressIndicator(container, DuiProgressIndicator::spinnerType);
    spinner->setUnknownDuration(true);
    container->setCentralWidget(spinner);

    description = new DuiLabel(centralWidget());
    //% "Spinner can be used in container while e.g. content is being fetched."
    description->setText("<a></a>" + qtTrId("xx_spinner_page_container_area_description"));
    description->setWordWrap(true);
    containerPolicy->addItem(description);
}

void SpinnerPage::inContainerHeader()
{
    reset();
    view = ContainerHeader;

    container = new DuiContainer(centralWidget());
    container->setProgressIndicatorVisible(true);
    //% "Online albums"
    container->setTitle(qtTrId("xx_spinner_page_container_title"));
    containerPolicy->addItem(container);

    DuiLayout *layout = new DuiLayout(container->centralWidget());
    imageContainerPolicy = new DuiFlowLayoutPolicy(layout);

    description = new DuiLabel(centralWidget());
    //% "Spinner can be used in container header to indicate that the items inside the container "
    //% "are being updated, but visible items can be interacted with."
    description->setText("<a></a>" + qtTrId("xx_spinner_page_container_header_description"));
    description->setWordWrap(true);
    containerPolicy->addItem(description);

    connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
    timer.setSingleShot(true);
    timer.start(2000);
}

void SpinnerPage::timeout()
{
    Q_ASSERT(imageContainerPolicy != NULL);

    if (imageContainerPolicy) {
        QString contactsDir = Utils::contactsDir();
        QDir imagesDir(contactsDir);
        imagesDir.setNameFilters(QStringList() << "*.png");

        QStringList imageContacts = imagesDir.entryList(QDir::Files);

        DuiImageWidget *image = new DuiImageWidget(container->centralWidget());
        image->setPixmap(QPixmap(contactsDir + QDir::separator() + imageContacts[imageContainerPolicy->count() % imageContacts.size()]));
        image->setMinimumSize(ImageSize, ImageSize);
        image->setMaximumSize(ImageSize, ImageSize);
        imageContainerPolicy->addItem(image);

        if (imageContainerPolicy->count() < 12) {
            timer.setSingleShot(true);
            timer.start(2000);
        }
    }
}

void SpinnerPage::inViewmenu()
{
    reset();
    view = Menu;

    description = new DuiLabel(centralWidget());
    //% "Spinner in the application menu area indicates that the whole view is pending for update."
    description->setText("<a></a>" + qtTrId("xx_spinner_page_application_menu_description"));
    description->setWordWrap(true);
    containerPolicy->addItem(description);
    setProgressIndicatorVisible(true);

}

void SpinnerPage::inDialog()
{
    reset();
    view = Dialog;

    description = new DuiLabel(centralWidget());
    //% "Spinner can be placed in dialog header to indicate changing content."
    description->setText("<a></a>" + qtTrId("xx_spinner_page_dialog_header_description"));
    description->setWordWrap(true);
    containerPolicy->addItem(description);

    launchDialog();
}

void SpinnerPage::launchDialog()
{
    DuiWidget *container = new DuiWidget();
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
    container->setLayout(layout);

    DuiButton *wifi = new DuiButton("Free Wifi", container);
    layout->addItem(wifi);
    DuiButton *wlan = new DuiButton("Public WLAN", container);
    layout->addItem(wlan);
    DuiButton *starbucks = new DuiButton("Starbucks", container);
    layout->addItem(starbucks);

    //% "Select Internet connection"
    DuiDialog dialog(qtTrId("xx_spinner_page_dialog_header"),
                     Dui::NoStandardButton);
    dialog.setCentralWidget(container);
    dialog.setProgressIndicatorVisible(true);
    dialog.exec();
}

void SpinnerPage::reset()
{
    switch (view) {
    case Unknown:
        break;

    case ContainerMainArea: {
        containerPolicy->removeItem(header);
        containerPolicy->removeItem(container);
        containerPolicy->removeItem(description);
        delete container;
        delete description;
        delete header;
        container = NULL;
        description = NULL;
        header = NULL;
    } break;

    case ContainerHeader: {
        disconnect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
        containerPolicy->removeItem(container);
        containerPolicy->removeItem(description);
        delete container;
        delete description;
        container = NULL;
        description = NULL;
    } break;

    case Menu: {
        setProgressIndicatorVisible(false);
        containerPolicy->removeItem(description);
        delete description;
        description = NULL;
    } break;

    case Dialog: {
        containerPolicy->removeItem(description);
        delete description;
        description = NULL;
    } break;
    }

    imageContainerPolicy = NULL;
    view = Unknown;
}