aboutsummaryrefslogtreecommitdiffhomepage
path: root/debugger/QT/SkCanvasWidget.cpp
blob: 6e8265627bcddc36423b3d437328952ca33289fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkPicture.h"
#include "SkStream.h"
#include "SkCanvasWidget.h"
#include <iostream>

SkCanvasWidget::SkCanvasWidget(QWidget *parent) :
    QWidget(parent) {

    fBitmap = new SkBitmap();
    fBitmap->setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
    fBitmap->allocPixels();
    fBitmap->eraseColor(0);
    fDevice = new SkDevice(*fBitmap);
    fCanvas = new SkCanvas(fDevice);
    fDebugCanvas = new SkDebugCanvas();
    this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
}

SkCanvasWidget::~SkCanvasWidget() {}

void SkCanvasWidget::drawTo(int index) {
    delete fCanvas;
    fCanvas = new SkCanvas(fDevice);
    fDebugCanvas->drawTo(fCanvas, index+1);
    this->update();
}

void SkCanvasWidget::loadPicture(QString filename) {
    SkStream *stream = new SkFILEStream(filename.toAscii());
    SkPicture *picture = new SkPicture(stream);

    delete fDebugCanvas;
    fDebugCanvas = new SkDebugCanvas();

    picture->draw(fDebugCanvas);
    fDebugCanvas->draw(fCanvas);

    /* NOTE(chudy): This was a test to determine if the canvas size is accurately
     * saved in the bounds of the recorded picture. It is not. Everyone of the
     * sample GM images is 1000x1000. Even the one that claims it is
     * 2048x2048.
    std::cout << "Width: " << picture->width();
    std::cout << " Height: " <<  picture->height() << std::endl; */

    /* NOTE(chudy): Updated style sheet without a background specified to
     * draw over our SkCanvas. */
    this->setStyleSheet("QWidget {border: 1px solid #cccccc;}");
    this->update();
}

void SkCanvasWidget::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    QStyleOption opt;
    opt.init(this);

    if (fBitmap) {
        const QPoint origin(0,0);
        QImage image((uchar *)fBitmap->getPixels(), fBitmap->width(), fBitmap->height(), QImage::Format_ARGB32_Premultiplied);
        painter.drawImage(origin,image);
    }

    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
    painter.end();
}