WebColorChooserProxyQt.cpp [plain text]
#include "config.h"
#include "WebColorChooserProxyQt.h"
#include "qquickwebview_p.h"
#include "qquickwebview_p_p.h"
#include <QtQml/QQmlContext>
#include <QtQml/QQmlEngine>
using namespace WebCore;
namespace WebKit {
class ColorChooserContextObject : public QObject {
Q_OBJECT
Q_PROPERTY(QColor currentColor READ currentColor CONSTANT FINAL)
Q_PROPERTY(QRectF elementRect READ elementRect CONSTANT FINAL)
public:
ColorChooserContextObject(const QColor& color, const QRectF& rect)
: m_currentColor(color)
, m_rect(rect)
{
}
QColor currentColor() const { return m_currentColor; }
QRectF elementRect() const { return m_rect; }
Q_INVOKABLE void accept(const QColor& color) { emit accepted(color); }
Q_INVOKABLE void reject() { emit rejected(); }
Q_SIGNALS:
void accepted(const QColor&);
void rejected();
private:
QColor m_currentColor;
QRectF m_rect;
};
WebColorChooserProxyQt::WebColorChooserProxyQt(WebColorChooserProxy::Client* client, QQuickWebView* webView, const Color& initialColor, const IntRect& elementRect)
: WebColorChooserProxy(client)
, m_webView(webView)
{
const QRectF mappedRect= m_webView->mapRectFromWebContent(QRect(elementRect));
ColorChooserContextObject* contextObject = new ColorChooserContextObject(initialColor, mappedRect);
createItem(contextObject);
}
WebColorChooserProxyQt::~WebColorChooserProxyQt()
{
}
void WebColorChooserProxyQt::createItem(QObject* contextObject)
{
QQmlComponent* component = m_webView->experimental()->colorChooser();
if (!component) {
delete contextObject;
return;
}
createContext(component, contextObject);
QObject* object = component->beginCreate(m_context.get());
if (!object) {
m_context.clear();
return;
}
m_colorChooser = adoptPtr(qobject_cast<QQuickItem*>(object));
if (!m_colorChooser) {
m_context.clear();
return;
}
connect(contextObject, SIGNAL(accepted(QColor)), SLOT(notifyColorSelected(QColor)), Qt::QueuedConnection);
connect(contextObject, SIGNAL(rejected()), SLOT(endChooser()), Qt::QueuedConnection);
QQuickWebViewPrivate::get(m_webView)->addAttachedPropertyTo(m_colorChooser.get());
m_colorChooser->setParentItem(m_webView);
component->completeCreate();
}
void WebColorChooserProxyQt::createContext(QQmlComponent* component, QObject* contextObject)
{
QQmlContext* baseContext = component->creationContext();
if (!baseContext)
baseContext = QQmlEngine::contextForObject(m_webView);
m_context = adoptPtr(new QQmlContext(baseContext));
contextObject->setParent(m_context.get());
m_context->setContextProperty(QLatin1String("model"), contextObject);
m_context->setContextObject(contextObject);
}
void WebColorChooserProxyQt::setSelectedColor(const Color&)
{
}
void WebColorChooserProxyQt::notifyColorSelected(const QColor& color)
{
if (!m_client)
return;
Color coreColor = makeRGB(color.red(), color.green(), color.blue());
m_client->didChooseColor(coreColor);
endChooser();
}
void WebColorChooserProxyQt::endChooser()
{
m_colorChooser.clear();
m_context.clear();
if (!m_client)
return;
m_client->didEndColorChooser();
}
}
#include "WebColorChooserProxyQt.moc"
#include "moc_WebColorChooserProxyQt.cpp"