2007-07-24 23:50:37 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 Zack Rusin <zack@kde.org>
|
2007-07-24 23:50:37 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 21:06:18 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-07-24 23:50:37 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "glapplet.h"
|
|
|
|
|
|
|
|
#include <QtOpenGL/QGLPixelBuffer>
|
|
|
|
#include <QtGui/QPainter>
|
|
|
|
#include <QtGui/QImage>
|
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class GLAppletPrivate
|
2007-07-24 23:50:37 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-07-01 20:56:43 +02:00
|
|
|
GLAppletPrivate()
|
2007-07-24 23:50:37 +02:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
2008-07-01 20:56:43 +02:00
|
|
|
~GLAppletPrivate()
|
2007-07-24 23:50:37 +02:00
|
|
|
{
|
|
|
|
delete pbuf;
|
|
|
|
delete dummy;
|
|
|
|
}
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
dummy = new QGLWidget((QWidget *) 0);
|
|
|
|
QGLFormat format = QGLFormat::defaultFormat();
|
|
|
|
format.setSampleBuffers(true);
|
|
|
|
format.setAlphaBufferSize(8);
|
|
|
|
//dummy size construction
|
|
|
|
pbuf = new QGLPixelBuffer(300, 300, format, dummy);
|
2007-11-26 19:23:51 +01:00
|
|
|
if (pbuf->isValid()) {
|
2007-07-24 23:50:37 +02:00
|
|
|
pbuf->makeCurrent();
|
2007-11-26 19:23:51 +01:00
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
}
|
|
|
|
void updateGlSize(const QSize &size)
|
|
|
|
{
|
|
|
|
if (size.width() > pbuf->width() ||
|
|
|
|
size.height() > pbuf->height()) {
|
|
|
|
QGLFormat format = pbuf->format();
|
|
|
|
delete pbuf;
|
|
|
|
pbuf = new QGLPixelBuffer(size, format, dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
QGLPixelBuffer *pbuf;
|
|
|
|
QGLWidget *dummy;
|
|
|
|
};
|
|
|
|
|
|
|
|
GLApplet::GLApplet(QGraphicsItem *parent,
|
|
|
|
const QString &serviceId,
|
|
|
|
int appletId)
|
|
|
|
: Applet(parent, serviceId, appletId),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new GLAppletPrivate)
|
2007-07-24 23:50:37 +02:00
|
|
|
{
|
|
|
|
if (!d->dummy->isValid() ||
|
2007-12-10 06:03:47 +01:00
|
|
|
!QGLPixelBuffer::hasOpenGLPbuffers() ||
|
2007-07-24 23:50:37 +02:00
|
|
|
!d->pbuf->isValid()) {
|
2007-07-26 18:47:39 +02:00
|
|
|
setFailedToLaunch(true, i18n("This system does not support OpenGL applets."));
|
2007-07-24 23:50:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-29 04:33:22 +02:00
|
|
|
GLApplet::GLApplet(QObject *parent, const QVariantList &args)
|
2007-07-24 23:50:37 +02:00
|
|
|
: Applet(parent, args),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new GLAppletPrivate)
|
2007-07-24 23:50:37 +02:00
|
|
|
{
|
2007-12-10 06:03:47 +01:00
|
|
|
if (!d->dummy->isValid() ||
|
|
|
|
!QGLPixelBuffer::hasOpenGLPbuffers() ||
|
|
|
|
!d->pbuf->isValid()) {
|
|
|
|
setFailedToLaunch(true, i18n("This system does not support OpenGL applets."));
|
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GLApplet::~GLApplet()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GLApplet::bindTexture(const QImage &image, GLenum target)
|
|
|
|
{
|
|
|
|
Q_ASSERT(d->pbuf);
|
2008-10-11 18:19:04 +02:00
|
|
|
if (!d->dummy->isValid()) {
|
2007-07-24 23:50:37 +02:00
|
|
|
return 0;
|
2008-10-11 18:19:04 +02:00
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
return d->dummy->bindTexture(image, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLApplet::deleteTexture(GLuint textureId)
|
|
|
|
{
|
|
|
|
Q_ASSERT(d->pbuf);
|
|
|
|
d->dummy->deleteTexture(textureId);
|
|
|
|
}
|
|
|
|
|
2007-08-30 06:34:09 +02:00
|
|
|
void GLApplet::paintGLInterface(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option)
|
|
|
|
{
|
|
|
|
Q_UNUSED(painter)
|
|
|
|
Q_UNUSED(option)
|
|
|
|
}
|
|
|
|
|
2007-07-24 23:50:37 +02:00
|
|
|
static inline QPainterPath headerPath(const QRectF &r, int roundness,
|
|
|
|
int headerHeight=10)
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
int xRnd = roundness;
|
|
|
|
int yRnd = roundness;
|
2008-10-11 18:19:04 +02:00
|
|
|
if (r.width() > r.height()) {
|
2008-10-12 00:48:38 +02:00
|
|
|
xRnd = int(roundness * r.height() / r.width());
|
2008-10-11 18:19:04 +02:00
|
|
|
} else {
|
2008-10-12 00:48:38 +02:00
|
|
|
yRnd = int(roundness * r.width() / r.height());
|
2008-10-11 18:19:04 +02:00
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
if(xRnd >= 100) { // fix ranges
|
2007-07-24 23:50:37 +02:00
|
|
|
xRnd = 99;
|
2008-10-11 18:19:04 +02:00
|
|
|
}
|
|
|
|
if(yRnd >= 100) {
|
2007-07-24 23:50:37 +02:00
|
|
|
yRnd = 99;
|
2008-10-11 18:19:04 +02:00
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
if(xRnd <= 0 || yRnd <= 0) { // add normal rectangle
|
|
|
|
path.addRect(r);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF rect = r.normalized();
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
if (rect.isNull()) {
|
2007-07-24 23:50:37 +02:00
|
|
|
return path;
|
2008-10-12 00:48:38 +02:00
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
|
|
|
|
qreal x = rect.x();
|
|
|
|
qreal y = rect.y();
|
|
|
|
qreal w = rect.width();
|
|
|
|
qreal h = rect.height();
|
2008-10-11 18:19:04 +02:00
|
|
|
qreal rxx = w * xRnd / 200;
|
|
|
|
qreal ryy = h * yRnd / 200;
|
2007-07-24 23:50:37 +02:00
|
|
|
// were there overflows?
|
2008-10-11 18:19:04 +02:00
|
|
|
if (rxx < 0) {
|
|
|
|
rxx = w / 200 * xRnd;
|
|
|
|
}
|
|
|
|
if (ryy < 0) {
|
|
|
|
ryy = h / 200 * yRnd;
|
|
|
|
}
|
2008-10-12 00:48:38 +02:00
|
|
|
qreal rxx2 = 2 * rxx;
|
|
|
|
qreal ryy2 = 2 * ryy;
|
2007-07-24 23:50:37 +02:00
|
|
|
|
|
|
|
path.arcMoveTo(x, y, rxx2, ryy2, 90);
|
2008-10-11 18:19:04 +02:00
|
|
|
path.arcTo(x, y, rxx2, ryy2, 90, 90);
|
2007-07-24 23:50:37 +02:00
|
|
|
QPointF pt = path.currentPosition();
|
2008-10-12 00:48:38 +02:00
|
|
|
path.lineTo(x, pt.y() + headerHeight);
|
2008-10-11 18:19:04 +02:00
|
|
|
path.lineTo(x + w, pt.y() + headerHeight);
|
|
|
|
path.lineTo(x + w, pt.y());
|
|
|
|
path.arcTo(x + w - rxx2, y, rxx2, ryy2, 0, 90);
|
2007-07-24 23:50:37 +02:00
|
|
|
path.closeSubpath();
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLApplet::paintInterface(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
const QRect &contentsRect)
|
|
|
|
{
|
2007-11-09 01:41:07 +01:00
|
|
|
Q_UNUSED(contentsRect)
|
2007-07-24 23:50:37 +02:00
|
|
|
Q_ASSERT(d->pbuf);
|
2007-07-26 04:31:36 +02:00
|
|
|
if ((!d->dummy->isValid() ||
|
|
|
|
!d->pbuf->isValid())) {
|
2008-04-16 23:15:38 +02:00
|
|
|
if (!hasFailedToLaunch()) {
|
2007-08-13 23:02:47 +02:00
|
|
|
setFailedToLaunch(true, i18n("Your machine does not support OpenGL applets."));
|
2007-07-26 04:31:36 +02:00
|
|
|
}
|
|
|
|
|
2007-07-24 23:50:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->pbuf->makeCurrent();
|
|
|
|
|
|
|
|
// handle background filling
|
|
|
|
glClearColor(0, 0, 0, 0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
QMatrix m = painter->worldMatrix();
|
2008-04-13 15:23:39 +02:00
|
|
|
QRect deviceRect = m.mapRect(QRect(QPoint(23, 25), boundingRect().size().toSize()));
|
2007-07-24 23:50:37 +02:00
|
|
|
d->updateGlSize(deviceRect.size());
|
|
|
|
|
|
|
|
// redirect this widget's painting into the pbuffer
|
|
|
|
QPainter p(d->pbuf);
|
|
|
|
paintGLInterface(&p, option);
|
|
|
|
|
|
|
|
// draw the pbuffer contents to the backingstore
|
|
|
|
QImage image = d->pbuf->toImage();
|
|
|
|
painter->drawImage(0, 0, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLApplet::makeCurrent()
|
|
|
|
{
|
2008-10-12 00:48:38 +02:00
|
|
|
if (!d->dummy->isValid() || !d->pbuf->isValid()) {
|
2007-07-24 23:50:37 +02:00
|
|
|
d->dummy->makeCurrent();
|
2008-10-12 00:48:38 +02:00
|
|
|
}
|
2007-07-24 23:50:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "glapplet.moc"
|