Class: QObject

Inherits:
Object
  • Object
show all
Defined in:
lib/QWebChannel/QObject.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data, webChannel) ⇒ QObject

def addMethod(methodData)



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/QWebChannel/QObject.rb', line 178

def initialize(name, data, webChannel)
    @signalNameObjectHash={} #信号名字与信号对象的映射。
    @__id__=name
    @webChannel=webChannel
    webChannel.objects[name]=self
    
    puts "__id__: #{@__id__}" #debug.
    
    
    @__objectSignals__ = {}
    
    @__propertyCache__ = {}
    
    @object=self
    
    data['methods'].each do |method|
        puts "current method: #{method}" #debug
        addMethod(method)
    end
    
    data['properties'].each do |property|
        bindGetterSetter(property)
    end
    
    data['signals'].each do |signal|
        addSignal(signal, false)
    end
    
    puts "enums: #{data['enums']}" #Debug.
    
    if data['enums']
        data['enums'].each do |name|
            @object[name]=data['enums'][name]
        end #data['enums'].each do |name|
    end #if data['enums']
end

Instance Attribute Details

#__id__Object

Returns the value of attribute __id__.



3
4
5
# File 'lib/QWebChannel/QObject.rb', line 3

def __id__
  @__id__
end

#__objectSignals__Object

Returns the value of attribute __objectSignals__.



4
5
6
# File 'lib/QWebChannel/QObject.rb', line 4

def __objectSignals__
  @__objectSignals__
end

#__propertyCache__Object

Returns the value of attribute __propertyCache__.



2
3
4
# File 'lib/QWebChannel/QObject.rb', line 2

def __propertyCache__
  @__propertyCache__
end

#webChannelObject

Returns the value of attribute webChannel.



5
6
7
# File 'lib/QWebChannel/QObject.rb', line 5

def webChannel
  @webChannel
end

Instance Method Details

#[](signalName) ⇒ Object

def []=(signalName, signalObject)



64
65
66
# File 'lib/QWebChannel/QObject.rb', line 64

def [](signalName)
    @signalNameObjectHash[signalName]
end

#[]=(signalName, signalObject) ⇒ Object

def unwrapProperties()



60
61
62
# File 'lib/QWebChannel/QObject.rb', line 60

def []=(signalName, signalObject)
    @signalNameObjectHash[signalName]=signalObject
end

#addMethod(methodData) ⇒ Object

def addSignal(signalData, isPropertyNotifySignal)



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/QWebChannel/QObject.rb', line 142

def addMethod(methodData)
    methodName=methodData[0]
    methodIdx=methodData[1]
    
    @object.define_singleton_method(methodName) do |*arguments|
        args=[]
        callback=nil
        
        arguments.each do |argument|
            puts "argument class: #{argument.class}" #debug.
            if (argument.is_a?(Proc))
                callback=argument
            elsif ( (argument.is_a?(QObject))  && (@webChannel.objects[argument.__id__] !=nil ) )
                args << ({"id" => argument.__id__})
            else
                args << argument
            end
        end #arguments.each do |argument|
        
        puts "__id__: #{@object.__id__}" #debug.

        
        @webChannel.exec({ "type" => QWebChannelMessageTypes::INVOKEMETHOD,  "object" => @object.__id__, "method" => methodIdx, "args" => args }) do |response|
            if (response!=nil)
                result = @object.unwrapQObject(response)
                
                if (callback)
                    callback.call(result)
                end
            end #if (response!=nil)
        end #@webChannel.exec() do |response|
        
        
    end #@object.define_singleton_method(methodName) do |*arguments|
end

#addSignal(signalData, isPropertyNotifySignal) ⇒ Object

def signalEmitted(signalName, signalArgs)



135
136
137
138
139
140
# File 'lib/QWebChannel/QObject.rb', line 135

def addSignal(signalData, isPropertyNotifySignal)
    signalName=signalData[0]
    signalIndex=signalData[1]
    
    @object[signalName]=QSignal.new(signalName, signalIndex, @object, isPropertyNotifySignal) #添加信号。
end

#bindGetterSetter(propertyInfo) ⇒ Object

def [](signalName)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/QWebChannel/QObject.rb', line 68

def bindGetterSetter(propertyInfo)
    propertyIndex=propertyInfo[0]
    propertyName=propertyInfo[1]
    notifySignalData=propertyInfo[2]
    
    @object.__propertyCache__[propertyIndex]=propertyInfo[3]
    
    if (notifySignalData)
        if (notifySignalData[0] == 1)
            notifySignalData[0]=propertyName+"Changed"
        end
        addSignal(notifySignalData, true)
    end #if (notifySignalData)
    
    defineProperty(propertyName) #定义属性。对应于Object.defineProperty.
end

#defineProperty(propertyName) ⇒ Object

def bindGetterSetter(propertyInfo)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/QWebChannel/QObject.rb', line 85

def defineProperty(propertyName)
#         define_property_by_prototype(propertyName)
    
    
            #getter
#         self.class_eval( " def #{attr_name}; @#{attr_name};   end  " )
    self.instance_eval %Q{
        def #{propertyName}
           @object.__propertyCache__[propertyIndex]
        end
        }

    #setter
    self.instance_eval %Q{
        def #{propertyName}=(val)

            @object.__propertyCache__[propertyIndex]=val
            
            valueToSend=val
            
            if (valueToSend.is_a?(QObject) && @webChannel.objects[valueToSend.__id__] != nil)
                valueToSend={"id" => valueToSend.__id__}
                
                @webChannel.exec( {"type" => QWebChannelMessageTypes::SETPROPERTY, "object" => @object.__id__ , "property" => propertyIndex, "value" => valueToSend } )
            end
            
            

        end


        }

end

#invokeSignalCallbacks(signalName, signalArgs) ⇒ Object

def defineProperty(propertyName)



120
121
122
123
124
125
126
127
128
# File 'lib/QWebChannel/QObject.rb', line 120

def invokeSignalCallbacks(signalName, signalArgs)
    connections=@object.__objectSignals__[signalName]
    
    if (connections) #存在连接。
        connections.each do |callback| #一个个连接地处理。
            callback.call(*signalArgs) #抹平数组。
        end #connections.each do |callback| #一个个连接地处理。
    end #if (connections)
end

#signalEmitted(signalName, signalArgs) ⇒ Object

def invokeSignalCallbacks(signalName, signalArgs)



130
131
132
133
# File 'lib/QWebChannel/QObject.rb', line 130

def signalEmitted(signalName, signalArgs)
    puts "signalArgs: #{signalArgs}" #debug.
    invokeSignalCallbacks(signalName, self.unwrapQObject(signalArgs))
end

#unwrapPropertiesObject

def unwrapQObject(response)



54
55
56
57
58
# File 'lib/QWebChannel/QObject.rb', line 54

def unwrapProperties()
    @object.__propertyCache__.each do |propertyIdx, propertyObject|
        @object.__propertyCache__[propertyIdx]=@object.unwrapQObject(@object.__propertyCache__[propertyIdx])
    end
end

#unwrapQObject(response) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/QWebChannel/QObject.rb', line 7

def unwrapQObject(response)
    puts "response class: #{response.class}" #debug
    if (response.is_a?(Array))
        ret = []
        
        response.each do |responseI|
            ret << @object.unwrapQObject(responseI)
        end
        
        return ret
    end #if (response.is_a?(Array))
    
    
    if (!response || response.is_a?(Fixnum) || response.is_a?(TrueClass) || !response["__QObject*__"] || response.id == nil)
        return response
    end
    
    objectId=response.id
    
    if (@webChannel.objects[objectId])
        return @webChannel.objects[objectId]
    end


    qObject=QObject.new(objectId, response.data, @webChannel)
    
    qObject.destroyed.connect() do 
        if (@webChannel.objects[objectId]==qObject)
            @webChannel.objects.delete(objectId)
            
            propertyNames=[]
            
            qObject.each do |propertyName|
                propertyNames << propertyName
            end
            
            propertyNames.each do |propertyName|
                qObject.delete(propertyName)
            end
        end #if (@webChannel.objects[objectId]==qObject)
    end #qObject.destroyed.connect() do 
    
    qObject.unwrapProperties()
    
    return qObject
end