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 "qmlprofilercanvas.h"
#include "qdeclarativecontext2d_p.h"
#include <qpixmap.h>
#include <qpainter.h>
namespace QmlProfiler {
namespace Internal {
QmlProfilerCanvas::QmlProfilerCanvas()
: m_context2d(new Context2D(this))
{
setAcceptedMouseButtons(Qt::LeftButton);
m_drawTimer.setSingleShot(true);
connect(&m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
m_drawTimer.start();
}
void QmlProfilerCanvas::requestPaint()
{
if (m_context2d->size().width() != width()
|| m_context2d->size().height() != height()) {
m_drawTimer.start();
} else {
update();
}
}
void QmlProfilerCanvas::requestRedraw()
{
m_drawTimer.start();
}
// called from GUI thread. Draws into m_context2d.
void QmlProfilerCanvas::draw()
{
QMutexLocker lock(&m_pixmapMutex);
m_context2d->reset();
m_context2d->setSize(width(), height());
if (width() > 0 && height() > 0)
emit drawRegion(m_context2d, QRect(0, 0, width(), height()));
update();
}
// called from OpenGL thread. Renders m_context2d into OpenGL buffer.
void QmlProfilerCanvas::paint(QPainter *p)
{
QMutexLocker lock(&m_pixmapMutex);
p->drawPixmap(0, 0, m_context2d->pixmap());
}
void QmlProfilerCanvas::componentComplete()
{
const QMetaObject *metaObject = this->metaObject();
int propertyCount = metaObject->propertyCount();
int requestPaintMethod = metaObject->indexOfMethod("requestPaint()");
for (int ii = QmlProfilerCanvas::staticMetaObject.propertyCount(); ii < propertyCount; ++ii) {
QMetaProperty p = metaObject->property(ii);
if (p.hasNotifySignal())
QMetaObject::connect(this, p.notifySignalIndex(), this, requestPaintMethod, 0, 0);
}
QQuickItem::componentComplete();
requestRedraw();
}
void QmlProfilerCanvas::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChanged(newGeometry, oldGeometry);
requestRedraw();
}
}
}
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**