initial import
[vuplus_webkit] / Source / WebKit / qt / examples / platformplugin / WebPlugin.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 #include "WebPlugin.h"
21
22 #include <QEvent>
23 #include <QHBoxLayout>
24 #include <QKeyEvent>
25 #include <QListWidget>
26 #include <QListWidgetItem>
27 #include <QPainter>
28 #include <QtPlugin>
29 #include <QPushButton>
30 #include <QStyledItemDelegate>
31 #include <QVBoxLayout>
32
33 static const int gMaemoListItemSize = 70;
34 static const int gMaemoListPadding = 38;
35 static const int gMaemoMaxVisibleItems = 5;
36
37 void Popup::populateList()
38 {
39     QListWidgetItem* listItem;
40     for (int i = 0; i < m_data.itemCount(); ++i) {
41         if (m_data.itemType(i) == QWebSelectData::Option) {
42             listItem = new QListWidgetItem(m_data.itemText(i));
43             m_list->addItem(listItem);
44             listItem->setSelected(m_data.itemIsSelected(i));
45         } else if (m_data.itemType(i) == QWebSelectData::Group) {
46             listItem = new QListWidgetItem(m_data.itemText(i));
47             m_list->addItem(listItem);
48             listItem->setSelected(false);
49             listItem->setFlags(Qt::NoItemFlags);
50         }
51     }
52     connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemSelected(QListWidgetItem*)));
53 }
54
55 void Popup::onItemSelected(QListWidgetItem* item)
56 {
57     if (item->flags() != Qt::NoItemFlags)
58         emit itemClicked(m_list->row(item));
59 }
60
61 WebPopup::WebPopup()
62     : m_popup(0)
63 {
64 }
65
66 WebPopup::~WebPopup()
67 {
68     if (m_popup)
69         m_popup->deleteLater();
70 }
71
72 Popup* WebPopup::createSingleSelectionPopup(const QWebSelectData& data)
73 {
74     return new SingleSelectionPopup(data);
75 }
76
77 Popup* WebPopup::createMultipleSelectionPopup(const QWebSelectData& data)
78 {
79     return new MultipleSelectionPopup(data);
80 }
81
82 Popup* WebPopup::createPopup(const QWebSelectData& data)
83 {
84     Popup* result = data.multiple() ? createMultipleSelectionPopup(data) : createSingleSelectionPopup(data);
85     connect(result, SIGNAL(finished(int)), this, SLOT(popupClosed()));
86     connect(result, SIGNAL(itemClicked(int)), this, SLOT(itemClicked(int)));
87     return result;
88 }
89
90 void WebPopup::show(const QWebSelectData& data)
91 {
92     if (m_popup)
93         return;
94
95     m_popup = createPopup(data);
96     m_popup->show();
97 }
98
99 void WebPopup::hide()
100 {
101     if (!m_popup)
102         return;
103
104     m_popup->accept();
105 }
106
107 void WebPopup::popupClosed()
108 {
109     if (!m_popup)
110         return;
111
112     m_popup->deleteLater();
113     m_popup = 0;
114     emit didHide();
115 }
116
117 void WebPopup::itemClicked(int idx)
118 {
119     emit selectItem(idx, true, false);
120 }
121
122 SingleSelectionPopup::SingleSelectionPopup(const QWebSelectData& data)
123     : Popup(data)
124 {
125     const char* title = "select";
126     if (qstrcmp(title, "weba_ti_texlist_single"))
127         setWindowTitle(QString::fromUtf8(title));
128     else
129         setWindowTitle("Select item");
130
131     QHBoxLayout* hLayout = new QHBoxLayout(this);
132     hLayout->setContentsMargins(0, 0, 0, 0);
133
134     m_list = new QListWidget(this);
135     populateList();
136
137     hLayout->addSpacing(gMaemoListPadding);
138     hLayout->addWidget(m_list);
139     hLayout->addSpacing(gMaemoListPadding);
140
141     connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(accept()));
142
143     const int visibleItemCount = (m_list->count() > gMaemoMaxVisibleItems) ? gMaemoMaxVisibleItems : m_list->count();
144     resize(size().width(), visibleItemCount * gMaemoListItemSize);
145 }
146
147
148 class MultipleItemListDelegate : public QStyledItemDelegate {
149 public:
150     MultipleItemListDelegate(QObject* parent = 0)
151            : QStyledItemDelegate(parent)
152     {
153         tickMark = QIcon::fromTheme("widgets_tickmark_list").pixmap(48, 48);
154     }
155
156     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
157     {
158         QStyledItemDelegate::paint(painter, option, index);
159
160         if (option.state & QStyle::State_Selected)
161             painter->drawPixmap(option.rect.width() - tickMark.rect().width(), option.rect.y() + (option.rect.height() / 2 - tickMark.rect().height() / 2), tickMark);
162     }
163
164 private:
165     QPixmap tickMark;
166 };
167
168 MultipleSelectionPopup::MultipleSelectionPopup(const QWebSelectData& data)
169     : Popup(data)
170 {
171     const char* title = "select";
172     if (qstrcmp(title, "weba_ti_textlist_multi"))
173         setWindowTitle(QString::fromUtf8(title));
174     else
175         setWindowTitle("Select items");
176
177     QHBoxLayout* hLayout = new QHBoxLayout(this);
178     hLayout->setContentsMargins(0, 0, 0, 0);
179
180     m_list = new QListWidget(this);
181     m_list->setSelectionMode(QAbstractItemView::MultiSelection);
182     populateList();
183
184     MultipleItemListDelegate* delegate = new MultipleItemListDelegate(this);
185     m_list->setItemDelegate(delegate);
186
187     hLayout->addSpacing(gMaemoListPadding);
188     hLayout->addWidget(m_list);
189
190     QVBoxLayout* vLayout = new QVBoxLayout();
191
192     const int visibleItemCount = (m_list->count() > gMaemoMaxVisibleItems) ? gMaemoMaxVisibleItems : m_list->count();
193     vLayout->addSpacing((visibleItemCount - 1) * gMaemoListItemSize);
194
195     QPushButton* done = new QPushButton(this);
196     title = "done";
197     if (qstrcmp(title, "wdgt_bd_done"))
198         done->setText(QString::fromUtf8(title));
199     else
200         done->setText("Done");
201
202     done->setMinimumWidth(178);
203     vLayout->addWidget(done);
204
205     hLayout->addSpacing(8);
206     hLayout->addLayout(vLayout);
207     hLayout->addSpacing(18);
208
209     connect(done, SIGNAL(clicked()), this, SLOT(accept()));
210     resize(size().width(), visibleItemCount * gMaemoListItemSize);
211 }
212
213 #if defined(WTF_USE_QT_MULTIMEDIA) && WTF_USE_QT_MULTIMEDIA
214 FullScreenVideoWidget::FullScreenVideoWidget(QMediaPlayer* player)
215     : QVideoWidget()
216     , m_mediaPlayer(player)
217 {
218     Q_ASSERT(m_mediaPlayer);
219
220     setFullScreen(true);
221     m_mediaPlayer->setVideoOutput(this);
222 }
223
224 bool FullScreenVideoWidget::event(QEvent* ev)
225 {
226     if (ev->type() ==  QEvent::MouseButtonDblClick) {
227         emit fullScreenClosed();
228         ev->accept();
229         return true;
230     } 
231     return QWidget::event(ev);
232 }
233
234 void FullScreenVideoWidget::keyPressEvent(QKeyEvent* ev)
235 {
236     if (ev->key() == Qt::Key_Space) {
237         if (m_mediaPlayer->state() == QMediaPlayer::PlayingState)
238             m_mediaPlayer->pause();
239         else
240             m_mediaPlayer->play();
241         ev->accept();
242         return;
243     }
244 }
245
246 FullScreenVideoHandler::FullScreenVideoHandler()
247     : m_mediaWidget(0)
248 {
249 }
250
251 FullScreenVideoHandler::~FullScreenVideoHandler()
252 {
253     delete m_mediaWidget;
254 }
255
256 bool FullScreenVideoHandler::requiresFullScreenForVideoPlayback() const
257 {
258     return true;
259 }
260
261 void FullScreenVideoHandler::enterFullScreen(QMediaPlayer* player)
262 {
263     Q_ASSERT(player);
264
265     m_mediaWidget = new FullScreenVideoWidget(player);
266     connect(m_mediaWidget, SIGNAL(fullScreenClosed()), this, SIGNAL(fullScreenClosed()));
267     m_mediaWidget->showFullScreen();
268 }
269
270 void FullScreenVideoHandler::exitFullScreen()
271 {
272     m_mediaWidget->hide();
273     delete m_mediaWidget;
274     m_mediaWidget = 0;
275 }
276 #endif
277
278 bool WebPlugin::supportsExtension(Extension extension) const
279 {
280     switch (extension) {
281     case MultipleSelections:
282         return true;
283 #if ENABLE_NOTIFICATIONS
284     case Notifications:
285         return true;
286 #endif
287     case TouchInteraction:
288         return true;
289 #if defined(WTF_USE_QT_MULTIMEDIA) && WTF_USE_QT_MULTIMEDIA
290     case FullScreenVideoPlayer:
291         return true;
292 #endif
293     default:
294         return false;
295     }
296 }
297
298 QObject* WebPlugin::createExtension(Extension extension) const
299 {
300     switch (extension) {
301     case MultipleSelections:
302         return new WebPopup();
303 #if ENABLE_NOTIFICATIONS
304     case Notifications:
305         return new WebNotificationPresenter();
306 #endif
307     case TouchInteraction:
308         return new TouchModifier();
309 #if defined(WTF_USE_QT_MULTIMEDIA) && WTF_USE_QT_MULTIMEDIA
310     case FullScreenVideoPlayer:
311         return new FullScreenVideoHandler();
312 #endif
313     default:
314         return 0;
315     }
316 }
317
318 Q_EXPORT_PLUGIN2(platformplugin, WebPlugin)