[Qt] Transform QtFallbackWebPopupCombo into QtWebComboBox
[WebKit-https.git] / Source / WebKit / qt / WebCoreSupport / QtFallbackWebPopup.cpp
1 /*
2  * Copyright (C) 2010 Girish Ramakrishnan <girish@forwardbias.in>
3  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 #include "config.h"
22 #include "QtFallbackWebPopup.h"
23
24 #ifndef QT_NO_COMBOBOX
25
26 #include "ChromeClientQt.h"
27 #include "QtWebComboBox.h"
28 #include "QWebPageClient.h"
29 #include "qgraphicswebview.h"
30 #include <QGraphicsProxyWidget>
31 #include <QStandardItemModel>
32
33 namespace WebCore {
34
35 QtFallbackWebPopup::QtFallbackWebPopup(const ChromeClientQt* chromeClient)
36     : m_combo(0)
37     , m_chromeClient(chromeClient)
38 {
39 }
40
41 QtFallbackWebPopup::~QtFallbackWebPopup()
42 {
43     deleteComboBox();
44 }
45
46 void QtFallbackWebPopup::show(const QWebSelectData& data)
47 {
48     if (!pageClient())
49         return;
50
51     deleteComboBox();
52
53     m_combo = new QtWebComboBox();
54     connect(m_combo, SIGNAL(activated(int)), SLOT(activeChanged(int)), Qt::QueuedConnection);
55     connect(m_combo, SIGNAL(didHide()), SLOT(deleteComboBox()));
56     connect(m_combo, SIGNAL(didHide()), SIGNAL(didHide()));
57
58     populate(data);
59
60     QRect rect = geometry();
61     if (QGraphicsWebView *webView = qobject_cast<QGraphicsWebView*>(pageClient()->pluginParent())) {
62         QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget(webView);
63         proxy->setWidget(m_combo);
64         proxy->setGeometry(rect);
65     } else {
66         m_combo->setParent(pageClient()->ownerWidget());
67         m_combo->setGeometry(QRect(rect.left(), rect.top(),
68                                rect.width(), m_combo->sizeHint().height()));
69     }
70
71     m_combo->showPopupAtCursorPosition();
72 }
73
74 void QtFallbackWebPopup::hide()
75 {
76     // Destroying the QComboBox here cause problems if the popup is in the
77     // middle of its show animation. Instead we rely on the fact that the
78     // Qt::Popup window will hide itself on mouse events outside its window.
79 }
80
81 void QtFallbackWebPopup::populate(const QWebSelectData& data)
82 {
83     QStandardItemModel* model = qobject_cast<QStandardItemModel*>(m_combo->model());
84     Q_ASSERT(model);
85
86 #if !defined(Q_OS_SYMBIAN)
87     m_combo->setFont(font());
88 #endif
89
90     int currentIndex = -1;
91     for (int i = 0; i < data.itemCount(); ++i) {
92         switch (data.itemType(i)) {
93         case QWebSelectData::Separator:
94             m_combo->insertSeparator(i);
95             break;
96         case QWebSelectData::Group:
97             m_combo->insertItem(i, data.itemText(i));
98             model->item(i)->setEnabled(false);
99             break;
100         case QWebSelectData::Option:
101             m_combo->insertItem(i, data.itemText(i));
102             model->item(i)->setEnabled(data.itemIsEnabled(i));
103 #ifndef QT_NO_TOOLTIP
104             model->item(i)->setToolTip(data.itemToolTip(i));
105 #endif
106             model->item(i)->setBackground(data.itemBackgroundColor(i));
107             model->item(i)->setForeground(data.itemForegroundColor(i));
108             if (data.itemIsSelected(i))
109                 currentIndex = i;
110             break;
111         }
112     }
113
114     if (currentIndex >= 0)
115         m_combo->setCurrentIndex(currentIndex);
116 }
117
118 void QtFallbackWebPopup::activeChanged(int index)
119 {
120     if (index < 0)
121         return;
122
123     emit selectItem(index, false, false);
124 }
125
126 void QtFallbackWebPopup::deleteComboBox()
127 {
128     if (!m_combo)
129         return;
130     m_combo->deleteLater();
131     m_combo = 0;
132 }
133
134 QWebPageClient* QtFallbackWebPopup::pageClient() const
135 {
136     return m_chromeClient->platformPageClient();
137 }
138
139 }
140
141 #endif // QT_NO_COMBOBOX