blob: e4716af439ca31274e99621f26f1800bb6ac244b [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.com2d537a12012-07-31 12:49:52 +000012SkCanvasWidget::SkCanvasWidget() : QWidget()
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);
28 fTransform.set(0,0);
chudy@google.comea5488b2012-07-26 19:38:22 +000029 fScaleFactor = 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.com902ebe52012-06-29 14:21:22 +000033}
34
chudy@google.com2d537a12012-07-31 12:49:52 +000035SkCanvasWidget::~SkCanvasWidget() {
36 if (fDebugCanvas) {
37 delete fDebugCanvas;
38 }
39}
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.com2f891792012-07-03 16:05:59 +000063 fIndex = fDebugCanvas->getSize();
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
67 // TODO(chudy): Remove bounds from debug canvas storage.
68 fDebugCanvas->setBounds(this->width(), this->height());
chudy@google.com2f891792012-07-03 16:05:59 +000069}
70
71void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
chudy@google.com2f891792012-07-03 16:05:59 +000072 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
73 fTransform += eventPoint - fPreviousPoint;
74 fPreviousPoint = eventPoint;
chudy@google.comea5488b2012-07-26 19:38:22 +000075 updateWidgetTransform(kTranslate);
chudy@google.com7dcae672012-07-09 20:26:53 +000076 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +000077}
78
79void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
80 fPreviousPoint.set(event->globalX(), event->globalY());
chudy@google.com80a4a602012-07-30 18:54:07 +000081 if (fDebugCanvas) {
82 fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y());
83 fDebugCanvas->isCalculatingHits(true);
84 drawTo(fIndex);
85 emit hitChanged(fDebugCanvas->getHitBoxPoint());
86 fDebugCanvas->isCalculatingHits(false);
87 }
chudy@google.com2f891792012-07-03 16:05:59 +000088}
89
90void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
91 fTransform.set(0,0);
chudy@google.com7dcae672012-07-09 20:26:53 +000092 fScaleFactor = 1.0;
93 emit scaleFactorChanged(fScaleFactor);
chudy@google.comea5488b2012-07-26 19:38:22 +000094 // TODO(chudy): Change to signal / slot mechanism.
95 resetWidgetTransform();
chudy@google.com7dcae672012-07-09 20:26:53 +000096 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +000097}
98
chudy@google.comea5488b2012-07-26 19:38:22 +000099void SkCanvasWidget::resetWidgetTransform() {
100 fTransform.set(0,0);
101 fScaleFactor = 1.0;
102 updateWidgetTransform(kTranslate);
103 updateWidgetTransform(kScale);
104}
chudy@google.com902ebe52012-06-29 14:21:22 +0000105
chudy@google.comea5488b2012-07-26 19:38:22 +0000106void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) {
107 if (type == kRaster_8888_WidgetType) {
108 fRasterWidget.setHidden(isHidden);
109 } else if (type == kGPU_WidgetType) {
110 fGLWidget.setHidden(isHidden);
111 }
112}
chudy@google.com2f891792012-07-03 16:05:59 +0000113
chudy@google.comea5488b2012-07-26 19:38:22 +0000114void SkCanvasWidget::updateWidgetTransform(TransformType type) {
115 if (type == kTranslate) {
116 fRasterWidget.setTranslate(fTransform);
117 fGLWidget.setTranslate(fTransform);
118 } else if (type == kScale) {
119 fRasterWidget.setScale(fScaleFactor);
120 fGLWidget.setScale(fScaleFactor);
121 }
chudy@google.com2f891792012-07-03 16:05:59 +0000122}
123
chudy@google.coma1226312012-07-26 20:26:44 +0000124void SkCanvasWidget::zoom(float zoomIncrement) {
125 fScaleFactor += zoomIncrement;
chudy@google.com2f891792012-07-03 16:05:59 +0000126
127 /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
chudy@google.coma1226312012-07-26 20:26:44 +0000128 * Based on the code below, -1 and 1 both scale the image to it's original
129 * size we do the following to never have a registered wheel scroll
130 * not effect the fScaleFactor. */
chudy@google.com2f891792012-07-03 16:05:59 +0000131 if (fScaleFactor == 0) {
chudy@google.coma1226312012-07-26 20:26:44 +0000132 fScaleFactor = 2 * zoomIncrement;
chudy@google.com902ebe52012-06-29 14:21:22 +0000133 }
chudy@google.com7dcae672012-07-09 20:26:53 +0000134 emit scaleFactorChanged(fScaleFactor);
chudy@google.comea5488b2012-07-26 19:38:22 +0000135 updateWidgetTransform(kScale);
chudy@google.com7dcae672012-07-09 20:26:53 +0000136 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +0000137}