move the glapplet into libplasma since it's actually getting used and i hate having to fix this file in N places

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=692023
This commit is contained in:
Aaron J. Seigo 2007-07-24 21:50:37 +00:00
parent 2c251118fd
commit 508d373bbe
3 changed files with 328 additions and 4 deletions

View File

@ -38,9 +38,15 @@ set(plasma_LIB_SRCS
widgets/flash.cpp
)
if(QT_QTOPENGL_FOUND)
set(plasma_LIB_SRCS
${plasma_LIB_SRCS}
glapplet.cpp)
endif(QT_QTOPENGL_FOUND)
kde4_add_library(plasma SHARED ${plasma_LIB_SRCS})
target_link_libraries(plasma ${KDE4_KIO_LIBS})
target_link_libraries(plasma ${KDE4_KIO_LIBS} ${QT_QTOPENGL_LIBRARY} ${OPENGL_gl_LIBRARY})
set_target_properties(plasma PROPERTIES VERSION 1.0.0 SOVERSION 1)
install(TARGETS plasma DESTINATION ${LIB_INSTALL_DIR})
@ -56,7 +62,7 @@ set(plasmagik_HEADERS
install(FILES ${plasmagik_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/plasma/)
install(FILES
set(plasma_LIB_INCLUDES
abstractrunner.h
animator.h
applet.h
@ -70,8 +76,16 @@ install(FILES
plasma_export.h
scriptengine.cpp
svg.h
theme.h
DESTINATION ${INCLUDE_INSTALL_DIR}/plasma)
theme.h)
if(QT_QTOPENGL_FOUND)
set(plasma_LIB_INCLUDES
glapplet.h)
endif(QT_QTOPENGL_FOUND)
install(FILES
${plasma_LIB_INCLUDES}
DESTINATION ${INCLUDE_INSTALL_DIR}/plasma)
install(FILES
widgets/icon.h

225
glapplet.cpp Normal file
View File

@ -0,0 +1,225 @@
/*
* Copyright (C) 2007 Zack Rusin <zack@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* 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 {
class GLApplet::Private
{
public:
Private()
{
init();
}
~Private()
{
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);
if (pbuf->isValid())
pbuf->makeCurrent();
}
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),
d(new Private)
{
if (!d->dummy->isValid() ||
!d->pbuf->isValid()) {
setFailedToLaunch(true, i18n("Your machine doesn't support OpenGL applets."));
}
}
GLApplet::GLApplet(QObject *parent, const QStringList &args)
: Applet(parent, args),
d(new Private)
{
}
GLApplet::~GLApplet()
{
delete d;
}
GLuint GLApplet::bindTexture(const QImage &image, GLenum target)
{
Q_ASSERT(d->pbuf);
if (!d->dummy->isValid())
return 0;
return d->dummy->bindTexture(image, target);
}
void GLApplet::deleteTexture(GLuint textureId)
{
Q_ASSERT(d->pbuf);
d->dummy->deleteTexture(textureId);
}
static inline QPainterPath headerPath(const QRectF &r, int roundness,
int headerHeight=10)
{
QPainterPath path;
int xRnd = roundness;
int yRnd = roundness;
if (r.width() > r.height())
xRnd = int(roundness * r.height()/r.width());
else
yRnd = int(roundness * r.width()/r.height());
if(xRnd >= 100) // fix ranges
xRnd = 99;
if(yRnd >= 100)
yRnd = 99;
if(xRnd <= 0 || yRnd <= 0) { // add normal rectangle
path.addRect(r);
return path;
}
QRectF rect = r.normalized();
if (rect.isNull())
return path;
qreal x = rect.x();
qreal y = rect.y();
qreal w = rect.width();
qreal h = rect.height();
qreal rxx = w*xRnd/200;
qreal ryy = h*yRnd/200;
// were there overflows?
if (rxx < 0)
rxx = w/200*xRnd;
if (ryy < 0)
ryy = h/200*yRnd;
qreal rxx2 = 2*rxx;
qreal ryy2 = 2*ryy;
path.arcMoveTo(x, y, rxx2, ryy2, 90);
path.arcTo(x, y, rxx2, ryy2, 90, 90);
QPointF pt = path.currentPosition();
path.lineTo(x, pt.y()+headerHeight);
path.lineTo(x+w, pt.y()+headerHeight);
path.lineTo(x+w, pt.y());
path.arcTo(x+w-rxx2, y, rxx2, ryy2, 0, 90);
path.closeSubpath();
return path;
}
void GLApplet::paintInterface(QPainter *painter,
const QStyleOptionGraphicsItem *option,
const QRect &contentsRect)
{
Q_ASSERT(d->pbuf);
if (!d->dummy->isValid() ||
!d->pbuf->isValid()) {
//FIXME: this is evil hand rendering
// svg would be nicer. or even better a global
// way of saying "this applet can't run on this machine"
QFont font("ComicSans", 10);
QRectF boxRect = boundingRect();
QPainterPath frame;
frame.addRoundRect(boxRect, 25);
QPainterPath header = headerPath(boxRect, 25);
painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->setPen(Qt::black);
painter->setBrush(QBrush(Qt::white));
painter->drawPath(frame);
painter->save();
painter->setPen(QPen(Qt::NoPen));
painter->setBrush(QColor(120, 255, 185));
painter->drawPath(header);
painter->restore();
painter->setPen(QPen(Qt::black));
font.setBold(true);
painter->setFont(font);
painter->drawText(QPointF(boxRect.x()+10, boxRect.y()+15),
i18n("Error"));
painter->setPen(QPen(Qt::black));
font.setPointSize(8);
font.setBold(false);
painter->setFont(font);
painter->drawText(QPointF(boxRect.x()+10,
boxRect.y()+30),
i18n("Your machine doesn't support OpenGL applets."));
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();
QRect deviceRect = m.mapRect(boundingRect()).toRect();
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()
{
if (!d->dummy->isValid() ||
!d->pbuf->isValid())
d->dummy->makeCurrent();
}
} // Plasma namespace
#include "glapplet.moc"

85
glapplet.h Normal file
View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2007 Zack Rusin <zack@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* 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.
*/
#ifndef PLASMA_GLAPPLET_H
#define PLASMA_GLAPPLET_H
#include <plasma/applet.h>
#include <QtOpenGL/QGLWidget>
namespace Plasma
{
/**
* @short Plasma Applet that is fully rendererd using OpengGL
*
*/
class PLASMA_EXPORT GLApplet : public Applet
{
Q_OBJECT
public:
/**
* @arg parent the QGraphicsItem this applet is parented to
* @arg servideId the name of the .desktop file containing the
* information about the widget
* @arg appletId a unique id used to differentiate between multiple
* instances of the same Applet type
*/
GLApplet(QGraphicsItem *parent,
const QString &serviceId,
int appletId);
/**
* This constructor is to be used with the plugin loading systems
* found in KPluginInfo and KService. The argument list is expected
* to have two elements: the KService service ID for the desktop entry
* and an applet ID which must be a base 10 number.
*
* @arg parent a QObject parent; you probably want to pass in 0
* @arg args a list of strings containing two entries: the service id
* and the applet id
*/
GLApplet(QObject *parent, const QStringList &args);
~GLApplet();
GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D);
void deleteTexture(GLuint texture_id);
/**
* Reimplement this method to render using OpenGL. QPainter passed
* to this method will always use OpenGL engine and rendering
* using OpenGL api directly is supported.
*/
virtual void paintGLInterface(QPainter *painter,
const QStyleOptionGraphicsItem *option) = 0;
void makeCurrent();
private:
virtual void paintInterface(QPainter *painter,
const QStyleOptionGraphicsItem *option,
const QRect &contentsRect);
private:
class Private;
Private *const d;
};
}
#endif