blob: 6b5faa4bec8862d826d48dfd28fdd5e302876638 [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#include <iostream>
15
16SkCanvasWidget::SkCanvasWidget(QWidget *parent) :
17 QWidget(parent) {
18
chudy@google.com2f891792012-07-03 16:05:59 +000019 /* TODO(chudy): The 800x800 is a default number. Change it to be
20 * set dynamically. Also need to pass size into debugCanvas for current
21 * command filter. */
22 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
23 fBitmap.allocPixels();
24 fBitmap.eraseColor(0);
25
26 /* TODO(chudy): Add fCanvas, fDevice to the stack. The bitmap being
27 * cleared does get rid of fDevices link to it. See if there's someway around
28 * it so that we don't have to delete both canvas and device to clear out
29 * the bitmap. */
30 fDevice = new SkDevice(fBitmap);
chudy@google.com902ebe52012-06-29 14:21:22 +000031 fCanvas = new SkCanvas(fDevice);
32 fDebugCanvas = new SkDebugCanvas();
chudy@google.com2f891792012-07-03 16:05:59 +000033
chudy@google.com7dcae672012-07-09 20:26:53 +000034 fScaleFactor = 1.0;
chudy@google.com2f891792012-07-03 16:05:59 +000035 fIndex = 0;
36 fPreviousPoint.set(0,0);
37 fTransform.set(0,0);
38
chudy@google.com902ebe52012-06-29 14:21:22 +000039 this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
40}
41
chudy@google.com2f891792012-07-03 16:05:59 +000042SkCanvasWidget::~SkCanvasWidget() {
43 delete fCanvas;
44 delete fDevice;
45 delete fDebugCanvas;
46}
chudy@google.com902ebe52012-06-29 14:21:22 +000047
chudy@google.com2f891792012-07-03 16:05:59 +000048void SkCanvasWidget::resizeEvent(QResizeEvent* event) {
49 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height());
50 fBitmap.allocPixels();
51 fBitmap.eraseColor(0);
52
53 delete fCanvas;
54 delete fDevice;
55
56 fDevice = new SkDevice(fBitmap);
57 fCanvas = new SkCanvas(fDevice);
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000058<<<<<<< HEAD
59 fDebugCanvas->setBounds(event->size().width(), event->size().height());
60 fDebugCanvas->drawTo(fCanvas, fIndex);
61=======
chudy@google.com7dcae672012-07-09 20:26:53 +000062 drawTo(fIndex);
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000063>>>>>>> refs/remotes/git-svn
chudy@google.com2f891792012-07-03 16:05:59 +000064 this->update();
65}
66
67void SkCanvasWidget::drawTo(int fIndex) {
chudy@google.com902ebe52012-06-29 14:21:22 +000068 delete fCanvas;
69 fCanvas = new SkCanvas(fDevice);
chudy@google.com2f891792012-07-03 16:05:59 +000070
71 fCanvas->translate(fTransform.fX, fTransform.fY);
72 if(fScaleFactor < 0) {
73 fCanvas->scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor));
74 } else if (fScaleFactor > 0) {
75 fCanvas->scale(fScaleFactor, fScaleFactor);
76 }
77
chudy@google.com7dcae672012-07-09 20:26:53 +000078 emit commandChanged(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +000079 fDebugCanvas->drawTo(fCanvas, fIndex+1);
chudy@google.com902ebe52012-06-29 14:21:22 +000080 this->update();
chudy@google.com2f891792012-07-03 16:05:59 +000081 this->fIndex = fIndex;
chudy@google.com902ebe52012-06-29 14:21:22 +000082}
83
84void SkCanvasWidget::loadPicture(QString filename) {
85 SkStream *stream = new SkFILEStream(filename.toAscii());
86 SkPicture *picture = new SkPicture(stream);
87
88 delete fDebugCanvas;
89 fDebugCanvas = new SkDebugCanvas();
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000090 fDebugCanvas->setBounds(this->width(), this->height());
chudy@google.com902ebe52012-06-29 14:21:22 +000091
92 picture->draw(fDebugCanvas);
93 fDebugCanvas->draw(fCanvas);
94
chudy@google.com2f891792012-07-03 16:05:59 +000095 fIndex = fDebugCanvas->getSize();
96
97 SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1);
98
99 int r = SkColorGetR(color);
100 int g = SkColorGetG(color);
101 int b = SkColorGetB(color);
102
chudy@google.com902ebe52012-06-29 14:21:22 +0000103 /* NOTE(chudy): This was a test to determine if the canvas size is accurately
104 * saved in the bounds of the recorded picture. It is not. Everyone of the
105 * sample GM images is 1000x1000. Even the one that claims it is
106 * 2048x2048.
107 std::cout << "Width: " << picture->width();
108 std::cout << " Height: " << picture->height() << std::endl; */
109
chudy@google.com2f891792012-07-03 16:05:59 +0000110 /* Updated style sheet without a background specified. If not removed
111 * QPainter paints the specified background color on top of our canvas. */
112
113 QString style("QWidget {border: 1px solid #cccccc; background-color: #");
114 style.append(QString::number(r, 16));
115 style.append(QString::number(g, 16));
116 style.append(QString::number(b, 16));
117 style.append(";}");
118 this->setStyleSheet(style);
119 this->update();
120}
121
122void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
123
124 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
125 fTransform += eventPoint - fPreviousPoint;
126 fPreviousPoint = eventPoint;
127
128 // TODO(chudy): Fix and remove +1 from drawTo calls.
chudy@google.com7dcae672012-07-09 20:26:53 +0000129 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +0000130 this->update();
131}
132
133void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
134 fPreviousPoint.set(event->globalX(), event->globalY());
135}
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}