[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[556] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[556] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[556] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "starwidget.h"
|
---|
| 42 |
|
---|
| 43 | StarWidget::StarWidget(QWidget *parent)
|
---|
| 44 | : QWidget(parent)
|
---|
| 45 | , path(VG_INVALID_HANDLE)
|
---|
| 46 | , pen(Qt::red, 4.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)
|
---|
| 47 | , brush(Qt::yellow)
|
---|
| 48 | {
|
---|
| 49 | setMinimumSize(220, 250);
|
---|
| 50 | setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | static VGubyte const starSegments[] = {
|
---|
| 54 | VG_MOVE_TO_ABS,
|
---|
| 55 | VG_LINE_TO_REL,
|
---|
| 56 | VG_LINE_TO_REL,
|
---|
| 57 | VG_LINE_TO_REL,
|
---|
| 58 | VG_LINE_TO_REL,
|
---|
| 59 | VG_CLOSE_PATH
|
---|
| 60 | };
|
---|
| 61 | static VGfloat const starCoords[] = {
|
---|
| 62 | 110, 35,
|
---|
| 63 | 50, 160,
|
---|
| 64 | -130, -100,
|
---|
| 65 | 160, 0,
|
---|
| 66 | -130, 100
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | void StarWidget::paintEvent(QPaintEvent *)
|
---|
| 70 | {
|
---|
| 71 | QPainter painter;
|
---|
| 72 | painter.begin(this);
|
---|
| 73 |
|
---|
| 74 | // Make sure that we are using the OpenVG paint engine.
|
---|
| 75 | if (painter.paintEngine()->type() != QPaintEngine::OpenVG) {
|
---|
| 76 | #ifdef Q_WS_QWS
|
---|
| 77 | qWarning("Not using OpenVG: use the '-display' option to specify an OpenVG driver");
|
---|
| 78 | #else
|
---|
| 79 | qWarning("Not using OpenVG: specify '-graphicssystem OpenVG'");
|
---|
| 80 | #endif
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | // Select a pen and a brush for drawing the star.
|
---|
| 85 | painter.setPen(pen);
|
---|
| 86 | painter.setBrush(brush);
|
---|
| 87 |
|
---|
| 88 | // We want the star border to be anti-aliased.
|
---|
| 89 | painter.setRenderHints(QPainter::Antialiasing);
|
---|
| 90 |
|
---|
| 91 | // Flush the state changes to the OpenVG implementation
|
---|
| 92 | // and prepare to perform raw OpenVG calls.
|
---|
| 93 | painter.beginNativePainting();
|
---|
| 94 |
|
---|
[769] | 95 | // Cache the path if we haven't already or if the path has
|
---|
| 96 | // become invalid because the window's context has changed.
|
---|
| 97 | if (path == VG_INVALID_HANDLE || !vgGetPathCapabilities(path)) {
|
---|
[556] | 98 | path = vgCreatePath(VG_PATH_FORMAT_STANDARD,
|
---|
| 99 | VG_PATH_DATATYPE_F,
|
---|
| 100 | 1.0f, // scale
|
---|
| 101 | 0.0f, // bias
|
---|
| 102 | 6, // segmentCapacityHint
|
---|
| 103 | 10, // coordCapacityHint
|
---|
| 104 | VG_PATH_CAPABILITY_ALL);
|
---|
| 105 | vgAppendPathData(path, sizeof(starSegments), starSegments, starCoords);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | // Draw the star directly using the OpenVG API.
|
---|
| 109 | vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
|
---|
| 110 |
|
---|
| 111 | // Restore normal QPainter operations.
|
---|
| 112 | painter.endNativePainting();
|
---|
| 113 |
|
---|
| 114 | painter.end();
|
---|
| 115 | }
|
---|