Monday, April 17. 2006Poking the Qt API with QtRuby
Several months ago, Leinir made a mockup (not sure what happened to it) for the Dynamic Playlist bar. Its mostly been implemented, but the original mockup looked pretty snazzy - more colors and bevels if I remember correctly.
I often open up the Ruby interactive shell `irb`, run a require 'Korundum' and ask simple questions about the API with the help of Ruby's great introspection features. What does this KURL function do? What color is colorGroup.highlight? This time the task was more complicated; I've never used QPainter before. Rather then code it in C++ and have to deal with the code-compile-link-run cycle that inevitably follows whenever I'm using an unfamiliar API, I decided to use Ruby. Even when not used interactively, code-run is just faster and is a real benefit when having to test and fiddle around with the details. So here is the beveled result: ![]() Not much, but at least I know how to use QPainter now. Read on to see the source code. #!/usr/kde/3.5/bin/krubyinit
require 'Korundum' class Border < Qt::HBox def initialize super(nil, "paint me") @curveWidth = 5 @text = 'Dynamic Mode' @font = Qt::Font.new @font.setBold(true) #@font.setPointSize( 2 ) fm = Qt::FontMetrics.new(@font) @textWidth = fm.width(@text) if fm.height > 16 then @height = fm.height else @height = 16 end setMinimumWidth(@curveWidth*3 + @textWidth+16) setMinimumHeight(@height) end def paintEvent(e) p = Qt::Painter.new() p.begin(self, false) pen = Qt::Pen.new( colorGroup.highlightedText, 0) p.setPen( pen ); p.setBrush( colorGroup.highlight ); p.setFont(@font) #p.drawRoundRect(0, 0, fm.width(@text) + 10, @height() + 10,60,60) imageWidth = 16 p.drawEllipse(0, 0, @curveWidth * 2, @height) p.drawEllipse(@textWidth+@curveWidth + imageWidth, 0, 10, @height) p.fillRect(@curveWidth, 0, @textWidth+imageWidth+@curveWidth, @height, Qt::Brush.new(colorGroup.highlight) ) p.drawPixmap( @curveWidth, (@height - imageWidth) / 2, Qt::Pixmap.new(KDE::SmallIcon("dynamic")) ) p.drawText( Qt::Rect.new( imageWidth, 0, @curveWidth+@textWidth +imageWidth, @height), Qt::AlignCenter, @text ) #not sure why first arg of Rect shouldn't add @curveWidth p..end() end end about = KDE::AboutData.new("amarok", "amarok", "1.0") KDE::CmdLineArgs.init(ARGV, about) app = KDE::Application.new() Qt.debug_level = Qt::DebugLevel::High parentWidget = Border.new app.mainWidget = parentWidget spacer = Qt::SpacerItem.new(50, 1) parentWidget.layout.addItem(spacer) parentWidget.show app.exec Edit: And now here is the C++ equivalent that I commited last night: DynamicTitle::DynamicTitle(QWidget* w) : QWidget(w, "dynamic title") { m_font.setBold( true ); setTitle(""); } void DynamicTitle::setTitle(const QString& newTitle) { m_title = i18n("Dynamic Playlist: ") + newTitle; QFontMetrics fm(m_font); setMinimumWidth( s_curveWidth*3 + fm.width(m_title) + s_imageSize ); setMinimumHeight( fm.height() ); } void DynamicTitle::paintEvent(QPaintEvent* /*e*/) { QPainter p; p.begin( this, false ); QPen pen( colorGroup().highlightedText(), 0, Qt::NoPen ); p.setPen( pen ); p.setBrush( colorGroup().highlight() ); p.setFont(m_font); QFontMetrics fm(m_font); int textHeight = fm.height(); if (textHeight < s_imageSize) textHeight = s_imageSize; int textWidth = fm.width(m_title); int yStart = (height() - textHeight) / 2; if(yStart < 0) yStart = 0; p.drawEllipse( 0, yStart, s_curveWidth * 2, textHeight); p.drawEllipse( s_curveWidth + textWidth + s_imageSize, yStart, s_curveWidth*2, textHeight); p.fillRect( s_curveWidth, yStart, textWidth + s_imageSize + s_curveWidth, textHeight , QBrush( colorGroup().highlight()) ); p.drawPixmap( s_curveWidth, yStart + ((textHeight - s_imageSize) /2), SmallIcon("dynamic") ); //not sure why first arg of Rect shouldn't add @curveWidth p.drawText( QRect(s_imageSize, yStart, s_curveWidth + textWidth +s_imageSize, textHeight), Qt::AlignCenter, m_title); } Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
You wouldn't happen to know whether there are any plans to include Korundum by default with KDE 4? That would be very nice for those of us who have fallen in love with Ruby.
No I don't. Its kind of a chicken/egg problem... no reason to put Korundum in kdelibs unless there are apps written that will actually use it, but apps won't be written for it when the installed base of Korundum is so small.
There are plans for Plasma to support for Ruby scripting.
I vote for ruby bindings in KDE4!
personally i feel KDE and Ruby both share a taste for elegance, pragmatism, simplicity, etc.. (i can go on) i think ruby and KDE could be a very nice 'match' but i dont think it is a good idea to put them in the kdelibs, that would allow kdebase apps to depend on ruby, which means the end of the super-fast low-mem-footprint KDE as we know it... _cies.
A C++ KDE program doesn't just randomly start becoming a Ruby program just because a KDE ruby library is available to it.
You think I could get this working on Windows? I only Qt on Windows and a quick test environment would be absolutely brilliant.
Not to mention ruby scripting in general.
There are efforts to get the QtRuby in trunk/ which works with Qt 4.0 to compile on Windows. You can check out the forums at:
http://rubyforge.org/forum/forum.php?forum_id=723 You might be in an unique position to help, I stopped following the forum, but when I did most of the people who seemed to be trying to compile QtRuby on Windows were (unsurprisingly) Windows folks and didn't have much experience compiling anything.
I started getting it to compile, then I needed perl, then I needed autoconf, then I discovered someone else already made it compile.
But with cygwin only so far, so I may still persevere next time I need a Qt-sandbox. |
Amarok LinksCalendar
QuicksearchCategoriesSyndicate This BlogBlog Administration |
|||||||||||||||||||||||||||||||||||||||||||||||||

