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