Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

alpha qt support

  • Loading branch information...
commit 8cc6a87f5326afd0758be7c6b1d5c6b6ba31b2f5 1 parent 3f615c5
authored November 11, 2009
2  Rakefile
@@ -9,7 +9,7 @@ require 'rake/testtask'
9 9
 
10 10
 spec = Gem::Specification.new do |s|
11 11
   s.name = 'freightrain'
12  
-  s.version = '0.3.0'
  12
+  s.version = '0.3.1'
13 13
   s.has_rdoc = false
14 14
   s.summary = 'ruby desktop development made easy'
15 15
   s.description = s.summary
33  lib/freightrain.rb
... ...
@@ -1,6 +1,18 @@
1 1
 
2 2
 require 'rubygems'
3  
-require 'libglade2'
  3
+
  4
+begin
  5
+  require 'libglade2'
  6
+rescue
  7
+  p "libglade2 is not available"
  8
+end
  9
+
  10
+begin
  11
+  require 'Qt4'
  12
+  require 'qtuitools'
  13
+rescue
  14
+  p "qt is not available"
  15
+end
4 16
 require 'needle'
5 17
 require 'require_all'
6 18
 require 'tmpdir'
@@ -10,12 +22,25 @@
10 22
 module Freightrain
11 23
 
12 24
   attr_accessor :app_path
  25
+  attr_accessor :toolkit
13 26
 
14 27
   def self.start(viewmodel)
15 28
     auto_require!
16  
-    configure_container!
17  
-    Freightrain[(viewmodel.to_s + "_view_model").to_sym].show
18  
-    Gtk.main
  29
+    configure_container!    
  30
+    start_toolkit_mainloop(viewmodel)
  31
+  end
  32
+
  33
+  private
  34
+  def start_toolkit_mainloop(viewmodel)    
  35
+    if !@toolkit || @toolkit.to_sym == :gtk
  36
+      Freightrain[(viewmodel.to_s + "_view_model").to_sym].show
  37
+      Gtk.main
  38
+    end
  39
+    if @toolkit.to_sym == :qt
  40
+      app = Qt::Application.new([])
  41
+      Freightrain[(viewmodel.to_s + "_view_model").to_sym].show
  42
+      app.exec()
  43
+    end
19 44
   end
20 45
 
21 46
 end
17  lib/freightrain/auto_requirer.rb
@@ -3,10 +3,19 @@ module Freightrain
3 3
 
4 4
   def self.auto_require!
5 5
 
6  
-    require_all File.join(self.app_path, "views")
7  
-    require_all File.join(self.app_path, "viewmodels")
8  
-    require_all File.join(self.app_path, "services")
9  
-    require_all File.join(self.app_path, "domain")
  6
+    views_folder      = File.join(self.app_path, "views")
  7
+    viewmodels_folder = File.join(self.app_path, "viewmodels")
  8
+    services_folder   = File.join(self.app_path, "services")
  9
+    domain_folder     = File.join(self.app_path, "domain")
  10
+
  11
+    require_all File.join(views_folder)
  12
+    require_all File.join(viewmodels_folder)
  13
+    if File.directory?(services_folder) && !Dir.entries(services_folder).empty?
  14
+      require_all File.join(services_folder)
  15
+    end
  16
+    if File.directory?(domain_folder) && !Dir.entries(domain_folder).empty?
  17
+      require_all File.join(domain_folder)
  18
+    end
10 19
     
11 20
   end
12 21
   
10  lib/freightrain/freight_view.rb
@@ -2,7 +2,7 @@
2 2
 module Freightrain
3 3
 
4 4
   class FreightView
5  
-    include GtkBuilderHelper
  5
+    include InterfaceBuilder
6 6
     include DialogHelper
7 7
     include BindingHost
8 8
     extend SignalHost
@@ -13,16 +13,16 @@ def self.container_options
13 13
     end
14 14
 
15 15
     def toplevel
16  
-      return @builder.objects.first.toplevel
  16
+      return @builder.toplevel
17 17
     end
18 18
 
19 19
     def widgets
20  
-      return @builder.objects.select { |widget| widget.kind_of? Gtk::Widget }
  20
+      return @builder.objects
21 21
     end   
22 22
     
23  
-    def initialize(builder = Gtk::Builder.new)
  23
+    def initialize(builder = GtkInterfaceBuilder.new)
24 24
       @builder = builder
25  
-      load_from_file(File.join(Freightrain.app_path,"views","#{self.class.name}.glade"),@builder)
  25
+      load_from_file(File.join(Freightrain.app_path,"views","#{self.class.name}"),@builder)
26 26
       create_signals
27 27
     end
28 28
 
23  lib/freightrain/gtk_builder_helper.rb
... ...
@@ -1,23 +0,0 @@
1  
-
2  
-module Freightrain
3  
-  module GtkBuilderHelper
4  
-
5  
-    def load_from_file(file_path,builder = GtkInterfaceBuilder.new)
6  
-      if File.exists?(file_path)
7  
-        builder.add_from_file(file_path)
8  
-        builder.objects.each do |widget|
9  
-          if widget.respond_to? :name
10  
-            method_name = widget.name
11  
-            instance_eval "def #{method_name}; return @builder.get_object('#{method_name}');end"
12  
-          end
13  
-        end
14  
-        builder.connect_signals do |handler|          
15  
-          if self.respond_to? handler
16  
-            method(handler)
17  
-          end
18  
-        end
19  
-      end
20  
-    end
21  
-
22  
-  end
23  
-end
22  lib/freightrain/interface_builder.rb
... ...
@@ -0,0 +1,22 @@
  1
+
  2
+module Freightrain
  3
+  module InterfaceBuilder
  4
+
  5
+    def load_from_file(file_path,builder = GtkInterfaceBuilder.new)
  6
+      definition_file_path = file_path + "." + builder.extension
  7
+      if File.exists?(definition_file_path)
  8
+        builder.add_from_file(definition_file_path)
  9
+        builder.objects.each do |widget|
  10
+          method_name = widget.name
  11
+          instance_eval "def #{method_name}; return @builder.get_object('#{method_name}');end;"
  12
+        end
  13
+        builder.connect_signals do |handler|          
  14
+          if self.respond_to? handler
  15
+            method(handler)
  16
+          end
  17
+        end
  18
+      end
  19
+    end
  20
+
  21
+  end
  22
+end
12  lib/freightrain/toolkit_extensions/gtk/gtk_interface_builder.rb
@@ -6,13 +6,23 @@ class GtkInterfaceBuilder
6 6
     def initialize
7 7
       @builder = Gtk::Builder.new
8 8
     end
  9
+
  10
+    def extension
  11
+      return "glade"
  12
+    end
9 13
     
10 14
     def add_from_file(file_path)
11 15
       @builder.add_from_file(file_path)
12 16
     end
13 17
 
  18
+    def toplevel
  19
+      return objects.first.toplevel
  20
+    end
  21
+
14 22
     def objects
15  
-      return @builder.objects
  23
+      return @builder.objects.select do |widget|
  24
+        widget.respond_to?(:name) && widget.kind_of?(Gtk::Widget)
  25
+      end
16 26
     end
17 27
 
18 28
     def get_object(name)
0  ...ghtrain/binding/rubygnome_extensions/gtk_image.rb → ...train/toolkit_extensions/gtk/widgets/gtk_image.rb
File renamed without changes
0  ...htrain/binding/rubygnome_extensions/gtk_layout.rb → ...rain/toolkit_extensions/gtk/widgets/gtk_layout.rb
File renamed without changes
0  ...rain/binding/rubygnome_extensions/gtk_treeview.rb → ...in/toolkit_extensions/gtk/widgets/gtk_treeview.rb
File renamed without changes
0  ...inding/rubygnome_extensions/gtk_treeviewcolumn.rb → ...lkit_extensions/gtk/widgets/gtk_treeviewcolumn.rb
File renamed without changes
0  ...htrain/binding/rubygnome_extensions/gtk_widget.rb → ...rain/toolkit_extensions/gtk/widgets/gtk_widget.rb
File renamed without changes
12  lib/freightrain/toolkit_extensions/qt/qt_base.rb
... ...
@@ -0,0 +1,12 @@
  1
+
  2
+module Qt
  3
+
  4
+  class Base
  5
+
  6
+    def name
  7
+      return objectName if self.respond_to? :objectName
  8
+    end
  9
+
  10
+  end
  11
+
  12
+end
16  lib/freightrain/toolkit_extensions/qt/qt_interface_builder.rb
... ...
@@ -1,4 +1,7 @@
1 1
 
  2
+
  3
+
  4
+
2 5
 module Freightrain
3 6
 
4 7
   class QtInterfaceBuilder
@@ -6,20 +9,29 @@ class QtInterfaceBuilder
6 9
     def initialize
7 10
       @builder = Qt::UiLoader.new
8 11
     end
  12
+
  13
+    def extension
  14
+      return "ui"
  15
+    end
  16
+
  17
+    def toplevel
  18
+      return @toplevel
  19
+    end
9 20
     
10 21
     def add_from_file(file_path)
11 22
       file = Qt::File.new(file_path)
12 23
       file.open(Qt::File::ReadOnly)
13 24
       @toplevel = @builder.load(file)
  25
+      p @toplevel
14 26
       file.close
15 27
     end
16 28
 
17 29
     def objects
18  
-      return get_all_objects(@toplevel)      
  30
+      return get_all_objects(@toplevel).select { |widget| widget.objectName && !widget.objectName == ""}
19 31
     end
20 32
 
21 33
     def get_object(name)
22  
-      return get_all_objects(@toplevel).select { |widget| widget.objectName == name}.first
  34
+      return objects.select { |widget| widget.objectName == name}.first
23 35
     end
24 36
 
25 37
     def connect_signals
34  spec/freightrain/auto_requirer_spec.rb
@@ -8,6 +8,8 @@
8 8
     before :each do
9 9
       Freightrain.app_path = "testpath"
10 10
       Freightrain.stubs(:require_all)
  11
+      File.stubs(:directory?).returns(false)
  12
+      Dir.stubs(:entries).returns(false)
11 13
     end
12 14
      
13 15
     it "should call require_all for views folder" do 
@@ -20,16 +22,44 @@
20 22
       Freightrain.auto_require!
21 23
     end
22 24
 
23  
-    it "should call require_all for services folder" do
  25
+    it "should call require_all for services folder if exists and contains files" do
  26
+      File.stubs(:directory?).with("testpath/services").returns(true)
  27
+      Dir.stubs(:entries).with("testpath/services").returns([1])
24 28
       Freightrain.expects(:require_all).with("testpath/services")
25 29
       Freightrain.auto_require!
26 30
     end
27 31
 
28  
-    it "should call require_all for domain folder" do 
  32
+    it "should not call require_all for services folder if does not exist" do
  33
+      Freightrain.expects(:require_all).with("testpath/services").never
  34
+      Freightrain.auto_require!
  35
+    end
  36
+
  37
+    it "should not call require_all for services folder if folder is empty" do
  38
+      File.stubs(:directory?).with("testpath/services").returns(true)
  39
+      Dir.stubs(:entries).with("testpath/services").returns([])
  40
+      Freightrain.expects(:require_all).with("testpath/services").never
  41
+      Freightrain.auto_require!
  42
+    end
  43
+
  44
+    it "should call require_all for domain folder" do
  45
+      File.stubs(:directory?).with("testpath/domain").returns(true)
  46
+      Dir.stubs(:entries).with("testpath/domain").returns([1])
29 47
       Freightrain.expects(:require_all).with("testpath/domain")
30 48
       Freightrain.auto_require!
31 49
     end
32 50
 
  51
+    it "should not call require_all for domain folder if does not exist" do
  52
+      Freightrain.expects(:require_all).with("testpath/domain").never
  53
+      Freightrain.auto_require!
  54
+    end
  55
+
  56
+    it "should not call require_all for domain folder if folder is empty" do
  57
+      File.stubs(:directory?).with("testpath/domain").returns(true)
  58
+      Dir.stubs(:entries).with("testpath/domain").returns([])
  59
+      Freightrain.expects(:require_all).with("testpath/domain").never
  60
+      Freightrain.auto_require!
  61
+    end
  62
+
33 63
   end
34 64
 
35 65
 end
19  spec/freightrain/freight_view_spec.rb
@@ -22,7 +22,7 @@
22 22
   end
23 23
 
24 24
   it "should always include GtkBuilderHelper" do
25  
-    @class.included_modules.include?(GtkBuilderHelper).should == true
  25
+    @class.included_modules.include?(InterfaceBuilder).should == true
26 26
   end
27 27
 
28 28
   it "should always extend SignalHost" do
@@ -43,7 +43,7 @@
43 43
 
44 44
     it "should always call load_from_file with right params" do
45 45
       @class.stubs(:name).returns("myname")
46  
-      File.stubs(:join).with(anything, "views", "myname.glade").returns(:correct)
  46
+      File.stubs(:join).with(anything, "views", "myname").returns(:correct)
47 47
       @class.send(:define_method, :load_from_file) do |path, builder|
48 48
         @called = true if (path == :correct && builder == :builder)
49 49
       end
@@ -71,9 +71,8 @@
71 71
 
72 72
     it "should always return first object's toplevel" do
73 73
       builder = stub()
74  
-      builder.stubs(:objects).returns(
75  
-        stub(:first => stub(
76  
-          :toplevel => :correct)))
  74
+      builder.stubs(:extension).returns("dmy")
  75
+      builder.stubs(:toplevel).returns(:correct)
77 76
       view = @class.new(builder)
78 77
       view.toplevel.should == :correct
79 78
     end
@@ -82,14 +81,12 @@
82 81
 
83 82
   describe "widgets" do
84 83
 
85  
-    it "should return all objects in builder who derive from Gtk::Widget" do
  84
+    it "should return all objects in builder" do
86 85
       builder = stub()
87  
-      builder.stubs(:objects).returns([
88  
-          stub(:kind_of? => Gtk::Widget),
89  
-          nil,
90  
-          nil])
  86
+      builder.stubs(:extension).returns("dmy")
  87
+      builder.stubs(:objects).returns([:a, :b, :c])
91 88
       view = @class.new(builder)
92  
-      view.widgets.length.should == 1
  89
+      view.widgets.length.should == 3
93 90
     end
94 91
 
95 92
  end
16  spec/freightrain/gtk_builder_helper_spec.rb → spec/freightrain/interface_builder_spec.rb
... ...
@@ -1,7 +1,7 @@
1 1
 
2 2
 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3 3
 
4  
-describe GtkBuilderHelper do
  4
+describe InterfaceBuilder do
5 5
   
6 6
   describe "load from file" do
7 7
 
@@ -10,8 +10,9 @@
10 10
       @builder.stubs(:add_from_file)
11 11
       @builder.stubs(:objects).returns([])
12 12
       @builder.stubs(:connect_signals)
  13
+      @builder.stubs(:extension).returns("dmy")
13 14
       @class = Class.new()
14  
-      @class.instance_eval("include GtkBuilderHelper")
  15
+      @class.instance_eval("include InterfaceBuilder")
15 16
     end
16 17
 
17 18
     it "should not create a view definition if file is not found" do
@@ -39,17 +40,6 @@
39 40
       instance.respond_to?(:my_name_two).should == true
40 41
     end
41 42
 
42  
-    it "should not create the accessor method if widget does not respond to name" do
43  
-      File.stubs(:exists?).returns(true)
44  
-      widget_one = stub()
45  
-      widget_two = stub()
46  
-      @builder.stubs(:objects).returns([widget_one, widget_two])
47  
-      instance = @class.new
48  
-      instance.load_from_file("dummy", @builder)
49  
-      instance.respond_to?(:my_name_one).should == false
50  
-      instance.respond_to?(:my_name_two).should == false
51  
-    end
52  
-    
53 43
     it "should attach correct handler to widget if method is present" do
54 44
       @class.send(:define_method, :on_widget_clicked) { }
55 45
       @builder.stubs(:connect_signals).yields("on_widget_clicked")      

0 notes on commit 8cc6a87

Please sign in to comment.
Something went wrong with that request. Please try again.