• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

glapplet.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2007 Zack Rusin <zack@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "glapplet.h"
00021 
00022 #include <QtOpenGL/QGLPixelBuffer>
00023 #include <QtGui/QPainter>
00024 #include <QtGui/QImage>
00025 
00026 namespace Plasma {
00027 
00028 class GLAppletPrivate
00029 {
00030 public:
00031     GLAppletPrivate()
00032     {
00033         init();
00034     }
00035     ~GLAppletPrivate()
00036     {
00037         delete pbuf;
00038         delete dummy;
00039     }
00040     void init()
00041     {
00042         dummy = new QGLWidget((QWidget *) 0);
00043         QGLFormat format = QGLFormat::defaultFormat();
00044         format.setSampleBuffers(true);
00045         format.setAlphaBufferSize(8);
00046         //dummy size construction
00047         pbuf = new QGLPixelBuffer(300, 300, format, dummy);
00048         if (pbuf->isValid()) {
00049             pbuf->makeCurrent();
00050         }
00051     }
00052     void updateGlSize(const QSize &size)
00053     {
00054         if (size.width() > pbuf->width() ||
00055             size.height() > pbuf->height()) {
00056             QGLFormat format = pbuf->format();
00057             delete pbuf;
00058             pbuf = new QGLPixelBuffer(size, format, dummy);
00059         }
00060     }
00061 
00062 public:
00063     QGLPixelBuffer *pbuf;
00064     QGLWidget      *dummy;
00065 };
00066 
00067 GLApplet::GLApplet(QGraphicsItem *parent,
00068                    const QString &serviceId,
00069                    int appletId)
00070     : Applet(parent, serviceId, appletId),
00071       d(new GLAppletPrivate)
00072 {
00073     if (!d->dummy->isValid() ||
00074         !QGLPixelBuffer::hasOpenGLPbuffers() ||
00075         !d->pbuf->isValid()) {
00076         setFailedToLaunch(true, i18n("This system does not support OpenGL applets."));
00077     }
00078 }
00079 
00080 GLApplet::GLApplet(QObject *parent, const QVariantList &args)
00081     : Applet(parent, args),
00082       d(new GLAppletPrivate)
00083 {
00084     if (!d->dummy->isValid() ||
00085         !QGLPixelBuffer::hasOpenGLPbuffers() ||
00086         !d->pbuf->isValid()) {
00087         setFailedToLaunch(true, i18n("This system does not support OpenGL applets."));
00088     }
00089 }
00090 
00091 GLApplet::~GLApplet()
00092 {
00093     delete d;
00094 }
00095 
00096 GLuint GLApplet::bindTexture(const QImage &image, GLenum target)
00097 {
00098     Q_ASSERT(d->pbuf);
00099     if (!d->dummy->isValid())
00100         return 0;
00101     return d->dummy->bindTexture(image, target);
00102 }
00103 
00104 void GLApplet::deleteTexture(GLuint textureId)
00105 {
00106     Q_ASSERT(d->pbuf);
00107     d->dummy->deleteTexture(textureId);
00108 }
00109 
00110 void GLApplet::paintGLInterface(QPainter *painter,
00111                                 const QStyleOptionGraphicsItem *option)
00112 {
00113     Q_UNUSED(painter)
00114     Q_UNUSED(option)
00115 }
00116 
00117 static inline QPainterPath headerPath(const QRectF &r, int roundness,
00118                                       int headerHeight=10)
00119 {
00120     QPainterPath path;
00121     int xRnd = roundness;
00122     int yRnd = roundness;
00123     if (r.width() > r.height())
00124         xRnd = int(roundness * r.height()/r.width());
00125     else
00126         yRnd = int(roundness * r.width()/r.height());
00127 
00128     if(xRnd >= 100)                          // fix ranges
00129         xRnd = 99;
00130     if(yRnd >= 100)
00131         yRnd = 99;
00132     if(xRnd <= 0 || yRnd <= 0) {             // add normal rectangle
00133         path.addRect(r);
00134         return path;
00135     }
00136 
00137     QRectF rect = r.normalized();
00138 
00139     if (rect.isNull())
00140         return path;
00141 
00142     qreal x = rect.x();
00143     qreal y = rect.y();
00144     qreal w = rect.width();
00145     qreal h = rect.height();
00146     qreal rxx = w*xRnd/200;
00147     qreal ryy = h*yRnd/200;
00148     // were there overflows?
00149     if (rxx < 0)
00150         rxx = w/200*xRnd;
00151     if (ryy < 0)
00152         ryy = h/200*yRnd;
00153     qreal rxx2 = 2*rxx;
00154     qreal ryy2 = 2*ryy;
00155 
00156     path.arcMoveTo(x, y, rxx2, ryy2, 90);
00157     path.arcTo(x,        y, rxx2, ryy2, 90, 90);
00158     QPointF pt = path.currentPosition();
00159     path.lineTo(x, pt.y()+headerHeight);
00160     path.lineTo(x+w, pt.y()+headerHeight);
00161     path.lineTo(x+w, pt.y());
00162     path.arcTo(x+w-rxx2, y, rxx2, ryy2, 0, 90);
00163     path.closeSubpath();
00164 
00165     return path;
00166 }
00167 
00168 void GLApplet::paintInterface(QPainter *painter,
00169                               const QStyleOptionGraphicsItem *option,
00170                               const QRect &contentsRect)
00171 {
00172     Q_UNUSED(contentsRect)
00173     Q_ASSERT(d->pbuf);
00174     if ((!d->dummy->isValid() ||
00175          !d->pbuf->isValid())) {
00176         if (!hasFailedToLaunch()) {
00177             setFailedToLaunch(true, i18n("Your machine does not support OpenGL applets."));
00178         }
00179 
00180         return;
00181     }
00182     d->pbuf->makeCurrent();
00183 
00184     // handle background filling
00185     glClearColor(0, 0, 0, 0);
00186     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00187 
00188     QMatrix m = painter->worldMatrix();
00189     QRect deviceRect = m.mapRect(QRect(QPoint(23, 25), boundingRect().size().toSize()));
00190     d->updateGlSize(deviceRect.size());
00191 
00192     // redirect this widget's painting into the pbuffer
00193     QPainter p(d->pbuf);
00194     paintGLInterface(&p, option);
00195 
00196     // draw the pbuffer contents to the backingstore
00197     QImage image = d->pbuf->toImage();
00198     painter->drawImage(0, 0, image);
00199 }
00200 
00201 void GLApplet::makeCurrent()
00202 {
00203     if (!d->dummy->isValid() ||
00204         !d->pbuf->isValid())
00205         d->dummy->makeCurrent();
00206 }
00207 
00208 } // Plasma namespace
00209 
00210 #include "glapplet.moc"

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal