| 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); |
| 28 | fTransform.set(0,0); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 29 | fScaleFactor = 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() { |
| 36 | if (fDebugCanvas) { |
| 37 | delete fDebugCanvas; |
| 38 | } |
| 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 | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 63 | fIndex = fDebugCanvas->getSize(); |
| 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 | |
| 67 | // TODO(chudy): Remove bounds from debug canvas storage. |
| 68 | fDebugCanvas->setBounds(this->width(), this->height()); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 72 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
| 73 | fTransform += eventPoint - fPreviousPoint; |
| 74 | fPreviousPoint = eventPoint; |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 75 | updateWidgetTransform(kTranslate); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 76 | drawTo(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 80 | fPreviousPoint.set(event->globalX(), event->globalY()); |
| chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 81 | if (fDebugCanvas) { |
| 82 | fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y()); |
| 83 | fDebugCanvas->isCalculatingHits(true); |
| 84 | drawTo(fIndex); |
| 85 | emit hitChanged(fDebugCanvas->getHitBoxPoint()); |
| 86 | fDebugCanvas->isCalculatingHits(false); |
| 87 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
| 91 | fTransform.set(0,0); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 92 | fScaleFactor = 1.0; |
| 93 | emit scaleFactorChanged(fScaleFactor); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 94 | // TODO(chudy): Change to signal / slot mechanism. |
| 95 | resetWidgetTransform(); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 96 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 99 | void SkCanvasWidget::resetWidgetTransform() { |
| 100 | fTransform.set(0,0); |
| 101 | fScaleFactor = 1.0; |
| 102 | updateWidgetTransform(kTranslate); |
| 103 | updateWidgetTransform(kScale); |
| 104 | } |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 105 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 106 | void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) { |
| 107 | if (type == kRaster_8888_WidgetType) { |
| 108 | fRasterWidget.setHidden(isHidden); |
| 109 | } else if (type == kGPU_WidgetType) { |
| 110 | fGLWidget.setHidden(isHidden); |
| 111 | } |
| 112 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 113 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 114 | void SkCanvasWidget::updateWidgetTransform(TransformType type) { |
| 115 | if (type == kTranslate) { |
| 116 | fRasterWidget.setTranslate(fTransform); |
| 117 | fGLWidget.setTranslate(fTransform); |
| 118 | } else if (type == kScale) { |
| 119 | fRasterWidget.setScale(fScaleFactor); |
| 120 | fGLWidget.setScale(fScaleFactor); |
| 121 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 124 | void SkCanvasWidget::zoom(float zoomIncrement) { |
| 125 | fScaleFactor += zoomIncrement; |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 126 | |
| 127 | /* The range of the fScaleFactor crosses over the range -1,0,1 frequently. |
| chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 128 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 129 | * size we do the following to never have a registered wheel scroll |
| 130 | * not effect the fScaleFactor. */ |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 131 | if (fScaleFactor == 0) { |
| chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 132 | fScaleFactor = 2 * zoomIncrement; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 133 | } |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 134 | emit scaleFactorChanged(fScaleFactor); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 135 | updateWidgetTransform(kScale); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 136 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 137 | } |