blob: ecb949cc906f24d1c496a6d7ffb3c87124c863b6 [file] [log] [blame]
chudy@google.com38b08ce2012-07-28 23:26:10 +00001
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 "SkRasterWidget.h"
11
chudy@google.com830b8792012-08-01 15:57:52 +000012SkRasterWidget::SkRasterWidget() : QWidget() {
chudy@google.com38b08ce2012-07-28 23:26:10 +000013 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
14 fBitmap.allocPixels();
15 fBitmap.eraseColor(0);
16 fTransform.set(0,0);
17 fScaleFactor = 1.0;
18 fIndex = 0;
chudy@google.com830b8792012-08-01 15:57:52 +000019 fDevice = new SkDevice(fBitmap);
chudy@google.com80a4a602012-07-30 18:54:07 +000020 fDebugCanvas = NULL;
chudy@google.com830b8792012-08-01 15:57:52 +000021 fCanvas = new SkCanvas(fDevice);
chudy@google.com38b08ce2012-07-28 23:26:10 +000022 this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
23}
24
25SkRasterWidget::~SkRasterWidget() {
chudy@google.com830b8792012-08-01 15:57:52 +000026 SkSafeUnref(fCanvas);
27 SkSafeUnref(fDevice);
chudy@google.com38b08ce2012-07-28 23:26:10 +000028}
29
30void SkRasterWidget::resizeEvent(QResizeEvent* event) {
31 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height());
32 fBitmap.allocPixels();
chudy@google.com830b8792012-08-01 15:57:52 +000033 SkSafeUnref(fCanvas);
34 SkSafeUnref(fDevice);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000035 fDevice = new SkDevice(fBitmap);
chudy@google.com830b8792012-08-01 15:57:52 +000036 fCanvas = new SkCanvas(fDevice);
chudy@google.com2d537a12012-07-31 12:49:52 +000037 this->update();
chudy@google.com38b08ce2012-07-28 23:26:10 +000038}
39
40void SkRasterWidget::paintEvent(QPaintEvent* event) {
chudy@google.com80a4a602012-07-30 18:54:07 +000041 if (fDebugCanvas) {
chudy@google.com830b8792012-08-01 15:57:52 +000042 fDebugCanvas->drawTo(fCanvas, fIndex);
chudy@google.com80a4a602012-07-30 18:54:07 +000043 QPainter painter(this);
44 QStyleOption opt;
45 opt.init(this);
46
47 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
48
49 QPoint origin(0,0);
50 QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
51 fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
52
53 painter.drawImage(origin, image);
54 painter.end();
chudy@google.coma9e937c2012-08-03 17:32:05 +000055 emit drawComplete();
chudy@google.com38b08ce2012-07-28 23:26:10 +000056 }
chudy@google.com38b08ce2012-07-28 23:26:10 +000057}