blob: fe8db94dbd83c016f89d7f31af61e999f9e960a4 [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.com7dcae672012-07-09 20:26:53 +000058 drawTo(fIndex);
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.com2f891792012-07-03 16:05:59 +000074 fDebugCanvas->drawTo(fCanvas, fIndex+1);
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();
85
86 picture->draw(fDebugCanvas);
87 fDebugCanvas->draw(fCanvas);
88
chudy@google.com2f891792012-07-03 16:05:59 +000089 fIndex = fDebugCanvas->getSize();
90
91 SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1);
92
93 int r = SkColorGetR(color);
94 int g = SkColorGetG(color);
95 int b = SkColorGetB(color);
96
chudy@google.com902ebe52012-06-29 14:21:22 +000097 /* NOTE(chudy): This was a test to determine if the canvas size is accurately
98 * saved in the bounds of the recorded picture. It is not. Everyone of the
99 * sample GM images is 1000x1000. Even the one that claims it is
100 * 2048x2048.
101 std::cout << "Width: " << picture->width();
102 std::cout << " Height: " << picture->height() << std::endl; */
103
chudy@google.com2f891792012-07-03 16:05:59 +0000104 /* Updated style sheet without a background specified. If not removed
105 * QPainter paints the specified background color on top of our canvas. */
106
107 QString style("QWidget {border: 1px solid #cccccc; background-color: #");
108 style.append(QString::number(r, 16));
109 style.append(QString::number(g, 16));
110 style.append(QString::number(b, 16));
111 style.append(";}");
112 this->setStyleSheet(style);
113 this->update();
114}
115
116void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
117
118 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
119 fTransform += eventPoint - fPreviousPoint;
120 fPreviousPoint = eventPoint;
121
122 // TODO(chudy): Fix and remove +1 from drawTo calls.
chudy@google.com7dcae672012-07-09 20:26:53 +0000123 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +0000124 this->update();
125}
126
127void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
128 fPreviousPoint.set(event->globalX(), event->globalY());
129}
130
131void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
132 fTransform.set(0,0);
chudy@google.com7dcae672012-07-09 20:26:53 +0000133 fScaleFactor = 1.0;
134 emit scaleFactorChanged(fScaleFactor);
135 drawTo(fIndex);
chudy@google.com902ebe52012-06-29 14:21:22 +0000136 this->update();
137}
138
139void SkCanvasWidget::paintEvent(QPaintEvent *event) {
140 QPainter painter(this);
141 QStyleOption opt;
142 opt.init(this);
143
chudy@google.com2f891792012-07-03 16:05:59 +0000144 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
145
146 QPoint origin(0,0);
147 QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
148 fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
149
150 painter.drawImage(origin, image);
151 painter.end();
152}
153
154void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
155 fScaleFactor += event->delta()/120;
156
157 /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
158 * Based on the code below, -1 and 1 both scale the image to it's original
159 * size we do the following to never have a registered wheel scroll
160 * not effect the fScaleFactor. */
161 if (fScaleFactor == 0) {
162 fScaleFactor += (event->delta()/120) * 2;
chudy@google.com902ebe52012-06-29 14:21:22 +0000163 }
164
chudy@google.com7dcae672012-07-09 20:26:53 +0000165 emit scaleFactorChanged(fScaleFactor);
166
chudy@google.com2f891792012-07-03 16:05:59 +0000167 // TODO(chudy): Fix and remove +1 from drawTo calls.
chudy@google.com7dcae672012-07-09 20:26:53 +0000168 drawTo(fIndex);
chudy@google.com2f891792012-07-03 16:05:59 +0000169 this->update();
chudy@google.com902ebe52012-06-29 14:21:22 +0000170}