blob: db89de70fe754499cd9fe1633f5bfa390e673333 [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
12SkRasterWidget::SkRasterWidget(QWidget* parent) : QWidget(parent) {
13 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.com2d537a12012-07-31 12:49:52 +000019 fDevice = NULL;
chudy@google.com80a4a602012-07-30 18:54:07 +000020 fDebugCanvas = NULL;
chudy@google.com38b08ce2012-07-28 23:26:10 +000021 this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
22}
23
24SkRasterWidget::~SkRasterWidget() {
25 delete fDevice;
chudy@google.com38b08ce2012-07-28 23:26:10 +000026}
27
28void SkRasterWidget::resizeEvent(QResizeEvent* event) {
29 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height());
30 fBitmap.allocPixels();
chudy@google.com2d537a12012-07-31 12:49:52 +000031 if (fDevice) {
32 delete fDevice;
chudy@google.com80a4a602012-07-30 18:54:07 +000033 }
chudy@google.com2d537a12012-07-31 12:49:52 +000034 fDevice = new SkDevice(fBitmap);
35
36
37 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) {
42 fBitmap.eraseColor(0);
43 SkCanvas canvas(fDevice);
44 canvas.translate(fTransform.fX, fTransform.fY);
45 if (fScaleFactor < 0) {
46 canvas.scale((1.0 / -fScaleFactor), (1.0 / -fScaleFactor));
47 } else if (fScaleFactor > 0) {
48 canvas.scale(fScaleFactor, fScaleFactor);
49 }
50
51 fMatrix = canvas.getTotalMatrix();
52 fClip = canvas.getTotalClip().getBounds();
53 fDebugCanvas->drawTo(&canvas, fIndex+1, &fBitmap);
54
55 QPainter painter(this);
56 QStyleOption opt;
57 opt.init(this);
58
59 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
60
61 QPoint origin(0,0);
62 QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
63 fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
64
65 painter.drawImage(origin, image);
66 painter.end();
chudy@google.com38b08ce2012-07-28 23:26:10 +000067 }
chudy@google.com38b08ce2012-07-28 23:26:10 +000068}