Class: MyPrintOperation

Inherits:
Gtk::PrintOperation
  • Object
show all
Includes:
Conf, ManqodCommon
Defined in:
lib/Print.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

Methods included from Conf

#get_conf, #load_conf, #save_conf, #set_conf

Constructor Details

#initialize(caller_object, moditemid, ids_to_print = nil) ⇒ MyPrintOperation

Returns a new instance of MyPrintOperation.



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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/Print.rb', line 9

def initialize(caller_object,moditemid,ids_to_print=nil)
	@parentselected=nil
	@is_test=ids_to_print.nil?
	@caller_object=caller_object
	@ids_to_print=ids_to_print
	case caller_object.class.name 
		when 'MyEditableList' then @parentselected=caller_object.get_cursor_id
		when 'WysiwygPrintEditor'
		else
			edebug("unknown routing: "+caller_object.class.name,"printing","error")
	end
	@moditemid=moditemid
	if is_test?
		@moditem=qrow("select moditems.*,gtkprintpages.*,queries.sql from moditems left join queries on moditems.query_sql_id = queries.id left join gtkprintpages on moditems.id = gtkprintpages.moditem_id where moditems.id='#{@moditemid}'")
	else
		@moditem=admin.qrow("select moditems.*,gtkprintpages.*,queries.sql from moditems left join queries on moditems.query_sql_id = queries.id left join gtkprintpages on moditems.id = gtkprintpages.moditem_id where moditems.id='#{@moditemid}'")
	end
	super()
	set_show_progress(true)
	set_use_full_page(false)
	set_unit(Gtk::PaperSize::UNIT_POINTS)

	set_default_page_setup(Gtk::PageSetup.new)
	default_page_setup.paper_size=Gtk::PaperSize.new(@moditem["paper_size_name"])
	default_page_setup.orientation=case @moditem["orientation"]
		when "portrait" then Gtk::PrintSettings::PageOrientation::PORTRAIT
		when "reverse_portrait" then Gtk::PrintSettings::PageOrientation::REVERSE_PORTRAIT
		when "landscape" then Gtk::PrintSettings::PageOrientation::LANDSCAPE
		when "reverse_landscape" then Gtk::PrintSettings::PageOrientation::REVERSE_LANDSCAPE
		else Gtk::PrintSettings::PageOrientation::PORTRAIT
	end
	default_page_setup.set_left_margin(@moditem["left_margin"].to_f,Gtk::PaperSize::UNIT_MM) if !@moditem["left_margin"].nil?
	default_page_setup.set_right_margin(@moditem["right_margin"].to_f,Gtk::PaperSize::UNIT_MM) if !@moditem["right_margin"].nil?
	default_page_setup.set_top_margin(@moditem["top_margin"].to_f,Gtk::PaperSize::UNIT_MM) if !@moditem["top_margin"].nil?
	default_page_setup.set_bottom_margin(@moditem["bottom_margin"].to_f,Gtk::PaperSize::UNIT_MM) if !@moditem["bottom_margin"].nil?

	signal_connect("begin-print") { |pop, context|
		edebug("begin print","printing","info")
		cr = context.cairo_context
#			page_height=pop.default_page_setup.get_page_height(Gtk::PaperSize::UNIT_POINTS)
#			paper_height=pop.default_page_setup.get_paper_height(Gtk::PaperSize::UNIT_POINTS)
		@paper_height = context.page_setup.get_page_height(Gtk::PaperSize::UNIT_POINTS).to_f
		@paper_width = context.page_setup.get_page_width(Gtk::PaperSize::UNIT_POINTS).to_f

		@item_res=if is_test? then query("select * from gtkprintitems where printid='#{@moditemid}' order by page_number,y")
							else item_res=admin.query("select * from gtkprintitems where printid='#{@moditemid}' order by page_number,y") end
		@items_data=Array.new
		while itemdata=@item_res.fetch_hash()
			@items_data.push(itemdata.rehash)
		end
		@items_data_iter=nil
		@items=Array.new

		@sql=@moditem["sql"]
		@sql=@moditem["querysql"] if @moditem["querysql"].length>5
		if is_test?
			@sql="#{@sql} limit 1"
			myres=client.query(@sql)
		else
			myres=query(add_where(@sql,"#{guess_base(@sql)}.id in (#{@ids_to_print})"))
		end
		@myrows=Hash.new
		@row_num=0
		while myrow=myres.fetch_hash(true)
			@row_num+=1
			@myrows[@row_num]=myrow.rehash
		end

#			set_n_pages(@myrows.size*@pages.size)
		@page_n=0
		@current_row=1
		@current_page=0
		@page_shift=0
		@sublist_start_iter=nil
		pop.set_n_pages(@page_n)
	}
	
	signal_connect('paginate'){|pop,context|
		cr=context.cairo_context
		#start iterating PrintItems here
		@items_data_iter=@items_data.first if @items_data_iter.nil?

		if @items_data.include?(@items_data_iter)
			@items.push(i=PrintItem.new(self,@items_data_iter,cr))
			
			#start the broken sublist at top of page
			i.set_height(i.y+i.height).set_y(0) if @prev_extended and i.type?("list")

			i.set_txt(text_from_mysql(@myrows[current_row],@items_data_iter["txt"])).
				set_page_shift(@page_shift).
				set_start_iter(@sublist_start_iter).
				update
			#making sure page_n is max(i.page)
			@page_n=i.page if @page_n<i.page
			einfo("paginated: #{i}","printing")
			#list continuation
			@sublist_start_iter=i.next_iter
			@prev_extended=i.extended?
		end
		if @sublist_start_iter or i.extended?
			#we continue on next page
			@page_shift+=1
			einfo("sublist forced page_shift: #{@page_shift}","printing")
		end
		unless @sublist_start_iter
			#iterate through the raw data
			if @items_data_iter=@items_data[@items_data.index(@items_data_iter)+1]
				false
				else
				if @current_row<@row_num
					#next mysql row
					@current_row+=1
					@items_data_iter=@items_data.first
					@page_shift=@page_n+1
					einfo("page_shift: #{@page_shift}","printing")
					false
				else
					#finish, setting n_pages
					pop.set_n_pages(@page_n+1)
					edebug("finished paginating at #{pop.n_pages}","printing")
					true
				end
			end
		end
	}

	signal_connect('draw-page'){|pop,context,page_number|
		einfo("drawing page #{page_number+1}/#{pop.n_pages})","printing")
		cr = context.cairo_context
		@items.each{|item|
			cr.set_line_width(1)
			item.draw(cr) if item.page?(page_number) and !item.type?('image')
		}

		#workaround for the not printing text after an image in .ps
		@items.each{|item|
			edebug("drawing item #{item}","printing")
			item.draw(cr) if item.page?(page_number) and item.type?('image')
		}
	}

	self
end

Instance Attribute Details

#caller_objectObject

Returns the value of attribute caller_object.



152
153
154
# File 'lib/Print.rb', line 152

def caller_object
  @caller_object
end

#current_rowObject

Returns the value of attribute current_row.



152
153
154
# File 'lib/Print.rb', line 152

def current_row
  @current_row
end

#is_testObject

Returns the value of attribute is_test.



152
153
154
# File 'lib/Print.rb', line 152

def is_test
  @is_test
end

#moditemidObject

Returns the value of attribute moditemid.



152
153
154
# File 'lib/Print.rb', line 152

def moditemid
  @moditemid
end

#myrowsObject (readonly)

Returns the value of attribute myrows.



153
154
155
# File 'lib/Print.rb', line 153

def myrows
  @myrows
end

#page_setupObject

Returns the value of attribute page_setup.



152
153
154
# File 'lib/Print.rb', line 152

def page_setup
  @page_setup
end

#pagesObject

Returns the value of attribute pages.



152
153
154
# File 'lib/Print.rb', line 152

def pages
  @pages
end

#paper_heightObject (readonly)

Returns the value of attribute paper_height.



153
154
155
# File 'lib/Print.rb', line 153

def paper_height
  @paper_height
end

#paper_widthObject (readonly)

Returns the value of attribute paper_width.



153
154
155
# File 'lib/Print.rb', line 153

def paper_width
  @paper_width
end

#parentselectedObject

Returns the value of attribute parentselected.



152
153
154
# File 'lib/Print.rb', line 152

def parentselected
  @parentselected
end

Instance Method Details

#is_test?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/Print.rb', line 196

def is_test?
	@is_test
end

#run(action) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/Print.rb', line 155

def run(action)
	#set_n_pages(pages["page_count"].to_i)
#	set_default_page_setup(page_setup)
	self.print_settings=Gtk::PrintSettings.new.deserialize(get_conf(moditemid,0,'print_settings'))
	run_events(@moditemid,'print-Action')
	super(action,caller_object.get_ancestor(Gtk::Window))
	set_conf(moditemid,0,'print_settings',print_settings.serialize)
end

#text_from_mysql(mydata, nt = text) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/Print.rb', line 164

def text_from_mysql(mydata,nt=text)
	found=false
	rett=""
	while first=nt.index("{")
		found=true
		rett=rett+nt[0 .. first-1] if first-1>=0
		if last=nt[first .. nt.length].index("}")
			last=last+first
			datak=nt[first+1 .. last-1]
			if mydata.has_key?(datak)
				rett=rett+mydata[datak].to_s
				nt=nt[last+1 .. nt.length]
			else
				rett=rett+datak
				nt=nt[last+1 .. nt.length]
			end#if
		else
			nt=nt[first+1 .. nt.length]
			rett=rett+"{"+nt
		end#if
	end
	if found
		rett=rett+nt[1 .. nt.length] if nt.length>=1
	else
		rett=rett+nt if !nt.nil?
	end
	rett
end

#to_sObject



193
194
195
# File 'lib/Print.rb', line 193

def to_s
	"<MyPrintOperation:#{@moditem["display"]}>"
end