blob: 2f714d79ae8f3b17c77f0cabae28bbe5bd47d231 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001
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.com902ebe52012-06-29 14:21:22 +000010#include "SkCanvasWidget.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000011
chudy@google.com607357f2012-08-07 16:12:23 +000012SkCanvasWidget::SkCanvasWidget(QWidget* parent,
13 SkDebugger* debugger) : QWidget(parent)
chudy@google.comea5488b2012-07-26 19:38:22 +000014 , fHorizontalLayout(this)
chudy@google.com607357f2012-08-07 16:12:23 +000015 , fRasterWidget(debugger)
16 , fGLWidget(debugger)
chudy@google.comea5488b2012-07-26 19:38:22 +000017{
chudy@google.com607357f2012-08-07 16:12:23 +000018
19 fDebugger = debugger;
20
chudy@google.comea5488b2012-07-26 19:38:22 +000021 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.com902ebe52012-06-29 14:21:22 +000027
chudy@google.comea5488b2012-07-26 19:38:22 +000028 fHorizontalLayout.addWidget(&fRasterWidget);
29 fHorizontalLayout.addWidget(&fGLWidget);
chudy@google.com2f891792012-07-03 16:05:59 +000030
chudy@google.com2f891792012-07-03 16:05:59 +000031 fPreviousPoint.set(0,0);
chudy@google.com830b8792012-08-01 15:57:52 +000032 fUserOffset.set(0,0);
33 fUserScaleFactor = 1.0;
chudy@google.com2f891792012-07-03 16:05:59 +000034
chudy@google.comea5488b2012-07-26 19:38:22 +000035 setWidgetVisibility(kGPU_WidgetType, true);
chudy@google.coma9e937c2012-08-03 17:32:05 +000036 connect(&fRasterWidget, SIGNAL(drawComplete()),
37 this->parentWidget(), SLOT(drawComplete()));
chudy@google.com902ebe52012-06-29 14:21:22 +000038}
39
chudy@google.com607357f2012-08-07 16:12:23 +000040SkCanvasWidget::~SkCanvasWidget() {}
chudy@google.com2d537a12012-07-31 12:49:52 +000041
chudy@google.comea5488b2012-07-26 19:38:22 +000042void SkCanvasWidget::drawTo(int index) {
chudy@google.com607357f2012-08-07 16:12:23 +000043 fDebugger->setIndex(index);
44 fRasterWidget.draw();
45 fGLWidget.draw();
46 emit commandChanged(fDebugger->index());
chudy@google.com2f891792012-07-03 16:05:59 +000047}
48
49void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
chudy@google.com2f891792012-07-03 16:05:59 +000050 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
chudy@google.com830b8792012-08-01 15:57:52 +000051 fUserOffset += eventPoint - fPreviousPoint;
chudy@google.com2f891792012-07-03 16:05:59 +000052 fPreviousPoint = eventPoint;
chudy@google.com607357f2012-08-07 16:12:23 +000053 fDebugger->setUserOffset(fUserOffset);
54 drawTo(fDebugger->index());
chudy@google.com2f891792012-07-03 16:05:59 +000055}
56
57void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
58 fPreviousPoint.set(event->globalX(), event->globalY());
chudy@google.com607357f2012-08-07 16:12:23 +000059 emit hitChanged(fDebugger->getCommandAtPoint(event->x(), event->y(),
60 fDebugger->index()));
chudy@google.com2f891792012-07-03 16:05:59 +000061}
62
63void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
chudy@google.comea5488b2012-07-26 19:38:22 +000064 resetWidgetTransform();
chudy@google.com902ebe52012-06-29 14:21:22 +000065}
66
chudy@google.comea5488b2012-07-26 19:38:22 +000067void SkCanvasWidget::resetWidgetTransform() {
chudy@google.com830b8792012-08-01 15:57:52 +000068 fUserOffset.set(0,0);
69 fUserScaleFactor = 1.0;
chudy@google.com607357f2012-08-07 16:12:23 +000070 fDebugger->setUserOffset(fUserOffset);
71 fDebugger->setUserScale(fUserScaleFactor);
chudy@google.com830b8792012-08-01 15:57:52 +000072 emit scaleFactorChanged(fUserScaleFactor);
chudy@google.com607357f2012-08-07 16:12:23 +000073 drawTo(fDebugger->index());
chudy@google.comea5488b2012-07-26 19:38:22 +000074}
chudy@google.com902ebe52012-06-29 14:21:22 +000075
chudy@google.comea5488b2012-07-26 19:38:22 +000076void 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.com2f891792012-07-03 16:05:59 +000083
chudy@google.coma1226312012-07-26 20:26:44 +000084void SkCanvasWidget::zoom(float zoomIncrement) {
chudy@google.com830b8792012-08-01 15:57:52 +000085 fUserScaleFactor += zoomIncrement;
chudy@google.com2f891792012-07-03 16:05:59 +000086
chudy@google.com830b8792012-08-01 15:57:52 +000087 /* The range of the fUserScaleFactor crosses over the range -1,0,1 frequently.
chudy@google.coma1226312012-07-26 20:26:44 +000088 * 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.com830b8792012-08-01 15:57:52 +000090 * not effect the fUserScaleFactor. */
91 if (fUserScaleFactor == 0) {
92 fUserScaleFactor = 2 * zoomIncrement;
chudy@google.com902ebe52012-06-29 14:21:22 +000093 }
chudy@google.com830b8792012-08-01 15:57:52 +000094 emit scaleFactorChanged(fUserScaleFactor);
chudy@google.com607357f2012-08-07 16:12:23 +000095 fDebugger->setUserScale(fUserScaleFactor);
96 drawTo(fDebugger->index());
chudy@google.com902ebe52012-06-29 14:21:22 +000097}