2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
21 #include "WebPlugin.h"
23 #if defined(WTF_USE_QT_MULTIMEDIA) && WTF_USE_QT_MULTIMEDIA
24 #include "HTML5VideoPlugin.h"
29 #include <akndiscreetpopup.h>
31 class ItemListDelegate : public QStyledItemDelegate {
33 ItemListDelegate(QObject* parent = 0)
34 : QStyledItemDelegate(parent)
38 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
40 Q_ASSERT(index.isValid());
42 QStyleOptionViewItemV4 opt = option;
43 opt.state = opt.state & (~QStyle::State_HasFocus);
44 opt.text = index.data().toString();
45 initStyleOption(&opt, index);
47 const QWidget* widget = 0;
48 Popup* selectPopup = qobject_cast<Popup*>(parent());
50 widget = selectPopup->listWidget();
52 QStyle* style = widget ? widget->style() : QApplication::style();
53 style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
57 Popup::Popup(const QWebSelectData& data)
61 m_list = new QListWidget(this);
62 int width = QApplication::desktop()->size().width();
63 QVBoxLayout* vLayout = new QVBoxLayout(this);
64 vLayout->setContentsMargins(width / 20, 5, 5, 5);
66 m_list->setSelectionMode(QAbstractItemView::MultiSelection);
68 vLayout->addWidget(m_list);
70 ItemListDelegate* itemDelegate = new ItemListDelegate(this);
71 m_list->setItemDelegate(itemDelegate);
74 void Popup::resizeEvent(QResizeEvent* e)
76 QDialog::resizeEvent(e);
77 m_list->setGeometry(m_list->geometry().x(), m_list->geometry().y(), m_list->width(), m_list->height());
80 void Popup::populateList()
82 QListWidgetItem* listItem;
83 m_preSelectedIndices.clear();
84 for (int i = 0; i < m_data.itemCount(); ++i) {
85 if (m_data.itemType(i) == QWebSelectData::Option) {
86 listItem = new QListWidgetItem(m_data.itemText(i));
87 m_list->addItem(listItem);
88 listItem->setSelected(m_data.itemIsSelected(i));
89 if (m_data.itemIsSelected(i)) {
90 m_preSelectedIndices.append(i);
91 m_list->setCurrentItem(listItem);
93 } else if (m_data.itemType(i) == QWebSelectData::Group) {
94 listItem = new QListWidgetItem(m_data.itemText(i));
95 m_list->addItem(listItem);
96 listItem->setSelected(false);
97 listItem->setFlags(Qt::NoItemFlags);
100 connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemSelected(QListWidgetItem*)));
103 void Popup::onItemSelected(QListWidgetItem* item)
105 if (item->flags() != Qt::NoItemFlags) {
106 if (!m_data.multiple())
111 void Popup::updateSelectionsBeforeDialogClosing()
113 QList<QListWidgetItem*> selected = m_list->selectedItems();
114 if (m_data.multiple()) {
115 QList<int> selectedIndices;
116 Q_FOREACH(QListWidgetItem* item, selected)
117 selectedIndices.append(m_list->row(item));
119 for (int i = 0; i < m_data.itemCount(); ++i) {
120 if ((m_preSelectedIndices.contains(i) && !selectedIndices.contains(i)) || (!m_preSelectedIndices.contains(i) && selectedIndices.contains(i)))
124 Q_FOREACH(QListWidgetItem* item, selected)
125 emit itemClicked(m_list->row(item));
129 void Popup::updateAndClose()
131 updateSelectionsBeforeDialogClosing();
140 WebPopup::~WebPopup()
143 m_popup->deleteLater();
146 Popup* WebPopup::createSingleSelectionPopup(const QWebSelectData& data)
148 return new SingleSelectionPopup(data);
151 Popup* WebPopup::createMultipleSelectionPopup(const QWebSelectData& data)
153 return new MultipleSelectionPopup(data);
156 Popup* WebPopup::createPopup(const QWebSelectData& data)
158 Popup* result = data.multiple() ? createMultipleSelectionPopup(data) : createSingleSelectionPopup(data);
159 connect(result, SIGNAL(finished(int)), this, SLOT(popupClosed()));
160 connect(result, SIGNAL(itemClicked(int)), this, SLOT(itemClicked(int)));
164 void WebPopup::show(const QWebSelectData& data)
169 m_popup = createPopup(data);
170 m_popup->showMaximized();
173 void WebPopup::hide()
181 void WebPopup::popupClosed()
186 m_popup->deleteLater();
191 void WebPopup::itemClicked(int idx)
193 emit selectItem(idx, true, false);
196 SingleSelectionPopup::SingleSelectionPopup(const QWebSelectData& data)
199 setWindowTitle("Select item");
201 QAction* cancelAction = new QAction("Cancel", this);
202 cancelAction->setSoftKeyRole(QAction::NegativeSoftKey);
203 addAction(cancelAction);
204 connect(cancelAction, SIGNAL(triggered(bool)), this, SLOT(reject()));
207 MultipleSelectionPopup::MultipleSelectionPopup(const QWebSelectData& data)
210 setWindowTitle("Select items");
212 QAction* cancelAction = new QAction("Cancel", this);
213 cancelAction->setSoftKeyRole(QAction::NegativeSoftKey);
214 addAction(cancelAction);
215 connect(cancelAction, SIGNAL(triggered(bool)), this, SLOT(reject()));
217 QAction* okAction = new QAction("Ok", this);
218 okAction->setSoftKeyRole(QAction::PositiveSoftKey);
220 connect(okAction, SIGNAL(triggered(bool)), this, SLOT(updateAndClose()));
223 void WebNotificationPresenter::showNotification(const QWebNotificationData* data)
225 HBufC* title = HBufC::New(data->title().size());
226 *title = TPtrC(data->title().utf16(), data->title().size());
227 HBufC* message = HBufC::New(data->message().size());
228 *message = TPtrC(data->message().utf16(), data->message().size());
229 TRAP_IGNORE(CAknDiscreetPopup::ShowGlobalPopupL(*title, *message, KAknsIIDNone, KNullDesC));
230 emit notificationClosed();
236 bool WebPlugin::supportsExtension(Extension extension) const
238 if (extension == MultipleSelections)
240 if (extension == Notifications)
242 if (extension == FullScreenVideoPlayer)
247 QObject* WebPlugin::createExtension(Extension extension) const
250 case MultipleSelections:
251 return new WebPopup();
253 return new WebNotificationPresenter();
254 #if defined(WTF_USE_QT_MULTIMEDIA) && WTF_USE_QT_MULTIMEDIA
255 case FullScreenVideoPlayer:
256 return new HTML5FullScreenVideoHandler();
263 Q_EXPORT_PLUGIN2(qwebselectim, WebPlugin)