1/* 2 * Copyright (C) 2012 Intel Corporation. All rights reserved. 3 * Copyright (C) 2012 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 22#include "config.h" 23#include "WebColorChooserProxyQt.h" 24 25#include "qquickwebview_p.h" 26#include "qquickwebview_p_p.h" 27#include <QtQml/QQmlContext> 28#include <QtQml/QQmlEngine> 29 30using namespace WebCore; 31 32namespace WebKit { 33 34class ColorChooserContextObject : public QObject { 35 Q_OBJECT 36 Q_PROPERTY(QColor currentColor READ currentColor CONSTANT FINAL) 37 Q_PROPERTY(QRectF elementRect READ elementRect CONSTANT FINAL) 38 39public: 40 ColorChooserContextObject(const QColor& color, const QRectF& rect) 41 : m_currentColor(color) 42 , m_rect(rect) 43 { 44 } 45 46 QColor currentColor() const { return m_currentColor; } 47 QRectF elementRect() const { return m_rect; } 48 49 Q_INVOKABLE void accept(const QColor& color) { emit accepted(color); } 50 Q_INVOKABLE void reject() { emit rejected(); } 51 52Q_SIGNALS: 53 void accepted(const QColor&); 54 void rejected(); 55 56private: 57 QColor m_currentColor; 58 QRectF m_rect; 59}; 60 61WebColorChooserProxyQt::WebColorChooserProxyQt(WebColorChooserProxy::Client* client, QQuickWebView* webView, const Color& initialColor, const IntRect& elementRect) 62 : WebColorChooserProxy(client) 63 , m_webView(webView) 64{ 65 const QRectF mappedRect= m_webView->mapRectFromWebContent(QRect(elementRect)); 66 ColorChooserContextObject* contextObject = new ColorChooserContextObject(initialColor, mappedRect); 67 createItem(contextObject); 68} 69 70WebColorChooserProxyQt::~WebColorChooserProxyQt() 71{ 72} 73 74void WebColorChooserProxyQt::createItem(QObject* contextObject) 75{ 76 QQmlComponent* component = m_webView->experimental()->colorChooser(); 77 if (!component) { 78 delete contextObject; 79 return; 80 } 81 82 createContext(component, contextObject); 83 QObject* object = component->beginCreate(m_context.get()); 84 if (!object) { 85 m_context.clear(); 86 return; 87 } 88 89 m_colorChooser = adoptPtr(qobject_cast<QQuickItem*>(object)); 90 if (!m_colorChooser) { 91 m_context.clear(); 92 return; 93 } 94 95 // Needs to be enqueue because it might trigger deletion. 96 connect(contextObject, SIGNAL(accepted(QColor)), SLOT(notifyColorSelected(QColor)), Qt::QueuedConnection); 97 connect(contextObject, SIGNAL(rejected()), SLOT(endChooser()), Qt::QueuedConnection); 98 99 QQuickWebViewPrivate::get(m_webView)->addAttachedPropertyTo(m_colorChooser.get()); 100 m_colorChooser->setParentItem(m_webView); 101 102 component->completeCreate(); 103} 104 105void WebColorChooserProxyQt::createContext(QQmlComponent* component, QObject* contextObject) 106{ 107 QQmlContext* baseContext = component->creationContext(); 108 if (!baseContext) 109 baseContext = QQmlEngine::contextForObject(m_webView); 110 m_context = adoptPtr(new QQmlContext(baseContext)); 111 112 contextObject->setParent(m_context.get()); 113 m_context->setContextProperty(QLatin1String("model"), contextObject); 114 m_context->setContextObject(contextObject); 115} 116 117void WebColorChooserProxyQt::setSelectedColor(const Color&) 118{ 119 // This is suppose to be used to react to DOM changes. When 120 // a user script changes the input value, the method gives the 121 // option to update the color chooser UI if we were showing the 122 // current value. Since we don't, it is irrelevant right now. 123 // And yes, the name sounds misleading but comes from WebCore. 124} 125 126void WebColorChooserProxyQt::notifyColorSelected(const QColor& color) 127{ 128 if (!m_client) 129 return; 130 131 // Alpha is always ignored by the color input 132 Color coreColor = makeRGB(color.red(), color.green(), color.blue()); 133 m_client->didChooseColor(coreColor); 134 135 endChooser(); 136} 137 138void WebColorChooserProxyQt::endChooser() 139{ 140 m_colorChooser.clear(); 141 m_context.clear(); 142 143 if (!m_client) 144 return; 145 146 m_client->didEndColorChooser(); 147} 148 149} // namespace WebKit 150 151#include "WebColorChooserProxyQt.moc" 152#include "moc_WebColorChooserProxyQt.cpp" 153