Class: ListPrint::ListPrintOperation

Inherits:
Gtk::PrintOperation
  • Object
show all
Includes:
Conf
Defined in:
lib/ListHolder/EditableList/ListPrintOperation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Conf

#get_conf, #load_conf, #save_conf, #set_conf

Constructor Details

#initialize(list) ⇒ ListPrintOperation

Returns a new instance of ListPrintOperation.



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
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
177
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 9

def initialize(list)
    @list=list
    @print_sums=(list.gtk_attribute("print_sums") || "true") =="true"
    @sum_font=list.gtk_attribute("sum_font") || "verdana bold 6"
    @cell_font=list.gtk_attribute("cell_font") || "verdana 6"
    @columns_header_font=list.gtk_attribute("columns_header_font") || "verdana bold 6"
    @header_font=list.gtk_attribute("header_font") || "verdana bold 10"
    @footer_font=list.gtk_attribute("footer_font") || "verdana 6"
    @date_font=list.gtk_attribute("date_font") || "verdana 5"
    @print_date=(list.gtk_attribute("print_date") || "true") == "true"
    @row_spacing=(list.gtk_attribute("row_spacing") || 1.0).to_f
    @column_spacing=(list.gtk_attribute("column_spacing") || 1.0).to_f
    @draw_footer_line=(list.gtk_attribute("draw_footer_line") || "true") == "true"
    @line_above_parent=(list.gtk_attribute("line_above_parent") || "true") == "true"
    @draw_header_line=(list.gtk_attribute("draw_header_line") || "true") == "true"
    @print_title=(list.gtk_attribute("print_title") || "true") == "true"
    @draw_header_cell_borders=(list.gtk_attribute("draw_header_cell_borders") || "true") == "true"
    @fill_header_background=(list.gtk_attribute("fill_header_background") || "true") == "true"
    @row_underline=(list.gtk_attribute("row_underline") || 0.0).to_f

    if @print_with_child=list.gtk_attribute("print_with_child")
      @child_list=list.child_by_id(@print_with_child.to_i)
      @child_model=Gtk::TreeModelSort.new(@child_list.list_model.data)
      @child_model.set_sort_column_id(@child_list.list_model.sorter.sort_column_id[0],@child_list.list_model.sorter.sort_column_id[1]) unless @child_list.list_model.sorter.sort_column_id.nil?
    end
    @print_sub_sums=(list.gtk_attribute("print_sub_sums") || "true") == "true"
    @child_indent=(list.gtk_attribute("child_indent") || 100).to_f
    @child_cell_font=list.gtk_attribute("child_cell_font") || "verdana 5"
    @child_sum_font=list.gtk_attribute("child_sum_font") || "verdana bold 5"
    @child_columns_header_font=list.gtk_attribute("child_columns_header_font") || "verdana bold 5"

    @header_line_width=(list.gtk_attribute("header_line_width") || 1.0).to_f
    @footer_line_width=(list.gtk_attribute("footer_line_width") || 1.0).to_f
    @cell_line_width=(list.gtk_attribute("cell_line_width") || 0.1).to_f
    @sum_line_width=(list.gtk_attribute("sum_line_width") || 0.6).to_f
    
    @print_with_gantt=(list.gtk_attribute("print_with_gantt") || "false") == "true"
    @gantt_cell_font=list.gtk_attribute("gantt_cell_font") || "verdana 5"

    @line_width=1

    super()
    set_show_progress(true)
    set_use_full_page(false)
    set_unit(Gtk::PaperSize::UNIT_POINTS)
    
#     n_pages=1


    signal_connect("begin-print",@list) { |pop, context,list|
      edebug("begin print","printing","info")
      cr = context.cairo_context
      cr.set_line_width(@line_width)
      @height = context.page_setup.get_page_height(Gtk::PaperSize::UNIT_POINTS).to_f
      @width = context.page_setup.get_page_width(Gtk::PaperSize::UNIT_POINTS).to_f

      columns_width_sum=0.0
      col_cnt=0
      list.columns.each{|col| 
        if col.printable?
          columns_width_sum+=col.width.to_f 
          col_cnt+=1
        end
      } #summ the width of all columns
      columns_width_sum+=list.holder.widget.allocation.width - list.allocation.width if @print_with_gantt
      coef=(width-col_cnt*@column_spacing)/columns_width_sum
      einfo("colums width sum: #{columns_width_sum}","printing")
      
      #@column_width will store the column positions on the context
      @column_width=Hash.new
      @column_pos=Hash.new
      col_cur_pos=0.0
      list.columns.each{|col|
        if col.printable?
          @column_pos[col.data]=col_cur_pos
          @column_width[col.data]=col.width.to_f*coef
          col_cur_pos+=@column_width[col.data]+@column_spacing
        end
      }
      if @print_with_gantt
        @column_pos[:gantt]=col_cur_pos
        @column_width[:gantt]=(list.holder.widget.allocation.width - list.allocation.width).to_f*coef
      end
      if child_list
        columns_width_sum=@child_indent.to_f
        col_cnt=0
        child_list.columns.each{|col| 
          if col.printable?
            columns_width_sum+=col.width.to_f 
            col_cnt+=1
          end
        } #summ the width of all columns
        coef=(width-col_cnt*@column_spacing)/columns_width_sum
        einfo("child colums width sum: #{columns_width_sum}","printing")
      
        #@column_width will store the column positions on the context
        col_cur_pos=@child_indent.to_f*coef
        child_list.columns.each{|col|
          if col.printable?
            @column_pos[col.data]=col_cur_pos
            @column_width[col.data]=col.width.to_f*coef
            col_cur_pos+=@column_width[col.data]+@column_spacing
          end
        }
      end
      

      
      @current_height=0
      @header=HeaderLayout.new(cr,self)
      @current_height=@current_height+@header.height
      @columns_header=ColumnsHeaderLayout.new(self,cr,list.columns,@columns_header_font)
      @columns_header.set_y(@current_height)
      @current_height=@current_height+@columns_header.height+@row_spacing
      if child_list
        @child_columns_header=ColumnsHeaderLayout.new(self,cr,child_list.columns,@child_columns_header_font) if child_list
        @child_columns_header.set_y(@current_height)
        @current_height=@current_height+@child_columns_header.height+@row_spacing
      end
      @headers_height=@current_height
      
      @footer=FooterLayout.new(cr,self)

      @iter_layouts = []

      @sums=Hash.new
      list.columns.each{|col| @sums[col.colnum]=0 if col.summable?}

      if child_list
        if print_sub_sums
          @sub_sums=Hash.new
          child_list.columns.each{|col| @sub_sums[col.colnum]=0 if col.summable?}
        end
        @citer=nil
      end
      
      @piter=nil
      #list.model.iter_first
      @past_last=false
      @page_n=0
    }
    signal_connect("paginate",@list) { |pop, context,list|
      edebug("paginate: #{@page_n}","printing","info")
      
      page_full=false
      cr = context.cairo_context
      while true
          if @child_list
              #step first on citer if citer is nil
              if @citer.nil?
                @citer=@child_model.iter_first
                else 
                @citer=nil unless @citer.next!
              end unless @piter.nil?
              #go to next valid citer which meets key-relation
              begin
                break if @citer.nil?
                go_next=false
                #check for parent_key
                go_next=true unless @citer[child_list.list_model.column_of_child_key] == @piter[child_list.list_model.column_of_parent_key]
                #check for parent_key2
                go_next=true unless @citer[child_list.list_model.column_of_child_key2] == @piter[child_list.list_model.column_of_parent_key2] if !child_list.list_model.column_of_child_key2.nil? && go_next
                #check child's filters
                filter_count=0
                filter_hits=0
                @child_list.columns.each{|column|
                  fl=get_conf(@child_list.get_id,column.header["id"],"filter").to_s.upcase
                  case column.header['type']
                    when 'gtk_combo', 'gtk_const_combo' then fl = (fl=="-1" or fl=="") ? "" : "=#{fl}"
                  end
                  comp=@citer[column.header["model_col"]].to_s.upcase
                  if fl.length>0
                    filter_count=filter_count+1
                    filter_hits=filter_hits+
                      if ['gtk_int','Integer','gtk_float','gtk_progress','gtk_duration','gtk_timestamp'].include? column.header['type']
                        case fl[0].chr
                          when "<" then comp.to_f <  fl[1 .. fl.length].to_f ? 1: 0
                          when ">" then comp.to_f >  fl[1 .. fl.length].to_f ? 1: 0
                          when "!" then comp.to_f != fl[1 .. fl.length].to_f ? 1: 0
                          when "=" then comp.to_f == fl[1 .. fl.length].to_f ? 1: 0
                          else comp.to_f == fl[1 .. fl.length].to_f ? 1: 0
                        end
                      else
                        case fl[0].chr
                          when "=" then comp == fl[1 .. fl.length] ? 1: 0
                          when "<" then comp <  fl[1 .. fl.length] ? 1: 0
                          when ">" then comp >  fl[1 .. fl.length] ? 1: 0
                          when "!" then comp != fl[1 .. fl.length] ? 1: 0
                          else comp.index(fl) ? 1: 0
                        end
                      end
                  end
                }
                go_next=filter_hits != filter_count unless go_next

                @citer=nil if !@citer.next! if go_next
              end while go_next
              #if citer is nil we go to next piter
              if @citer.nil?
                #print sub_sums
                if print_sub_sums and @sum_avail
                  il=IterLayout.new(cr,@sub_sums,child_list.columns, self,child_sum_font )
#                   print il.inspect
                  il.set_line_above(true)
                  lay_it(il)
                  #clear sums
                  @sub_sums.each_key{|key| @sub_sums[key]=0} 
                  @sum_avail=false
                end

                #we print piter
                if @piter.nil?
                  break if past_last=!@piter=list.model.iter_first
                else
                  break if past_last=!@piter.next_with_children
                end
                @sums.each_key{|key| @sums[key]=@sums[key]+@piter[key].to_f}
                il=IterLayout.new(cr,@piter,list.columns, self,cell_font)
                il.set_line_above(line_above_parent)
                break if lay_it(il)
              else
                #calc sums
                if print_sub_sums
                  @sub_sums.each_key{|key| @sub_sums[key]=@sub_sums[key]+@citer[key].to_f} 
                  @sum_avail=true
                end
#                 p "citer: #{@citer[child_list.column_of_id]} #{@past_last} #{il.inspect}"
                #we print citer
                il=IterLayout.new(cr,@citer,child_list.columns, self,child_cell_font)
                break if lay_it(il)
              end
            
            else
            if @piter.nil?
              break if past_last=!@piter=list.model.iter_first 
            end
            @sums.each_key{|key| @sums[key]=@sums[key]+@piter[key].to_f} if @piter
            il=IterLayout.new(cr,@piter,list.columns, self,cell_font)
            if lay_it(il)
              #do not print it twice
              past_last=!@piter.next_with_children
              break
            end
            break if past_last=!@piter=@piter.next_with_children
            edebug("piter: #{@piter[list.list_model.column_of_id]} #{@past_last}","printing","debug")
          end
      end

      if past_last and print_sums
        il=IterLayout.new(cr,@sums,list.columns, self,sum_font)
        il.set_line_above(true)
        lay_it(il)
      end

      pop.set_n_pages(@page_n+1)
      einfo("paginated #{pop.n_pages} pages","printing")
      past_last
    } #paginate
    
    signal_connect('draw-page'){|me,context,page_number|
      edebug("draw-page: #{page_number}","printing","info")
      cr = context.cairo_context
      @header.draw(cr)
      @columns_header.draw(cr)
      if child_list
        @child_columns_header.draw(cr) 
      else
        cr.set_line_width(@cell_line_width)
        lcol=nil
        list.columns.each{|col|
          if col.printable?
            cr.move_to(@column_pos[col.data],@columns_header.y + @columns_header.height + @header_line_width)
            cr.line_to(@column_pos[col.data],@footer.y) #FIXME
            lcol=col
          end
        }
        cr.move_to(@column_pos[lcol.data]+@column_width[lcol.data],@columns_header.y + @columns_header.height + @header_line_width)
        cr.line_to(@column_pos[lcol.data]+@column_width[lcol.data],@footer.y) #FIXME
        cr.stroke
      end

      @footer.draw(cr,page_number)
      
      #draw cells  
      @iter_layouts.each{|il|
        il.draw(cr) if il.page == page_number
      }
      cr.stroke
    }
    signal_connect('status-changed'){|pop|
      einfo("status:#{pop.status_string}","printing")
    }
end

Instance Attribute Details

#bottomObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def bottom
  @bottom
end

#cell_fontObject

Returns the value of attribute cell_font.



313
314
315
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 313

def cell_font
  @cell_font
end

#cell_line_widthObject (readonly)

Returns the value of attribute cell_line_width.



327
328
329
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 327

def cell_line_width
  @cell_line_width
end

#child_cell_fontObject

Returns the value of attribute child_cell_font.



316
317
318
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 316

def child_cell_font
  @child_cell_font
end

#child_columns_header_fontObject

Returns the value of attribute child_columns_header_font.



310
311
312
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 310

def child_columns_header_font
  @child_columns_header_font
end

#child_indentObject

Returns the value of attribute child_indent.



318
319
320
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 318

def child_indent
  @child_indent
end

#child_listObject

Returns the value of attribute child_list.



317
318
319
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 317

def child_list
  @child_list
end

#child_sum_fontObject

Returns the value of attribute child_sum_font.



315
316
317
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 315

def child_sum_font
  @child_sum_font
end

#column_posObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def column_pos
  @column_pos
end

#column_spacingObject

Returns the value of attribute column_spacing.



326
327
328
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 326

def column_spacing
  @column_spacing
end

#column_widthObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def column_width
  @column_width
end

#columnsObject

Returns the value of attribute columns.



319
320
321
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 319

def columns
  @columns
end

#columns_header_fontObject

Returns the value of attribute columns_header_font.



309
310
311
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 309

def columns_header_font
  @columns_header_font
end

#custom_page_setupObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def custom_page_setup
  @custom_page_setup
end

#date_fontObject

Returns the value of attribute date_font.



312
313
314
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 312

def date_font
  @date_font
end

Returns the value of attribute draw_footer_line.



304
305
306
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 304

def draw_footer_line
  @draw_footer_line
end

#draw_header_cell_bordersObject

Returns the value of attribute draw_header_cell_borders.



306
307
308
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 306

def draw_header_cell_borders
  @draw_header_cell_borders
end

#draw_header_lineObject

Returns the value of attribute draw_header_line.



303
304
305
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 303

def draw_header_line
  @draw_header_line
end

#fill_header_backgroundObject

Returns the value of attribute fill_header_background.



324
325
326
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 324

def fill_header_background
  @fill_header_background
end

Returns the value of attribute footer_font.



311
312
313
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 311

def footer_font
  @footer_font
end

Returns the value of attribute footer_line_width.



322
323
324
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 322

def footer_line_width
  @footer_line_width
end

#gantt_cell_fontObject (readonly)

Returns the value of attribute gantt_cell_font.



327
328
329
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 327

def gantt_cell_font
  @gantt_cell_font
end

#header_fontObject

Returns the value of attribute header_font.



308
309
310
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 308

def header_font
  @header_font
end

#header_line_widthObject

Returns the value of attribute header_line_width.



324
325
326
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 324

def header_line_width
  @header_line_width
end

#heightObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def height
  @height
end

#leftObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def left
  @left
end

#line_above_parentObject

Returns the value of attribute line_above_parent.



305
306
307
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 305

def line_above_parent
  @line_above_parent
end

#listObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def list
  @list
end

Returns the value of attribute print_date.



307
308
309
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 307

def print_date
  @print_date
end

Returns the value of attribute print_sub_sums.



320
321
322
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 320

def print_sub_sums
  @print_sub_sums
end

Returns the value of attribute print_sums.



321
322
323
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 321

def print_sums
  @print_sums
end

Returns the value of attribute print_title.



327
328
329
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 327

def print_title
  @print_title
end

Returns the value of attribute print_with_gantt.



327
328
329
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 327

def print_with_gantt
  @print_with_gantt
end

#rightObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def right
  @right
end

#row_spacingObject

Returns the value of attribute row_spacing.



325
326
327
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 325

def row_spacing
  @row_spacing
end

#row_underlineObject (readonly)

Returns the value of attribute row_underline.



327
328
329
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 327

def row_underline
  @row_underline
end

#sum_fontObject

Returns the value of attribute sum_font.



314
315
316
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 314

def sum_font
  @sum_font
end

#sum_line_widthObject (readonly)

Returns the value of attribute sum_line_width.



327
328
329
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 327

def sum_line_width
  @sum_line_width
end

#topObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def top
  @top
end

#widthObject

initialize



302
303
304
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 302

def width
  @width
end

Instance Method Details

#lay_it(il) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 328

def lay_it(il)
  if @current_height + il.row_height > @footer.y
    #page full
    page_full=true
    einfo("page full","printing")
    edebug("current_height: #{@current_height}, row.height: #{il.height}, footer_position: #{@footer.y}","printing")
    @current_height=@headers_height
    @page_n=@page_n+1
  end
  il.set_page(@page_n)
  il.set_y(@current_height).set_row_spacing(@row_spacing)
  @current_height=@current_height+il.row_height+2*@row_spacing
  @iter_layouts.push(il)
  edebug(il.inspect,"printing")
  page_full
end

#pango_heightObject



348
349
350
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 348

def pango_height
  height * Pango::SCALE
end

#pango_widthObject



345
346
347
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 345

def pango_width
  width * Pango::SCALE
end

#run_print_dialogObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 352

def run_print_dialog

  res = run(Gtk::PrintOperation::Action::PRINT_DIALOG, list.get_ancestor(Gtk::Window))
  einfo("return: "+
  case res
    when RESULT_ERROR
      "error"
    when RESULT_CANCEL
      "cancelled"
    when RESULT_APPLY
      "applied"
    when RESULT_IN_PROGRESS
      "in progress"
  end,"printing")
end

#to_sObject



367
# File 'lib/ListHolder/EditableList/ListPrintOperation.rb', line 367

def to_s;"<ListPrintOperation:#{@list.moditem["display"]}>";end