qt_connect 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,502 @@
1
+ =begin
2
+ ** Copyright (C) 1992-2009 Nokia. All rights reserved.
3
+ **
4
+ ** This file is part of Qt Jambi.
5
+ **
6
+ ** ** $BEGIN_LICENSE$
7
+ ** Commercial Usage
8
+ ** Licensees holding valid Qt Commercial licenses may use this file in
9
+ ** accordance with the Qt Commercial License Agreement provided with the
10
+ ** Software or, alternatively, in accordance with the terms contained in
11
+ ** a written agreement between you and Nokia.
12
+ **
13
+ ** GNU Lesser General Public License Usage
14
+ ** Alternatively, this file may be used under the terms of the GNU Lesser
15
+ ** General Public License version 2.1 as published by the Free Software
16
+ ** Foundation and appearing in the file LICENSE.LGPL included in the
17
+ ** packaging of this file. Please review the following information to
18
+ ** ensure the GNU Lesser General Public License version 2.1 requirements
19
+ ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20
+ **
21
+ ** In addition, as a special exception, Nokia gives you certain
22
+ ** additional rights. These rights are described in the Nokia Qt LGPL
23
+ ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
24
+ ** package.
25
+ **
26
+ ** GNU General Public License Usage
27
+ ** Alternatively, this file may be used under the terms of the GNU
28
+ ** General Public License version 3.0 as published by the Free Software
29
+ ** Foundation and appearing in the file LICENSE.GPL included in the
30
+ ** packaging of this file. Please review the following information to
31
+ ** ensure the GNU General Public License version 3.0 requirements will be
32
+ ** met: http://www.gnu.org/copyleft/gpl.html.
33
+ **
34
+ ** If you are unsure which license is appropriate for your use, please
35
+ ** contact the sales department at qt-sales@nokia.com.
36
+ ** $END_LICENSE$
37
+
38
+ **
39
+ ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
40
+ ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41
+ **
42
+ ** Translated to QtRuby by Cees Zeelenberg
43
+ =end
44
+
45
+ require 'qt_connect'
46
+ require 'arthurframe'
47
+
48
+
49
+ class PainterPathElements
50
+ attr_accessor :path,:elements
51
+ def initialize
52
+ @path=nil
53
+ @elements=[]
54
+ end
55
+ end
56
+
57
+ class PathDeformRenderer < ArthurFrame
58
+
59
+ attr_accessor :animated,:radius,:fontSize,:intensity,:text
60
+
61
+ def initialize(parent)
62
+ @repaintTimer =Qt::BasicTimer.new
63
+ @repaintTracker = Qt::Time.new
64
+ @paths = []
65
+ @advances = []
66
+ @pathBounds = Qt::RectF.new
67
+ @text = ""
68
+ @lens_pixmap = nil
69
+ @lens_image = nil
70
+ @fontSize = 0
71
+ @animated = false
72
+
73
+ @textDirty = true;
74
+ super(parent)
75
+ @radius = 100
76
+ @pos = Qt::PointF.new(@radius, @radius)
77
+ @direction = Qt::PointF.new(1, 1)
78
+ @fontSize = 24
79
+ @animated = true;
80
+ @repaintTimer.start(100, self)
81
+ @repaintTracker.start
82
+ @intensity = 1
83
+ generateLensPixmap
84
+ end
85
+
86
+ def fontSize=(fontSize)
87
+ @fontSize = fontSize
88
+ self.text=@text
89
+ end
90
+
91
+ def sizeHint
92
+ return Qt::Size.new(600, 500)
93
+ end
94
+
95
+ def text=(text)
96
+ @text = text
97
+ @textDirty = true
98
+ update
99
+ end
100
+
101
+ def makeTextPaths
102
+ f = Qt::Font.new("times new roman,utopia")
103
+ f.setStyleStrategy(Qt::Font::ForceOutline)
104
+ f.setPointSize(@fontSize)
105
+ f.setStyleHint(Qt::Font::Times)
106
+
107
+
108
+ fm = Qt::FontMetrics.new(f)
109
+ @paths=[]
110
+ @pathBounds = Qt::RectF.new
111
+ advance = Qt::PointF.new(0, 0)
112
+ paths = []
113
+
114
+ do_quick = true;
115
+
116
+ if (do_quick)
117
+ @text.each_char{ |c|
118
+ path = Qt::PainterPath.new
119
+ path.addText(advance, f, c)
120
+ @pathBounds = @pathBounds.united(path.boundingRect)
121
+ paths << path
122
+ advance+=Qt::PointF.new(fm.width(c), 0)
123
+ }
124
+ else
125
+ path = Qt::PainterPath.new
126
+ path.addText(advance, f, @text)
127
+ @pathBounds = @pathBounds.united(path.boundingRect)
128
+ paths << path
129
+ end
130
+
131
+ m = Qt::Matrix.new(1, 0, 0, 1, -@pathBounds.x, -@pathBounds.y)
132
+ paths.each{ |path| addPath(m.map(path)) }
133
+ @textDirty = false;
134
+ end
135
+
136
+ def addPath(path)
137
+ p = PainterPathElements.new
138
+ p.path = path
139
+ (0...path.elementCount).each{ |i| p.elements.push(path.elementAt(i))}
140
+ @paths << p
141
+ end
142
+
143
+ def circle_bounds(center,radius,compensation)
144
+ return Qt::Rect.new((center.x - radius - compensation).round,
145
+ (center.y - radius - compensation).round,
146
+ ((radius + compensation) * 2).round,
147
+ ((radius + compensation) * 2).round)
148
+ end
149
+
150
+
151
+ LENS_EXTENT = 10;
152
+ def generateLensPixmap
153
+ rad = @radius + LENS_EXTENT
154
+
155
+ bounds = circle_bounds(Qt::PointF.new, rad, 0)
156
+
157
+ painter = Qt::Painter.new
158
+ if (preferImage)
159
+ @lens_image = Qt::Image.new(bounds.size, Qt::Image::Format_ARGB32_Premultiplied)
160
+ @lens_image.fill(0)
161
+ painter.begin(@lens_image)
162
+ else
163
+ @lens_pixmap = Qt::Pixmap.new(bounds.size)
164
+ @lens_pixmap.fill(Qt::Color.new(0, 0, 0, 0))
165
+ painter.begin(@lens_pixmap)
166
+ end
167
+
168
+ gr = Qt::RadialGradient.new(rad, rad, rad, 3 * rad / 5, 3 * rad / 5) {
169
+ setColorAt(0.0, Qt::Color.new(255, 255, 255, 191))
170
+ setColorAt(0.2, Qt::Color.new(255, 255, 127, 191))
171
+ setColorAt(0.9, Qt::Color.new(150, 150, 200, 63))
172
+ setColorAt(0.95, Qt::Color.new(0, 0, 0, 127))
173
+ setColorAt(1, Qt::Color.new(0, 0, 0, 0))
174
+ }
175
+
176
+ painter.setRenderHint(Qt::Painter::Antialiasing)
177
+ painter.setBrush(Qt::Brush.new(gr))
178
+ painter.setPen(Qt::NoPen)
179
+ painter.drawEllipse(0, 0, bounds.width, bounds.height)
180
+ painter.end
181
+ end
182
+
183
+ def setAnimated(animated)
184
+ @animated = animated;
185
+
186
+ if (@animated)
187
+ @repaintTimer.start(25, self)
188
+ @repaintTracker.start
189
+ else
190
+ @repaintTimer.stop
191
+ end
192
+ end
193
+
194
+ def paintEvent(e)
195
+ makeTextPaths if (@textDirty)
196
+ super(e)
197
+ end
198
+
199
+ def timerEvent(e)
200
+ if (e.timerId == @repaintTimer.timerId)
201
+ if ((Qt::LineF.new(Qt::PointF.new(0,0), @direction)).length > 1)
202
+ @direction*=0.995
203
+ end
204
+
205
+ time = @repaintTracker.restart
206
+
207
+ rectBefore = circle_bounds(@pos, @radius, @fontSize)
208
+
209
+ dx = @direction.x
210
+ dy = @direction.y
211
+
212
+ if time > 0
213
+ dx = dx * time * 0.1;
214
+ dy = dy * time * 0.1;
215
+ end
216
+
217
+ @pos+=Qt::PointF.new(dx, dy)
218
+
219
+ if @pos.x - @radius < 0
220
+ @direction.x=-@direction.x
221
+ @pos.x=@radius
222
+ elsif @pos.x + @radius > width
223
+ @direction.x=-@direction.x
224
+ @pos.x=width - @radius
225
+ end
226
+
227
+ if @pos.y - @radius < 0
228
+ @direction.y=-@direction.y
229
+ @pos.y=@radius
230
+ elsif @pos.y + @radius > height
231
+ @direction.y=-@direction.y
232
+ @pos.y=height - @radius
233
+ end
234
+
235
+ rectAfter = circle_bounds(@pos, @radius, @fontSize)
236
+ #update(rectBefore.united(rectAfter))
237
+ update(rectBefore|rectAfter)
238
+ Qt::Application.syncX
239
+ end
240
+ end
241
+
242
+ #@Override
243
+ def mousePressEvent(e)
244
+ setDescriptionEnabled(false)
245
+
246
+ @repaintTimer.stop
247
+ @offset = Qt::PointF.new
248
+
249
+ if (Qt::LineF.new(@pos, Qt::PointF.new(e.pos))).length <= @radius
250
+ @offset = Qt::PointF.new(@pos.x, @pos.y)
251
+ @offset.subtract(Qt::PointF.new(e.pos))
252
+ end
253
+
254
+ mouseMoveEvent(e)
255
+ end
256
+
257
+ #@Override
258
+ def mouseReleaseEvent(e)
259
+ if e.buttons.isSet(Qt::MouseButton::NoButton) && @animated
260
+ @repaintTimer.start(25, self)
261
+ @repaintTracker.start
262
+ end
263
+ end
264
+
265
+ #@Override
266
+ def mouseMoveEvent(e)
267
+ rectBefore = circle_bounds(@pos, @radius, @fontSize)
268
+ if e.type == Qt::Event::Type::MouseMove
269
+ epos = Qt::PointF.new(e.pos)
270
+ epos.add(@offset)
271
+ line = Qt::LineF.new(@pos, epos)
272
+ line.setLength(line.length * 0.1)
273
+ dir = Qt::PointF.new(line.dx, line.dy)
274
+ @direction.add(dir)
275
+ @direction.multiply(0.5)
276
+ end
277
+
278
+ @pos = Qt::PointF.new(e.pos)
279
+ @pos.add(@offset)
280
+ rectAfter = circle_bounds(@pos, @radius, @fontSize)
281
+
282
+ update(rectBefore.united(rectAfter))
283
+ end
284
+
285
+ def deformElement(e, offset, pts)
286
+ flip = @intensity;
287
+
288
+ x = e.x + offset.x
289
+ y = e.y + offset.y
290
+
291
+ dx = x - @pos.x
292
+ dy = y - @pos.y
293
+ len = @radius - Math.sqrt(dx * dx + dy * dy)
294
+
295
+ if len > 0
296
+ x += flip * dx * len / @radius;
297
+ y += flip * dy * len / @radius;
298
+ end
299
+
300
+ pts[0] = x;
301
+ pts[1] = y;
302
+ end
303
+
304
+ def lensDeform(source, offset)
305
+ path = Qt::PainterPath.new
306
+ pts = []
307
+ skip=0
308
+ c1x=c2x=c1y=c2y=0
309
+ source.elements.each{ |e|
310
+ case skip
311
+ when 0
312
+ if e.isLineTo
313
+ deformElement(e, offset, pts)
314
+ path.lineTo(pts[0], pts[1])
315
+ elsif e.isMoveTo
316
+ deformElement(e, offset, pts)
317
+ path.moveTo(pts[0], pts[1])
318
+ elsif e.isCurveTo
319
+ deformElement(e, offset, pts)
320
+ c1x = pts[0]; c1y = pts[1];
321
+ skip=2
322
+ end
323
+ when 2
324
+ deformElement(e, offset, pts)
325
+ c2x = pts[0]; c2y = pts[1];
326
+ skip=1
327
+ when 1
328
+ deformElement(e, offset, pts)
329
+ ex = pts[0]; ey = pts[1];
330
+ path.cubicTo(c1x.to_f, c1y.to_f, c2x.to_f, c2y.to_f, ex.to_f, ey.to_f)
331
+ skip=0
332
+ end
333
+ }
334
+ return path
335
+ end
336
+
337
+ #@Override
338
+ def paint(painter)
339
+ pad_x = 5
340
+ pad_y = 5
341
+ skip_x = (@pathBounds.width + pad_x + @fontSize / 2).round
342
+ skip_y = (@pathBounds.height + pad_y).round
343
+
344
+ painter.setPen(Qt::NoPen)
345
+ painter.setBrush(Qt::Brush.new(Qt::Color.black))
346
+
347
+ clip = painter.clipPath.boundingRect
348
+ overlap = pad_x / 2;
349
+
350
+ (0...height).step(skip_y){ |start_y|
351
+ break if start_y > clip.bottom
352
+ start_x=-overlap
353
+ (-overlap...width).step(skip_x){ |sx|
354
+ start_x=sx
355
+ if (start_y + skip_y >= clip.top &&
356
+ start_x + skip_x >= clip.left &&
357
+ start_x <= clip.right)
358
+ @paths.each{ |p|
359
+ path = lensDeform(p, Qt::PointF.new(start_x, start_y))
360
+ painter.drawPath(path)
361
+ }
362
+ end
363
+ }
364
+ overlap = skip_x - (start_x - width)
365
+ }
366
+ if (preferImage)
367
+ painter.drawImage((@pos.x - @radius - LENS_EXTENT).to_i,(@pos.y - @radius - LENS_EXTENT).to_i, @lens_image)
368
+ else
369
+ painter.drawPixmap((@pos.x - @radius - LENS_EXTENT).to_i,(@pos.y - @radius - LENS_EXTENT).to_i, @lens_pixmap)
370
+ end
371
+ end
372
+
373
+ def radius=(radius)
374
+ max = [@radius, radius].max
375
+ @radius = radius;
376
+ generateLensPixmap
377
+ update(circle_bounds(@pos, max, @fontSize)) if !@animated || @radius < max
378
+ end
379
+
380
+ def intensity=(intensity)
381
+ @intensity = intensity / 100.0
382
+ update(circle_bounds(@pos, @radius, @fontSize)) if (!@animated)
383
+ end
384
+ end
385
+ #@QtJambiExample(name = "Deform")
386
+ class Deform < Qt::Widget
387
+
388
+ def initialize
389
+ super
390
+
391
+ setWindowTitle("Vector deformation")
392
+ setWindowIcon(Qt::Icon.new("qt-logo.png"))
393
+
394
+ @renderer = PathDeformRenderer.new(self)
395
+ #@renderer.setSizePolicy(Qt::SizePolicy::Policy::Expanding, Qt::SizePolicy::Policy::Expanding)
396
+
397
+ mainGroup = Qt::GroupBox.new(self) {
398
+ setTitle("Vector Deformation")
399
+ setFixedWidth(180)
400
+ }
401
+
402
+ radiusGroup = Qt::GroupBox.new(mainGroup) {
403
+ setTitle("Lens radius")
404
+ }
405
+
406
+ radiusSlider = Qt::Slider.new(Qt::Horizontal, radiusGroup) {
407
+ setRange(50, 150)
408
+ setSizePolicy(Qt::SizePolicy::Preferred, Qt::SizePolicy::Fixed)
409
+ }
410
+
411
+ deformGroup = Qt::GroupBox.new(mainGroup) {
412
+ setTitle("Deformation")
413
+ }
414
+
415
+ deformSlider = Qt::Slider.new(Qt::Horizontal, deformGroup) {
416
+ setRange(-100, 100)
417
+ setSizePolicy(Qt::SizePolicy::Preferred, Qt::SizePolicy::Fixed)
418
+ }
419
+
420
+ fontSizeGroup = Qt::GroupBox.new(mainGroup) {
421
+ setTitle("Font Size")
422
+ }
423
+
424
+ fontSizeSlider = Qt::Slider.new(Qt::Horizontal, fontSizeGroup) {
425
+ setRange(16, 200)
426
+ setSizePolicy(Qt::SizePolicy::Preferred, Qt::SizePolicy::Fixed)
427
+ }
428
+
429
+ textGroup = Qt::GroupBox.new(mainGroup) {
430
+ setTitle("Text")
431
+ }
432
+
433
+ textInput = Qt::LineEdit.new(textGroup)
434
+
435
+ animateButton = Qt::PushButton.new(mainGroup) {
436
+ setText("Animated")
437
+ setCheckable(true)
438
+ }
439
+
440
+ showSourceButton = Qt::PushButton.new(mainGroup) {
441
+ setText("Show Source")
442
+ }
443
+
444
+ whatsThisButton = Qt::PushButton.new(mainGroup) {
445
+ setText("What's This?")
446
+ setCheckable(true)
447
+ }
448
+
449
+ mainLayout = Qt::HBoxLayout.new(self) { |l|
450
+ l.addWidget(@renderer)
451
+ l.addWidget(mainGroup)
452
+ }
453
+
454
+
455
+ mainGroupLayout = Qt::VBoxLayout.new(mainGroup) {
456
+ addWidget(radiusGroup)
457
+ addWidget(deformGroup)
458
+ addWidget(fontSizeGroup)
459
+ addWidget(textGroup)
460
+ addWidget(animateButton)
461
+ addStretch(1)
462
+ addWidget(showSourceButton)
463
+ addWidget(whatsThisButton)
464
+ }
465
+
466
+ radiusGroupLayout = Qt::VBoxLayout.new(radiusGroup) { addWidget(radiusSlider) }
467
+
468
+ deformGroupLayout = Qt::VBoxLayout.new(deformGroup) { addWidget(deformSlider) }
469
+
470
+ fontSizeGroupLayout = Qt::VBoxLayout.new(fontSizeGroup) { addWidget(fontSizeSlider) }
471
+
472
+ textGroupLayout = Qt::VBoxLayout.new(textGroup) { addWidget(textInput) }
473
+
474
+ textInput.textChanged.connect{ |str| @renderer.text=str}
475
+ radiusSlider.valueChanged.connect{ |r| @renderer.radius=r}
476
+ deformSlider.valueChanged.connect{ |i| @renderer.intensity=i}
477
+ fontSizeSlider.valueChanged.connect{ |i| @renderer.fontSize=i}
478
+ animateButton.clicked.connect{ |tf| @renderer.setAnimated(tf)}
479
+ whatsThisButton.clicked.connect(@renderer, :setDescriptionEnabled)
480
+ showSourceButton.clicked.connect(@renderer, :showSource)
481
+ @renderer.descriptionEnabledChanged.connect(whatsThisButton, :setChecked)
482
+
483
+ animateButton.animateClick
484
+ deformSlider.setValue(80)
485
+ radiusSlider.setValue(100)
486
+ fontSizeSlider.setValue(100)
487
+ textInput.setText("Qt Jambi")
488
+
489
+ #@renderer.loadSourceFile("classpath:com/trolltech/demos/Deform.java")
490
+ @renderer.loadSourceFile("deform.rb")
491
+ @renderer.loadDescription("deform.html")
492
+ @renderer.setDescriptionEnabled(false)
493
+ end
494
+ end
495
+
496
+
497
+
498
+ app=Qt::Application.new(ARGV)
499
+ w = Deform.new
500
+ w.show
501
+ app.exec
502
+