blob: 8c660a4a23bf0e90650c3e61993b3c28bb718a6d [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
10#include "SkPicture.h"
11#include "SkStream.h"
12#include "SkCanvasWidget.h"
chudy@google.com2f891792012-07-03 16:05:59 +000013#include "SkColor.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000014
15SkCanvasWidget::SkCanvasWidget(QWidget *parent) :
16 QWidget(parent) {
17
chudy@google.com2f891792012-07-03 16:05:59 +000018 /* TODO(chudy): The 800x800 is a default number. Change it to be
19 * set dynamically. Also need to pass size into debugCanvas for current
20 * command filter. */
21 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
22 fBitmap.allocPixels();
23 fBitmap.eraseColor(0);
24
25 /* TODO(chudy): Add fCanvas, fDevice to the stack. The bitmap being
26 * cleared does get rid of fDevices link to it. See if there's someway around
27 * it so that we don't have to delete both canvas and device to clear out
28 * the bitmap. */
29 fDevice = new SkDevice(fBitmap);
chudy@google.com902ebe52012-06-29 14:21:22 +000030 fCanvas = new SkCanvas(fDevice);
31 fDebugCanvas = new SkDebugCanvas();
chudy@google.com2f891792012-07-03 16:05:59 +000032
chudy@google.com7dcae672012-07-09 20:26:53 +000033 fScaleFactor = 1.0;
chudy@google.com2f891792012-07-03 16:05:59 +000034 fIndex = 0;
35 fPreviousPoint.set(0,0);
36 fTransform.set(0,0);
37
chudy@google.com902ebe52012-06-29 14:21:22 +000038 this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
39}
40
chudy@google.com2f891792012-07-03 16:05:59 +000041SkCanvasWidget::~SkCanvasWidget() {
42 delete fCanvas;
43 delete fDevice;
44 delete fDebugCanvas;
45}
chudy@google.com902ebe52012-06-29 14:21:22 +000046
chudy@google.com2f891792012-07-03 16:05:59 +000047void SkCanvasWidget::resizeEvent(QResizeEvent* event) {
48 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height());
49 fBitmap.allocPixels();
50 fBitmap.eraseColor(0);
51
52 delete fCanvas;
53 delete fDevice;
54
55 fDevice = new SkDevice(fBitmap);
56 fCanvas = new SkCanvas(fDevice);
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000057 fDebugCanvas->setBounds(event->size().width(), event->size().height());
chudy@google.come606d6e2012-07-12 14:31:25 +000058 fDebugCanvas->drawTo(fCanvas, fIndex+1, &fBitmap);
chudy@google.com2f891792012-07-03 16:05:59 +000059 this->update();
60}
61
62void SkCanvasWidget::drawTo(int fIndex) {
chudy@google.com902ebe52012-06-29 14:21:22 +000063 delete fCanvas;
64 fCanvas = new SkCanvas(fDevice);
chudy@google.com2f891792012-07-03 16:05:59 +000065
66 fCanvas->translate(fTransform.fX, fTransform.fY);
67 if(fScaleFactor < 0) {
68 fCanvas->scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor));
69 } else if (fScaleFactor > 0) {
70 fCanvas->scale(fScaleFactor, fScaleFactor);
71 }
72
chudy@google.com7dcae672012-07-09 20:26:53 +000073 emit commandChanged(fIndex);
chudy@google.come606d6e2012-07-12 14:31:25 +000074 fDebugCanvas->drawTo(fCanvas, fIndex+1, &fBitmap);
chudy@google.com902ebe52012-06-29 14:21:22 +000075 this->update();
chudy@google.com2f891792012-07-03 16:05:59 +000076 this->fIndex = fIndex;
chudy@google.com902ebe52012-06-29 14:21:22 +000077}
78
79void SkCanvasWidget::loadPicture(QString filename) {
80 SkStream *stream = new SkFILEStream(filename.toAscii());
81 SkPicture *picture = new SkPicture(stream);
82
83 delete fDebugCanvas;
84 fDebugCanvas = new SkDebugCanvas();
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000085 fDebugCanvas->setBounds(this->width(), this->height());
chudy@google.com902ebe52012-06-29 14:21:22 +000086
87 picture->draw(fDebugCanvas);
88 fDebugCanvas->draw(fCanvas);
89
chudy@google.com2f891792012-07-03 16:05:59 +000090 fIndex = fDebugCanvas->getSize();
91
92 SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1);
93
94 int r = SkColorGetR(color);
95 int g = SkColorGetG(color);
96 int b = SkColorGetB(color);
97
chudy@google.com902ebe52012-06-29 14:21:22 +000098 /* NOTE(chudy): This was a test to determine if the canvas size is accurately
99 * saved in the bounds of the recorded picture. It is not. Everyone of the
100 * sample GM images is 1000x1000. Even the one that claims it is
101 * 2048x2048.
102 std::cout << "Width: " << picture->width();
103 std::cout << " Height: " << picture->height() << std::endl; */
104
chudy@google.com2f891792012-07-03 16:05:59 +0000105 /* Updated style sheet without a background specified. If not removed
106 * QPainter paints the specified background color on top of our canvas. */
107
108 QString style("QWidget {border: 1px solid #cccccc; background-color: #");
109 style.append(QString::number(r, 16));
110 style.append(QString::number(g, 16));
111 style.append(QString::number(b, 16));
112 style.append(";}");
113 this->setStyleSheet(style);
114 this->update();
115}
116
117void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
118
119 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
120 fTransform += eventPoint - fPreviousPoint;
121 fPreviousPoint = eventPoint;
122
123 // TODO(chudy): Fix and remove +1 from drawTo calls.
chudy@google.com7dcae672012-07-09 20:26:53 +0000124 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +0000125 this->update();
126}
127
128void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
129 fPreviousPoint.set(event->globalX(), event->globalY());
chudy@google.come606d6e2012-07-12 14:31:25 +0000130 fDebugCanvas->getBoxClass()->setHitPoint(event->x(), event->y());
131 fDebugCanvas->isCalculatingHits(true);
132 drawTo(fIndex);
133 emit hitChanged(fDebugCanvas->getHitBoxPoint());
134 fDebugCanvas->isCalculatingHits(false);
chudy@google.com2f891792012-07-03 16:05:59 +0000135}
136
137void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
138 fTransform.set(0,0);
chudy@google.com7dcae672012-07-09 20:26:53 +0000139 fScaleFactor = 1.0;
140 emit scaleFactorChanged(fScaleFactor);
141 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +0000142 this->update();
143}
144
145void SkCanvasWidget::paintEvent(QPaintEvent *event) {
146 QPainter painter(this);
147 QStyleOption opt;
148 opt.init(this);
149
chudy@google.com2f891792012-07-03 16:05:59 +0000150 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
151
152 QPoint origin(0,0);
153 QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
154 fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
155
156 painter.drawImage(origin, image);
157 painter.end();
158}
159
160void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
161 fScaleFactor += event->delta()/120;
162
163 /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
164 * Based on the code below, -1 and 1 both scale the image to it's original
165 * size we do the following to never have a registered wheel scroll
166 * not effect the fScaleFactor. */
167 if (fScaleFactor == 0) {
168 fScaleFactor += (event->delta()/120) * 2;
chudy@google.com902ebe52012-06-29 14:21:22 +0000169 }
170
chudy@google.com7dcae672012-07-09 20:26:53 +0000171 emit scaleFactorChanged(fScaleFactor);
172
chudy@google.com2f891792012-07-03 16:05:59 +0000173 // TODO(chudy): Fix and remove +1 from drawTo calls.
chudy@google.com7dcae672012-07-09 20:26:53 +0000174 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +0000175 this->update();
chudy@google.com902ebe52012-06-29 14:21:22 +0000176}