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 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 10 | #include "SkCanvasWidget.h" |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 11 | |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame^] | 12 | SkCanvasWidget::SkCanvasWidget(QWidget* parent) : QWidget(parent) |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 13 | , fHorizontalLayout(this) |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 14 | { |
| 15 | fHorizontalLayout.setSpacing(6); |
| 16 | fHorizontalLayout.setContentsMargins(0,0,0,0); |
| 17 | fRasterWidget.setSizePolicy(QSizePolicy::Expanding, |
| 18 | QSizePolicy::Expanding); |
| 19 | fGLWidget.setSizePolicy(QSizePolicy::Expanding, |
| 20 | QSizePolicy::Expanding); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 21 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 22 | fHorizontalLayout.addWidget(&fRasterWidget); |
| 23 | fHorizontalLayout.addWidget(&fGLWidget); |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 24 | fDebugCanvas = NULL; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 25 | |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 26 | fIndex = 0; |
| 27 | fPreviousPoint.set(0,0); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 28 | fUserOffset.set(0,0); |
| 29 | fUserScaleFactor = 1.0; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 30 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 31 | setWidgetVisibility(kGPU_WidgetType, true); |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 32 | this->setDisabled(true); |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame^] | 33 | connect(&fRasterWidget, SIGNAL(drawComplete()), |
| 34 | this->parentWidget(), SLOT(drawComplete())); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 35 | } |
| 36 | |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 37 | SkCanvasWidget::~SkCanvasWidget() { |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 38 | delete fDebugCanvas; |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 39 | } |
| 40 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 41 | void SkCanvasWidget::drawTo(int index) { |
| 42 | fIndex = index; |
| 43 | if (!fRasterWidget.isHidden()) { |
| 44 | fRasterWidget.drawTo(index); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 45 | } |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 46 | if (!fGLWidget.isHidden()) { |
| 47 | fGLWidget.drawTo(index); |
| 48 | } |
chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 49 | emit commandChanged(fIndex); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void SkCanvasWidget::loadPicture(QString filename) { |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 53 | this->setDisabled(false); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 54 | SkStream* stream = new SkFILEStream(filename.toAscii()); |
| 55 | SkPicture* picture = new SkPicture(stream); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 56 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 57 | /* TODO(chudy): Implement function that doesn't require new |
| 58 | * instantiation of debug canvas. */ |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 59 | delete fDebugCanvas; |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 60 | fDebugCanvas = new SkDebugCanvas(picture->width(), picture->height()); |
| 61 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 62 | picture->draw(fDebugCanvas); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 63 | fIndex = fDebugCanvas->getSize() - 1; |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 64 | fRasterWidget.setDebugCanvas(fDebugCanvas); |
| 65 | fGLWidget.setDebugCanvas(fDebugCanvas); |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 66 | fDebugCanvas->setBounds(this->width(), this->height()); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 70 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 71 | fUserOffset += eventPoint - fPreviousPoint; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 72 | fPreviousPoint = eventPoint; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 73 | fDebugCanvas->setUserOffset(fUserOffset); |
chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 74 | drawTo(fIndex); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 78 | fPreviousPoint.set(event->globalX(), event->globalY()); |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 79 | emit hitChanged(fDebugCanvas->getCommandAtPoint(event->x(), event->y(), |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 80 | fIndex)); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 84 | resetWidgetTransform(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 85 | } |
| 86 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 87 | void SkCanvasWidget::resetWidgetTransform() { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 88 | fUserOffset.set(0,0); |
| 89 | fUserScaleFactor = 1.0; |
| 90 | fDebugCanvas->setUserOffset(fUserOffset); |
| 91 | fDebugCanvas->setUserScale(fUserScaleFactor); |
| 92 | emit scaleFactorChanged(fUserScaleFactor); |
| 93 | drawTo(fIndex); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 94 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 95 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 96 | void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) { |
| 97 | if (type == kRaster_8888_WidgetType) { |
| 98 | fRasterWidget.setHidden(isHidden); |
| 99 | } else if (type == kGPU_WidgetType) { |
| 100 | fGLWidget.setHidden(isHidden); |
| 101 | } |
| 102 | } |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 103 | |
chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 104 | void SkCanvasWidget::zoom(float zoomIncrement) { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 105 | fUserScaleFactor += zoomIncrement; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 106 | |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 107 | /* The range of the fUserScaleFactor crosses over the range -1,0,1 frequently. |
chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 108 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 109 | * size we do the following to never have a registered wheel scroll |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 110 | * not effect the fUserScaleFactor. */ |
| 111 | if (fUserScaleFactor == 0) { |
| 112 | fUserScaleFactor = 2 * zoomIncrement; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 113 | } |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 114 | emit scaleFactorChanged(fUserScaleFactor); |
| 115 | fDebugCanvas->setUserScale(fUserScaleFactor); |
chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 116 | drawTo(fIndex); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 117 | } |