Class: QObject

Inherits:
Gtk::HBox
  • Object
show all
Includes:
ManqodCommon
Defined in:
lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint([email protected])

Constant Summary

Constants included from ManqodCommon

ManqodCommon::CRITICAL, ManqodCommon::DEBUG, ManqodCommon::ERROR, ManqodCommon::INFO, ManqodCommon::NORMAL, ManqodCommon::WARNING

Constants included from Eprint

Eprint::DOMAIN, Eprint::LEVEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ManqodCommon

#add_where, #admin, #admin_cache, #admin_qrow, #admin_rows, #backtrace_to_debug, #cache, #changed_ids_of_base, #client, #client_fields, #client_image_of_id, #client_qrow, #client_query, #client_rows, #eeval, #escape_string, #getBinding, #guess_base, #guess_table, #image_of_id, #lzero, #manqod_db, #measure, #myexec, #nick, #nick_id, #number_format, #qrow, #query, #reconnect_manqod_db, #rows, #run_events, #send_message, #sendmail, #set_manqod_db_uri, #set_nick

Methods included from Eprint

#ecode, #edebug, #eerror, #einfo, #enormal, #eprint, #ewarn, #gtk_set_edebug, #set_edebug, #tell_exception

Constructor Details

#initialize(q, link_or_fields = nil) ⇒ QObject

q - the QueryBuilder link_or_fields - link: an object from the RelationBuilder - fields: the fields from the table ‘qobjects`



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 12

def initialize(q,link_or_fields=nil)
  whsv=false
  begin
    whsv=Gtk::SourceView::BUILD_VERSION
    rescue => err
    einfo("no Gtk::SourceView, falling back")
  end
  super() #("initializing")
  pack_start(Gtk::ScrolledWindow.new.set_policy(Gtk::PolicyType::AUTOMATIC,Gtk::PolicyType::NEVER).set_shadow_type(Gtk::ShadowType::NONE).add(@widget=whsv ? Gtk::SourceView.new : Gtk::TextView.new))
  @rel_reverted=false
  @q=q
  @custom="false"
  case link_or_fields.class.name
    when "Hash" then
      #load
      @my_id=link_or_fields["id"]
      @obj_type=link_or_fields["obj_type"]
      @obj_id=link_or_fields["obj_id"]
      @field=link_or_fields["field"]
      @custom=link_or_fields["custom"]
      @rel_reverted=!@field.nil? && @field.include?("reverted")
      set_link_from_data
    when "RField" then
      @link=link_or_fields
      @my_id=nil
      @obj_type="f"
      @field=link.field_name
      @obj_id=link.table.table_id
      store
    when "RTable" then
      @link=link_or_fields
      @my_id=nil
      @obj_type="b"
      @obj_id=link.table_id
      store
    when "Relation" then
      @link=link_or_fields
      @my_id=nil
      @obj_type="r"
      @obj_id=link.relation_id.to_i
      store
    else edebug("unknown link: #{@link}(#{link_or_fields.class.name})","relation-builder",WARNING)
  end

  if whsv
    @widget.buffer.set_language(Gtk::SourceLanguageManager.new.get_language("sql"))
    @widget.set_auto_indent(true).
    set_highlight_current_line(true).
    set_indent_on_tab(true).
    set_indent_on_tab(true).
    set_indent_width(4)
  end
  @widget.set_editable(is_custom?)
  @widget.modify_base(Gtk::StateType::NORMAL,Gdk::Color.parse("yellow")) if is_custom?
  @widget.signal_connect("focus-out-event"){|me,ev|
    @field=text
    store
    @q.update(@q)
    false
    }
  @q.qobjects.push(self)
  case @obj_type
    when "b" then @q.from.pack_start(self,false,false)
    when "f" then
      @q.select.pack_start(self,false,false)
      pack_start(rmbutton=Gtk::Button.new.set_image(Gtk::Image.new(Gtk::Stock::CANCEL,Gtk::IconSize::MENU)).set_relief(Gtk::ReliefStyle::NONE),false)
      rmbutton.signal_connect("clicked"){|me| self.delete}
    when "r" then
      @q.from.pack_start(self,false,false)
      pack_start(rotbutton=Gtk::ToggleButton.new.set_image(Gtk::Image.new(Gtk::Stock::REFRESH,Gtk::IconSize::MENU)).set_relief(Gtk::ReliefStyle::NONE),false)
      pack_start(rmbutton=Gtk::Button.new,false)
      rmbutton.set_image(Gtk::Image.new(Gtk::Stock::CANCEL,Gtk::IconSize::MENU)).set_relief(Gtk::ReliefStyle::NONE).signal_connect("clicked"){|me| self.delete}
      rotbutton.set_active(@rel_reverted)
      rotbutton.signal_connect("toggled"){|me| 
        @rel_reverted=me.active?
        store
        @q.update(@q)
      }
  end
  show_all
  case @link.class.name
    when "RField","Relation" then @link.set_active(true)
  end
end

Instance Attribute Details

Returns the value of attribute link.



96
97
98
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 96

def link
  @link
end

#obj_idObject (readonly)

Returns the value of attribute obj_id.



96
97
98
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 96

def obj_id
  @obj_id
end

#obj_typeObject (readonly)

Returns the value of attribute obj_type.



96
97
98
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 96

def obj_type
  @obj_type
end

Instance Method Details



264
265
266
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 264

def center_on_link
  link.center_on_me unless link.nil?
end

#deleteObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 221

def delete
  query("delete from `qobjects` where id='#{@my_id}'") if @my_id
  @q.qobjects.delete(self)
  self.destroy
  if is_custom?
    #so all qobjects get updated
    @q.update(@q)
  else
    #update the link, that will also update all qobjects
    case @link.class.name
      when "RField" then @link.set_active(false)
      when "Relation" then @link.set_active(false)
    end
  end
end

#find_base_dir(rel, been_there = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 103

def find_base_dir(rel,been_there=nil)
  if been_there.nil?
    been_there=Array.new
  end
  been_there.push(rel.relation_id)
  ret=nil
  ret=rel.rfield1 if rel.rfield1.table.table_id.to_i == @q.base_object.obj_id.to_i
  ret=rel.rfield2 if rel.rfield2.table.table_id.to_i == @q.base_object.obj_id.to_i
  unless ret
    @q.rb.relations.each_pair{|rel_id,r|
      unless been_there.include?(r.relation_id)
        if r.rfield1.table.table_id == rel.rfield1.table.table_id or r.rfield2.table.table_id == rel.rfield1.table.table_id 
          if other_dir=find_base_dir(r,been_there)
            ret=rel.rfield1
          end
        end
        if r.rfield1.table.table_id == rel.rfield2.table.table_id or r.rfield2.table.table_id == rel.rfield2.table.table_id 
          if other_dir=find_base_dir(r,been_there)
            ret=rel.rfield2
          end
        end
        break if ret
      end
    }
  end
  ret
end

#gen_textObject



131
132
133
134
135
136
137
138
139
140
141
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
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 131

def gen_text
  ret=""
  if is_custom?
    ret=@field || ""
  else
    begin
      case @obj_type
        when "f" then ret="`#{@link.table.table}`.`#{@link.field_name}`#{"," unless last_field?}"
        when "r" then
          dir=find_base_dir(@link)
          dir=@link.other_field(dir) if @rel_reverted #reverted path
          edebug("field to base: #{dir}","relation-builder",INFO)
          if dir
            other_field=@link.other_field(dir)
            edebug("field to base other_field: #{other_field}","relation-builder",INFO)
            if @link.rel_name == ""
              ret="left join `#{other_field.table.table}` on `#{other_field.table.table}`.`#{other_field.field_name}` = `#{dir.table.table}`.`#{dir.field_name}`"
            else
              ret="left join `#{other_field.table.table}` as #{@link.rel_name} on `#{@link.rel_name}`.`#{other_field.field_name}` = `#{dir.table.table}`.`#{dir.field_name}`"
            end
            unless @link.rel_custom == ""
              ret="#{ret} and #{@link.rel_custom}"
            end
          else
            ret="can't reach base"
          end
        when "b" then
          ret="`#{@link.table}`"
        else ret="unknown object type"
      end
    rescue =>err
      edebug("#{err}, #{backtrace_to_debug(err)}\n","relation-builder","warning")
      ret="unavailable object"
    end
  end
  ret
end

#inspectObject



256
257
258
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 256

def inspect
  "QObject of #{@link}(#{@obj_type} id:#{@my_id})"
end

#is_custom?Boolean

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 98

def is_custom?
  return false if @custom.nil?
  @custom == "true"
end

#last_field?Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
245
246
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 237

def last_field?
  last_field=nil
  @q.qobjects.reverse_each{|qobject|
    if qobject.obj_type == "f"
      last_field = qobject
      break
    end
  }
  last_field == self
end


187
188
189
190
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 187

def set_link(new_link)
  @link=new_link
  self
end


169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 169

def set_link_from_data
  #if any data changed this should be called. it'll try to link to another object
  unless is_custom?
    @link=case @obj_type
      when "b" then @q.rb.table_by_id(@obj_id)
      when "f" then if table=@q.rb.table_by_id(@obj_id) then table.fields[@field]; end
      when "r" then @q.rb.relations[@obj_id]
      else 
        edebug("unknown type: #{@obj_type}","relation-builder",WARNING)
        nil
    end
  end
  update
end

#set_obj_id(new_obj_id) ⇒ Object



191
192
193
194
195
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 191

def set_obj_id(new_obj_id)
  @obj_id=new_obj_id
  set_link_from_data
  store
end

#set_text(new_text) ⇒ Object



248
249
250
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 248

def set_text(new_text)
  @widget.buffer.set_text(new_text)
end

#storeObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 197

def store
  edebug("storing(#{@my_id})","relation-builder")
  if is_custom?
    isql="`obj_id`='#{@obj_id}', `obj_type`='#{@obj_type}', `field`='#{escape_string(@field)}', `custom`='#{@custom}', `query_id`='#{@q.query_id}'"
  else
    isql=nil
    case @link.class.name
      when "RField" then isql="`obj_id`='#{@obj_id}', `field`='#{@link.field_name}', `obj_type`='#{@obj_type}', `query_id`='#{@q.query_id}'"
      when "RTable" then isql="`obj_id`='#{@obj_id}', `obj_type`='#{@obj_type}', `query_id`='#{@q.query_id}'"
      when "Relation" then isql="`obj_id`='#{@obj_id}', `obj_type`='#{@obj_type}', `query_id`='#{@q.query_id}', `field`='#{"reverted" if @rel_reverted}'"
      else edebug("don't store: #{link.class.name}","relation-builder",INFO)
    end
  end
  if isql
    if @my_id
      sql="update `qobjects` set #{isql} where `id`='#{@my_id}'"
      else
      sql="insert into `qobjects` set #{isql}"
    end
    query(sql)
    @my_id=qrow("select id from `qobjects` order by id desc limit 1")["id"] unless @my_id
  end
end

#textObject



252
253
254
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 252

def text
  @widget.buffer.text
end

#to_sObject



260
261
262
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 260

def to_s
  inspect
end

#updateObject



184
185
186
# File 'lib/FormHolder/Form/InputHolder/QBuilder/QObject.rb', line 184

def update
  @widget.buffer.set_text(gen_text)
end