Project

General

Profile

Patch #10464 » pdf-1.diff

Yuri Makarov, 2012-03-19 11:37

View differences:

pdf.rb Mon Mar 19 14:24:14 2012
144 144
        end
145 145
      end
146 146

  
147
	  # fetch row values
148
	  def fetch_row_values(issue, query, level)	
149
	    query.columns.collect do |column|
150
		  s = if column.is_a?(QueryCustomFieldColumn)
151
		    cv = issue.custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
152
		    show_value(cv)
153
		  else
154
		    value = issue.send(column.name)
155
		    if column.name == :subject
156
			  value = "  " * level + value
157
		    end
158
		    if value.is_a?(Date)
159
			  format_date(value)
160
		    elsif value.is_a?(Time)
161
			  format_time(value)
162
		    else
163
			  value
164
		    end
165
		  end
166
		  s.to_s
167
	    end
168
	  end
169

  
170
	  # calculate columns width
171
	  def calc_col_width(issues, query, table_width, pdf)
172
		  # calculate statistics
173
		  #  by captions
174
          pdf.SetFontStyle('B',8)
175
		  col_padding = pdf.GetStringWidth('OO')
176
		  col_width_min = query.columns.map {|v| pdf.GetStringWidth(v.caption) + col_padding}
177
		  col_width_max = Array.new(col_width_min)
178
		  col_width_avg = Array.new(col_width_min)
179
		  word_width_max = query.columns.map {|c| 
180
			n = 10
181
			c.caption.split.each {|w| 
182
			  x = pdf.GetStringWidth(w) + col_padding
183
			  n = x if n < x
184
			}
185
			n
186
		  }
187

  
188
		  #  by properties of issues
189
          pdf.SetFontStyle('',8)
190
		  col_padding = pdf.GetStringWidth('OO')
191
		  k = 1
192
		  issue_list(issues) {|issue, level|
193
			k += 1
194
			values = fetch_row_values(issue, query, level)
195
		    values.each_with_index {|v,i| 
196
			  n = pdf.GetStringWidth(v) + col_padding
197
			  col_width_max[i] = n if col_width_max[i] < n
198
			  col_width_min[i] = n if col_width_min[i] > n
199
			  col_width_avg[i] += n
200
			  v.split.each {|w| 
201
				x = pdf.GetStringWidth(w) + col_padding
202
				word_width_max[i] = x if word_width_max[i] < x
203
			  }
204
			}
205
		  }
206
		  col_width_avg.map! {|x| x / k}
207
		  
208
		  # calculate columns width
209
          ratio = table_width / col_width_avg.inject(0) {|s,w| s += w}
210
          col_width = col_width_avg.map {|w| w * ratio}
211
		  
212
		  # correct max word width if too many columns
213
		  ratio = table_width / word_width_max.inject(0) {|s,w| s += w}
214
		  word_width_max.map! {|v| v * ratio} if ratio < 1
215
		  
216
		  # correct and lock width of some columns
217
		  done = 1
218
		  col_fix = []
219
		  col_width.each_with_index do |w,i| 
220
		    if w > col_width_max[i]
221
			  col_width[i] = col_width_max[i]
222
			  col_fix[i] = 1
223
			  done = 0
224
			elsif w < word_width_max[i]
225
			  col_width[i] = word_width_max[i]
226
			  col_fix[i] = 1
227
			  done = 0
228
			else
229
			  col_fix[i] = 0
230
			end
231
		  end
232

  
233
		  # iterate while need to correct and lock coluns width
234
		  while done == 0 
235
			# calculate free & locked columns width
236
		    done = 1
237
			fix_col_width = 0
238
			free_col_width = 0
239
			col_width.each_with_index do |w,i|
240
			  if col_fix[i] == 1
241
				fix_col_width += w
242
			  else
243
				free_col_width += w
244
			  end
245
			end
246
			
247
			# calculate column normalizing ratio
248
			if free_col_width == 0
249
			  ratio = table_width / col_width.inject(0) {|s,w| s += w}
250
			else
251
			  ratio = (table_width - fix_col_width) / free_col_width
252
			end
253

  
254
			# correct columns width
255
			col_width.each_with_index do |w,i| 
256
			  if col_fix[i] == 0
257
				col_width[i] = w * ratio
258
				
259
				# check if column width less then max word width
260
				if col_width[i] < word_width_max[i]
261
				  col_width[i] = word_width_max[i]
262
				  col_fix[i] = 1
263
				  done = 0
264
				elsif col_width[i] > col_width_max[i]
265
				  col_width[i] = col_width_max[i]
266
				  col_fix[i] = 1
267
				  done = 0
268
				end
269
			  end
270
			end
271
		  end 
272
		  
273
		  # calculate columns alignment
274
		  @col_align = col_width.map {|x| x > table_width / (col_width.count + 1) ? 'L' : 'C'}
275
		  
276
		  col_width
277
	  end
278

  
279
	  def render_table_header(pdf, query, col_width, row_height, col_id_width, table_width)
280
        # headers
281
        pdf.SetFontStyle('B',8)
282
        pdf.SetFillColor(230, 230, 230)
283

  
284
        # render it background to find the max height used
285
        base_x = pdf.GetX
286
        base_y = pdf.GetY
287
        max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
288
        pdf.Rect(base_x, base_y, table_width + col_id_width, max_height, 'FD');
289
        pdf.SetXY(base_x, base_y);
290

  
291
        # write the cells on page
292
        pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
293
        issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
294
        issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
295
        pdf.SetY(base_y + max_height);
296

  
297
        # rows
298
        pdf.SetFontStyle('',8)
299
        pdf.SetFillColor(255, 255, 255)
300
	  end
301
	  # ---
302
	  
147 303
      # Returns a PDF string of a list of issues
148 304
      def issues_to_pdf(issues, project, query)
149 305
        pdf = ITCPDF.new(current_language)
......
161 317
        right_margin  = 10
162 318
        bottom_margin = 20
163 319
        col_id_width  = 10
164
        row_height    = 5
320
        row_height    = 4
165 321

  
166 322
        # column widths
167 323
        table_width = page_width - right_margin - 10  # fixed left margin
168 324
        col_width = []
169 325
        unless query.columns.empty?
170
          col_width = query.columns.collect do |c|
171
            (c.name == :subject || (c.is_a?(QueryCustomFieldColumn) &&
172
              ['string', 'text'].include?(c.custom_field.field_format))) ? 4.0 : 1.0
173
          end
174
          ratio = (table_width - col_id_width) / col_width.inject(0) {|s,w| s += w}
175
          col_width = col_width.collect {|w| w * ratio}
326
		  col_width = calc_col_width(issues, query, table_width - col_id_width, pdf)
327
		  table_width = col_width.inject(0) {|s,v| s += v}
176 328
        end
177 329

  
178 330
        # title
......
180 332
        pdf.RDMCell(190,10, title)
181 333
        pdf.Ln
182 334

  
183
        # headers
184
        pdf.SetFontStyle('B',8)
185
        pdf.SetFillColor(230, 230, 230)
186

  
187
        # render it background to find the max height used
188
        base_x = pdf.GetX
189
        base_y = pdf.GetY
190
        max_height = issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
191
        pdf.Rect(base_x, base_y, table_width, max_height, 'FD');
192
        pdf.SetXY(base_x, base_y);
193

  
194
        # write the cells on page
195
        pdf.RDMCell(col_id_width, row_height, "#", "T", 0, 'C', 1)
196
        issues_to_pdf_write_cells(pdf, query.columns, col_width, row_height, true)
197
        issues_to_pdf_draw_borders(pdf, base_x, base_y, base_y + max_height, col_id_width, col_width)
198
        pdf.SetY(base_y + max_height);
199

  
200
        # rows
201
        pdf.SetFontStyle('',8)
202
        pdf.SetFillColor(255, 255, 255)
335
		render_table_header(pdf, query, col_width, row_height, col_id_width, table_width)
336
		
203 337
        previous_group = false
204 338
        issue_list(issues) do |issue, level|
205 339
          if query.grouped? &&
206 340
               (group = query.group_by_column.value(issue)) != previous_group
207
            pdf.SetFontStyle('B',9)
208
            pdf.RDMCell(277, row_height,
209
              (group.blank? ? 'None' : group.to_s) + " (#{query.issue_count_by_group[group]})",
341
            pdf.SetFontStyle('B',10)
342
            pdf.RDMCell(table_width + col_id_width, row_height * 2,
343
              (group.blank? ? '  None' : '  ' + group.to_s) + " (#{query.issue_count_by_group[group]})",
210 344
              1, 1, 'L')
211 345
            pdf.SetFontStyle('',8)
212 346
            previous_group = group
213 347
          end
214
          # fetch all the row values
215
          col_values = query.columns.collect do |column|
216
            s = if column.is_a?(QueryCustomFieldColumn)
217
              cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
218
              show_value(cv)
219
            else
220
              value = issue.send(column.name)
221
              if column.name == :subject
222
                value = "  " * level + value
223
              end
224
              if value.is_a?(Date)
225
                format_date(value)
226
              elsif value.is_a?(Time)
227
                format_time(value)
228
              else
229
                value
230
              end
231
            end
232
            s.to_s
233
          end
348

  
349
          # fetch row values
350
		  col_values = fetch_row_values(issue, query, level)
234 351

  
235 352
          # render it off-page to find the max height used
236 353
          base_x = pdf.GetX
......
243 360
          space_left = page_height - base_y - bottom_margin
244 361
          if max_height > space_left
245 362
            pdf.AddPage("L")
363
			render_table_header(pdf, query, col_width, row_height, col_id_width, table_width)
246 364
            base_x = pdf.GetX
247 365
            base_y = pdf.GetY
248 366
          end
......
264 382
      # Renders MultiCells and returns the maximum height used
265 383
      def issues_to_pdf_write_cells(pdf, col_values, col_widths,
266 384
                                    row_height, head=false)
267
        base_y = pdf.GetY
385
		@col_align ||= []
386
		base_y = pdf.GetY
268 387
        max_height = row_height
269 388
        col_values.each_with_index do |column, i|
270 389
          col_x = pdf.GetX
271 390
          if head == true
272
            pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'L', 1)
391
            pdf.RDMMultiCell(col_widths[i], row_height, column.caption, "T", 'C', 1)
273 392
          else
274
            pdf.RDMMultiCell(col_widths[i], row_height, column, "T", 'L', 1)
393
			@col_align[i] = 'L' if @col_align[i].nil?
394
            pdf.RDMMultiCell(col_widths[i], row_height, column, "T", @col_align[i], 1)
275 395
          end
276 396
          max_height = (pdf.GetY - base_y) if (pdf.GetY - base_y) > max_height
277 397
          pdf.SetXY(col_x + col_widths[i], base_y);
(9-9/10)