add some comments
[makneto-zunavac1.git] / src / ui-mobile / view-graber.cpp
blob098a512d0a249c109882fe5d6ae14b35d0686fb7
1 /*
2 * Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <qt4/QtOpenGL/qgl.h>
21 #include <QGraphicsScene>
22 #include <qt4/QtDeclarative/qdeclarativeitem.h>
24 #include "view-graber.h"
26 ViewGraber::ViewGraber(QDeclarativeItem *parent) :
27 QDeclarativeItem(parent),
28 scheduledSnapshot(false),
29 paintSnapshot(false) {
33 ViewGraber::~ViewGraber() {
34 //qDebug() << "ViewGraber: destroyed";
37 void ViewGraber::takeSnapshot() {
38 //QDeclarativeItem *parent = parentItem();
39 if (!_delegate)
40 return;
42 scheduledSnapshot = true;
43 paintSnapshot = true;
45 setFlag(QGraphicsItem::ItemHasNoContents, false);
46 update(_delegate->boundingRect());
49 void ViewGraber::releaseSnapshot() {
50 paintSnapshot = false;
53 void ViewGraber::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0) {
54 Q_UNUSED(option);
56 if (!_delegate)
57 return;
59 if (scheduledSnapshot) {
60 QGLWidget *qgl = reinterpret_cast<QGLWidget *> (widget);
61 if (!qgl) {
62 qWarning() << "ViewGraber: cast" << widget << "to QGLWidget failed";
63 return;
66 _delegate->paint(painter, option, widget);
67 scheduledSnapshot = false;
68 snapshot = qgl->grabFrameBuffer(false);
69 qDebug() << "MyWebView: take snapshot ";
70 emit snapshotTaken();
73 if (!paintSnapshot)
74 return;
76 QPointF sceneMapRect = _delegate->mapToScene(0, 0);
77 QPointF selfMapRect = ((QGraphicsItem*) _delegate)->mapToItem((QGraphicsItem*) this, QPointF(0, 0));
78 //rect = this->scene()->mapToItem(this, rect);
79 //qDebug() << "MyWebView: paint" << sceneMapRect << selfMapRect;
80 painter->drawImage((sceneMapRect.x() - selfMapRect.x()) *-1, (sceneMapRect.y() - selfMapRect.y()) *-1, snapshot);
83 #include "view-graber.moc"