summaryrefslogtreecommitdiffstats
path: root/WebKit/qt/WebCoreSupport/QtMaemoWebPopup.cpp
blob: 29a16cb8cde41ad7638948eee96a617887fee75c (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
/*
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */
#include "config.h"
#include "QtMaemoWebPopup.h"

#include <QHBoxLayout>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPainter>
#include <QPushButton>
#include <QStyledItemDelegate>
#include <QVBoxLayout>

#include <libintl.h>


namespace WebCore {

static const int gMaemoListItemSize = 70;
static const int gMaemoListPadding = 38;
static const int gMaemoMaxVisibleItems = 5;

void Maemo5Popup::populateList()
{
    QListWidgetItem* listItem;
    for (int i = 0; i < m_data.itemCount(); ++i) {
        if (m_data.itemType(i) == QtAbstractWebPopup::Option) {
            listItem = new QListWidgetItem(m_data.itemText(i));
            m_list->addItem(listItem);
            listItem->setSelected(m_data.itemIsSelected(i));
        } else if (m_data.itemType(i) == QtAbstractWebPopup::Group) {
            listItem = new QListWidgetItem(m_data.itemText(i));
            m_list->addItem(listItem);
            listItem->setSelected(false);
            listItem->setFlags(Qt::NoItemFlags);
        }
    }
    connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemSelected(QListWidgetItem*)));
}

void Maemo5Popup::onItemSelected(QListWidgetItem* item)
{
    if (item->flags() != Qt::NoItemFlags)
        emit itemClicked(m_list->row(item));
}

QtMaemoWebPopup::QtMaemoWebPopup()
    : QtAbstractWebPopup()
    , m_popup(0)
{
}

QtMaemoWebPopup::~QtMaemoWebPopup()
{
    if (m_popup)
        m_popup->deleteLater();
}

Maemo5Popup* QtMaemoWebPopup::createSingleSelectionPopup()
{
    return new Maemo5SingleSelectionPopup(*this);
}

Maemo5Popup* QtMaemoWebPopup::createMultipleSelectionPopup()
{
    return new Maemo5MultipleSelectionPopup(*this);
}

Maemo5Popup* QtMaemoWebPopup::createPopup()
{
    Maemo5Popup* result = multiple() ? createMultipleSelectionPopup() : createSingleSelectionPopup();
    connect(result, SIGNAL(finished(int)), this, SLOT(popupClosed()));
    connect(result, SIGNAL(itemClicked(int)), this, SLOT(itemClicked(int)));
    return result;
}

void QtMaemoWebPopup::show()
{
    if (!pageClient() || m_popup)
        return;

    m_popup = createPopup();
    m_popup->show();
}

void QtMaemoWebPopup::hide()
{
    if (!m_popup)
        return;

    m_popup->accept();
}

void QtMaemoWebPopup::popupClosed()
{
    if (!m_popup)
        return;

    m_popup->deleteLater();
    m_popup = 0;
    popupDidHide();
}

void QtMaemoWebPopup::itemClicked(int idx)
{
    selectItem(idx, true, false);
}

Maemo5SingleSelectionPopup::Maemo5SingleSelectionPopup(QtAbstractWebPopup& data)
    : Maemo5Popup(data)
{
    // we try to get the standard list title the web browser is using
    const char* title = ::dgettext("osso-browser-ui", "weba_ti_texlist_single");
    if (qstrcmp(title, "weba_ti_texlist_single"))
        setWindowTitle(QString::fromUtf8(title));
    else
        setWindowTitle("Select item");

    QHBoxLayout* hLayout = new QHBoxLayout(this);
    hLayout->setContentsMargins(0, 0, 0, 0);

    m_list = new QListWidget(this);
    populateList();

    hLayout->addSpacing(gMaemoListPadding);
    hLayout->addWidget(m_list);
    hLayout->addSpacing(gMaemoListPadding);

    connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(accept()));

    const int visibleItemCount = (m_list->count() > gMaemoMaxVisibleItems) ? gMaemoMaxVisibleItems : m_list->count();
    resize(size().width(), visibleItemCount * gMaemoListItemSize);
}


class MultipleItemListDelegate : public QStyledItemDelegate {
public:
    MultipleItemListDelegate(QObject* parent = 0)
           : QStyledItemDelegate(parent)
    {
        tickMark = QIcon::fromTheme("widgets_tickmark_list").pixmap(48, 48);
    }

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        QStyledItemDelegate::paint(painter, option, index);

        if (option.state & QStyle::State_Selected)
            painter->drawPixmap(option.rect.width() - tickMark.rect().width(), option.rect.y() + (option.rect.height() / 2 - tickMark.rect().height() / 2), tickMark);
    }

private:
    QPixmap tickMark;
};

Maemo5MultipleSelectionPopup::Maemo5MultipleSelectionPopup(QtAbstractWebPopup& data)
    : Maemo5Popup(data)
{
    // we try to get the standard list title the web browser is using
    const char* title = ::dgettext("osso-browser-ui", "weba_ti_textlist_multi");
    if (qstrcmp(title, "weba_ti_textlist_multi"))
        setWindowTitle(QString::fromUtf8(title));
    else
        setWindowTitle("Select items");

    QHBoxLayout* hLayout = new QHBoxLayout(this);
    hLayout->setContentsMargins(0, 0, 0, 0);

    m_list = new QListWidget(this);
    m_list->setSelectionMode(QAbstractItemView::MultiSelection);
    populateList();

    MultipleItemListDelegate* delegate = new MultipleItemListDelegate(this);
    m_list->setItemDelegate(delegate);

    hLayout->addSpacing(gMaemoListPadding);
    hLayout->addWidget(m_list);

    QVBoxLayout* vLayout = new QVBoxLayout();

    const int visibleItemCount = (m_list->count() > gMaemoMaxVisibleItems) ? gMaemoMaxVisibleItems : m_list->count();
    vLayout->addSpacing((visibleItemCount - 1) * gMaemoListItemSize);

    // we try to get the standard Done button title
    QPushButton* done = new QPushButton(this);
    title = ::dgettext("hildon-libs", "wdgt_bd_done");
    if (qstrcmp(title, "wdgt_bd_done"))
        done->setText(QString::fromUtf8(title));
    else
        done->setText("Done");

    done->setMinimumWidth(178);
    vLayout->addWidget(done);

    hLayout->addSpacing(8);
    hLayout->addLayout(vLayout);
    hLayout->addSpacing(18);

    connect(done, SIGNAL(clicked()), this, SLOT(accept()));
    resize(size().width(), visibleItemCount * gMaemoListItemSize);
}

}