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) |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 17 | , fGLWidget(debugger) |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 18 | #endif |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 19 | { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 20 | |
| 21 | fDebugger = debugger; |
| 22 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 23 | fHorizontalLayout.setSpacing(6); |
| 24 | fHorizontalLayout.setContentsMargins(0,0,0,0); |
| 25 | fRasterWidget.setSizePolicy(QSizePolicy::Expanding, |
| 26 | QSizePolicy::Expanding); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 27 | #if SK_SUPPORT_GPU |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 28 | fGLWidget.setSizePolicy(QSizePolicy::Expanding, |
| 29 | QSizePolicy::Expanding); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 30 | #endif |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 31 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 32 | fHorizontalLayout.addWidget(&fRasterWidget); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 33 | #if SK_SUPPORT_GPU |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 34 | fHorizontalLayout.addWidget(&fGLWidget); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 35 | #endif |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 36 | |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 37 | fPreviousPoint.set(0,0); |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 38 | fUserMatrix.reset(); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 39 | |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 40 | #if SK_SUPPORT_GPU |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 41 | setWidgetVisibility(kGPU_WidgetType, true); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 42 | #endif |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 43 | connect(&fRasterWidget, SIGNAL(drawComplete()), |
| 44 | this->parentWidget(), SLOT(drawComplete())); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 45 | } |
| 46 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 47 | SkCanvasWidget::~SkCanvasWidget() {} |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 48 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 49 | void SkCanvasWidget::drawTo(int index) { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 50 | fDebugger->setIndex(index); |
| 51 | fRasterWidget.draw(); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 52 | #if SK_SUPPORT_GPU |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 53 | fGLWidget.draw(); |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 54 | #endif |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 55 | emit commandChanged(fDebugger->index()); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) { |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 59 | SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY()); |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 60 | SkIPoint eventOffset = eventPoint - fPreviousPoint; |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 61 | fPreviousPoint = eventPoint; |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 62 | fUserMatrix.postTranslate(eventOffset.fX, eventOffset.fY); |
| 63 | fDebugger->setUserMatrix(fUserMatrix); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 64 | drawTo(fDebugger->index()); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void SkCanvasWidget::mousePressEvent(QMouseEvent* event) { |
| 68 | fPreviousPoint.set(event->globalX(), event->globalY()); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 69 | emit hitChanged(fDebugger->getCommandAtPoint(event->x(), event->y(), |
| 70 | fDebugger->index())); |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) { |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 74 | Qt::KeyboardModifiers modifiers = event->modifiers(); |
| 75 | if (modifiers.testFlag(Qt::ControlModifier)) { |
| 76 | snapWidgetTransform(); |
| 77 | } else { |
| 78 | resetWidgetTransform(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #define ZOOM_FACTOR (1.25f) |
| 83 | |
| 84 | void SkCanvasWidget::wheelEvent(QWheelEvent* event) { |
| 85 | Qt::KeyboardModifiers modifiers = event->modifiers(); |
| 86 | if (modifiers.testFlag(Qt::ControlModifier)) { |
| 87 | zoom(event->delta() > 0 ? ZOOM_FACTOR : (1.0f / ZOOM_FACTOR), event->x(), event->y()); |
| 88 | } else { |
| 89 | if (Qt::Horizontal == event->orientation()) { |
| 90 | fUserMatrix.postTranslate(event->delta(), 0.0f); |
| 91 | } else { |
| 92 | fUserMatrix.postTranslate(0.0f, event->delta()); |
| 93 | } |
| 94 | fDebugger->setUserMatrix(fUserMatrix); |
| 95 | drawTo(fDebugger->index()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void SkCanvasWidget::zoom(int zoomCommand) { |
| 100 | zoom(kIn_ZoomCommand == zoomCommand ? ZOOM_FACTOR : (1.0f / ZOOM_FACTOR), |
| 101 | this->size().width() / 2, this->size().height() / 2); |
| 102 | } |
| 103 | |
| 104 | void SkCanvasWidget::snapWidgetTransform() { |
| 105 | double x, y; |
| 106 | modf(fUserMatrix.getTranslateX(), &x); |
| 107 | modf(fUserMatrix.getTranslateY(), &y); |
| 108 | fUserMatrix[SkMatrix::kMTransX] = x; |
| 109 | fUserMatrix[SkMatrix::kMTransY] = y; |
| 110 | fDebugger->setUserMatrix(fUserMatrix); |
| 111 | drawTo(fDebugger->index()); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 112 | } |
| 113 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 114 | void SkCanvasWidget::resetWidgetTransform() { |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 115 | fUserMatrix.reset(); |
| 116 | fDebugger->setUserMatrix(fUserMatrix); |
| 117 | emit scaleFactorChanged(fUserMatrix.getScaleX()); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 118 | drawTo(fDebugger->index()); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 119 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 120 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 121 | void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) { |
| 122 | if (type == kRaster_8888_WidgetType) { |
| 123 | fRasterWidget.setHidden(isHidden); |
skia.committer@gmail.com | 3d18d06 | 2013-02-14 07:01:34 +0000 | [diff] [blame] | 124 | } |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 125 | #if SK_SUPPORT_GPU |
| 126 | else if (type == kGPU_WidgetType) { |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 127 | fGLWidget.setHidden(isHidden); |
| 128 | } |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 129 | #endif |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 130 | } |
chudy@google.com | 2f89179 | 2012-07-03 16:05:59 +0000 | [diff] [blame] | 131 | |
commit-bot@chromium.org | fde1e7c | 2013-08-02 13:59:50 +0000 | [diff] [blame] | 132 | #if SK_SUPPORT_GPU |
| 133 | void SkCanvasWidget::setGLSampleCount(int sampleCount) |
| 134 | { |
| 135 | fGLWidget.setSampleCount(sampleCount); |
| 136 | } |
| 137 | #endif |
| 138 | |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 139 | void SkCanvasWidget::zoom(float scale, int px, int py) { |
| 140 | fUserMatrix.postScale(scale, scale, px, py); |
| 141 | emit scaleFactorChanged(fUserMatrix.getScaleX()); |
| 142 | fDebugger->setUserMatrix(fUserMatrix); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 143 | drawTo(fDebugger->index()); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 144 | } |