blob: 8384fb11cf1dd48f4ee0853a77ae34acce4fe13b [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.com902ebe52012-06-29 14:21:22 +000026 fDebugCanvas = new SkDebugCanvas();
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.com902ebe52012-06-29 14:21:22 +000034}
35
chudy@google.comea5488b2012-07-26 19:38:22 +000036void SkCanvasWidget::drawTo(int index) {
37 fIndex = index;
38 if (!fRasterWidget.isHidden()) {
39 fRasterWidget.drawTo(index);
chudy@google.com2f891792012-07-03 16:05:59 +000040 }
chudy@google.comea5488b2012-07-26 19:38:22 +000041 if (!fGLWidget.isHidden()) {
42 fGLWidget.drawTo(index);
43 }
chudy@google.com7dcae672012-07-09 20:26:53 +000044 emit commandChanged(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +000045}
46
47void SkCanvasWidget::loadPicture(QString filename) {
chudy@google.comea5488b2012-07-26 19:38:22 +000048 SkStream* stream = new SkFILEStream(filename.toAscii());
49 SkPicture* picture = new SkPicture(stream);
chudy@google.com902ebe52012-06-29 14:21:22 +000050
chudy@google.comea5488b2012-07-26 19:38:22 +000051 /* TODO(chudy): Implement function that doesn't require new
52 * instantiation of debug canvas. */
chudy@google.com902ebe52012-06-29 14:21:22 +000053 delete fDebugCanvas;
54 fDebugCanvas = new SkDebugCanvas();
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000055 fDebugCanvas->setBounds(this->width(), this->height());
chudy@google.com902ebe52012-06-29 14:21:22 +000056 picture->draw(fDebugCanvas);
chudy@google.com2f891792012-07-03 16:05:59 +000057 fIndex = fDebugCanvas->getSize();
chudy@google.comea5488b2012-07-26 19:38:22 +000058 fRasterWidget.setDebugCanvas(fDebugCanvas);
59 fGLWidget.setDebugCanvas(fDebugCanvas);
chudy@google.com2f891792012-07-03 16:05:59 +000060}
61
62void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
chudy@google.com2f891792012-07-03 16:05:59 +000063 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
64 fTransform += eventPoint - fPreviousPoint;
65 fPreviousPoint = eventPoint;
chudy@google.comea5488b2012-07-26 19:38:22 +000066 updateWidgetTransform(kTranslate);
chudy@google.com7dcae672012-07-09 20:26:53 +000067 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +000068}
69
70void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
71 fPreviousPoint.set(event->globalX(), event->globalY());
chudy@google.come606d6e2012-07-12 14:31:25 +000072 fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y());
73 fDebugCanvas->isCalculatingHits(true);
74 drawTo(fIndex);
75 emit hitChanged(fDebugCanvas->getHitBoxPoint());
76 fDebugCanvas->isCalculatingHits(false);
chudy@google.com2f891792012-07-03 16:05:59 +000077}
78
79void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
80 fTransform.set(0,0);
chudy@google.com7dcae672012-07-09 20:26:53 +000081 fScaleFactor = 1.0;
82 emit scaleFactorChanged(fScaleFactor);
chudy@google.comea5488b2012-07-26 19:38:22 +000083 // TODO(chudy): Change to signal / slot mechanism.
84 resetWidgetTransform();
chudy@google.com7dcae672012-07-09 20:26:53 +000085 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +000086}
87
chudy@google.comea5488b2012-07-26 19:38:22 +000088void SkCanvasWidget::resetWidgetTransform() {
89 fTransform.set(0,0);
90 fScaleFactor = 1.0;
91 updateWidgetTransform(kTranslate);
92 updateWidgetTransform(kScale);
93}
chudy@google.com902ebe52012-06-29 14:21:22 +000094
chudy@google.comea5488b2012-07-26 19:38:22 +000095void SkCanvasWidget::setWidgetVisibility(WidgetType type, bool isHidden) {
96 if (type == kRaster_8888_WidgetType) {
97 fRasterWidget.setHidden(isHidden);
98 } else if (type == kGPU_WidgetType) {
99 fGLWidget.setHidden(isHidden);
100 }
101}
chudy@google.com2f891792012-07-03 16:05:59 +0000102
chudy@google.comea5488b2012-07-26 19:38:22 +0000103void SkCanvasWidget::updateWidgetTransform(TransformType type) {
104 if (type == kTranslate) {
105 fRasterWidget.setTranslate(fTransform);
106 fGLWidget.setTranslate(fTransform);
107 } else if (type == kScale) {
108 fRasterWidget.setScale(fScaleFactor);
109 fGLWidget.setScale(fScaleFactor);
110 }
chudy@google.com2f891792012-07-03 16:05:59 +0000111}
112
113void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
114 fScaleFactor += event->delta()/120;
115
116 /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
117 * Based on the code below, -1 and 1 both scale the image to it's original
118 * size we do the following to never have a registered wheel scroll
119 * not effect the fScaleFactor. */
120 if (fScaleFactor == 0) {
121 fScaleFactor += (event->delta()/120) * 2;
chudy@google.com902ebe52012-06-29 14:21:22 +0000122 }
chudy@google.com7dcae672012-07-09 20:26:53 +0000123 emit scaleFactorChanged(fScaleFactor);
chudy@google.comea5488b2012-07-26 19:38:22 +0000124 updateWidgetTransform(kScale);
chudy@google.com7dcae672012-07-09 20:26:53 +0000125 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +0000126}