Commit d36cb8b4 authored by Ulf Hermann's avatar Ulf Hermann

QmlProfiler: Remove custom canvas implementation

The canvas integrated in QtQuick does the same thing. We can remove a
lot of code like this.

Change-Id: I6425ae4e1b542107defd9d76fa5755712a0f8613
Reviewed-by: default avatarUlf Hermann <ulf.hermann@digia.com>
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
Reviewed-by: default avatarKai Koehne <kai.koehne@digia.com>
parent 1233eb56
HEADERS += $$PWD/qdeclarativecontext2d_p.h \
$$PWD/qdeclarativecanvas_p.h \
$$PWD/qmlprofilercanvas.h \
$$PWD/qdeclarativecanvastimer_p.h
SOURCES += $$PWD/qdeclarativecontext2d.cpp \
$$PWD/qdeclarativecanvas.cpp \
$$PWD/qmlprofilercanvas.cpp \
$$PWD/qdeclarativecanvastimer.cpp
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qdeclarativecanvas_p.h"
#include "qdeclarativecanvastimer_p.h"
#include "qdeclarativecontext2d_p.h"
#include <qpainter.h>
QT_BEGIN_NAMESPACE
Canvas::Canvas(QQuickPaintedItem *parent)
: QQuickPaintedItem(parent),
m_context(new Context2D(this)),
m_canvasWidth(0),
m_canvasHeight(0),
m_fillMode(Canvas::Stretch),
m_color(Qt::white)
{
}
void Canvas::componentComplete()
{
if (m_canvasWidth == 0 && m_canvasHeight == 0)
m_context->setSize(width(), height());
else
m_context->setSize(m_canvasWidth, m_canvasHeight);
connect(m_context, SIGNAL(changed()), this, SLOT(requestPaint()));
emit init();
QQuickItem::componentComplete();
}
void Canvas::paint(QPainter *painter)
{
m_context->setInPaint(true);
emit paint();
bool oldAA = painter->testRenderHint(QPainter::Antialiasing);
bool oldSmooth = painter->testRenderHint(QPainter::SmoothPixmapTransform);
if (smooth())
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, smooth());
if (m_context->pixmap().isNull()) {
painter->fillRect(0, 0, width(), height(), m_color);
} else if (width() != m_context->pixmap().width() || height() != m_context->pixmap().height()) {
if (m_fillMode>= Tile) {
if (m_fillMode== Tile) {
painter->drawTiledPixmap(QRectF(0,0,width(),height()), m_context->pixmap());
} else {
qreal widthScale = width() / qreal(m_context->pixmap().width());
qreal heightScale = height() / qreal(m_context->pixmap().height());
QTransform scale;
if (m_fillMode== TileVertically) {
scale.scale(widthScale, 1.0);
QTransform old = painter->transform();
painter->setWorldTransform(scale * old);
painter->drawTiledPixmap(QRectF(0,0,m_context->pixmap().width(),height()), m_context->pixmap());
painter->setWorldTransform(old);
} else {
scale.scale(1.0, heightScale);
QTransform old = painter->transform();
painter->setWorldTransform(scale * old);
painter->drawTiledPixmap(QRectF(0,0,width(),m_context->pixmap().height()), m_context->pixmap());
painter->setWorldTransform(old);
}
}
} else {
qreal widthScale = width() / qreal(m_context->pixmap().width());
qreal heightScale = height() / qreal(m_context->pixmap().height());
QTransform scale;
if (m_fillMode== PreserveAspectFit) {
if (widthScale <= heightScale) {
heightScale = widthScale;
scale.translate(0, (height() - heightScale * m_context->pixmap().height()) / 2);
} else if (heightScale < widthScale) {
widthScale = heightScale;
scale.translate((width() - widthScale * m_context->pixmap().width()) / 2, 0);
}
} else if (m_fillMode== PreserveAspectCrop) {
if (widthScale < heightScale) {
widthScale = heightScale;
scale.translate((width() - widthScale * m_context->pixmap().width()) / 2, 0);
} else if (heightScale < widthScale) {
heightScale = widthScale;
scale.translate(0, (height() - heightScale * m_context->pixmap().height()) / 2);
}
}
if (clip()) {
painter->save();
painter->setClipRect(boundingRect(), Qt::IntersectClip);
}
scale.scale(widthScale, heightScale);
QTransform old = painter->transform();
painter->setWorldTransform(scale * old);
painter->drawPixmap(0, 0, m_context->pixmap());
painter->setWorldTransform(old);
if (clip())
painter->restore();
}
} else {
painter->drawPixmap(0, 0, m_context->pixmap());
}
if (smooth()) {
painter->setRenderHint(QPainter::Antialiasing, oldAA);
painter->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
}
m_context->setInPaint(false);
}
Context2D *Canvas::getContext(const QString &contextId)
{
if (contextId == QLatin1String("2d"))
return m_context;
qDebug("Canvas:requesting unsupported context");
return 0;
}
void Canvas::requestPaint()
{
update();
}
void Canvas::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
if (m_canvasWidth == 0 && m_canvasHeight == 0
&& newGeometry.width() > 0 && newGeometry.height() > 0) {
m_context->setSize(width(), height());
}
QQuickItem::geometryChanged(newGeometry, oldGeometry);
}
void Canvas::setCanvasWidth(int newWidth)
{
if (m_canvasWidth != newWidth) {
m_canvasWidth = newWidth;
m_context->setSize(m_canvasWidth, m_canvasHeight);
emit canvasWidthChanged();
}
}
void Canvas::setCanvasHeight(int newHeight)
{
if (m_canvasHeight != newHeight) {
m_canvasHeight = newHeight;
m_context->setSize(m_canvasWidth, m_canvasHeight);
emit canvasHeightChanged();
}
}
void Canvas::setFillMode(FillMode mode)
{
if (m_fillMode == mode)
return;
m_fillMode = mode;
update();
emit fillModeChanged();
}
QColor Canvas::color()
{
return m_color;
}
void Canvas::setColor(const QColor &color)
{
if (m_color !=color) {
m_color = color;
colorChanged();
}
}
Canvas::FillMode Canvas::fillMode() const
{
return m_fillMode;
}
bool Canvas::save(const QString &filename) const
{
return m_context->pixmap().save(filename);
}
CanvasImage *Canvas::toImage() const
{
return new CanvasImage(m_context->pixmap());
}
void Canvas::setTimeout(const QJSValue &handler, long timeout)
{
if (handler.isCallable())
CanvasTimer::createTimer(this, handler, timeout, true);
}
void Canvas::setInterval(const QJSValue &handler, long interval)
{
if (handler.isCallable())
CanvasTimer::createTimer(this, handler, interval, false);
}
void Canvas::clearTimeout(const QJSValue &handler)
{
CanvasTimer::removeTimer(handler);
}
void Canvas::clearInterval(const QJSValue &handler)
{
CanvasTimer::removeTimer(handler);
}
QT_END_NAMESPACE
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef QDECLARATIVECANVAS_P_H
#define QDECLARATIVECANVAS_P_H
#include <QQuickPaintedItem>
#include "qdeclarativecontext2d_p.h"
#include "qdeclarativecanvastimer_p.h"
QT_BEGIN_NAMESPACE
class Canvas : public QQuickPaintedItem
{
Q_OBJECT
Q_ENUMS(FillMode)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged);
Q_PROPERTY(int canvasWidth READ canvasWidth WRITE setCanvasWidth NOTIFY canvasWidthChanged);
Q_PROPERTY(int canvasHeight READ canvasHeight WRITE setCanvasHeight NOTIFY canvasHeightChanged);
Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
public:
Canvas(QQuickPaintedItem *parent = 0);
enum FillMode { Stretch, PreserveAspectFit, PreserveAspectCrop, Tile, TileVertically, TileHorizontally };
void paint(QPainter *);
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
void setCanvasWidth(int newWidth);
int canvasWidth() {return m_canvasWidth;}
void setCanvasHeight(int canvasHeight);
int canvasHeight() {return m_canvasHeight;}
void componentComplete();
public Q_SLOTS:
Context2D *getContext(const QString & = QLatin1String("2d"));
void requestPaint();
FillMode fillMode() const;
void setFillMode(FillMode);
QColor color();
void setColor(const QColor &);
// Save current canvas to disk
bool save(const QString& filename) const;
// Timers
void setInterval(const QJSValue &handler, long timeout);
void setTimeout(const QJSValue &handler, long timeout);
void clearInterval(const QJSValue &handler);
void clearTimeout(const QJSValue &handler);
Q_SIGNALS:
void fillModeChanged();
void canvasWidthChanged();
void canvasHeightChanged();
void colorChanged();
void init();
void paint();
private:
// Return canvas contents as a drawable image
CanvasImage *toImage() const;
Context2D *m_context;
int m_canvasWidth;
int m_canvasHeight;
FillMode m_fillMode;
QColor m_color;
friend class Context2D;
};
QT_END_NAMESPACE
#endif //QDECLARATIVECANVAS_P_H
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qdeclarativecanvastimer_p.h"
#include <QJSEngine>
#include <QJSValue>
#include <qtimer.h>
QT_BEGIN_NAMESPACE
Q_GLOBAL_STATIC(QList<CanvasTimer*> , activeTimers);
CanvasTimer::CanvasTimer(QObject *parent, const QJSValue &data)
: QTimer(parent), m_value(data)
{
}
void CanvasTimer::handleTimeout()
{
Q_ASSERT(m_value.isCallable());
m_value.call();
if (isSingleShot())
removeTimer(this);
}
void CanvasTimer::createTimer(QObject *parent, const QJSValue &val, long timeout, bool singleshot)
{
CanvasTimer *timer = new CanvasTimer(parent, val);
timer->setInterval(timeout);
timer->setSingleShot(singleshot);
connect(timer, SIGNAL(timeout()), timer, SLOT(handleTimeout()));
activeTimers()->append(timer);
timer->start();
}
void CanvasTimer::removeTimer(CanvasTimer *timer)
{
activeTimers()->removeAll(timer);
timer->deleteLater();
}
void CanvasTimer::removeTimer(const QJSValue &val)
{
if (!val.isCallable())
return;
for (int i = 0 ; i < activeTimers()->count() ; ++i) {
CanvasTimer *timer = activeTimers()->at(i);
if (timer->equals(val)) {
removeTimer(timer);
return;
}
}
}
QT_END_NAMESPACE
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef QDECLARATIVECANVASTIMER_P_H
#define QDECLARATIVECANVASTIMER_P_H
#include <QJSValue>
#include <qtimer.h>
#include <qlist.h>
QT_BEGIN_NAMESPACE
class CanvasTimer : public QTimer
{
Q_OBJECT
public:
CanvasTimer(QObject *parent, const QJSValue &data);
public Q_SLOTS:
void handleTimeout();
bool equals(const QJSValue &value){return m_value.equals(value);}
public:
static void createTimer(QObject *parent, const QJSValue &val, long timeout, bool singleshot);
static void removeTimer(CanvasTimer *timer);
static void removeTimer(const QJSValue &);
private:
QJSValue m_value;
};
QT_END_NAMESPACE
#endif // QDECLARATIVECANVASTIMER_P_H
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "qdeclarativecontext2d_p.h"
#include "qdeclarativecanvas_p.h"
#include <qdebug.h>
#include <math.h>
#include <qgraphicsitem.h>
#include <qapplication.h>
#include <qgraphicseffect.h>
#include <QImage>
#include <QWidget>
QT_BEGIN_NAMESPACE
static const double Q_PI = 3.14159265358979323846; // pi
class CustomDropShadowEffect : public QGraphicsDropShadowEffect
{
public:
void draw(QPainter *painter) { QGraphicsDropShadowEffect::draw(painter);}
void drawSource(QPainter *painter) { QGraphicsDropShadowEffect::drawSource(painter);}
};
// Note, this is exported but in a private header as qtopengl depends on it.
// But it really should be considered private API
void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed = 0);
#define DEGREES(t) ((t) * 180.0 / Q_PI)
#define qClamp(val, min, max) qMin(qMax(val, min), max)
static QList<qreal> parseNumbersList(QString::const_iterator &itr)
{
QList<qreal> points;
QString temp;
while ((*itr).isSpace())
++itr;
while ((*itr).isNumber() ||
(*itr) == QLatin1Char('-') || (*itr) == QLatin1Char('+') || (*itr) == QLatin1Char('.')) {
temp.clear();
if ((*itr) == QLatin1Char('-'))
temp += *itr++;
else if ((*itr) == QLatin1Char('+'))
temp += *itr++;
while ((*itr).isDigit())
temp += *itr++;
if ((*itr) == QLatin1Char('.'))
temp += *itr++;
while ((*itr).isDigit())
temp += *itr++;
while ((*itr).isSpace())
++itr;
if ((*itr) == QLatin1Char(','))
++itr;
points.append(temp.toDouble());
//eat spaces
while ((*itr).isSpace())
++itr;
}
return points;
}
QColor colorFromString(const QString &name)
{
QString::const_iterator itr = name.constBegin();
QList<qreal> compo;
if (name.startsWith(QLatin1String("rgba("))) {
++itr; ++itr; ++itr; ++itr; ++itr;
compo = parseNumbersList(itr);
if (compo.size() != 4)
return QColor();
//alpha seems to be always between 0-1
compo[3] *= 255;
return QColor((int)compo[0], (int)compo[1],