| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "SkPicture.h" |
| 11 | #include "SkStream.h" |
| 12 | #include "SkCanvasWidget.h" |
| 13 | #include <iostream> |
| 14 | |
| 15 | SkCanvasWidget::SkCanvasWidget(QWidget *parent) : |
| 16 | QWidget(parent) { |
| 17 | |
| 18 | fBitmap = new SkBitmap(); |
| 19 | fBitmap->setConfig(SkBitmap::kARGB_8888_Config, 800, 800); |
| 20 | fBitmap->allocPixels(); |
| 21 | fBitmap->eraseColor(0); |
| 22 | fDevice = new SkDevice(*fBitmap); |
| 23 | fCanvas = new SkCanvas(fDevice); |
| 24 | fDebugCanvas = new SkDebugCanvas(); |
| 25 | this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}"); |
| 26 | } |
| 27 | |
| 28 | SkCanvasWidget::~SkCanvasWidget() {} |
| 29 | |
| 30 | void SkCanvasWidget::drawTo(int index) { |
| 31 | delete fCanvas; |
| 32 | fCanvas = new SkCanvas(fDevice); |
| 33 | fDebugCanvas->drawTo(fCanvas, index+1); |
| 34 | this->update(); |
| 35 | } |
| 36 | |
| 37 | void SkCanvasWidget::loadPicture(QString filename) { |
| 38 | SkStream *stream = new SkFILEStream(filename.toAscii()); |
| 39 | SkPicture *picture = new SkPicture(stream); |
| 40 | |
| 41 | delete fDebugCanvas; |
| 42 | fDebugCanvas = new SkDebugCanvas(); |
| 43 | |
| 44 | picture->draw(fDebugCanvas); |
| 45 | fDebugCanvas->draw(fCanvas); |
| 46 | |
| 47 | /* NOTE(chudy): This was a test to determine if the canvas size is accurately |
| 48 | * saved in the bounds of the recorded picture. It is not. Everyone of the |
| 49 | * sample GM images is 1000x1000. Even the one that claims it is |
| 50 | * 2048x2048. |
| 51 | std::cout << "Width: " << picture->width(); |
| 52 | std::cout << " Height: " << picture->height() << std::endl; */ |
| 53 | |
| 54 | /* NOTE(chudy): Updated style sheet without a background specified to |
| 55 | * draw over our SkCanvas. */ |
| 56 | this->setStyleSheet("QWidget {border: 1px solid #cccccc;}"); |
| 57 | this->update(); |
| 58 | } |
| 59 | |
| 60 | void SkCanvasWidget::paintEvent(QPaintEvent *event) { |
| 61 | QPainter painter(this); |
| 62 | QStyleOption opt; |
| 63 | opt.init(this); |
| 64 | |
| 65 | if (fBitmap) { |
| 66 | const QPoint origin(0,0); |
| 67 | QImage image((uchar *)fBitmap->getPixels(), fBitmap->width(), fBitmap->height(), QImage::Format_ARGB32_Premultiplied); |
| 68 | painter.drawImage(origin,image); |
| 69 | } |
| 70 | |
| 71 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); |
| 72 | painter.end(); |
| 73 | } |