Class: RelationBuilder

Inherits:
Gtk::ScrolledWindow
  • Object
show all
Includes:
ManqodCommon, Observable
Defined in:
lib/RelationBuilder.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(show_only_query = false) ⇒ RelationBuilder

Returns a new instance of RelationBuilder.



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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/RelationBuilder.rb', line 8

def initialize(show_only_query=false)
	@img_unique=Gdk::Pixdata.deserialize([71, 100, 107, 80, 0, 0, 1, 216, 2, 1, 0, 2, 0, 0, 0, 64, 0, 0, 0, 16, 0, 0, 0, 16, 145, 0, 0, 0, 0, 142, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 1, 102, 102, 102, 255, 136, 153, 153, 204, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 1, 102, 102, 102, 255, 136, 153, 153, 204, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 142, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 136, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 3, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 130, 204, 0, 0, 255, 131, 255, 255, 255, 255, 130, 204, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 132, 255, 255, 255, 255, 130, 204, 0, 0, 255, 131, 255, 255, 255, 255, 130, 204, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 3, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 130, 204, 0, 0, 255, 131, 255, 255, 255, 255, 130, 204, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 132, 255, 255, 255, 255, 130, 204, 0, 0, 255, 131, 255, 255, 255, 255, 130, 204, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 3, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 130, 204, 0, 0, 255, 131, 255, 255, 255, 255, 130, 204, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 132, 255, 255, 255, 255, 135, 204, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 6, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 133, 204, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 136, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 142, 102, 102, 102, 255, 145, 0, 0, 0, 0]).to_pixbuf(true)
	@img_index=Gdk::Pixdata.deserialize([71, 100, 107, 80, 0, 0, 2, 106, 2, 1, 0, 2, 0, 0, 0, 64, 0, 0, 0, 16, 0, 0, 0, 16, 145, 0, 0, 0, 0, 142, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 1, 102, 102, 102, 255, 132, 153, 153, 204, 255, 5, 128, 128, 128, 255, 255, 255, 255, 255, 255, 255, 153, 255, 255, 204, 51, 255, 0, 0, 0, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 4, 128, 128, 128, 255, 255, 255, 255, 255, 255, 255, 153, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 130, 0, 0, 0, 0, 135, 102, 102, 102, 255, 4, 128, 128, 128, 255, 255, 255, 255, 255, 255, 255, 153, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 255, 255, 255, 255, 3, 128, 128, 128, 255, 255, 255, 153, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 9, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 128, 128, 128, 255, 255, 255, 255, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 3, 102, 102, 102, 255, 128, 128, 128, 255, 255, 255, 255, 255, 133, 255, 204, 51, 255, 130, 0, 0, 0, 255, 130, 0, 0, 0, 0, 5, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 132, 0, 0, 0, 255, 2, 255, 255, 255, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 255, 255, 255, 255, 3, 128, 128, 128, 255, 255, 255, 255, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 2, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 9, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 128, 128, 128, 255, 255, 255, 255, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 255, 255, 255, 255, 1, 255, 204, 51, 255, 130, 0, 0, 0, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 7, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 255, 204, 51, 255, 130, 0, 0, 0, 255, 132, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 2, 102, 102, 102, 255, 255, 255, 255, 255, 130, 0, 0, 0, 255, 133, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 142, 102, 102, 102, 255, 145, 0, 0, 0, 0]).to_pixbuf(true)
	@img_primary=Gdk::Pixdata.deserialize([71, 100, 107, 80, 0, 0, 2, 190, 2, 1, 0, 2, 0, 0, 0, 64, 0, 0, 0, 16, 0, 0, 0, 16, 145, 0, 0, 0, 0, 135, 102, 102, 102, 255, 133, 0, 0, 0, 255, 130, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 10, 102, 102, 102, 255, 153, 153, 204, 255, 0, 0, 0, 255, 230, 184, 0, 255, 234, 187, 0, 255, 240, 192, 0, 255, 245, 196, 0, 255, 250, 200, 0, 255, 0, 0, 0, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 153, 153, 204, 255, 7, 102, 102, 102, 255, 0, 0, 0, 255, 232, 186, 0, 255, 234, 187, 0, 255, 238, 190, 0, 255, 0, 0, 0, 255, 249, 199, 0, 255, 130, 254, 203, 0, 255, 1, 0, 0, 0, 255, 130, 0, 0, 0, 0, 133, 102, 102, 102, 255, 3, 0, 0, 0, 255, 236, 189, 0, 255, 238, 190, 0, 255, 131, 244, 195, 0, 255, 3, 252, 202, 0, 255, 249, 199, 0, 255, 0, 0, 0, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 10, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 251, 201, 0, 255, 253, 202, 0, 255, 245, 196, 0, 255, 239, 191, 0, 255, 234, 187, 0, 255, 0, 0, 0, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 5, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 130, 255, 255, 255, 255, 7, 0, 0, 0, 255, 250, 200, 0, 255, 243, 194, 0, 255, 236, 189, 0, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 3, 0, 0, 0, 255, 241, 193, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 6, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 130, 0, 0, 0, 255, 3, 249, 199, 0, 255, 242, 194, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 3, 0, 0, 0, 255, 243, 194, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 6, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 130, 0, 0, 0, 255, 3, 250, 200, 0, 255, 245, 196, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 3, 0, 0, 0, 255, 247, 198, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 6, 102, 102, 102, 255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, 102, 102, 102, 255, 255, 255, 255, 255, 130, 0, 0, 0, 255, 3, 252, 202, 0, 255, 248, 198, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 1, 102, 102, 102, 255, 131, 255, 255, 255, 255, 3, 0, 0, 0, 255, 249, 199, 0, 255, 0, 0, 0, 255, 130, 255, 255, 255, 255, 1, 102, 102, 102, 255, 130, 0, 0, 0, 0, 137, 102, 102, 102, 255, 1, 0, 0, 0, 255, 132, 102, 102, 102, 255, 145, 0, 0, 0, 0]).to_pixbuf(true)

	@field_style={
		'tinyint'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0x4000,0x5700,0xdd00),
		'int'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0x4000,0xa500,0xdd00),
		'bigint'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0x4000,0xd800,0xdd00),
		'double'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0x5800,0xdd00,0x4000),
		'float'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0x4000,0xdd00,0x6600),
		'char'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0xdd00,0x4000,0xd200),
		'tinytext'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0xdd00,0xd400,0x0000),
		'text'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0xdd00,0xd400,0x4000),
		'varchar'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0xef00,0xe300,0x1400),
		'date'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0xc000,0xb000,0x4000),
		'datetime'=>Gtk::Style.new.set_bg(Gtk::STATE_NORMAL,0xef00,0xb000,0x1000),
	}
	@field_style.each_value{|fs|
		norm=fs.bg(Gtk::STATE_NORMAL)
		fs.set_bg(Gtk::STATE_ACTIVE,norm.red,norm.green,norm.blue)
		fs.set_bg(Gtk::STATE_PRELIGHT,
			norm.red  + 0x1000,
			norm.green+ 0x1000,
			norm.blue + 0x1000)
	}

	super()
	@base_object=nil
	@rel_ed=Array.new
	@relations=Hash.new
	@tables=Hash.new
	@holder=Gtk::VBox.new
	@widget=Gtk::Fixed.new
	@widget.signal_connect("expose-event"){|me,ev| draw_focused;draw_relations; true}
	add_with_viewport(@eb=Gtk::EventBox.new.add(@widget))
	visible_window=false

	@eb.signal_connect('button-press-event'){|me,ev|
		if ev.button == 2
			@dragging = true
			@xshift=ev.x
			@yshift=ev.y
		end
		if ev.button == 3
			if @rel_ed_status == :start
				rel_edit(nil,:cancel)
			else
			menu=Gtk::Menu.new
			menu.append(newt=Gtk::ImageMenuItem.new("create table").set_image(Gtk::Image.new(Gtk::Stock::NEW,Gtk::IconSize::MENU)))
			newt.signal_connect("activate"){|me|
					dialog=Gtk::MessageDialog.new(nil,Gtk::Dialog::Flags::MODAL,Gtk::MessageDialog::QUESTION,Gtk::MessageDialog::ButtonsType::OK_CANCEL,"creating table")
					dialog.vbox.pack_start(Gtk::Label.new("table name"))
					dialog.vbox.pack_start(table_name=Gtk::Entry.new)
					dialog.show_all
					if dialog.run == Gtk::Dialog::ResponseType::OK
						client.query("CREATE TABLE `#{table_name.text}` (`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY) ENGINE = MYISAM")
						add_existing_table(table_name.text)
					end
					dialog.destroy
				
			}
			
			#add tables to the menu
			menu.append(a=Gtk::ImageMenuItem.new("add existing table").set_image(Gtk::Image.new(Gtk::Stock::OPEN,Gtk::IconSize::MENU)))
			a.set_submenu(Gtk::Menu.new)
			client.query("show tables").each{|row|
				#row2=qrow("select count(id) as c from tables where name='#{row[0]}'")
				#if row2["c"]=="0"
				unless @tables.has_key?(row[0])
					a.submenu.append(tn=Gtk::ImageMenuItem.new(row[0],false).set_image(Gtk::Image.new(Gtk::Stock::ADD,Gtk::IconSize::MENU)))

					tn.signal_connect("activate",row[0],menu){|me,table_name,menu|
						#add the table
						add_existing_table(table_name)
						menu.destroy
					}
				end
			}

			menu.append(reload_srv=Gtk::ImageMenuItem.new("Reload Server Model").set_image(Gtk::Image.new(Gtk::Stock::REFRESH,Gtk::IconSize::MENU)))
			reload_srv.signal_connect("activate"){|me|
				reload_server_rb
			}

			menu.show_all
			menu.popup(nil,nil,ev.button,ev.time)
			end
		end
		true
	}
	@eb.signal_connect('button-release-event'){|me,ev|
		@dragging=false if @dragging
		true
	}
	@eb.signal_connect('motion-notify-event'){|me,event|
#			p "moving: #{px}(#{@x}) #{py}(#{@y}) H:#{hadjustment.lower} #{hadjustment.upper} V:#{vadjustment.lower} #{vadjustment.upper}"
#			ch=false
		if @dragging
			px=@xshift - self.pointer[0]
			py=@yshift - self.pointer[1]
			px=hadjustment.lower if px < hadjustment.lower
			py=vadjustment.lower if py < hadjustment.lower
			px=hadjustment.upper - hadjustment.page_size if px > hadjustment.upper - hadjustment.page_size
			py=vadjustment.upper - vadjustment.page_size if py > vadjustment.upper - vadjustment.page_size
			hadjustment.value=px
			vadjustment.value=py
		end
		true
	}

	@rel_ed_status=nil
	@dragging=false
	signal_connect("realize"){|me| @base_object.center_on_me unless @base_object.nil?}

	@show_only_query=show_only_query
end

Instance Attribute Details

#field_styleObject (readonly)

Returns the value of attribute field_style.



126
127
128
# File 'lib/RelationBuilder.rb', line 126

def field_style
  @field_style
end

#focused_itemObject (readonly)

Returns the value of attribute focused_item.



125
126
127
# File 'lib/RelationBuilder.rb', line 125

def focused_item
  @focused_item
end

#holderObject (readonly)

Returns the value of attribute holder.



125
126
127
# File 'lib/RelationBuilder.rb', line 125

def holder
  @holder
end

#img_indexObject (readonly)

Returns the value of attribute img_index.



126
127
128
# File 'lib/RelationBuilder.rb', line 126

def img_index
  @img_index
end

#img_primaryObject (readonly)

Returns the value of attribute img_primary.



126
127
128
# File 'lib/RelationBuilder.rb', line 126

def img_primary
  @img_primary
end

#img_uniqueObject (readonly)

Returns the value of attribute img_unique.



126
127
128
# File 'lib/RelationBuilder.rb', line 126

def img_unique
  @img_unique
end

#rel_ed_statusObject (readonly)

Returns the value of attribute rel_ed_status.



125
126
127
# File 'lib/RelationBuilder.rb', line 125

def rel_ed_status
  @rel_ed_status
end

#relationsObject (readonly)

Returns the value of attribute relations.



125
126
127
# File 'lib/RelationBuilder.rb', line 125

def relations
  @relations
end

#show_only_queryObject (readonly)

Returns the value of attribute show_only_query.



126
127
128
# File 'lib/RelationBuilder.rb', line 126

def show_only_query
  @show_only_query
end

#tablesObject (readonly)

Returns the value of attribute tables.



125
126
127
# File 'lib/RelationBuilder.rb', line 125

def tables
  @tables
end

#widgetObject (readonly)

Returns the value of attribute widget.



125
126
127
# File 'lib/RelationBuilder.rb', line 125

def widget
  @widget
end

Instance Method Details

#add_existing_table(table_name) ⇒ Object



438
439
440
441
442
443
# File 'lib/RelationBuilder.rb', line 438

def add_existing_table(table_name)
	unless @tables.has_key?(table_name)
		query("insert into tables (name,rbx,rby) values('#{table_name}','#{@widget.pointer[0]}','#{@widget.pointer[1]}')")
		reload_server_rb(true,table_name)
	end
end

#build_queryObject



452
453
454
455
# File 'lib/RelationBuilder.rb', line 452

def build_query
	sql=@query_id
	sql
end

#changed(notifier = nil) ⇒ Object



460
461
462
463
464
# File 'lib/RelationBuilder.rb', line 460

def changed(notifier=nil)
	edebug("#{notifier} changed, notifying observers","relation-builder")
	super()
	notify_observers(self,notifier)
end

#clearObject



389
390
391
# File 'lib/RelationBuilder.rb', line 389

def clear
	@widget.queue_draw_area(0, 0, @widget.allocation.width, @widget.allocation.height)
end

#draw_focusedObject



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/RelationBuilder.rb', line 219

def draw_focused
	return if @focused_item.nil? || !@focused_item.realized?
	cr=@widget.window.create_cairo_context
	x1,y1=@focused_item.widget.translate_coordinates(@widget,0,0)
	width=@focused_item.widget.allocation.width
	height=@focused_item.widget.allocation.height

	cr.set_source_rgba(0.2,0.9,0.4,0.4)
	cr.set_line_width(10)

	cr.rounded_rectangle(x1-10,y1-10,width+20,height+20,20)
	cr.stroke
end

#draw_relationsObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/RelationBuilder.rb', line 233

def draw_relations
	cr=@widget.window.create_cairo_context
	@relations.each_value{|rel|
		next unless rel.visible? && rel.handle.realized?
		begin
			x1=rel.rfield1.table.widget.translate_coordinates(@widget,0,0)[0]
			y1=rel.rfield1.translate_coordinates(@widget,0,0)[1]
			x2=rel.rfield2.table.widget.translate_coordinates(@widget,0,0)[0]
			y2=rel.rfield2.translate_coordinates(@widget,0,0)[1]
			xr,yr=rel.handle.translate_coordinates(@widget,0,0)
			if x1<x2
				t=x1
				x1=x2
				x2=t

				t=y1
				y1=y2
				y2=t

				w2,w1,wr=[rel.rfield1.table_width, rel.rfield2.table_width, rel.handle.allocation.width]
			else
				w1,w2,wr=[rel.rfield1.table_width, rel.rfield2.table_width, rel.handle.allocation.width]
			end
			high=(@focused_item == rel.rfield1.table) || (@focused_item == rel.rfield2.table)
			rescue
			x1=nil
		end
		unless x1.nil?
			y1+=rel.rfield1.allocation.height/2	#vertical midle of field1
			y2+=rel.rfield2.allocation.height/2	#vertical middle of field2
			yr+=rel.handle.allocation.height/2	#vertical middle of relation_handle
			xr+=rel.handle.allocation.width/2		#horizontal middle of relation_handle
			m1,m2=[0,0]

#				f1=Math.atan(1.0*(yr-y1)/(x1-xr))
#				f2=Math.atan(1.0*(yr-y2)/(x2-xr))
#				f3=(f1+f2)/2

			if x1+w1/2 < xr
				x1+=w1
				d1=1
				m1=x1-xr
				else
				m1=xr-x1
				d1=-1
			end

			if x2+w2/2 < xr
				x2+=w2
				m2=x2-xr
				d2=1
				else
				m2=xr-x2
				d2=-1
			end

			f1=Math.atan(1.0*(yr-y1)/(x1-xr))
			f1=f1-Math::PI if xr>x1

			f2=Math.atan(1.0*(yr-y2)/(x2-xr))
			f2=f2-Math::PI if xr>x2

			f3=(f1+f2)/2+Math::PI/2
#				f3=f3-Math::PI if xr>x1 && x1<x2 || x1>x2 && xr>x2

			r1=((x1-xr).abs+(yr-y1).abs)/2
			r2=((x2-xr).abs+(yr-y2).abs)/2

			#draw highlighted
			if high
				cr.set_source_rgba(0.9,0.9,0.2,0.4)
				cr.set_line_width(8)
				#arrow
				cr.move_to(x2+5*(m2<=>0),y2-5)
				cr.line_to(x2,y2)
				cr.line_to(x2+5*(m2<=>0),y2+5)
				#second arrow
				if rel.rel_type == "o"
					cr.move_to(x1+5*(m1<=>0),y1-5)
					cr.line_to(x1,y1)
					cr.line_to(x1+5*(m1<=>0),y1+5)
				end
				#relation
				cr.move_to(x1,y1)
				cr.curve_to(x1+r1*d1,y1, xr+r1*Math.cos(f3),yr-r1*Math.sin(f3),xr,yr)

				cr.move_to(x2,y2)
				cr.curve_to(x2+r2*d2,y2, xr+r2*Math.cos(f3-Math::PI),yr-r2*Math.sin(f3-Math::PI),xr,yr)
				cr.stroke
			end

			if rel.active?
				cr.set_source_rgba(0,0,1,0.8)
				cr.set_line_width(4)
				else
				cr.set_source_rgba(0,0.4,0,0.8)
				cr.set_line_width(2)
			end

			#draw arrow
			cr.move_to(x2+5*d2,y2-5)
			cr.line_to(x2,y2)
			cr.line_to(x2+5*d2,y2+5)
			
			if rel.rel_type == "o"
				#draw second arrow
				cr.move_to(x1+5*d1,y1-5)
				cr.line_to(x1,y1)
				cr.line_to(x1+5*d1,y1+5)
			end

			
			cr.move_to(x1,y1)
			cr.curve_to(x1+r1*d1,y1, xr+r1*Math.cos(f3),yr-r1*Math.sin(f3),xr,yr)

			cr.move_to(x2,y2)
			cr.curve_to(x2+r2*d2,y2, xr+r2*Math.cos(f3-Math::PI),yr-r2*Math.sin(f3-Math::PI),xr,yr)

			cr.stroke
=begin
			#draw control angle 1
			cr.set_source_rgba(0.1,0.9,0.1,0.8).set_line_width(0.5).set_dash(5)
			cr.move_to(xr,yr).line_to(xr+r1*Math.cos(f1),yr-r1*Math.sin(f1))
			cr.stroke

			#draw control angle 2
			cr.set_source_rgba(0.1,0.1,0.9,0.8).set_line_width(0.5).set_dash(10)
			cr.move_to(xr,yr).line_to(xr+r2*Math.cos(f2),yr-r2*Math.sin(f2))
			cr.stroke
			
			#draw the control points

			cr.set_dash(100000)

			cr.set_source_rgba(0.9,0.1,0.1,0.4)
			cr.set_line_width(1)

			cr.move_to(x1,y1)
			cr.line_to(x1+r1*d1,y1)
			cr.move_to(xr,yr)
			cr.line_to(xr+r1*Math.cos(f3),yr-r1*Math.sin(f3))

			cr.stroke
			cr.set_source_rgba(0.1,0.1,0.9,0.4)

			cr.move_to(x2,y2)
			cr.line_to(x2+r2*d2,y2)
			cr.move_to(xr,yr)
			cr.line_to(xr+r2*Math.cos(f3-Math::PI),yr-r2*Math.sin(f3-Math::PI))

			cr.stroke
=end
		end
	}
end

#find_relation(rfield1, rfield2) ⇒ Object



411
412
413
414
415
416
417
418
419
# File 'lib/RelationBuilder.rb', line 411

def find_relation(rfield1,rfield2)
	rel=nil
	@relations.each_value{|r| 
		if [r.rfield1,r.rfield2] == [rfield1,rfield2] or [r.rfield1,r.rfield2] == [rfield2,rfield1]
			rel=r 
		end
		}
	rel
end

#inspectObject



195
196
197
# File 'lib/RelationBuilder.rb', line 195

def inspect
	title
end

#rel_edit(rfield, status) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/RelationBuilder.rb', line 393

def rel_edit(rfield,status)
	@rel_ed_status=status
#		@rel_ed.push(rfield) if rfield.active?
	case status
		when :start
			x1,y1=rfield.translate_coordinates(@widget,0,0)
			@rel_in_ed=Relation.new(self,{"src_table" => rfield.table_name, "src_field" => rfield.field_name, "rbx" => x1-30, "rby" => y1-30})
			@relations[0]=@rel_in_ed
			@rel_in_ed.show_me
		when :finish
			@rel_in_ed.set_rfield2(rfield)
			@rel_in_ed.edit
			@rel_in_ed.show_me
		when :cancel
			@rel_in_ed.remove
	end
end

#reload(target = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/RelationBuilder.rb', line 149

def reload(target=nil)
	@search_window=SearchWindow.new(self) unless @search_window
	@rq_window=RunQuery.new(self) unless @rq_window

	ctables=cache.get("tables")
	crelations=cache.get("relations")
	ctables.each_pair{|table_name,table|
		if @tables.has_key?(table_name)
			@tables[table_name].update(table)
		else
			@tables[table_name]=RTable.new(self,table)
		end
	}
	crelations.each_pair{|rel_id,rel|
		if @relations.has_key?(rel_id)
			@relations[rel_id].reload(rel)
		else
			@relations[rel_id]=Relation.new(self,rel)
		end
	}
	review_all
end

#reload_server_rb(reload_me_too = true, table = nil, relation = nil) ⇒ Object



445
446
447
448
449
450
# File 'lib/RelationBuilder.rb', line 445

def reload_server_rb(reload_me_too=true,table=nil,relation=nil)
	Thread.new{
		ManqodDB.instance.manqod_db.relation_builder.update(table,relation) if ManqodDB.instance.manqod_db.relation_builder
		reload if reload_me_too
	}
end

#remove_table(table, from_sql = false) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/RelationBuilder.rb', line 421

def remove_table(table,from_sql=false)
	@tables[table].fields.each{|field,field_wg|
		@relations.each_value{|rel|
			if rel.rfield1 == field_wg or rel.rfield2 == field_wg
				edebug("removing #{rel}","relation-builder")
				rel.remove
			end
		}
	}
	query("delete from tables where id='#{@tables[table].table_id}'")
	client.query("drop table `#{table}`") if from_sql
	@tables[table].widget.destroy
	@tables.delete(table)
	reload_server_rb(true,table)
	changed(@tables[table])
end

#review_allObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/RelationBuilder.rb', line 172

def review_all
	if @show_only_query
		#show only related to query
		@tables.each_value{|table| table.set_visibility(false)}
		@relations.each_value{|rel| rel.set_visibility(rel.active?)}
		@relations.each_value{|rel|
			if rel.active?
				rel.rfield1.table.show_neighbours
				rel.rfield2.table.show_neighbours
			end
		}
		@base_object.show_neighbours if @base_object
	else
		#show all
		@tables.each_value{|table| table.set_visibility(true)}
		@relations.each_value{|rel| rel.set_visibility(true)}
	end
	clear
end

#set_base_object(new_bo) ⇒ Object



456
457
458
459
# File 'lib/RelationBuilder.rb', line 456

def set_base_object(new_bo)
	@base_object=new_bo
	@base_object.center_on_me
end

#set_focused_item(item) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/RelationBuilder.rb', line 202

def set_focused_item(item)
	return if @focused_item == item
	if !@focused_item.nil?
		@focused_item.set_state(Gtk::STATE_NORMAL)
		@focused_item.dragging=false
	end
	@focused_item=item
	set_focus_child(@focused_item)
	if !@focused_item.nil?
		@focused_item.set_state(Gtk::STATE_PRELIGHT) 
		@focused_item.set_has_focus(true)
	end
	clear
#		info.update(focused_item)
	@focused_item
end

#table_by_id(table_id) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/RelationBuilder.rb', line 132

def table_by_id(table_id)
	found=nil
	@tables.each_pair{|table_name,tableo|
		if tableo.table_id.to_i == table_id.to_i
			found=tableo
			break
		end
	}
	found
end

#table_by_name(table_name) ⇒ Object



128
129
130
# File 'lib/RelationBuilder.rb', line 128

def table_by_name(table_name)
	tables[table_name]
end

#titleObject



192
193
194
# File 'lib/RelationBuilder.rb', line 192

def title
	"Relation Builder on #{manqod_db}"
end

#to_sObject



198
199
200
# File 'lib/RelationBuilder.rb', line 198

def to_s
	inspect
end

#update(notifier = nil) ⇒ Object



143
144
145
146
147
# File 'lib/RelationBuilder.rb', line 143

def update(notifier=nil)
	reload
	@search_window.update(get_ancestor(Gtk::Window))
	@rq_window.update(get_ancestor(Gtk::Window))
end