• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SkCanvasWidget(QWidget * parent,SkDebugger * debugger)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 #if SK_SUPPORT_GPU
46     connect(&fGLWidget, SIGNAL(drawComplete()), this->parentWidget(), SLOT(drawComplete()));
47 #endif
48 }
49 
~SkCanvasWidget()50 SkCanvasWidget::~SkCanvasWidget() {}
51 
drawTo(int index)52 void SkCanvasWidget::drawTo(int index) {
53     fDebugger->setIndex(index);
54     fRasterWidget.updateImage();
55 #if SK_SUPPORT_GPU
56     fGLWidget.updateImage();
57 #endif
58     emit commandChanged(fDebugger->index());
59 }
60 
mouseMoveEvent(QMouseEvent * event)61 void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
62     SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
63     SkIPoint eventOffset = eventPoint - fPreviousPoint;
64     fPreviousPoint = eventPoint;
65     fUserMatrix.postTranslate(eventOffset.fX, eventOffset.fY);
66     fDebugger->setUserMatrix(fUserMatrix);
67     drawTo(fDebugger->index());
68 }
69 
mousePressEvent(QMouseEvent * event)70 void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
71     fPreviousPoint.set(event->globalX(), event->globalY());
72     emit hitChanged(fDebugger->getCommandAtPoint(event->x(), event->y(),
73             fDebugger->index()));
74 }
75 
mouseDoubleClickEvent(QMouseEvent * event)76 void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
77     Qt::KeyboardModifiers modifiers = event->modifiers();
78     if (modifiers.testFlag(Qt::ControlModifier)) {
79         snapWidgetTransform();
80     } else {
81         resetWidgetTransform();
82     }
83 }
84 
85 #define ZOOM_FACTOR (1.25f)
86 
wheelEvent(QWheelEvent * event)87 void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
88     Qt::KeyboardModifiers modifiers = event->modifiers();
89     if (modifiers.testFlag(Qt::ControlModifier)) {
90         zoom(event->delta() > 0 ? ZOOM_FACTOR : (1.0f / ZOOM_FACTOR), event->x(), event->y());
91     } else {
92         if (Qt::Horizontal == event->orientation()) {
93             fUserMatrix.postTranslate(event->delta(), 0.0f);
94         } else {
95             fUserMatrix.postTranslate(0.0f, event->delta());
96         }
97         fDebugger->setUserMatrix(fUserMatrix);
98         drawTo(fDebugger->index());
99     }
100 }
101 
zoom(int zoomCommand)102 void SkCanvasWidget::zoom(int zoomCommand) {
103     zoom(kIn_ZoomCommand == zoomCommand ? ZOOM_FACTOR : (1.0f / ZOOM_FACTOR),
104          this->size().width() / 2, this->size().height() / 2);
105 }
106 
snapWidgetTransform()107 void SkCanvasWidget::snapWidgetTransform() {
108     double x, y;
109     modf(fUserMatrix.getTranslateX(), &x);
110     modf(fUserMatrix.getTranslateY(), &y);
111     fUserMatrix[SkMatrix::kMTransX] = x;
112     fUserMatrix[SkMatrix::kMTransY] = y;
113     fDebugger->setUserMatrix(fUserMatrix);
114     drawTo(fDebugger->index());
115 }
116 
resetWidgetTransform()117 void SkCanvasWidget::resetWidgetTransform() {
118     fUserMatrix.reset();
119     fDebugger->setUserMatrix(fUserMatrix);
120     emit scaleFactorChanged(fUserMatrix.getScaleX());
121     drawTo(fDebugger->index());
122 }
123 
setWidgetVisibility(WidgetType type,bool isHidden)124 void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) {
125     if (type == kRaster_8888_WidgetType) {
126         fRasterWidget.setHidden(isHidden);
127     }
128 #if SK_SUPPORT_GPU
129     else if (type == kGPU_WidgetType) {
130         fGLWidget.setHidden(isHidden);
131     }
132 #endif
133 }
134 
135 #if SK_SUPPORT_GPU
setGLSampleCount(int sampleCount)136 void SkCanvasWidget::setGLSampleCount(int sampleCount)
137 {
138     fGLWidget.setSampleCount(sampleCount);
139 }
140 #endif
141 
zoom(float scale,int px,int py)142 void SkCanvasWidget::zoom(float scale, int px, int py) {
143     fUserMatrix.postScale(scale, scale, px, py);
144     emit scaleFactorChanged(fUserMatrix.getScaleX());
145     fDebugger->setUserMatrix(fUserMatrix);
146     drawTo(fDebugger->index());
147 }
148