2012-06-29 14:21:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkCanvasWidget.h"
|
2012-07-03 16:05:59 +00:00
|
|
|
#include "SkColor.h"
|
2012-06-29 14:21:22 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
SkCanvasWidget::SkCanvasWidget(QWidget *parent) :
|
|
|
|
QWidget(parent) {
|
|
|
|
|
2012-07-03 16:05:59 +00:00
|
|
|
/* TODO(chudy): The 800x800 is a default number. Change it to be
|
|
|
|
* set dynamically. Also need to pass size into debugCanvas for current
|
|
|
|
* command filter. */
|
|
|
|
fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800);
|
|
|
|
fBitmap.allocPixels();
|
|
|
|
fBitmap.eraseColor(0);
|
|
|
|
|
|
|
|
/* TODO(chudy): Add fCanvas, fDevice to the stack. The bitmap being
|
|
|
|
* cleared does get rid of fDevices link to it. See if there's someway around
|
|
|
|
* it so that we don't have to delete both canvas and device to clear out
|
|
|
|
* the bitmap. */
|
|
|
|
fDevice = new SkDevice(fBitmap);
|
2012-06-29 14:21:22 +00:00
|
|
|
fCanvas = new SkCanvas(fDevice);
|
|
|
|
fDebugCanvas = new SkDebugCanvas();
|
2012-07-03 16:05:59 +00:00
|
|
|
|
2012-07-09 20:26:53 +00:00
|
|
|
fScaleFactor = 1.0;
|
2012-07-03 16:05:59 +00:00
|
|
|
fIndex = 0;
|
|
|
|
fPreviousPoint.set(0,0);
|
|
|
|
fTransform.set(0,0);
|
|
|
|
|
2012-06-29 14:21:22 +00:00
|
|
|
this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
|
|
|
|
}
|
|
|
|
|
2012-07-03 16:05:59 +00:00
|
|
|
SkCanvasWidget::~SkCanvasWidget() {
|
|
|
|
delete fCanvas;
|
|
|
|
delete fDevice;
|
|
|
|
delete fDebugCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkCanvasWidget::resizeEvent(QResizeEvent* event) {
|
|
|
|
fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height());
|
|
|
|
fBitmap.allocPixels();
|
|
|
|
fBitmap.eraseColor(0);
|
|
|
|
|
|
|
|
delete fCanvas;
|
|
|
|
delete fDevice;
|
|
|
|
|
|
|
|
fDevice = new SkDevice(fBitmap);
|
|
|
|
fCanvas = new SkCanvas(fDevice);
|
2012-07-10 14:14:50 +00:00
|
|
|
fDebugCanvas->setBounds(event->size().width(), event->size().height());
|
|
|
|
fDebugCanvas->drawTo(fCanvas, fIndex);
|
2012-07-03 16:05:59 +00:00
|
|
|
this->update();
|
|
|
|
}
|
2012-06-29 14:21:22 +00:00
|
|
|
|
2012-07-03 16:05:59 +00:00
|
|
|
void SkCanvasWidget::drawTo(int fIndex) {
|
2012-06-29 14:21:22 +00:00
|
|
|
delete fCanvas;
|
|
|
|
fCanvas = new SkCanvas(fDevice);
|
2012-07-03 16:05:59 +00:00
|
|
|
|
|
|
|
fCanvas->translate(fTransform.fX, fTransform.fY);
|
|
|
|
if(fScaleFactor < 0) {
|
|
|
|
fCanvas->scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor));
|
|
|
|
} else if (fScaleFactor > 0) {
|
|
|
|
fCanvas->scale(fScaleFactor, fScaleFactor);
|
|
|
|
}
|
|
|
|
|
2012-07-09 20:26:53 +00:00
|
|
|
emit commandChanged(fIndex);
|
2012-07-03 16:05:59 +00:00
|
|
|
fDebugCanvas->drawTo(fCanvas, fIndex+1);
|
2012-06-29 14:21:22 +00:00
|
|
|
this->update();
|
2012-07-03 16:05:59 +00:00
|
|
|
this->fIndex = fIndex;
|
2012-06-29 14:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkCanvasWidget::loadPicture(QString filename) {
|
|
|
|
SkStream *stream = new SkFILEStream(filename.toAscii());
|
|
|
|
SkPicture *picture = new SkPicture(stream);
|
|
|
|
|
|
|
|
delete fDebugCanvas;
|
|
|
|
fDebugCanvas = new SkDebugCanvas();
|
2012-07-10 14:14:50 +00:00
|
|
|
fDebugCanvas->setBounds(this->width(), this->height());
|
2012-06-29 14:21:22 +00:00
|
|
|
|
|
|
|
picture->draw(fDebugCanvas);
|
|
|
|
fDebugCanvas->draw(fCanvas);
|
|
|
|
|
2012-07-03 16:05:59 +00:00
|
|
|
fIndex = fDebugCanvas->getSize();
|
|
|
|
|
|
|
|
SkColor color = fBitmap.getColor(fBitmap.width()-1,fBitmap.height()-1);
|
|
|
|
|
|
|
|
int r = SkColorGetR(color);
|
|
|
|
int g = SkColorGetG(color);
|
|
|
|
int b = SkColorGetB(color);
|
|
|
|
|
2012-06-29 14:21:22 +00:00
|
|
|
/* NOTE(chudy): This was a test to determine if the canvas size is accurately
|
|
|
|
* saved in the bounds of the recorded picture. It is not. Everyone of the
|
|
|
|
* sample GM images is 1000x1000. Even the one that claims it is
|
|
|
|
* 2048x2048.
|
|
|
|
std::cout << "Width: " << picture->width();
|
|
|
|
std::cout << " Height: " << picture->height() << std::endl; */
|
|
|
|
|
2012-07-03 16:05:59 +00:00
|
|
|
/* Updated style sheet without a background specified. If not removed
|
|
|
|
* QPainter paints the specified background color on top of our canvas. */
|
|
|
|
|
|
|
|
QString style("QWidget {border: 1px solid #cccccc; background-color: #");
|
|
|
|
style.append(QString::number(r, 16));
|
|
|
|
style.append(QString::number(g, 16));
|
|
|
|
style.append(QString::number(b, 16));
|
|
|
|
style.append(";}");
|
|
|
|
this->setStyleSheet(style);
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkCanvasWidget::mouseMoveEvent(QMouseEvent* event) {
|
|
|
|
|
|
|
|
SkIPoint eventPoint = SkIPoint::Make(event->globalX(), event->globalY());
|
|
|
|
fTransform += eventPoint - fPreviousPoint;
|
|
|
|
fPreviousPoint = eventPoint;
|
|
|
|
|
|
|
|
// TODO(chudy): Fix and remove +1 from drawTo calls.
|
2012-07-09 20:26:53 +00:00
|
|
|
drawTo(fIndex);
|
2012-07-03 16:05:59 +00:00
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkCanvasWidget::mousePressEvent(QMouseEvent* event) {
|
|
|
|
fPreviousPoint.set(event->globalX(), event->globalY());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkCanvasWidget::mouseDoubleClickEvent(QMouseEvent* event) {
|
|
|
|
fTransform.set(0,0);
|
2012-07-09 20:26:53 +00:00
|
|
|
fScaleFactor = 1.0;
|
|
|
|
emit scaleFactorChanged(fScaleFactor);
|
|
|
|
drawTo(fIndex);
|
2012-06-29 14:21:22 +00:00
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkCanvasWidget::paintEvent(QPaintEvent *event) {
|
|
|
|
QPainter painter(this);
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
|
2012-07-03 16:05:59 +00:00
|
|
|
|
|
|
|
QPoint origin(0,0);
|
|
|
|
QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(),
|
|
|
|
fBitmap.height(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
|
|
|
painter.drawImage(origin, image);
|
2012-06-29 14:21:22 +00:00
|
|
|
painter.end();
|
|
|
|
}
|
2012-07-03 16:05:59 +00:00
|
|
|
|
|
|
|
void SkCanvasWidget::wheelEvent(QWheelEvent* event) {
|
|
|
|
fScaleFactor += event->delta()/120;
|
|
|
|
|
|
|
|
/* The range of the fScaleFactor crosses over the range -1,0,1 frequently.
|
|
|
|
* Based on the code below, -1 and 1 both scale the image to it's original
|
|
|
|
* size we do the following to never have a registered wheel scroll
|
|
|
|
* not effect the fScaleFactor. */
|
|
|
|
if (fScaleFactor == 0) {
|
|
|
|
fScaleFactor += (event->delta()/120) * 2;
|
|
|
|
}
|
|
|
|
|
2012-07-09 20:26:53 +00:00
|
|
|
emit scaleFactorChanged(fScaleFactor);
|
|
|
|
|
2012-07-03 16:05:59 +00:00
|
|
|
// TODO(chudy): Fix and remove +1 from drawTo calls.
|
2012-07-09 20:26:53 +00:00
|
|
|
drawTo(fIndex);
|
2012-07-03 16:05:59 +00:00
|
|
|
this->update();
|
|
|
|
}
|