chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +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 "SkRasterWidget.h" |
| 11 | |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 12 | SkRasterWidget::SkRasterWidget() : QWidget() { |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 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.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 19 | fDevice = new SkDevice(fBitmap); |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 20 | fDebugCanvas = NULL; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 21 | fCanvas = new SkCanvas(fDevice); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 22 | this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}"); |
| 23 | } |
| 24 | |
| 25 | SkRasterWidget::~SkRasterWidget() { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 26 | SkSafeUnref(fCanvas); |
| 27 | SkSafeUnref(fDevice); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void SkRasterWidget::resizeEvent(QResizeEvent* event) { |
| 31 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height()); |
| 32 | fBitmap.allocPixels(); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 33 | SkSafeUnref(fCanvas); |
| 34 | SkSafeUnref(fDevice); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 35 | fDevice = new SkDevice(fBitmap); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 36 | fCanvas = new SkCanvas(fDevice); |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 37 | this->update(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void SkRasterWidget::paintEvent(QPaintEvent* event) { |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 41 | if (fDebugCanvas) { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 42 | fDebugCanvas->drawTo(fCanvas, fIndex); |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 43 | 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.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 55 | emit drawComplete(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 56 | } |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 57 | } |