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