blob: c38fdbb81054c13d89b46c806b54401d003daa7d [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
34 fScaleFactor = 1;
35 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);
58 fDebugCanvas->drawTo(fCanvas, fIndex);
59 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
73 fDebugCanvas->drawTo(fCanvas, fIndex+1);
chudy@google.com902ebe52012-06-29 14:21:22 +000074 this->update();
chudy@google.com2f891792012-07-03 16:05:59 +000075 this->fIndex = fIndex;
chudy@google.com902ebe52012-06-29 14:21:22 +000076}
77
78void SkCanvasWidget::loadPicture(QString filename) {
79 SkStream *stream = new SkFILEStream(filename.toAscii());
80 SkPicture *picture = new SkPicture(stream);
81
82 delete fDebugCanvas;
83 fDebugCanvas = new SkDebugCanvas();
84
85 picture->draw(fDebugCanvas);
86 fDebugCanvas->draw(fCanvas);
87
chudy@google.com2f891792012-07-03 16:05:59 +000088 fIndex = fDebugCanvas->getSize();
89
90 SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1);
91
92 int r = SkColorGetR(color);
93 int g = SkColorGetG(color);
94 int b = SkColorGetB(color);
95
chudy@google.com902ebe52012-06-29 14:21:22 +000096 /* NOTE(chudy): This was a test to determine if the canvas size is accurately
97 * saved in the bounds of the recorded picture. It is not. Everyone of the
98 * sample GM images is 1000x1000. Even the one that claims it is
99 * 2048x2048.
100 std::cout << "Width: " << picture->width();
101 std::cout << " Height: " << picture->height() << std::endl; */
102
chudy@google.com2f891792012-07-03 16:05:59 +0000103 /* Updated style sheet without a background specified. If not removed
104 * QPainter paints the specified background color on top of our canvas. */
105
106 QString style("QWidget {border: 1px solid #cccccc; background-color: #");
107 style.append(QString::number(r, 16));
108 style.append(QString::number(g, 16));
109 style.append(QString::number(b, 16));
110 style.append(";}");
111 this->setStyleSheet(style);
112 this->update();
113}
114
115void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
116
117 SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
118 fTransform += eventPoint - fPreviousPoint;
119 fPreviousPoint = eventPoint;
120
121 // TODO(chudy): Fix and remove +1 from drawTo calls.
122 drawTo(fIndex+1);
123 this->update();
124}
125
126void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
127 fPreviousPoint.set(event->globalX(), event->globalY());
128}
129
130void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
131 fTransform.set(0,0);
132 fScaleFactor = 0;
133 drawTo(fIndex+1);
chudy@google.com902ebe52012-06-29 14:21:22 +0000134 this->update();
135}
136
137void SkCanvasWidget::paintEvent(QPaintEvent *event) {
138 QPainter painter(this);
139 QStyleOption opt;
140 opt.init(this);
141
chudy@google.com2f891792012-07-03 16:05:59 +0000142 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
143
144 QPoint origin(0,0);
145 QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
146 fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
147
148 painter.drawImage(origin, image);
149 painter.end();
150}
151
152void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
153 fScaleFactor += event->delta()/120;
154
155 /* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
156 * Based on the code below, -1 and 1 both scale the image to it's original
157 * size we do the following to never have a registered wheel scroll
158 * not effect the fScaleFactor. */
159 if (fScaleFactor == 0) {
160 fScaleFactor += (event->delta()/120) * 2;
chudy@google.com902ebe52012-06-29 14:21:22 +0000161 }
162
chudy@google.com2f891792012-07-03 16:05:59 +0000163 // TODO(chudy): Fix and remove +1 from drawTo calls.
164 drawTo(fIndex+1);
165 this->update();
chudy@google.com902ebe52012-06-29 14:21:22 +0000166}