| 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 | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 26 | fDebugCanvas = new SkDebugCanvas(); |
| 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 | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 36 | void SkCanvasWidget::drawTo(int index) { |
| 37 | fIndex = index; |
| 38 | if (!fRasterWidget.isHidden()) { |
| 39 | fRasterWidget.drawTo(index); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 40 | } |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 41 | if (!fGLWidget.isHidden()) { |
| 42 | fGLWidget.drawTo(index); |
| 43 | } |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 44 | emit commandChanged(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void SkCanvasWidget::loadPicture(QString filename) { |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 48 | SkStream* stream = new SkFILEStream(filename.toAscii()); |
| 49 | SkPicture* picture = new SkPicture(stream); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 50 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 51 | /* TODO(chudy): Implement function that doesn't require new |
| 52 | * instantiation of debug canvas. */ |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 53 | delete fDebugCanvas; |
| 54 | fDebugCanvas = new SkDebugCanvas(); |
| chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame] | 55 | fDebugCanvas->setBounds(this->width(), this->height()); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 56 | picture->draw(fDebugCanvas); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 57 | fIndex = fDebugCanvas->getSize(); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 58 | fRasterWidget.setDebugCanvas(fDebugCanvas); |
| 59 | fGLWidget.setDebugCanvas(fDebugCanvas); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 63 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
| 64 | fTransform += eventPoint - fPreviousPoint; |
| 65 | fPreviousPoint = eventPoint; |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 66 | updateWidgetTransform(kTranslate); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 67 | drawTo(fIndex); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 71 | fPreviousPoint.set(event->globalX(), event->globalY()); |
| chudy@google.com | e606d6e | 2012-07-12 14:31:25 +0000 | [diff] [blame] | 72 | fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y()); |
| 73 | fDebugCanvas->isCalculatingHits(true); |
| 74 | drawTo(fIndex); |
| 75 | emit hitChanged(fDebugCanvas->getHitBoxPoint()); |
| 76 | fDebugCanvas->isCalculatingHits(false); |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
| 80 | fTransform.set(0,0); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 81 | fScaleFactor = 1.0; |
| 82 | emit scaleFactorChanged(fScaleFactor); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 83 | // TODO(chudy): Change to signal / slot mechanism. |
| 84 | resetWidgetTransform(); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 85 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 88 | void SkCanvasWidget::resetWidgetTransform() { |
| 89 | fTransform.set(0,0); |
| 90 | fScaleFactor = 1.0; |
| 91 | updateWidgetTransform(kTranslate); |
| 92 | updateWidgetTransform(kScale); |
| 93 | } |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 94 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 95 | void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) { |
| 96 | if (type == kRaster_8888_WidgetType) { |
| 97 | fRasterWidget.setHidden(isHidden); |
| 98 | } else if (type == kGPU_WidgetType) { |
| 99 | fGLWidget.setHidden(isHidden); |
| 100 | } |
| 101 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 102 | |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 103 | void SkCanvasWidget::updateWidgetTransform(TransformType type) { |
| 104 | if (type == kTranslate) { |
| 105 | fRasterWidget.setTranslate(fTransform); |
| 106 | fGLWidget.setTranslate(fTransform); |
| 107 | } else if (type == kScale) { |
| 108 | fRasterWidget.setScale(fScaleFactor); |
| 109 | fGLWidget.setScale(fScaleFactor); |
| 110 | } |
| chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void SkCanvasWidget::wheelEvent(QWheelEvent* event) { |
| 114 | fScaleFactor += event->delta()/120; |
| 115 | |
| 116 | /* The range of the fScaleFactor crosses over the range -1,0,1 frequently. |
| 117 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 118 | * size we do the following to never have a registered wheel scroll |
| 119 | * not effect the fScaleFactor. */ |
| 120 | if (fScaleFactor == 0) { |
| 121 | fScaleFactor += (event->delta()/120) * 2; |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 122 | } |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 123 | emit scaleFactorChanged(fScaleFactor); |
| chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame^] | 124 | updateWidgetTransform(kScale); |
| chudy@google.com | 7dcae67 | 2012-07-09 20:26:53 +0000 | [diff] [blame] | 125 | drawTo(fIndex); |
| chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 126 | } |