WebKit/qt/examples/platformplugin/WebPlugin.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 <QHBoxLayout>
       
    23 #include <QListWidget>
       
    24 #include <QListWidgetItem>
       
    25 #include <QPainter>
       
    26 #include <QtPlugin>
       
    27 #include <QPushButton>
       
    28 #include <QStyledItemDelegate>
       
    29 #include <QVBoxLayout>
       
    30 
       
    31 static const int gMaemoListItemSize = 70;
       
    32 static const int gMaemoListPadding = 38;
       
    33 static const int gMaemoMaxVisibleItems = 5;
       
    34 
       
    35 void Popup::populateList()
       
    36 {
       
    37     QListWidgetItem* listItem;
       
    38     for (int i = 0; i < m_data.itemCount(); ++i) {
       
    39         if (m_data.itemType(i) == QWebSelectData::Option) {
       
    40             listItem = new QListWidgetItem(m_data.itemText(i));
       
    41             m_list->addItem(listItem);
       
    42             listItem->setSelected(m_data.itemIsSelected(i));
       
    43         } else if (m_data.itemType(i) == QWebSelectData::Group) {
       
    44             listItem = new QListWidgetItem(m_data.itemText(i));
       
    45             m_list->addItem(listItem);
       
    46             listItem->setSelected(false);
       
    47             listItem->setFlags(Qt::NoItemFlags);
       
    48         }
       
    49     }
       
    50     connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemSelected(QListWidgetItem*)));
       
    51 }
       
    52 
       
    53 void Popup::onItemSelected(QListWidgetItem* item)
       
    54 {
       
    55     if (item->flags() != Qt::NoItemFlags)
       
    56         emit itemClicked(m_list->row(item));
       
    57 }
       
    58 
       
    59 WebPopup::WebPopup()
       
    60     : m_popup(0)
       
    61 {
       
    62 }
       
    63 
       
    64 WebPopup::~WebPopup()
       
    65 {
       
    66     if (m_popup)
       
    67         m_popup->deleteLater();
       
    68 }
       
    69 
       
    70 Popup* WebPopup::createSingleSelectionPopup(const QWebSelectData& data)
       
    71 {
       
    72     return new SingleSelectionPopup(data);
       
    73 }
       
    74 
       
    75 Popup* WebPopup::createMultipleSelectionPopup(const QWebSelectData& data)
       
    76 {
       
    77     return new MultipleSelectionPopup(data);
       
    78 }
       
    79 
       
    80 Popup* WebPopup::createPopup(const QWebSelectData& data)
       
    81 {
       
    82     Popup* result = data.multiple() ? createMultipleSelectionPopup(data) : createSingleSelectionPopup(data);
       
    83     connect(result, SIGNAL(finished(int)), this, SLOT(popupClosed()));
       
    84     connect(result, SIGNAL(itemClicked(int)), this, SLOT(itemClicked(int)));
       
    85     return result;
       
    86 }
       
    87 
       
    88 void WebPopup::show(const QWebSelectData& data)
       
    89 {
       
    90     if (m_popup)
       
    91         return;
       
    92 
       
    93     m_popup = createPopup(data);
       
    94     m_popup->show();
       
    95 }
       
    96 
       
    97 void WebPopup::hide()
       
    98 {
       
    99     if (!m_popup)
       
   100         return;
       
   101 
       
   102     m_popup->accept();
       
   103 }
       
   104 
       
   105 void WebPopup::popupClosed()
       
   106 {
       
   107     if (!m_popup)
       
   108         return;
       
   109 
       
   110     m_popup->deleteLater();
       
   111     m_popup = 0;
       
   112     emit didHide();
       
   113 }
       
   114 
       
   115 void WebPopup::itemClicked(int idx)
       
   116 {
       
   117     emit selectItem(idx, true, false);
       
   118 }
       
   119 
       
   120 SingleSelectionPopup::SingleSelectionPopup(const QWebSelectData& data)
       
   121     : Popup(data)
       
   122 {
       
   123     const char* title = "select";
       
   124     if (qstrcmp(title, "weba_ti_texlist_single"))
       
   125         setWindowTitle(QString::fromUtf8(title));
       
   126     else
       
   127         setWindowTitle("Select item");
       
   128 
       
   129     QHBoxLayout* hLayout = new QHBoxLayout(this);
       
   130     hLayout->setContentsMargins(0, 0, 0, 0);
       
   131 
       
   132     m_list = new QListWidget(this);
       
   133     populateList();
       
   134 
       
   135     hLayout->addSpacing(gMaemoListPadding);
       
   136     hLayout->addWidget(m_list);
       
   137     hLayout->addSpacing(gMaemoListPadding);
       
   138 
       
   139     connect(m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(accept()));
       
   140 
       
   141     const int visibleItemCount = (m_list->count() > gMaemoMaxVisibleItems) ? gMaemoMaxVisibleItems : m_list->count();
       
   142     resize(size().width(), visibleItemCount * gMaemoListItemSize);
       
   143 }
       
   144 
       
   145 
       
   146 class MultipleItemListDelegate : public QStyledItemDelegate {
       
   147 public:
       
   148     MultipleItemListDelegate(QObject* parent = 0)
       
   149            : QStyledItemDelegate(parent)
       
   150     {
       
   151         tickMark = QIcon::fromTheme("widgets_tickmark_list").pixmap(48, 48);
       
   152     }
       
   153 
       
   154     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
       
   155     {
       
   156         QStyledItemDelegate::paint(painter, option, index);
       
   157 
       
   158         if (option.state & QStyle::State_Selected)
       
   159             painter->drawPixmap(option.rect.width() - tickMark.rect().width(), option.rect.y() + (option.rect.height() / 2 - tickMark.rect().height() / 2), tickMark);
       
   160     }
       
   161 
       
   162 private:
       
   163     QPixmap tickMark;
       
   164 };
       
   165 
       
   166 MultipleSelectionPopup::MultipleSelectionPopup(const QWebSelectData& data)
       
   167     : Popup(data)
       
   168 {
       
   169     const char* title = "select";
       
   170     if (qstrcmp(title, "weba_ti_textlist_multi"))
       
   171         setWindowTitle(QString::fromUtf8(title));
       
   172     else
       
   173         setWindowTitle("Select items");
       
   174 
       
   175     QHBoxLayout* hLayout = new QHBoxLayout(this);
       
   176     hLayout->setContentsMargins(0, 0, 0, 0);
       
   177 
       
   178     m_list = new QListWidget(this);
       
   179     m_list->setSelectionMode(QAbstractItemView::MultiSelection);
       
   180     populateList();
       
   181 
       
   182     MultipleItemListDelegate* delegate = new MultipleItemListDelegate(this);
       
   183     m_list->setItemDelegate(delegate);
       
   184 
       
   185     hLayout->addSpacing(gMaemoListPadding);
       
   186     hLayout->addWidget(m_list);
       
   187 
       
   188     QVBoxLayout* vLayout = new QVBoxLayout();
       
   189 
       
   190     const int visibleItemCount = (m_list->count() > gMaemoMaxVisibleItems) ? gMaemoMaxVisibleItems : m_list->count();
       
   191     vLayout->addSpacing((visibleItemCount - 1) * gMaemoListItemSize);
       
   192 
       
   193     QPushButton* done = new QPushButton(this);
       
   194     title = "done";
       
   195     if (qstrcmp(title, "wdgt_bd_done"))
       
   196         done->setText(QString::fromUtf8(title));
       
   197     else
       
   198         done->setText("Done");
       
   199 
       
   200     done->setMinimumWidth(178);
       
   201     vLayout->addWidget(done);
       
   202 
       
   203     hLayout->addSpacing(8);
       
   204     hLayout->addLayout(vLayout);
       
   205     hLayout->addSpacing(18);
       
   206 
       
   207     connect(done, SIGNAL(clicked()), this, SLOT(accept()));
       
   208     resize(size().width(), visibleItemCount * gMaemoListItemSize);
       
   209 }
       
   210 
       
   211 bool WebPlugin::supportsExtension(Extension extension) const
       
   212 {
       
   213     if (extension == MultipleSelections)
       
   214         return true;
       
   215     if (extension == Notifications)
       
   216 #if ENABLE_NOTIFICATIONS
       
   217         return true;
       
   218 #else
       
   219         return false;
       
   220 #endif
       
   221     return false;
       
   222 }
       
   223 
       
   224 Q_EXPORT_PLUGIN2(platformplugin, WebPlugin)