blob: ee9e5dacb811240c8c2eb997ddd3fb35ef7b8472 [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.coma9e937c2012-08-03 17:32:05 +000012SkCanvasWidget::SkCanvasWidget(QWidget* parent) : QWidget(parent)
chudy@google.comea5488b2012-07-26 19:38:22 +000013 , fHorizontalLayout(this)
chudy@google.comea5488b2012-07-26 19:38:22 +000014{
15 fHorizontalLayout.setSpacing(6);
16 fHorizontalLayout.setContentsMargins(0,0,0,0);
17 fRasterWidget.setSizePolicy(QSizePolicy::Expanding,
18 QSizePolicy::Expanding);
19 fGLWidget.setSizePolicy(QSizePolicy::Expanding,
20 QSizePolicy::Expanding);
chudy@google.com902ebe52012-06-29 14:21:22 +000021
chudy@google.comea5488b2012-07-26 19:38:22 +000022 fHorizontalLayout.addWidget(&fRasterWidget);
23 fHorizontalLayout.addWidget(&fGLWidget);
chudy@google.com80a4a602012-07-30 18:54:07 +000024 fDebugCanvas = NULL;
chudy@google.com2f891792012-07-03 16:05:59 +000025
chudy@google.com2f891792012-07-03 16:05:59 +000026 fIndex = 0;
27 fPreviousPoint.set(0,0);
chudy@google.com830b8792012-08-01 15:57:52 +000028 fUserOffset.set(0,0);
29 fUserScaleFactor = 1.0;
chudy@google.com2f891792012-07-03 16:05:59 +000030
chudy@google.comea5488b2012-07-26 19:38:22 +000031 setWidgetVisibility(kGPU_WidgetType, true);
chudy@google.com80a4a602012-07-30 18:54:07 +000032 this->setDisabled(true);
chudy@google.coma9e937c2012-08-03 17:32:05 +000033 connect(&fRasterWidget, SIGNAL(drawComplete()),
34 this->parentWidget(), SLOT(drawComplete()));
chudy@google.com902ebe52012-06-29 14:21:22 +000035}
36
chudy@google.com2d537a12012-07-31 12:49:52 +000037SkCanvasWidget::~SkCanvasWidget() {
chudy@google.com0b5bbb02012-07-31 19:55:32 +000038 delete fDebugCanvas;
chudy@google.com2d537a12012-07-31 12:49:52 +000039}
40
chudy@google.comea5488b2012-07-26 19:38:22 +000041void SkCanvasWidget::drawTo(int index) {
42 fIndex = index;
43 if (!fRasterWidget.isHidden()) {
44 fRasterWidget.drawTo(index);
chudy@google.com2f891792012-07-03 16:05:59 +000045 }
chudy@google.comea5488b2012-07-26 19:38:22 +000046 if (!fGLWidget.isHidden()) {
47 fGLWidget.drawTo(index);
48 }
chudy@google.com7dcae672012-07-09 20:26:53 +000049 emit commandChanged(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +000050}
51
52void SkCanvasWidget::loadPicture(QString filename) {
chudy@google.com80a4a602012-07-30 18:54:07 +000053 this->setDisabled(false);
chudy@google.comea5488b2012-07-26 19:38:22 +000054 SkStream* stream = new SkFILEStream(filename.toAscii());
55 SkPicture* picture = new SkPicture(stream);
chudy@google.com902ebe52012-06-29 14:21:22 +000056
chudy@google.comea5488b2012-07-26 19:38:22 +000057 /* TODO(chudy): Implement function that doesn't require new
58 * instantiation of debug canvas. */
chudy@google.com902ebe52012-06-29 14:21:22 +000059 delete fDebugCanvas;
chudy@google.com80a4a602012-07-30 18:54:07 +000060 fDebugCanvas = new SkDebugCanvas(picture->width(), picture->height());
61
chudy@google.com902ebe52012-06-29 14:21:22 +000062 picture->draw(fDebugCanvas);
chudy@google.com830b8792012-08-01 15:57:52 +000063 fIndex = fDebugCanvas->getSize() - 1;
chudy@google.comea5488b2012-07-26 19:38:22 +000064 fRasterWidget.setDebugCanvas(fDebugCanvas);
65 fGLWidget.setDebugCanvas(fDebugCanvas);
chudy@google.com80a4a602012-07-30 18:54:07 +000066 fDebugCanvas->setBounds(this->width(), this->height());
chudy@google.com2f891792012-07-03 16:05:59 +000067}
68
69void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
chudy@google.com2f891792012-07-03 16:05:59 +000070 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
chudy@google.com830b8792012-08-01 15:57:52 +000071 fUserOffset += eventPoint - fPreviousPoint;
chudy@google.com2f891792012-07-03 16:05:59 +000072 fPreviousPoint = eventPoint;
chudy@google.com830b8792012-08-01 15:57:52 +000073 fDebugCanvas->setUserOffset(fUserOffset);
chudy@google.com7dcae672012-07-09 20:26:53 +000074 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +000075}
76
77void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
78 fPreviousPoint.set(event->globalX(), event->globalY());
chudy@google.com751961d2012-07-31 20:07:42 +000079 emit hitChanged(fDebugCanvas->getCommandAtPoint(event->x(), event->y(),
chudy@google.com830b8792012-08-01 15:57:52 +000080 fIndex));
chudy@google.com2f891792012-07-03 16:05:59 +000081}
82
83void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
chudy@google.comea5488b2012-07-26 19:38:22 +000084 resetWidgetTransform();
chudy@google.com902ebe52012-06-29 14:21:22 +000085}
86
chudy@google.comea5488b2012-07-26 19:38:22 +000087void SkCanvasWidget::resetWidgetTransform() {
chudy@google.com830b8792012-08-01 15:57:52 +000088 fUserOffset.set(0,0);
89 fUserScaleFactor = 1.0;
90 fDebugCanvas->setUserOffset(fUserOffset);
91 fDebugCanvas->setUserScale(fUserScaleFactor);
92 emit scaleFactorChanged(fUserScaleFactor);
93 drawTo(fIndex);
chudy@google.comea5488b2012-07-26 19:38:22 +000094}
chudy@google.com902ebe52012-06-29 14:21:22 +000095
chudy@google.comea5488b2012-07-26 19:38:22 +000096void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) {
97 if (type == kRaster_8888_WidgetType) {
98 fRasterWidget.setHidden(isHidden);
99 } else if (type == kGPU_WidgetType) {
100 fGLWidget.setHidden(isHidden);
101 }
102}
chudy@google.com2f891792012-07-03 16:05:59 +0000103
chudy@google.coma1226312012-07-26 20:26:44 +0000104void SkCanvasWidget::zoom(float zoomIncrement) {
chudy@google.com830b8792012-08-01 15:57:52 +0000105 fUserScaleFactor += zoomIncrement;
chudy@google.com2f891792012-07-03 16:05:59 +0000106
chudy@google.com830b8792012-08-01 15:57:52 +0000107 /* The range of the fUserScaleFactor crosses over the range -1,0,1 frequently.
chudy@google.coma1226312012-07-26 20:26:44 +0000108 * Based on the code below, -1 and 1 both scale the image to it's original
109 * size we do the following to never have a registered wheel scroll
chudy@google.com830b8792012-08-01 15:57:52 +0000110 * not effect the fUserScaleFactor. */
111 if (fUserScaleFactor == 0) {
112 fUserScaleFactor = 2 * zoomIncrement;
chudy@google.com902ebe52012-06-29 14:21:22 +0000113 }
chudy@google.com830b8792012-08-01 15:57:52 +0000114 emit scaleFactorChanged(fUserScaleFactor);
115 fDebugCanvas->setUserScale(fUserScaleFactor);
chudy@google.com7dcae672012-07-09 20:26:53 +0000116 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +0000117}