diff options
Diffstat (limited to 'qtruby/rubylib/examples/qt-examples/hello')
-rw-r--r-- | qtruby/rubylib/examples/qt-examples/hello/hello.rb | 78 | ||||
-rwxr-xr-x | qtruby/rubylib/examples/qt-examples/hello/main.rb | 23 |
2 files changed, 101 insertions, 0 deletions
diff --git a/qtruby/rubylib/examples/qt-examples/hello/hello.rb b/qtruby/rubylib/examples/qt-examples/hello/hello.rb new file mode 100644 index 00000000..ce957c75 --- /dev/null +++ b/qtruby/rubylib/examples/qt-examples/hello/hello.rb @@ -0,0 +1,78 @@ +require 'Qt' + +class Hello < Qt::Widget + + signals 'clicked()' + slots 'animate()' + + # Constructs a Hello widget. Starts a 40 ms animation timer + def initialize (text) + super() + + @b = 0 + @text = text + @sin_tbl = [0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38] + timer = Qt::Timer.new(self); + connect(timer, SIGNAL('timeout()'), SLOT('animate()')) + timer.start(40); + + resize(260, 130) + end + + # This slot is called each time the timer fires. + def animate + @b = (@b + 1) & 15 + repaint(false) + end + + # Handles mouse button release events for the Hello widget. + # + # We emit the clicked() signal when the mouse is released inside + # the widget. + def mouseReleaseEvent(e) + if (rect.contains(e.pos)) + emit clicked + end + end + + # Handles paint events for the Hello widget. + # + # Flicker-free update. The text is first drawn in the pixmap and the + # pixmap is then blt'ed to the screen. + def paintEvent(e) + if @text.empty? + return + end + + # 1: Compute some sizes, positions etc. + fm = fontMetrics + + w = fm.width(@text) + 20 + h = fm.height * 2 + pmx = width/2 - w/2 + pmy = height/2 - h/2 + + # 2: Create the pixmap and fill it with the widget's background + pm = Qt::Pixmap.new(w, h) + pm.fill(self, pmx, pmy) + + # 3: Paint the pixmap. Cool wave effect + p = Qt::Painter.new; + x = 10 + y = h/2 + fm.descent + i = 0 + p.begin(pm) + p.setFont(font) + + for i in [email protected] + j = (@b+i) & 15 + p.setPen(Qt::Color.new((15-j)*16,255,255,Qt::Color.Hsv) ) + p.drawText( x, y-@sin_tbl[j]*h/800, @text[i,1], 1 ) + x += fm.width(@text[i,1]) + end + p.end + + #4: Copy the pixmap to the Hello widget + bitBlt(self, pmx, pmy, pm) + end +end diff --git a/qtruby/rubylib/examples/qt-examples/hello/main.rb b/qtruby/rubylib/examples/qt-examples/hello/main.rb new file mode 100755 index 00000000..a6d3447f --- /dev/null +++ b/qtruby/rubylib/examples/qt-examples/hello/main.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +require 'Qt' +require 'hello' + +a = Qt::Application.new(ARGV) +s = '' + +s = ARGV[0..ARGV.size-1].join(' ') if ARGV.length + +if (s.empty?) + s = 'Hello, World' +end + +h = Hello.new(s) +h.setCaption('QtRuby says hello') +h.connect(h, SIGNAL('clicked()'), a, SLOT('quit()')) +h.setFont(Qt::Font.new('times', 32, Qt::Font.Bold)) # default font +h.setBackgroundColor(Qt::white) # default bg color +a.setMainWidget(h) +h.show + +a.exec |