~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/demos-deform-pathdeform-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
    <title>Qt 4.0: pathdeform.cpp Example File (demos/deform/pathdeform.cpp)</title>
 
7
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
8
a:link { color: #004faf; text-decoration: none }
 
9
a:visited { color: #672967; text-decoration: none }
 
10
td.postheader { font-family: sans-serif }
 
11
tr.address { font-family: sans-serif }
 
12
body { background: #ffffff; color: black; }</style>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
18
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">pathdeform.cpp Example File<br /><small><small>demos/deform/pathdeform.cpp</small></small></h1>
 
20
<pre>&nbsp;   #include &quot;pathdeform.h&quot;
 
21
 
 
22
    #include &lt;qapplication.h&gt;
 
23
    #include &lt;qdebug.h&gt;
 
24
    #include &lt;qlayout.h&gt;
 
25
    #include &lt;qlineedit.h&gt;
 
26
    #include &lt;qevent.h&gt;
 
27
 
 
28
    #include &lt;math.h&gt;
 
29
 
 
30
    PathDeformWidget::PathDeformWidget(QWidget *parent)
 
31
        : QWidget(parent)
 
32
    {
 
33
        setWindowTitle(&quot;Vector Deformation&quot;);
 
34
 
 
35
        m_renderer = new PathDeformRenderer(this);
 
36
        m_renderer-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
37
 
 
38
        ArthurGroupBox *mainGroup = new ArthurGroupBox(this);
 
39
        mainGroup-&gt;setTitle(&quot;Vector Deformation&quot;);
 
40
 
 
41
        ArthurGroupBox *radiusGroup = new ArthurGroupBox(mainGroup);
 
42
        radiusGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
 
43
        radiusGroup-&gt;setTitle(&quot;Lens radius&quot;);
 
44
        QSlider *radiusSlider = new QSlider(Qt::Horizontal, radiusGroup);
 
45
        radiusSlider-&gt;setRange(50, 150);
 
46
        radiusSlider-&gt;setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
 
47
 
 
48
        ArthurGroupBox *deformGroup = new ArthurGroupBox(mainGroup);
 
49
        deformGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
 
50
        deformGroup-&gt;setTitle(&quot;Deformation&quot;);
 
51
        QSlider *deformSlider = new QSlider(Qt::Horizontal, deformGroup);
 
52
        deformSlider-&gt;setRange(-100, 100);
 
53
        deformSlider-&gt;setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
 
54
 
 
55
        ArthurGroupBox *fontSizeGroup = new ArthurGroupBox(mainGroup);
 
56
        fontSizeGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
 
57
        fontSizeGroup-&gt;setTitle(&quot;Font Size&quot;);
 
58
        QSlider *fontSizeSlider = new QSlider(Qt::Horizontal, fontSizeGroup);
 
59
        fontSizeSlider-&gt;setRange(16, 200);
 
60
        fontSizeSlider-&gt;setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
 
61
 
 
62
        ArthurGroupBox *textGroup = new ArthurGroupBox(mainGroup);
 
63
        textGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
 
64
        textGroup-&gt;setTitle(&quot;Text&quot;);
 
65
        QLineEdit *textInput = new QLineEdit(textGroup);
 
66
 
 
67
        QPushButton *animateButton = new QPushButton(mainGroup);
 
68
        animateButton-&gt;setText(&quot;Animated&quot;);
 
69
        animateButton-&gt;setCheckable(true);
 
70
 
 
71
        QPushButton *showSourceButton = new QPushButton(mainGroup);
 
72
        showSourceButton-&gt;setText(&quot;Show Source&quot;);
 
73
    //     showSourceButton-&gt;setCheckable(true);
 
74
 
 
75
        QPushButton *whatsThisButton = new QPushButton(mainGroup);
 
76
        whatsThisButton-&gt;setText(&quot;What's This?&quot;);
 
77
        whatsThisButton-&gt;setCheckable(true);
 
78
 
 
79
        // Layouts
 
80
        QHBoxLayout *mainLayout = new QHBoxLayout(this);
 
81
        mainLayout-&gt;addWidget(m_renderer);
 
82
        mainLayout-&gt;addWidget(mainGroup);
 
83
        mainGroup-&gt;setFixedWidth(180);
 
84
 
 
85
        QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
 
86
        mainGroupLayout-&gt;addWidget(radiusGroup);
 
87
        mainGroupLayout-&gt;addWidget(deformGroup);
 
88
        mainGroupLayout-&gt;addWidget(fontSizeGroup);
 
89
        mainGroupLayout-&gt;addWidget(textGroup);
 
90
        mainGroupLayout-&gt;addWidget(animateButton);
 
91
        mainGroupLayout-&gt;addStretch(1);
 
92
        mainGroupLayout-&gt;addWidget(showSourceButton);
 
93
        mainGroupLayout-&gt;addWidget(whatsThisButton);
 
94
 
 
95
        QVBoxLayout *radiusGroupLayout = new QVBoxLayout(radiusGroup);
 
96
        radiusGroupLayout-&gt;addWidget(radiusSlider);
 
97
 
 
98
        QVBoxLayout *deformGroupLayout = new QVBoxLayout(deformGroup);
 
99
        deformGroupLayout-&gt;addWidget(deformSlider);
 
100
 
 
101
        QVBoxLayout *fontSizeGroupLayout = new QVBoxLayout(fontSizeGroup);
 
102
        fontSizeGroupLayout-&gt;addWidget(fontSizeSlider);
 
103
 
 
104
        QVBoxLayout *textGroupLayout = new QVBoxLayout(textGroup);
 
105
        textGroupLayout-&gt;addWidget(textInput);
 
106
 
 
107
        connect(textInput, SIGNAL(textChanged(QString)), m_renderer, SLOT(setText(QString)));
 
108
        connect(radiusSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setRadius(int)));
 
109
        connect(deformSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setIntensity(int)));
 
110
        connect(fontSizeSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setFontSize(int)));
 
111
        connect(animateButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setAnimated(bool)));
 
112
        connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool)));
 
113
        connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource()));
 
114
        connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
 
115
                whatsThisButton, SLOT(setChecked(bool)));
 
116
 
 
117
        animateButton-&gt;animateClick();
 
118
        deformSlider-&gt;setValue(80);
 
119
        radiusSlider-&gt;setValue(100);
 
120
        fontSizeSlider-&gt;setValue(120);
 
121
        textInput-&gt;setText(&quot;Qt&quot;);
 
122
 
 
123
        m_renderer-&gt;loadSourceFile(&quot;:res/pathdeform.cpp&quot;);
 
124
        m_renderer-&gt;loadDescription(&quot;:res/pathdeform.html&quot;);
 
125
        m_renderer-&gt;setDescriptionEnabled(false);
 
126
    }
 
127
 
 
128
    static inline QRect circle_bounds(const QPointF &amp;center, double radius, double compensation)
 
129
    {
 
130
        return QRect(qRound(center.x() - radius - compensation),
 
131
                     qRound(center.y() - radius - compensation),
 
132
                     qRound((radius + compensation) * 2),
 
133
                     qRound((radius + compensation) * 2));
 
134
 
 
135
    }
 
136
 
 
137
    const int LENS_EXTENT = 10;
 
138
 
 
139
    PathDeformRenderer::PathDeformRenderer(QWidget *widget)
 
140
        : ArthurFrame(widget)
 
141
    {
 
142
        m_radius = 100;
 
143
        m_pos = QPointF(m_radius, m_radius);
 
144
        m_direction = QPointF(1, 1);
 
145
        m_fontSize = 24;
 
146
        m_animated = true;
 
147
        m_repaintTimer.start(25, this);
 
148
        m_repaintTracker.start();
 
149
        m_intensity = 1;
 
150
 
 
151
    //     m_fpsTimer.start(1000, this);
 
152
    //     m_fpsCounter = 0;
 
153
 
 
154
        generateLensPixmap();
 
155
    }
 
156
 
 
157
    void PathDeformRenderer::setText(const QString &amp;text)
 
158
    {
 
159
        m_text = text;
 
160
 
 
161
        QFont f(&quot;times new roman,utopia&quot;);
 
162
        f.setStyleStrategy(QFont::ForceOutline);
 
163
        f.setPointSize(m_fontSize);
 
164
        f.setStyleHint(QFont::Times);
 
165
 
 
166
        QFontMetrics fm(f);
 
167
 
 
168
        m_paths.clear();
 
169
        m_pathBounds = QRect();
 
170
 
 
171
        QPointF advance(0, 0);
 
172
 
 
173
        bool do_quick = true;
 
174
        for (int i=0; i&lt;text.size(); ++i) {
 
175
            if (text.at(i).unicode() &gt;= 0x4ff &amp;&amp; text.at(i).unicode() &lt;= 0x1e00) {
 
176
                do_quick = false;
 
177
                break;
 
178
            }
 
179
        }
 
180
 
 
181
        if (do_quick) {
 
182
            for (int i=0; i&lt;text.size(); ++i) {
 
183
                QPainterPath path;
 
184
                path.addText(advance, f, text.mid(i, 1));
 
185
                m_pathBounds |= path.boundingRect();
 
186
                m_paths &lt;&lt; path;
 
187
                advance += QPointF(fm.width(text.mid(i, 1)), 0);
 
188
            }
 
189
        } else {
 
190
            QPainterPath path;
 
191
            path.addText(advance, f, text);
 
192
            m_pathBounds |= path.boundingRect();
 
193
            m_paths &lt;&lt; path;
 
194
        }
 
195
 
 
196
        for (int i=0; i&lt;m_paths.size(); ++i)
 
197
            m_paths[i] = m_paths[i] * QMatrix(1, 0, 0, 1, -m_pathBounds.x(), -m_pathBounds.y());
 
198
 
 
199
        update();
 
200
    }
 
201
 
 
202
    void PathDeformRenderer::generateLensPixmap()
 
203
    {
 
204
        double rad = m_radius + LENS_EXTENT;
 
205
 
 
206
        QRect bounds = circle_bounds(QPointF(), rad, 0);
 
207
 
 
208
        QPainter painter;
 
209
 
 
210
        if (preferImage()) {
 
211
            m_lens_image = QImage(bounds.size(), QImage::Format_ARGB32_Premultiplied);
 
212
            m_lens_image.fill(0);
 
213
            painter.begin(&amp;m_lens_image);
 
214
        } else {
 
215
            m_lens_pixmap = QPixmap(bounds.size());
 
216
            m_lens_pixmap.fill(QColor(0, 0, 0, 0));
 
217
            painter.begin(&amp;m_lens_pixmap);
 
218
        }
 
219
 
 
220
        QRadialGradient gr(rad, rad, rad, 3 * rad / 5, 3 * rad / 5);
 
221
        gr.setColorAt(0.0, QColor(255, 255, 255, 191));
 
222
        gr.setColorAt(0.2, QColor(255, 255, 127, 191));
 
223
        gr.setColorAt(0.9, QColor(150, 150, 200, 63));
 
224
        gr.setColorAt(0.95, QColor(0, 0, 0, 127));
 
225
        gr.setColorAt(1, QColor(0, 0, 0, 0));
 
226
        painter.setRenderHint(QPainter::Antialiasing);
 
227
        painter.setBrush(gr);
 
228
        painter.setPen(Qt::NoPen);
 
229
        painter.drawEllipse(0, 0, bounds.width(), bounds.height());
 
230
    }
 
231
 
 
232
    void PathDeformRenderer::setAnimated(bool animated)
 
233
    {
 
234
        m_animated = animated;
 
235
 
 
236
        if (m_animated) {
 
237
    //         m_fpsTimer.start(1000, this);
 
238
    //         m_fpsCounter = 0;
 
239
            m_repaintTimer.start(25, this);
 
240
            m_repaintTracker.start();
 
241
        } else {
 
242
    //         m_fpsTimer.stop();
 
243
            m_repaintTimer.stop();
 
244
        }
 
245
    }
 
246
 
 
247
    void PathDeformRenderer::timerEvent(QTimerEvent *e)
 
248
    {
 
249
 
 
250
        if (e-&gt;timerId() == m_repaintTimer.timerId()) {
 
251
 
 
252
            if (QLineF(QPointF(0,0), m_direction).length() &gt; 1)
 
253
                m_direction *= 0.995;
 
254
            double time = m_repaintTracker.restart();
 
255
 
 
256
            QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
 
257
 
 
258
            double dx = m_direction.x();
 
259
            double dy = m_direction.y();
 
260
            if (time &gt; 0) {
 
261
                dx = dx * time * .1;
 
262
                dy = dy * time * .1;
 
263
            }
 
264
 
 
265
            m_pos += QPointF(dx, dy);
 
266
 
 
267
            if (m_pos.x() - m_radius &lt; 0) {
 
268
                m_direction.setX(-m_direction.x());
 
269
                m_pos.setX(m_radius);
 
270
            } else if (m_pos.x() + m_radius &gt; width()) {
 
271
                m_direction.setX(-m_direction.x());
 
272
                m_pos.setX(width() - m_radius);
 
273
            }
 
274
 
 
275
            if (m_pos.y() - m_radius &lt; 0) {
 
276
                m_direction.setY(-m_direction.y());
 
277
                m_pos.setY(m_radius);
 
278
            } else if (m_pos.y() + m_radius &gt; height()) {
 
279
                m_direction.setY(-m_direction.y());
 
280
                m_pos.setY(height() - m_radius);
 
281
            }
 
282
 
 
283
            QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
 
284
            update(QRect(rectBefore | rectAfter));
 
285
            QApplication::syncX();
 
286
        }
 
287
    //     else if (e-&gt;timerId() == m_fpsTimer.timerId()) {
 
288
    //         printf(&quot;fps: %d\n&quot;, m_fpsCounter);
 
289
    //         emit frameRate(m_fpsCounter);
 
290
    //         m_fpsCounter = 0;
 
291
 
 
292
    //     }
 
293
    }
 
294
 
 
295
    void PathDeformRenderer::mousePressEvent(QMouseEvent *e)
 
296
    {
 
297
        setDescriptionEnabled(false);
 
298
 
 
299
        m_repaintTimer.stop();
 
300
        m_offset = QPointF();
 
301
        if (QLineF(m_pos, e-&gt;pos()).length() &lt;= m_radius)
 
302
            m_offset = m_pos - e-&gt;pos();
 
303
 
 
304
        mouseMoveEvent(e);
 
305
    }
 
306
 
 
307
    void PathDeformRenderer::mouseReleaseEvent(QMouseEvent *e)
 
308
    {
 
309
        if (e-&gt;buttons() == Qt::NoButton &amp;&amp; m_animated) {
 
310
            m_repaintTimer.start(10, this);
 
311
            m_repaintTracker.start();
 
312
        }
 
313
    }
 
314
 
 
315
    void PathDeformRenderer::mouseMoveEvent(QMouseEvent *e)
 
316
    {
 
317
        QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
 
318
        if (e-&gt;type() == QEvent::MouseMove) {
 
319
            QLineF line(m_pos, e-&gt;pos() + m_offset);
 
320
            line.setLength(line.length() * .1);
 
321
            QPointF dir(line.dx(), line.dy());
 
322
            m_direction = (m_direction + dir) / 2;
 
323
        }
 
324
        m_pos = e-&gt;pos() + m_offset;
 
325
        QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
 
326
        update(rectBefore | rectAfter);
 
327
    }
 
328
 
 
329
    QPainterPath PathDeformRenderer::lensDeform(const QPainterPath &amp;source, const QPointF &amp;offset)
 
330
    {
 
331
        QPainterPath path;
 
332
        path.addPath(source);
 
333
 
 
334
        double flip = m_intensity;
 
335
 
 
336
        for (int i=0; i&lt;path.elementCount(); ++i) {
 
337
            QPainterPath::Element &amp;e = const_cast&lt;QPainterPath::Element &amp;&gt;(path.elementAt(i));
 
338
 
 
339
            double x = e.x + offset.x();
 
340
            double y = e.y + offset.y();
 
341
 
 
342
            double dx = x - m_pos.x();
 
343
            double dy = y - m_pos.y();
 
344
            double len = m_radius - sqrt(dx * dx + dy * dy);
 
345
 
 
346
            if (len &gt; 0) {
 
347
                e.x = x + flip * dx * len / m_radius;
 
348
                e.y = y + flip * dy * len / m_radius;
 
349
            } else {
 
350
                e.x = x;
 
351
                e.y = y;
 
352
            }
 
353
 
 
354
        }
 
355
 
 
356
        return path;
 
357
    }
 
358
 
 
359
    void PathDeformRenderer::paint(QPainter *painter)
 
360
    {
 
361
        int pad_x = 5;
 
362
        int pad_y = 5;
 
363
 
 
364
        int skip_x = qRound(m_pathBounds.width() + pad_x + m_fontSize/2);
 
365
        int skip_y = qRound(m_pathBounds.height() + pad_y);
 
366
 
 
367
        painter-&gt;setPen(Qt::NoPen);
 
368
        painter-&gt;setBrush(Qt::black);
 
369
 
 
370
        QRectF clip(painter-&gt;clipPath().boundingRect());
 
371
 
 
372
        int overlap = pad_x / 2;
 
373
 
 
374
        for (int start_y=0; start_y &lt; height(); start_y += skip_y) {
 
375
 
 
376
            if (start_y &gt; clip.bottom())
 
377
                break;
 
378
 
 
379
            int start_x = -overlap;
 
380
            for (; start_x &lt; width(); start_x += skip_x) {
 
381
 
 
382
                if (start_y + skip_y &gt;= clip.top() &amp;&amp;
 
383
                    start_x + skip_x &gt;= clip.left() &amp;&amp;
 
384
                    start_x &lt;= clip.right()) {
 
385
                    for (int i=0; i&lt;m_paths.size(); ++i) {
 
386
                        QPainterPath path = lensDeform(m_paths[i], QPointF(start_x, start_y));
 
387
                        painter-&gt;drawPath(path);
 
388
                    }
 
389
                }
 
390
            }
 
391
            overlap = skip_x - (start_x - width());
 
392
 
 
393
        }
 
394
 
 
395
        if (preferImage()) {
 
396
            painter-&gt;drawImage(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
 
397
                               m_lens_image);
 
398
        } else {
 
399
            painter-&gt;drawPixmap(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
 
400
                                m_lens_pixmap);
 
401
        }
 
402
    }
 
403
 
 
404
    void PathDeformRenderer::setRadius(int radius)
 
405
    {
 
406
        double max = qMax(m_radius, (double)radius);
 
407
        m_radius = radius;
 
408
        generateLensPixmap();
 
409
        if (!m_animated || m_radius &lt; max)
 
410
            update(circle_bounds(m_pos, max, m_fontSize));
 
411
    }
 
412
 
 
413
    void PathDeformRenderer::setIntensity(int intensity)
 
414
    {
 
415
        m_intensity = intensity / 100.0;
 
416
        if (!m_animated)
 
417
            update(circle_bounds(m_pos, m_radius, m_fontSize));
 
418
    }</pre>
 
419
<p /><address><hr /><div align="center">
 
420
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
421
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
422
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
423
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
424
</tr></table></div></address></body>
 
425
</html>