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