| 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" |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 13 | #include "SkColor.h" |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 14 | #include <iostream> |
| 15 | |
| 16 | SkCanvasWidget::SkCanvasWidget(QWidget *parent) : |
| 17 | QWidget(parent) { |
| 18 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 19 | /* TODO(chudy): The 800x800 is a default number. Change it to be |
| 20 | * set dynamically. Also need to pass size into debugCanvas for current |
| 21 | * command filter. */ |
| 22 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800); |
| 23 | fBitmap.allocPixels(); |
| 24 | fBitmap.eraseColor(0); |
| 25 | |
| 26 | /* TODO(chudy): Add fCanvas, fDevice to the stack. The bitmap being |
| 27 | * cleared does get rid of fDevices link to it. See if there's someway around |
| 28 | * it so that we don't have to delete both canvas and device to clear out |
| 29 | * the bitmap. */ |
| 30 | fDevice = new SkDevice(fBitmap); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 31 | fCanvas = new SkCanvas(fDevice); |
| 32 | fDebugCanvas = new SkDebugCanvas(); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 33 | |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 34 | fScaleFactor = 1.0; |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 35 | fIndex = 0; |
| 36 | fPreviousPoint.set(0,0); |
| 37 | fTransform.set(0,0); |
| 38 | |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 39 | this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}"); |
| 40 | } |
| 41 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 42 | SkCanvasWidget::~SkCanvasWidget() { |
| 43 | delete fCanvas; |
| 44 | delete fDevice; |
| 45 | delete fDebugCanvas; |
| 46 | } |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 47 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 48 | void SkCanvasWidget::resizeEvent(QResizeEvent* event) { |
| 49 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height()); |
| 50 | fBitmap.allocPixels(); |
| 51 | fBitmap.eraseColor(0); |
| 52 | |
| 53 | delete fCanvas; |
| 54 | delete fDevice; |
| 55 | |
| 56 | fDevice = new SkDevice(fBitmap); |
| 57 | fCanvas = new SkCanvas(fDevice); |
| chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame] | 58 | fDebugCanvas->setBounds(event->size().width(), event->size().height()); |
| 59 | fDebugCanvas->drawTo(fCanvas, fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 60 | this->update(); |
| 61 | } |
| 62 | |
| 63 | void SkCanvasWidget::drawTo(int fIndex) { |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 64 | delete fCanvas; |
| 65 | fCanvas = new SkCanvas(fDevice); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 66 | |
| 67 | fCanvas->translate(fTransform.fX, fTransform.fY); |
| 68 | if(fScaleFactor < 0) { |
| 69 | fCanvas->scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor)); |
| 70 | } else if (fScaleFactor > 0) { |
| 71 | fCanvas->scale(fScaleFactor, fScaleFactor); |
| 72 | } |
| 73 | |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 74 | emit commandChanged(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 75 | fDebugCanvas->drawTo(fCanvas, fIndex+1); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 76 | this->update(); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 77 | this->fIndex = fIndex; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void SkCanvasWidget::loadPicture(QString filename) { |
| 81 | SkStream *stream = new SkFILEStream(filename.toAscii()); |
| 82 | SkPicture *picture = new SkPicture(stream); |
| 83 | |
| 84 | delete fDebugCanvas; |
| 85 | fDebugCanvas = new SkDebugCanvas(); |
| chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame] | 86 | fDebugCanvas->setBounds(this->width(), this->height()); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 87 | |
| 88 | picture->draw(fDebugCanvas); |
| 89 | fDebugCanvas->draw(fCanvas); |
| 90 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 91 | fIndex = fDebugCanvas->getSize(); |
| 92 | |
| 93 | SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1); |
| 94 | |
| 95 | int r = SkColorGetR(color); |
| 96 | int g = SkColorGetG(color); |
| 97 | int b = SkColorGetB(color); |
| 98 | |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 99 | /* NOTE(chudy): This was a test to determine if the canvas size is accurately |
| 100 | * saved in the bounds of the recorded picture. It is not. Everyone of the |
| 101 | * sample GM images is 1000x1000. Even the one that claims it is |
| 102 | * 2048x2048. |
| 103 | std::cout << "Width: " << picture->width(); |
| 104 | std::cout << " Height: " << picture->height() << std::endl; */ |
| 105 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 106 | /* Updated style sheet without a background specified. If not removed |
| 107 | * QPainter paints the specified background color on top of our canvas. */ |
| 108 | |
| 109 | QString style("QWidget {border: 1px solid #cccccc; background-color: #"); |
| 110 | style.append(QString::number(r, 16)); |
| 111 | style.append(QString::number(g, 16)); |
| 112 | style.append(QString::number(b, 16)); |
| 113 | style.append(";}"); |
| 114 | this->setStyleSheet(style); |
| 115 | this->update(); |
| 116 | } |
| 117 | |
| 118 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
| 119 | |
| 120 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
| 121 | fTransform += eventPoint - fPreviousPoint; |
| 122 | fPreviousPoint = eventPoint; |
| 123 | |
| 124 | // TODO(chudy): Fix and remove +1 from drawTo calls. |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 125 | drawTo(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 126 | this->update(); |
| 127 | } |
| 128 | |
| 129 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 130 | fPreviousPoint.set(event->globalX(), event->globalY()); |
| 131 | } |
| 132 | |
| 133 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
| 134 | fTransform.set(0,0); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 135 | fScaleFactor = 1.0; |
| 136 | emit scaleFactorChanged(fScaleFactor); |
| 137 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 138 | this->update(); |
| 139 | } |
| 140 | |
| 141 | void SkCanvasWidget::paintEvent(QPaintEvent *event) { |
| 142 | QPainter painter(this); |
| 143 | QStyleOption opt; |
| 144 | opt.init(this); |
| 145 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 146 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); |
| 147 | |
| 148 | QPoint origin(0,0); |
| 149 | QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(), |
| 150 | fBitmap.height(), QImage::Format_ARGB32_Premultiplied); |
| 151 | |
| 152 | painter.drawImage(origin, image); |
| 153 | painter.end(); |
| 154 | } |
| 155 | |
| 156 | void SkCanvasWidget::wheelEvent(QWheelEvent* event) { |
| 157 | fScaleFactor += event->delta()/120; |
| 158 | |
| 159 | /* The range of the fScaleFactor crosses over the range -1,0,1 frequently. |
| 160 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 161 | * size we do the following to never have a registered wheel scroll |
| 162 | * not effect the fScaleFactor. */ |
| 163 | if (fScaleFactor == 0) { |
| 164 | fScaleFactor += (event->delta()/120) * 2; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 167 | emit scaleFactorChanged(fScaleFactor); |
| 168 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 169 | // TODO(chudy): Fix and remove +1 from drawTo calls. |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 170 | drawTo(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 171 | this->update(); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 172 | } |