Merge branch 'master' of git://labs.trolltech.com/qtscriptgenerator
[qtscriptgenerator/amarok.git] / examples / ConcentricCircles.qs
blobf20a22bbbff580367361d2782a05af36365a8ecf
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file.  Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function tr(s) { return s; }
27 function CircleWidget(parent)
29     QWidget.call(this, parent);
31     this.floatBased = false;
32     this.antialiased = false;
33     this.frameNo = 0;
35     this.setBackgroundRole(QPalette.Base);
36     this.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding);
39 CircleWidget.prototype = new QWidget();
41 CircleWidget.prototype.setFloatBased = function(floatBased)
43     this.floatBased = floatBased;
44     this.update();
47 CircleWidget.prototype.setAntialiased = function(antialiased)
49     this.antialiased = antialiased;
50     this.update();
53 CircleWidget.prototype.getMinimumSizeHint = function()
55     return new QSize(50, 50);
58 CircleWidget.prototype.getSizeHint = function()
60     return new QSize(180, 180);
63 CircleWidget.prototype.nextAnimationFrame = function()
65     ++this.frameNo;
66     this.update();
69 CircleWidget.prototype.paintEvent = function(/* event */)
71     var painter = new QPainter();
72     painter.begin(this);
73     painter.setRenderHint(QPainter.Antialiasing, this.antialiased);
74     painter.translate(this.width / 2, this.height / 2);
76     for (var diameter = 0; diameter < 256; diameter += 9) {
77         var delta = Math.abs((this.frameNo % 128) - diameter / 2);
78         var alpha = 255 - (delta * delta) / 4 - diameter;
79         if (alpha > 0) {
80             painter.setPen(new QPen(new QColor(0, diameter / 2, 127, alpha), 3));
82             if (this.floatBased) {
83                 painter.drawEllipse(new QRectF(-diameter / 2.0, -diameter / 2.0,
84                                                diameter, diameter));
85             } else {
86                 painter.drawEllipse(new QRect(-diameter / 2, -diameter / 2,
87                                               diameter, diameter));
88             }
89         }
90     }
92     painter.end();
96 function Window(parent)
98     QWidget.call(this, parent);
100     var aliasedLabel = this.createLabel(tr("Aliased"));
101     var antialiasedLabel = this.createLabel(tr("Antialiased"));
102     var intLabel = this.createLabel(tr("Int"));
103     var floatLabel = this.createLabel(tr("Float"));
105     var layout = new QGridLayout();
106     layout.addWidget(aliasedLabel, 0, 1);
107     layout.addWidget(antialiasedLabel, 0, 2);
108     layout.addWidget(intLabel, 1, 0);
109     layout.addWidget(floatLabel, 2, 0);
111     var timer = new QTimer(this);
113     for (var i = 0; i < 2; ++i) {
114         for (var j = 0; j < 2; ++j) {
115             var cw = new CircleWidget();
116             cw.setAntialiased(j != 0);
117             cw.setFloatBased(i != 0);
119             timer.timeout.connect(
120                     cw, cw.nextAnimationFrame);
122             layout.addWidget(cw, i + 1, j + 1);
123         }
124     }
125     timer.start(100);
126     this.setLayout(layout);
128     this.windowTitle = tr("Concentric Circles");
131 Window.prototype = new QWidget();
133 Window.prototype.createLabel = function(text)
135     var label = new QLabel(text);
136     label.alignment = Qt.AlignCenter;
137     label.margin = 2;
138     label.setFrameStyle(QFrame.Box | QFrame.Sunken);
139     return label;
142 var win = new Window();
143 win.show();
144 QCoreApplication.exec();