| 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 | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 12 | SkCanvasWidget::SkCanvasWidget(QWidget* parent) : QWidget(parent) |
| 13 | , fHorizontalLayout(this) |
| 14 | , fRasterWidget(this) |
| 15 | , fGLWidget(this) |
| 16 | { |
| 17 | fHorizontalLayout.setSpacing(6); |
| 18 | fHorizontalLayout.setContentsMargins(0,0,0,0); |
| 19 | fRasterWidget.setSizePolicy(QSizePolicy::Expanding, |
| 20 | QSizePolicy::Expanding); |
| 21 | fGLWidget.setSizePolicy(QSizePolicy::Expanding, |
| 22 | QSizePolicy::Expanding); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 23 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 24 | fHorizontalLayout.addWidget(&fRasterWidget); |
| 25 | fHorizontalLayout.addWidget(&fGLWidget); |
| chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 26 | fDebugCanvas = NULL; |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 27 | |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 28 | fIndex = 0; |
| 29 | fPreviousPoint.set(0,0); |
| 30 | fTransform.set(0,0); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 31 | fScaleFactor = 1.0; |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 32 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 33 | setWidgetVisibility(kGPU_WidgetType, true); |
| chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 34 | this->setDisabled(true); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 37 | void SkCanvasWidget::drawTo(int index) { |
| 38 | fIndex = index; |
| 39 | if (!fRasterWidget.isHidden()) { |
| 40 | fRasterWidget.drawTo(index); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 41 | } |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 42 | if (!fGLWidget.isHidden()) { |
| 43 | fGLWidget.drawTo(index); |
| 44 | } |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 45 | emit commandChanged(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void SkCanvasWidget::loadPicture(QString filename) { |
| chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 49 | this->setDisabled(false); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 50 | SkStream* stream = new SkFILEStream(filename.toAscii()); |
| 51 | SkPicture* picture = new SkPicture(stream); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 52 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 53 | /* TODO(chudy): Implement function that doesn't require new |
| 54 | * instantiation of debug canvas. */ |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 55 | delete fDebugCanvas; |
| chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 56 | fDebugCanvas = new SkDebugCanvas(picture->width(), picture->height()); |
| 57 | |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 58 | picture->draw(fDebugCanvas); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 59 | fIndex = fDebugCanvas->getSize(); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 60 | fRasterWidget.setDebugCanvas(fDebugCanvas); |
| 61 | fGLWidget.setDebugCanvas(fDebugCanvas); |
| chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 62 | |
| 63 | // TODO(chudy): Remove bounds from debug canvas storage. |
| 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()); |
| 69 | fTransform += eventPoint - fPreviousPoint; |
| 70 | fPreviousPoint = eventPoint; |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 71 | updateWidgetTransform(kTranslate); |
| 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 | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 77 | if (fDebugCanvas) { |
| 78 | fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y()); |
| 79 | fDebugCanvas->isCalculatingHits(true); |
| 80 | drawTo(fIndex); |
| 81 | emit hitChanged(fDebugCanvas->getHitBoxPoint()); |
| 82 | fDebugCanvas->isCalculatingHits(false); |
| 83 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
| 87 | fTransform.set(0,0); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 88 | fScaleFactor = 1.0; |
| 89 | emit scaleFactorChanged(fScaleFactor); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 90 | // TODO(chudy): Change to signal / slot mechanism. |
| 91 | resetWidgetTransform(); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 92 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 95 | void SkCanvasWidget::resetWidgetTransform() { |
| 96 | fTransform.set(0,0); |
| 97 | fScaleFactor = 1.0; |
| 98 | updateWidgetTransform(kTranslate); |
| 99 | updateWidgetTransform(kScale); |
| 100 | } |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 101 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 102 | void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) { |
| 103 | if (type == kRaster_8888_WidgetType) { |
| 104 | fRasterWidget.setHidden(isHidden); |
| 105 | } else if (type == kGPU_WidgetType) { |
| 106 | fGLWidget.setHidden(isHidden); |
| 107 | } |
| 108 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 109 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 110 | void SkCanvasWidget::updateWidgetTransform(TransformType type) { |
| 111 | if (type == kTranslate) { |
| 112 | fRasterWidget.setTranslate(fTransform); |
| 113 | fGLWidget.setTranslate(fTransform); |
| 114 | } else if (type == kScale) { |
| 115 | fRasterWidget.setScale(fScaleFactor); |
| 116 | fGLWidget.setScale(fScaleFactor); |
| 117 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 120 | void SkCanvasWidget::zoom(float zoomIncrement) { |
| 121 | fScaleFactor += zoomIncrement; |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 122 | |
| 123 | /* 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] | 124 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 125 | * size we do the following to never have a registered wheel scroll |
| 126 | * not effect the fScaleFactor. */ |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 127 | if (fScaleFactor == 0) { |
| chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 128 | fScaleFactor = 2 * zoomIncrement; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 129 | } |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 130 | emit scaleFactorChanged(fScaleFactor); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 131 | updateWidgetTransform(kScale); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 132 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 133 | } |