| 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 | <<<<<<< HEAD |
| 59 | fDebugCanvas->setBounds(event->size().width(), event->size().height()); |
| 60 | fDebugCanvas->drawTo(fCanvas, fIndex); |
| 61 | ======= |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 62 | drawTo(fIndex); |
| chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame^] | 63 | >>>>>>> refs/remotes/git-svn |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 64 | this->update(); |
| 65 | } |
| 66 | |
| 67 | void SkCanvasWidget::drawTo(int fIndex) { |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 68 | delete fCanvas; |
| 69 | fCanvas = new SkCanvas(fDevice); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 70 | |
| 71 | fCanvas->translate(fTransform.fX, fTransform.fY); |
| 72 | if(fScaleFactor < 0) { |
| 73 | fCanvas->scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor)); |
| 74 | } else if (fScaleFactor > 0) { |
| 75 | fCanvas->scale(fScaleFactor, fScaleFactor); |
| 76 | } |
| 77 | |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 78 | emit commandChanged(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 79 | fDebugCanvas->drawTo(fCanvas, fIndex+1); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 80 | this->update(); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 81 | this->fIndex = fIndex; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void SkCanvasWidget::loadPicture(QString filename) { |
| 85 | SkStream *stream = new SkFILEStream(filename.toAscii()); |
| 86 | SkPicture *picture = new SkPicture(stream); |
| 87 | |
| 88 | delete fDebugCanvas; |
| 89 | fDebugCanvas = new SkDebugCanvas(); |
| chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame^] | 90 | fDebugCanvas->setBounds(this->width(), this->height()); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 91 | |
| 92 | picture->draw(fDebugCanvas); |
| 93 | fDebugCanvas->draw(fCanvas); |
| 94 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 95 | fIndex = fDebugCanvas->getSize(); |
| 96 | |
| 97 | SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1); |
| 98 | |
| 99 | int r = SkColorGetR(color); |
| 100 | int g = SkColorGetG(color); |
| 101 | int b = SkColorGetB(color); |
| 102 | |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 103 | /* NOTE(chudy): This was a test to determine if the canvas size is accurately |
| 104 | * saved in the bounds of the recorded picture. It is not. Everyone of the |
| 105 | * sample GM images is 1000x1000. Even the one that claims it is |
| 106 | * 2048x2048. |
| 107 | std::cout << "Width: " << picture->width(); |
| 108 | std::cout << " Height: " << picture->height() << std::endl; */ |
| 109 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 110 | /* Updated style sheet without a background specified. If not removed |
| 111 | * QPainter paints the specified background color on top of our canvas. */ |
| 112 | |
| 113 | QString style("QWidget {border: 1px solid #cccccc; background-color: #"); |
| 114 | style.append(QString::number(r, 16)); |
| 115 | style.append(QString::number(g, 16)); |
| 116 | style.append(QString::number(b, 16)); |
| 117 | style.append(";}"); |
| 118 | this->setStyleSheet(style); |
| 119 | this->update(); |
| 120 | } |
| 121 | |
| 122 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
| 123 | |
| 124 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
| 125 | fTransform += eventPoint - fPreviousPoint; |
| 126 | fPreviousPoint = eventPoint; |
| 127 | |
| 128 | // TODO(chudy): Fix and remove +1 from drawTo calls. |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 129 | drawTo(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 130 | this->update(); |
| 131 | } |
| 132 | |
| 133 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 134 | fPreviousPoint.set(event->globalX(), event->globalY()); |
| 135 | } |
| 136 | |
| 137 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
| 138 | fTransform.set(0,0); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 139 | fScaleFactor = 1.0; |
| 140 | emit scaleFactorChanged(fScaleFactor); |
| 141 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 142 | this->update(); |
| 143 | } |
| 144 | |
| 145 | void SkCanvasWidget::paintEvent(QPaintEvent *event) { |
| 146 | QPainter painter(this); |
| 147 | QStyleOption opt; |
| 148 | opt.init(this); |
| 149 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 150 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); |
| 151 | |
| 152 | QPoint origin(0,0); |
| 153 | QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(), |
| 154 | fBitmap.height(), QImage::Format_ARGB32_Premultiplied); |
| 155 | |
| 156 | painter.drawImage(origin, image); |
| 157 | painter.end(); |
| 158 | } |
| 159 | |
| 160 | void SkCanvasWidget::wheelEvent(QWheelEvent* event) { |
| 161 | fScaleFactor += event->delta()/120; |
| 162 | |
| 163 | /* The range of the fScaleFactor crosses over the range -1,0,1 frequently. |
| 164 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 165 | * size we do the following to never have a registered wheel scroll |
| 166 | * not effect the fScaleFactor. */ |
| 167 | if (fScaleFactor == 0) { |
| 168 | fScaleFactor += (event->delta()/120) * 2; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 171 | emit scaleFactorChanged(fScaleFactor); |
| 172 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 173 | // TODO(chudy): Fix and remove +1 from drawTo calls. |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 174 | drawTo(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 175 | this->update(); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 176 | } |