22 #include <QtOpenGL/QGLPixelBuffer>
23 #include <QtGui/QPainter>
24 #include <QtGui/QImage>
42 dummy =
new QGLWidget((
QWidget *) 0);
43 QGLFormat format = QGLFormat::defaultFormat();
44 format.setSampleBuffers(
true);
45 format.setAlphaBufferSize(8);
47 pbuf =
new QGLPixelBuffer(300, 300, format, dummy);
48 if (pbuf->isValid()) {
52 void updateGlSize(
const QSize &size)
54 if (size.width() > pbuf->width() ||
55 size.height() > pbuf->height()) {
56 QGLFormat format = pbuf->format();
58 pbuf =
new QGLPixelBuffer(size, format, dummy);
68 const QString &serviceId,
70 :
Applet(parent, serviceId, appletId),
71 d(new GLAppletPrivate)
73 if (!d->dummy->isValid() ||
74 !QGLPixelBuffer::hasOpenGLPbuffers() ||
75 !d->pbuf->isValid()) {
82 d(new GLAppletPrivate)
84 if (!d->dummy->isValid() ||
85 !QGLPixelBuffer::hasOpenGLPbuffers() ||
86 !d->pbuf->isValid()) {
99 if (!d->dummy->isValid()) {
102 return d->dummy->bindTexture(image, target);
108 d->dummy->deleteTexture(textureId);
118 static inline QPainterPath
headerPath(
const QRectF &r,
int roundness,
122 int xRnd = roundness;
123 int yRnd = roundness;
124 if (r.width() > r.height()) {
125 xRnd = int(roundness * r.height() / r.width());
127 yRnd = int(roundness * r.width() / r.height());
136 if(xRnd <= 0 || yRnd <= 0) {
141 QRectF rect = r.normalized();
149 qreal w = rect.width();
150 qreal h = rect.height();
151 qreal rxx = w * xRnd / 200;
152 qreal ryy = h * yRnd / 200;
155 rxx = w / 200 * xRnd;
158 ryy = h / 200 * yRnd;
160 qreal rxx2 = 2 * rxx;
161 qreal ryy2 = 2 * ryy;
163 path.arcMoveTo(x, y, rxx2, ryy2, 90);
164 path.arcTo(x, y, rxx2, ryy2, 90, 90);
165 QPointF pt = path.currentPosition();
166 path.lineTo(x, pt.y() + headerHeight);
167 path.lineTo(x + w, pt.y() + headerHeight);
168 path.lineTo(x + w, pt.y());
169 path.arcTo(x + w - rxx2, y, rxx2, ryy2, 0, 90);
175 void GLApplet::paintInterface(QPainter *painter,
177 const QRect &contentsRect)
179 Q_UNUSED(contentsRect)
181 if ((!d->dummy->isValid() ||
182 !d->pbuf->isValid())) {
189 d->pbuf->makeCurrent();
191 QMatrix m = painter->worldMatrix();
192 QRect deviceRect = m.mapRect(QRect(QPoint(23, 25), boundingRect().size().toSize()));
193 d->updateGlSize(deviceRect.size());
200 QImage image = d->pbuf->toImage();
201 painter->drawImage(0, 0, image);
206 if (!d->dummy->isValid() || !d->pbuf->isValid()) {
207 d->dummy->makeCurrent();
213 #include "glapplet.moc"
void deleteTexture(GLuint texture_id)
GLuint bindTexture(const QImage &image, GLenum target=GL_TEXTURE_2D)
static QPainterPath headerPath(const QRectF &r, int roundness, int headerHeight=10)
void setFailedToLaunch(bool failed, const QString &reason=QString())
Call this method when the applet fails to launch properly.
GLApplet(QGraphicsItem *parent, const QString &serviceId, int appletId)
bool hasFailedToLaunch() const
If for some reason, the applet fails to get up on its feet (the library couldn't be loaded...
virtual void paintGLInterface(QPainter *painter, const QStyleOptionGraphicsItem *option)
Reimplement this method to render using OpenGL.