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 | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 12 | SkCanvasWidget::SkCanvasWidget(QWidget* parent, |
| 13 | SkDebugger* debugger) : QWidget(parent) |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 14 | , fHorizontalLayout(this) |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 15 | , fRasterWidget(debugger) |
| 16 | , fGLWidget(debugger) |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 17 | { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 18 | |
| 19 | fDebugger = debugger; |
| 20 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 21 | fHorizontalLayout.setSpacing(6); |
| 22 | fHorizontalLayout.setContentsMargins(0,0,0,0); |
| 23 | fRasterWidget.setSizePolicy(QSizePolicy::Expanding, |
| 24 | QSizePolicy::Expanding); |
| 25 | fGLWidget.setSizePolicy(QSizePolicy::Expanding, |
| 26 | QSizePolicy::Expanding); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 27 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 28 | fHorizontalLayout.addWidget(&fRasterWidget); |
| 29 | fHorizontalLayout.addWidget(&fGLWidget); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 30 | |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 31 | fPreviousPoint.set(0,0); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 32 | fUserOffset.set(0,0); |
| 33 | fUserScaleFactor = 1.0; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 34 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 35 | setWidgetVisibility(kGPU_WidgetType, true); |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 36 | connect(&fRasterWidget, SIGNAL(drawComplete()), |
| 37 | this->parentWidget(), SLOT(drawComplete())); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 38 | } |
| 39 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 40 | SkCanvasWidget::~SkCanvasWidget() {} |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 41 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 42 | void SkCanvasWidget::drawTo(int index) { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 43 | fDebugger->setIndex(index); |
| 44 | fRasterWidget.draw(); |
| 45 | fGLWidget.draw(); |
| 46 | emit commandChanged(fDebugger->index()); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 50 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 51 | fUserOffset += eventPoint - fPreviousPoint; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 52 | fPreviousPoint = eventPoint; |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 53 | fDebugger->setUserOffset(fUserOffset); |
| 54 | drawTo(fDebugger->index()); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 58 | fPreviousPoint.set(event->globalX(), event->globalY()); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 59 | emit hitChanged(fDebugger->getCommandAtPoint(event->x(), event->y(), |
| 60 | fDebugger->index())); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 64 | resetWidgetTransform(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 65 | } |
| 66 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 67 | void SkCanvasWidget::resetWidgetTransform() { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 68 | fUserOffset.set(0,0); |
| 69 | fUserScaleFactor = 1.0; |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 70 | fDebugger->setUserOffset(fUserOffset); |
| 71 | fDebugger->setUserScale(fUserScaleFactor); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 72 | emit scaleFactorChanged(fUserScaleFactor); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 73 | drawTo(fDebugger->index()); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 74 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 75 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 76 | void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) { |
| 77 | if (type == kRaster_8888_WidgetType) { |
| 78 | fRasterWidget.setHidden(isHidden); |
| 79 | } else if (type == kGPU_WidgetType) { |
| 80 | fGLWidget.setHidden(isHidden); |
| 81 | } |
| 82 | } |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 83 | |
chudy@google.com | a122631 | 2012-07-26 20:26:44 +0000 | [diff] [blame] | 84 | void SkCanvasWidget::zoom(float zoomIncrement) { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 85 | fUserScaleFactor += zoomIncrement; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 86 | |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 87 | /* 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] | 88 | * Based on the code below, -1 and 1 both scale the image to it's original |
| 89 | * 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] | 90 | * not effect the fUserScaleFactor. */ |
| 91 | if (fUserScaleFactor == 0) { |
| 92 | fUserScaleFactor = 2 * zoomIncrement; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 93 | } |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 94 | emit scaleFactorChanged(fUserScaleFactor); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame^] | 95 | fDebugger->setUserScale(fUserScaleFactor); |
| 96 | drawTo(fDebugger->index()); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 97 | } |