blob: 47db709d551209f3b7b47442180ffccd91b545ae [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.comea5488b2012-07-26 19:38:22 +000012SkCanvasWidget::SkCanvasWidget(QWidget* parent) : QWidget(parent)
13 , fHorizontalLayout(this)
14 , fRasterWidget(this)
15 , fGLWidget(this)
16{
17 fHorizontalLayout.setSpacing(6);
18 fHorizontalLayout.setContentsMargins(0,0,0,0);
19 fRasterWidget.setSizePolicy(QSizePolicy::Expanding,
20 QSizePolicy::Expanding);
21 fGLWidget.setSizePolicy(QSizePolicy::Expanding,
22 QSizePolicy::Expanding);
chudy@google.com902ebe52012-06-29 14:21:22 +000023
chudy@google.comea5488b2012-07-26 19:38:22 +000024 fHorizontalLayout.addWidget(&fRasterWidget);
25 fHorizontalLayout.addWidget(&fGLWidget);
chudy@google.com80a4a602012-07-30 18:54:07 +000026 fDebugCanvas = NULL;
chudy@google.com2f891792012-07-03 16:05:59 +000027
chudy@google.com2f891792012-07-03 16:05:59 +000028 fIndex = 0;
29 fPreviousPoint.set(0,0);
30 fTransform.set(0,0);
chudy@google.comea5488b2012-07-26 19:38:22 +000031 fScaleFactor = 1.0;
chudy@google.com2f891792012-07-03 16:05:59 +000032
chudy@google.comea5488b2012-07-26 19:38:22 +000033 setWidgetVisibility(kGPU_WidgetType, true);
chudy@google.com80a4a602012-07-30 18:54:07 +000034 this->setDisabled(true);
chudy@google.com902ebe52012-06-29 14:21:22 +000035}
36
chudy@google.comea5488b2012-07-26 19:38:22 +000037void SkCanvasWidget::drawTo(int index) {
38 fIndex = index;
39 if (!fRasterWidget.isHidden()) {
40 fRasterWidget.drawTo(index);
chudy@google.com2f891792012-07-03 16:05:59 +000041 }
chudy@google.comea5488b2012-07-26 19:38:22 +000042 if (!fGLWidget.isHidden()) {
43 fGLWidget.drawTo(index);
44 }
chudy@google.com7dcae672012-07-09 20:26:53 +000045 emit commandChanged(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +000046}
47
48void SkCanvasWidget::loadPicture(QString filename) {
chudy@google.com80a4a602012-07-30 18:54:07 +000049 this->setDisabled(false);
chudy@google.comea5488b2012-07-26 19:38:22 +000050 SkStream* stream = new SkFILEStream(filename.toAscii());
51 SkPicture* picture = new SkPicture(stream);
chudy@google.com902ebe52012-06-29 14:21:22 +000052
chudy@google.comea5488b2012-07-26 19:38:22 +000053 /* TODO(chudy): Implement function that doesn't require new
54 * instantiation of debug canvas. */
chudy@google.com902ebe52012-06-29 14:21:22 +000055 delete fDebugCanvas;
chudy@google.com80a4a602012-07-30 18:54:07 +000056 fDebugCanvas = new SkDebugCanvas(picture->width(), picture->height());
57
chudy@google.com902ebe52012-06-29 14:21:22 +000058 picture->draw(fDebugCanvas);
chudy@google.com2f891792012-07-03 16:05:59 +000059 fIndex = fDebugCanvas->getSize();
chudy@google.comea5488b2012-07-26 19:38:22 +000060 fRasterWidget.setDebugCanvas(fDebugCanvas);
61 fGLWidget.setDebugCanvas(fDebugCanvas);
chudy@google.com80a4a602012-07-30 18:54:07 +000062
63 // TODO(chudy): Remove bounds from debug canvas storage.
64 fDebugCanvas->setBounds(this->width(), this->height());
chudy@google.com2f891792012-07-03 16:05:59 +000065}
66
67void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
chudy@google.com2f891792012-07-03 16:05:59 +000068 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
69 fTransform += eventPoint - fPreviousPoint;
70 fPreviousPoint = eventPoint;
chudy@google.comea5488b2012-07-26 19:38:22 +000071 updateWidgetTransform(kTranslate);
chudy@google.com7dcae672012-07-09 20:26:53 +000072 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +000073}
74
75void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
76 fPreviousPoint.set(event->globalX(), event->globalY());
chudy@google.com80a4a602012-07-30 18:54:07 +000077 if (fDebugCanvas) {
78 fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y());
79 fDebugCanvas->isCalculatingHits(true);
80 drawTo(fIndex);
81 emit hitChanged(fDebugCanvas->getHitBoxPoint());
82 fDebugCanvas->isCalculatingHits(false);
83 }
chudy@google.com2f891792012-07-03 16:05:59 +000084}
85
86void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
87 fTransform.set(0,0);
chudy@google.com7dcae672012-07-09 20:26:53 +000088 fScaleFactor = 1.0;
89 emit scaleFactorChanged(fScaleFactor);
chudy@google.comea5488b2012-07-26 19:38:22 +000090 // TODO(chudy): Change to signal / slot mechanism.
91 resetWidgetTransform();
chudy@google.com7dcae672012-07-09 20:26:53 +000092 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +000093}
94
chudy@google.comea5488b2012-07-26 19:38:22 +000095void SkCanvasWidget::resetWidgetTransform() {
96 fTransform.set(0,0);
97 fScaleFactor = 1.0;
98 updateWidgetTransform(kTranslate);
99 updateWidgetTransform(kScale);
100}
chudy@google.com902ebe52012-06-29 14:21:22 +0000101
chudy@google.comea5488b2012-07-26 19:38:22 +0000102void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) {
103 if (type == kRaster_8888_WidgetType) {
104 fRasterWidget.setHidden(isHidden);
105 } else if (type == kGPU_WidgetType) {
106 fGLWidget.setHidden(isHidden);
107 }
108}
chudy@google.com2f891792012-07-03 16:05:59 +0000109
chudy@google.comea5488b2012-07-26 19:38:22 +0000110void SkCanvasWidget::updateWidgetTransform(TransformType type) {
111 if (type == kTranslate) {
112 fRasterWidget.setTranslate(fTransform);
113 fGLWidget.setTranslate(fTransform);
114 } else if (type == kScale) {
115 fRasterWidget.setScale(fScaleFactor);
116 fGLWidget.setScale(fScaleFactor);
117 }
chudy@google.com2f891792012-07-03 16:05:59 +0000118}
119
chudy@google.coma1226312012-07-26 20:26:44 +0000120void SkCanvasWidget::zoom(float zoomIncrement) {
121 fScaleFactor += zoomIncrement;
chudy@google.com2f891792012-07-03 16:05:59 +0000122
123 /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
chudy@google.coma1226312012-07-26 20:26:44 +0000124 * Based on the code below, -1 and 1 both scale the image to it's original
125 * size we do the following to never have a registered wheel scroll
126 * not effect the fScaleFactor. */
chudy@google.com2f891792012-07-03 16:05:59 +0000127 if (fScaleFactor == 0) {
chudy@google.coma1226312012-07-26 20:26:44 +0000128 fScaleFactor = 2 * zoomIncrement;
chudy@google.com902ebe52012-06-29 14:21:22 +0000129 }
chudy@google.com7dcae672012-07-09 20:26:53 +0000130 emit scaleFactorChanged(fScaleFactor);
chudy@google.comea5488b2012-07-26 19:38:22 +0000131 updateWidgetTransform(kScale);
chudy@google.com7dcae672012-07-09 20:26:53 +0000132 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +0000133}