Wednesday, April 29, 2009

Refactoring "Refactoring QtRuby Example #1"

In previous post : phosphorescence: Refactoring QtRuby Example #1, I had wrote below :
require 'Qt4'
require 'default/ui'

class Default < Qt::MainWindow
attr_reader :ui

def initialize(parent=nil)
super(parent)
@ui = UI::DefaultClass.new
@ui.setup_ui(self)
yield(@ui, self) if block_given?
ObjectSpace.define_finalizer(self, Default.delete_ui(@ui))
end

private
def Default.delete_ui(ui_to_delete)
proc {ui_to_delete.dispose unless ui_to_delete.disposed?}
end
end

But, it's still redundant. Look above in detail, attr_reader :ui is set, so it's OK just for yield(self). And 2nd reason, block argument of QtRuby's initializer is limited to one, so yield(@ui, self) is a bug. For these reasons, I refactor code block to below :
require 'Qt4'
require 'default/ui'

class Default < Qt::MainWindow
attr_reader :ui

def initialize(parent=nil)
super(parent)
@ui = UI::DefaultClass.new
@ui.setup_ui(self)
yield(self) if block_given?
ObjectSpace.define_finalizer(self, Default.delete_ui(@ui))
end

private
def Default.delete_ui(ui_to_delete)
proc {ui_to_delete.dispose unless ui_to_delete.disposed?}
end
end

Previous post phosphorescence: Refactoring QtRuby Example #1 has been corrected already. Then let's check working correctly.
irb(main):001:0> require 'Qt4'
=> true
irb(main):002:0> require 'default/default'
=> true
irb(main):003:0> Qt::Application.new(ARGV) do
irb(main):004:1* window = Default.new do|w|
irb(main):005:2* puts w.class
irb(main):006:2> puts w.ui.class
irb(main):007:2> end
irb(main):008:1> end
Default
NilClass
Default
UI::DefaultClass
=> #<Qt::Application:0x00000000c0acb0 objectName="irb">

Monday, April 27, 2009

Qt 4.5.1 and Qt Creator 1.1 were released

4 days ago, Qt 4.5.1 and Qt Creator 1.1 were released. I download and install Qt 4.5.1 SDK (LGPL) from here. It contains Qt 4.5.1 and Qt Creator 1.1 both.
#./qt-sdk-linux-x86_64-opensource-2009.02.bin

Installation instructions of QtRuby are same with phosphorescence: qt4-qtruby 2.0.3 is released except Qt's path: from /opt/qtsdk-2009.01 to /opt/qtsdk-2009.02. Then check QtRuby commands.
>/opt/ruby-1.9.1/bin/rbrcc -version
Ruby Resource Compiler for Qt version 4.5.1
>/opt/ruby-1.9.1/bin/rbuic4 -v
Qt User Interface Compiler version 4.5.1
>/opt/ruby-1.9.1/bin/rbqtapi -v
QtRuby 2.0.3 using Qt-4.5.1

Friday, April 24, 2009

Debug Hacks Conference 2009



Last night, Debug Hacks Conference 2009 was held at Jimbou-chou, Tokyo. The agenda was as follows:

OpeningPart 1 : Lightning talks about debug by authors
  • Debug with strace by Mr.Yamato
  • Debug with rpm options by Mr.Ooiwa
  • Debug with and without debuginfo by Mr.Abe
  • Debug for fault in malloc and free by Mr.Shimamoto
  • Troubleshooting Hacks by Mr.Yoshida

Part 2 : Yugui(Ruby 1.9.x release manager)'s case study
  • Log is important to reproduce a bug for debug
  • Debug for Rails app ... BDD, Design by Contract, etc.
  • Debug for Ruby Language ... assertion, debug.c, etc.
  • Debug around the thread ... try and error

Q & A was done in every sessions. And the content of book is introduced by Hiro Yoshioka himself at Someday Join Us: Debug Hacks.

At the end of conference, Hiro Yoshioka said "Please write your own debug hacks on your blog". So I write ... correctly, had wrote try and errors about QtRuby build process already(#1, #2, #3). And I will write some debugs if it occurs ... undoubtedly occurs!

Tuesday, April 21, 2009

Debug Hacks

O'Reilly Media publishes many "* hacks" series. But, Japan limited "hacks" book is released in a week. The title : "Debug Hacks" by O'Reilly Japan (written in Japanese) and Ohmsha.

Authors are Hiro Yoshioka and his colleagues. They(authors and publishers) will hold publishing anniversary conference named "Debug Hacks Conference 2009" in day after tomorrow(April 23). I will attend this conference.

Saturday, April 18, 2009

Single inheritance with explicit deletion

In my last post, I adopted The Single Inheritance Approach. But for years, The Multiple Inheritance Approach has been recommended in Qt's way. Why not?

Its keyword : "explicit deletion"! See my last post again, and in fact, it was written with explicit deletion to the UI form instance.

samplewidget.cpp
#include "samplewidget.h"
#include "ui_samplewidget.h"

SampleWidget::SampleWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::SampleWidgetClass)
{
ui->setupUi(this);
}

SampleWidget::~SampleWidget()
{
delete ui;
}

A reason of these approach is the scalability. For more detail, read the thread archives of QtCreator's mail list, title : "[Qt-creator] Multiple Iheritance".

Wednesday, April 15, 2009

Refactoring QtRuby Example #2

(continued from phosphorescence: Refactoring QtRuby Example #1)

In Qt application using UI file, there are 3 approaches.
  1. The Direct Approach
  2. The Single Inheritance Approach
  3. The Multiple Inheritance Approach
See more at Using a Designer .ui File in Your Application.

I planned to adopt The Single Inheritance Approach for two reasons. First, Ruby Language doesn't support multiple inheritance. Second, project creation wizard of Qt Creator suggests The Single Inheritance Approach like below:

samplewidget.h
#ifndef SAMPLEWIDGET_H
#define SAMPLEWIDGET_H

#include <QtGui/QWidget>

namespace Ui
{
class SampleWidgetClass; //UI form class
}

class SampleWidget : public QWidget //top widget class
{
Q_OBJECT

public:
SampleWidget(QWidget *parent = 0);
~SampleWidget();

private:
Ui::SampleWidgetClass *ui; //instantiate UI form class
};

#endif // SAMPLEWIDGET_H

samplewidget.cpp
#include "samplewidget.h"
#include "ui_samplewidget.h"

SampleWidget::SampleWidget(QWidget *parent) //instantiate top widget class
: QWidget(parent), ui(new Ui::SampleWidgetClass)
{
ui->setupUi(this);
}

SampleWidget::~SampleWidget()
{
delete ui;
}

main.cpp
#include <QtGui/QApplication>
#include "samplewidget.h"

int main(int argc, char *argv[]) //entry point
{
QApplication a(argc, argv);
SampleWidget w;
w.show();
return a.exec();
}

Then I wrote Ruby scripts corrsponding to Qt's UI form class, top widget class and entry point.

Monday, April 13, 2009

Refactoring QtRuby Example #1

On newbie seminar (see last post), I learned a structure of Qt Widgets. It's made from three parts: entry point, top widget class, and UI form class.

Look back my samples, these are not subdivided at all. So I refactor, for example, default.rb generated from rbuic4 command. I subdivide this script to 3 scripts.

ui.rb(UI form class)
require 'Qt4'

module UI
class DefaultClass < Qt::Object
# generated attr_reader properties

def setup_ui(mainWindow)
# generated codes
end

private
def retranslate_ui(mainWindow)
# generated codes
end

alias :setupUi :setup_ui
alias :retranslateUi :retranslate_ui
end
end

default.rb(top widget class)
require 'Qt4'
require 'default/ui'

class Default < Qt::MainWindow
attr_reader :ui

def initialize(parent=nil)
@ui = UI::DefaultClass.new
super
@ui.setup_ui(self)
ObjectSpace.define_finalizer(self, Default.delete_ui(@ui))
end

private
def Default.delete_ui(ui_to_delete)
proc {ui_to_delete.dispose unless ui_to_delete.disposed?}
end
end

main.rb(entry point)
require 'Qt4'
require 'default/default'

Qt::Application.new(ARGV) do
window = Default.new
window.show
exec
end

More details, to be continued...

Saturday, April 11, 2009

Qt newbie seminar in Tokyo

By the way, I'm a newbie for Qt.



So yesterday, I attended "Qt newbie seminar" by SRA Inc., in Ikebukuro, Tokyo. I learned basis of Qt Widget structure, usage of Qt Designer, and usage of Qt Linguist. Of course, kind of these lectures are able to read on various places, like on the web. But, hearing actually from lecturer is easier to understand.

Wednesday, April 8, 2009

Improvement for cmake options

When I was writing my last post, I realized that the cmake options should be improved more. So I read cmake files, I found 3 improvements.

1. MKSPECS

Because I use non-default Qt, I should be set cmake options not only QT_QMAKE_EXECUTABLE but also QT_MKSPECS_DIR that indicates root path of all mkspecs.

2. QTWEBKIT

QtWebkit libraries can be built. So ENABLE_QTWEBKIT_SMOKE and ENABLE_QTWEBKIT_RUBY are able to be on.

3. QTSCRIPT

QtScript libraries can be built. So ENABLE_QTSCRIPT_SMOKE and ENABLE_QTSCRIPT are able to be on.

Full cmake instructions with these improvements are below:
>cmake \
> -DCMAKE_INSTALL_PREFIX=/opt/ruby-1.9.1 \
> -DRUBY_EXECUTABLE=/opt/ruby-1.9.1/bin/ruby \
> -DRUBY_LIBRARY=/opt/ruby-1.9.1/lib/libruby.so.1.9.1 \
> -DRUBY_INCLUDE_PATH=/opt/ruby-1.9.1/include/ruby-1.9.1 \
> -DQT_QMAKE_EXECUTABLE=/opt/qtsdk-2009.01/qt/bin/qmake \
> -DQT_MKSPECS_DIR=/opt/qtsdk-2009.01/qt/mkspecs \
> -Wno-dev \
> -DENABLE_SMOKE=on \
> -DENABLE_QTRUBY=on \
> -DENABLE_QTWEBKIT_SMOKE=on \
> -DENABLE_QTSCRIPT_SMOKE=on \
> -DENABLE_QTUITOOLS_SMOKE=on \
> -DENABLE_QTTEST_SMOKE=off \
> -DENABLE_PHONON_SMOKE=off \
> -DENABLE_QSCI_SMOKE=off \
> -DENABLE_QWT_SMOKE=off \
> -DENABLE_KDE_SMOKE=off \
> -DENABLE_KDEVPLATFORM_SMOKE=off \
> -DENABLE_KHTML_SMOKE=off \
> -DENABLE_KTEXTEDITOR_SMOKE=off \
> -DENABLE_SOLID_SMOKE=off \
> -DENABLE_PLASMA_SMOKE=off \
> -DENABLE_QTWEBKIT_RUBY=on \
> -DENABLE_QTUITOOLS_RUBY=on \
> -DENABLE_QTSCRIPT=on \
> -DENABLE_QTTEST=off \
> -DENABLE_PHONON_RUBY=off \
> -DENABLE_QSCINTILLA_RUBY=off \
> -DENABLE_QWT_RUBY=off \
> -DENABLE_SOPRANO_RUBY=off \
> -DENABLE_KDEVPLATFORM_RUBY=off \
> -DENABLE_KORUNDUM_RUBY=off \
> -DENABLE_KHTML_RUBY=off \
> -DENABLE_KTEXTEDITOR_RUBY=off \
> -DENABLE_SOLID_RUBY=off \
> -DENABLE_KROSSRUBY=off \
> -DENABLE_PLASMA_RUBY=off \
> -DENABLE_QIMAGEBLITZ_SMOKE=off

Monday, April 6, 2009

Another usage of UI File from QtRuby #2

(continued from phosphorescence: Another usage of UI File from QtRuby #1)

Previous messages indicate missing ruby module 'qtuitools'. Let's make sure of load path.
>/opt/ruby-1.9.1/bin/ruby -e 'puts $:'
/opt/ruby-1.9.1/lib/ruby/site_ruby/1.9.1
/opt/ruby-1.9.1/lib/ruby/site_ruby/1.9.1/x86_64-linux
/opt/ruby-1.9.1/lib/ruby/site_ruby
/opt/ruby-1.9.1/lib/ruby/vendor_ruby/1.9.1
/opt/ruby-1.9.1/lib/ruby/vendor_ruby/1.9.1/x86_64-linux
/opt/ruby-1.9.1/lib/ruby/vendor_ruby
/opt/ruby-1.9.1/lib/ruby/1.9.1
/opt/ruby-1.9.1/lib/ruby/1.9.1/x86_64-linux
.

In these directories, ruby module 'qtuitools' is not found. How should I do? Seek back in the post phosphorescence: qt4-qtruby 2.0.3 is released, two cmake options are found:
>... \
> -DENABLE_QTUITOOLS_SMOKE=off \
> ... \
> -DENABLE_QTUITOOLS_RUBY=off \
> ...

So these options switch turned on.
>... \
> -DENABLE_QTUITOOLS_SMOKE=on \
> ... \
> -DENABLE_QTUITOOLS_RUBY=on \
> ...

After CMMI instructions, retry "ui_loader_example.rb":
>/opt/ruby-1.9.1/bin/ruby ui_loader_example.rb

Then succeed this time and same GUI is shown.

Saturday, April 4, 2009

openSUSE benkyoukai 2009/04

Today, in Tokyo, openSUSE benkyoukai (in Kanji characters: 勉強会) was held. What's benkyoukai? It's Japanese language means "Studying meeting" or "Studying party", and its origin is.., see more at Hiro Yoshioka's blog entries.

Then, openSUSE benkyoukai is keeping hosted by Nihon openSUSE Users Group (written in Japanese), and from this time, streaming test by UStream.tv is started. I watched benkyoukai by this UStream.tv because I cannot go to the venue at this time. There were 3 sessions:
  1. How to translate to Japanese for openSUSE Japanese official wiki
  2. Knoppix and mathmatics
  3. What is Prolog
These are so interest and enthusiastic. Next time, I would like to attend.

Friday, April 3, 2009

Another usage of UI File from QtRuby #1

In recent post, I mentioned of Qt Designer's XML file (aka UI file). This post explains how to use command rbuic4. But, another way to use UI file exists. It's QUiLoader. In QtRuby examples of KDE TechBase, samples are written with below caution:

This section needs improvements: Please help us to
cleanup confusing sections and
fix sections which contain a todo

Remove example that does not work

Really? So let's try with defaults.ui, that is corresponding UI file in recent post.
require 'Qt4'
require 'qtuitools'

UI_FILES_PATH = "/opt/qtsdk-2009.01/qt/examples/widgets/stylesheet/layouts"

Qt::Application.new(ARGV) do
file = Qt::File.new("#{UI_FILES_PATH}/default.ui")
file.open(Qt::File::ReadOnly)
window = Qt::UiLoader.new.load(file, nil)
file.close
window.show
exec
end

This code block is saved as file named "ui_loader_example.rb". Then execute this.
>/opt/ruby-1.9.1/bin/ruby ui_loader_example.rb
ui_loader_example.rb:2:in `require': no such file to load -- qtuitools (LoadError)
from ui_loader_example.rb:2:in `<main>'

It fails, but unexpected way. to be continued...

Wednesday, April 1, 2009

QtRuby 4.0.1

QtRuby 4.0.1 is released, this one is able to convert from Ruby class/module to Qt class. Sample code is below:
#include <date/RDateTime>

int main(int argc, char *argv[])
{
    RDateTime current = RDateTime::now;
    fprintf(stdout, current.toS());
    return 0;
}

First, Ruby's require 'date' is corresponding to #include <date/. Second, Ruby class/module DateTime is corresponding to RDateTime>, which is converted with prefix 'R'. Last, Ruby's snake_case method name(ex. to_s) is converted to camelCase method name(ex. toS()).