##// END OF EJS Templates
Gantt code cleaning....
Jean-Philippe Lang -
r4413:8a86b2d2bc8b
parent child
Show More
@@ -1,882 +1,837
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Helpers
20 20 # Simple class to handle gantt chart data
21 21 class Gantt
22 22 include ERB::Util
23 23 include Redmine::I18n
24 24
25 25 # :nodoc:
26 26 # Some utility methods for the PDF export
27 27 class PDF
28 28 MaxCharactorsForSubject = 45
29 29 TotalWidth = 280
30 30 LeftPaneWidth = 100
31 31
32 32 def self.right_pane_width
33 33 TotalWidth - LeftPaneWidth
34 34 end
35 35 end
36 36
37 37 attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows
38 38 attr_accessor :query
39 39 attr_accessor :project
40 40 attr_accessor :view
41 41
42 42 def initialize(options={})
43 43 options = options.dup
44 44
45 45 if options[:year] && options[:year].to_i >0
46 46 @year_from = options[:year].to_i
47 47 if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12
48 48 @month_from = options[:month].to_i
49 49 else
50 50 @month_from = 1
51 51 end
52 52 else
53 53 @month_from ||= Date.today.month
54 54 @year_from ||= Date.today.year
55 55 end
56 56
57 57 zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i
58 58 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
59 59 months = (options[:months] || User.current.pref[:gantt_months]).to_i
60 60 @months = (months > 0 && months < 25) ? months : 6
61 61
62 62 # Save gantt parameters as user preference (zoom and months count)
63 63 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
64 64 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
65 65 User.current.preference.save
66 66 end
67 67
68 68 @date_from = Date.civil(@year_from, @month_from, 1)
69 69 @date_to = (@date_from >> @months) - 1
70 70
71 71 @subjects = ''
72 72 @lines = ''
73 73 @number_of_rows = nil
74 74
75 75 @truncated = false
76 76 if options.has_key?(:max_rows)
77 77 @max_rows = options[:max_rows]
78 78 else
79 79 @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i
80 80 end
81 81 end
82 82
83 83 def common_params
84 84 { :controller => 'gantts', :action => 'show', :project_id => @project }
85 85 end
86 86
87 87 def params
88 88 common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months })
89 89 end
90 90
91 91 def params_previous
92 92 common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months })
93 93 end
94 94
95 95 def params_next
96 96 common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months })
97 97 end
98 98
99 99 ### Extracted from the HTML view/helpers
100 100 # Returns the number of rows that will be rendered on the Gantt chart
101 101 def number_of_rows
102 102 return @number_of_rows if @number_of_rows
103 103
104 104 rows = if @project
105 105 number_of_rows_on_project(@project)
106 106 else
107 107 Project.roots.visible.has_module('issue_tracking').inject(0) do |total, project|
108 108 total += number_of_rows_on_project(project)
109 109 end
110 110 end
111 111
112 112 rows > @max_rows ? @max_rows : rows
113 113 end
114 114
115 115 # Returns the number of rows that will be used to list a project on
116 116 # the Gantt chart. This will recurse for each subproject.
117 117 def number_of_rows_on_project(project)
118 118 # Remove the project requirement for Versions because it will
119 119 # restrict issues to only be on the current project. This
120 120 # ends up missing issues which are assigned to shared versions.
121 121 @query.project = nil if @query.project
122 122
123 123 # One Root project
124 124 count = 1
125 125 # Issues without a Version
126 126 count += project.issues.for_gantt.without_version.with_query(@query).count
127 127
128 128 # Versions
129 129 count += project.versions.count
130 130
131 131 # Issues on the Versions
132 132 project.versions.each do |version|
133 133 count += version.fixed_issues.for_gantt.with_query(@query).count
134 134 end
135 135
136 136 # Subprojects
137 137 project.children.visible.has_module('issue_tracking').each do |subproject|
138 138 count += number_of_rows_on_project(subproject)
139 139 end
140 140
141 141 count
142 142 end
143 143
144 144 # Renders the subjects of the Gantt chart, the left side.
145 145 def subjects(options={})
146 146 render(options.merge(:only => :subjects)) unless @subjects_rendered
147 147 @subjects
148 148 end
149 149
150 150 # Renders the lines of the Gantt chart, the right side
151 151 def lines(options={})
152 152 render(options.merge(:only => :lines)) unless @lines_rendered
153 153 @lines
154 154 end
155 155
156 156 def render(options={})
157 157 options = {:indent => 4, :render => :subject, :format => :html}.merge(options)
158 158
159 159 @subjects = '' unless options[:only] == :lines
160 160 @lines = '' unless options[:only] == :subjects
161 161 @number_of_rows = 0
162 162
163 163 if @project
164 164 render_project(@project, options)
165 165 else
166 166 Project.roots.visible.has_module('issue_tracking').each do |project|
167 167 render_project(project, options)
168 168 break if abort?
169 169 end
170 170 end
171 171
172 172 @subjects_rendered = true unless options[:only] == :lines
173 173 @lines_rendered = true unless options[:only] == :subjects
174 174
175 175 render_end(options)
176 176 end
177 177
178 178 def render_project(project, options={})
179 179 options[:top] = 0 unless options.key? :top
180 180 options[:indent_increment] = 20 unless options.key? :indent_increment
181 181 options[:top_increment] = 20 unless options.key? :top_increment
182 182
183 183 subject_for_project(project, options) unless options[:only] == :lines
184 184 line_for_project(project, options) unless options[:only] == :subjects
185 185
186 186 options[:top] += options[:top_increment]
187 187 options[:indent] += options[:indent_increment]
188 188 @number_of_rows += 1
189 189 return if abort?
190 190
191 191 # Second, Issues without a version
192 192 issues = project.issues.for_gantt.without_version.with_query(@query).all(:limit => current_limit)
193 193 sort_issues!(issues)
194 194 if issues
195 195 render_issues(issues, options)
196 196 return if abort?
197 197 end
198 198
199 199 # Third, Versions
200 200 project.versions.sort.each do |version|
201 201 render_version(version, options)
202 202 return if abort?
203 203 end
204 204
205 205 # Fourth, subprojects
206 206 project.children.visible.has_module('issue_tracking').each do |project|
207 207 render_project(project, options)
208 208 return if abort?
209 209 end unless project.leaf?
210 210
211 211 # Remove indent to hit the next sibling
212 212 options[:indent] -= options[:indent_increment]
213 213 end
214 214
215 215 def render_issues(issues, options={})
216 216 issues.each do |i|
217 217 subject_for_issue(i, options) unless options[:only] == :lines
218 218 line_for_issue(i, options) unless options[:only] == :subjects
219 219
220 220 options[:top] += options[:top_increment]
221 221 @number_of_rows += 1
222 222 return if abort?
223 223 end
224 224 end
225 225
226 226 def render_version(version, options={})
227 227 # Version header
228 228 subject_for_version(version, options) unless options[:only] == :lines
229 229 line_for_version(version, options) unless options[:only] == :subjects
230 230
231 231 options[:top] += options[:top_increment]
232 232 @number_of_rows += 1
233 233 return if abort?
234 234
235 235 # Remove the project requirement for Versions because it will
236 236 # restrict issues to only be on the current project. This
237 237 # ends up missing issues which are assigned to shared versions.
238 238 @query.project = nil if @query.project
239 239
240 240 issues = version.fixed_issues.for_gantt.with_query(@query).all(:limit => current_limit)
241 241 if issues
242 242 sort_issues!(issues)
243 243 # Indent issues
244 244 options[:indent] += options[:indent_increment]
245 245 render_issues(issues, options)
246 246 options[:indent] -= options[:indent_increment]
247 247 end
248 248 end
249 249
250 250 def render_end(options={})
251 251 case options[:format]
252 252 when :pdf
253 253 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
254 254 end
255 255 end
256 256
257 257 def subject_for_project(project, options)
258 258 case options[:format]
259 259 when :html
260 output = ''
261
262 output << "<div class='project-name' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
263 if project.is_a? Project
264 output << "<span class='icon icon-projects #{project.overdue? ? 'project-overdue' : ''}'>"
265 output << view.link_to_project(project)
266 output << '</span>'
267 else
268 ActiveRecord::Base.logger.debug "Gantt#subject_for_project was not given a project"
269 ''
270 end
271 output << "</small></div>"
272 @subjects << output
273 output
260 subject = "<span class='icon icon-projects #{project.overdue? ? 'project-overdue' : ''}'>"
261 subject << view.link_to_project(project)
262 subject << '</span>'
263 html_subject(options, subject, :css => "project-name")
274 264 when :image
275
276 options[:image].fill('black')
277 options[:image].stroke('transparent')
278 options[:image].stroke_width(1)
279 options[:image].text(options[:indent], options[:top] + 2, project.name)
265 image_subject(options, project.name)
280 266 when :pdf
281 267 pdf_new_page?(options)
282 options[:pdf].SetY(options[:top])
283 options[:pdf].SetX(15)
284
285 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
286 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{project.name}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
287
288 options[:pdf].SetY(options[:top])
289 options[:pdf].SetX(options[:subject_width])
290 options[:pdf].Cell(options[:g_width], 5, "", "LR")
268 pdf_subject(options, project.name)
291 269 end
292 270 end
293 271
294 272 def line_for_project(project, options)
295 273 # Skip versions that don't have a start_date or due date
296 274 if project.is_a?(Project) && project.start_date && project.due_date
297 275 options[:zoom] ||= 1
298 276 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
299 277
300 278 coords = coordinates(project.start_date, project.due_date, project.completed_percent(:include_subprojects => true), options[:zoom])
301 279 label = "#{h project } #{h project.completed_percent(:include_subprojects => true).to_i.to_s}%"
302 280
303 281 case options[:format]
304 282 when :html
305 283 html_task(options, coords, :css => "project task", :label => label, :markers => true)
306 284 when :image
307 285 image_task(options, coords, :label => label, :markers => true, :height => 3)
308 286 when :pdf
309 287 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
310 288 end
311 289 else
312 290 ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date"
313 291 ''
314 292 end
315 293 end
316 294
317 295 def subject_for_version(version, options)
318 296 case options[:format]
319 297 when :html
320 output = ''
321 output << "<div class='version-name' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
322 if version.is_a? Version
323 output << "<span class='icon icon-package #{version.behind_schedule? ? 'version-behind-schedule' : ''} #{version.overdue? ? 'version-overdue' : ''}'>"
324 output << view.link_to_version(version)
325 output << '</span>'
326 else
327 ActiveRecord::Base.logger.debug "Gantt#subject_for_version was not given a version"
328 ''
329 end
330 output << "</small></div>"
331 @subjects << output
332 output
298 subject = "<span class='icon icon-package #{version.behind_schedule? ? 'version-behind-schedule' : ''} #{version.overdue? ? 'version-overdue' : ''}'>"
299 subject << view.link_to_version(version)
300 subject << '</span>'
301 html_subject(options, subject, :css => "version-name")
333 302 when :image
334 options[:image].fill('black')
335 options[:image].stroke('transparent')
336 options[:image].stroke_width(1)
337 options[:image].text(options[:indent], options[:top] + 2, version.to_s_with_project)
303 image_subject(options, version.to_s_with_project)
338 304 when :pdf
339 305 pdf_new_page?(options)
340 options[:pdf].SetY(options[:top])
341 options[:pdf].SetX(15)
342
343 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
344 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{version.to_s_with_project}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
345
346 options[:pdf].SetY(options[:top])
347 options[:pdf].SetX(options[:subject_width])
348 options[:pdf].Cell(options[:g_width], 5, "", "LR")
306 pdf_subject(options, version.to_s_with_project)
349 307 end
350 308 end
351 309
352 310 def line_for_version(version, options)
353 311 # Skip versions that don't have a start_date
354 312 if version.is_a?(Version) && version.start_date && version.due_date
355 313 options[:zoom] ||= 1
356 314 options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom]
357 315
358 316 coords = coordinates(version.fixed_issues.minimum('start_date'), version.due_date, version.completed_pourcent, options[:zoom])
359 317 label = "#{h version } #{h version.completed_pourcent.to_i.to_s}%"
360 318 label = h("#{version.project} -") + label unless @project && @project == version.project
361 319
362 320 case options[:format]
363 321 when :html
364 322 html_task(options, coords, :css => "version task", :label => label, :markers => true)
365 323 when :image
366 324 image_task(options, coords, :label => label, :markers => true, :height => 3)
367 325 when :pdf
368 326 pdf_task(options, coords, :label => label, :markers => true, :height => 0.8)
369 327 end
370 328 else
371 329 ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date"
372 330 ''
373 331 end
374 332 end
375 333
376 334 def subject_for_issue(issue, options)
377 335 case options[:format]
378 336 when :html
379 output = ''
380 output << "<div class='tooltip'>"
381 output << "<div class='issue-subject' style='position: absolute;line-height:1.2em;height:16px;top:#{options[:top]}px;left:#{options[:indent]}px;overflow:hidden;'><small> "
382 if issue.is_a? Issue
383 css_classes = []
384 css_classes << 'issue-overdue' if issue.overdue?
385 css_classes << 'issue-behind-schedule' if issue.behind_schedule?
386 css_classes << 'icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
387
388 if issue.assigned_to.present?
389 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
390 output << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string)
391 end
392 output << "<span class='#{css_classes.join(' ')}'>"
393 output << view.link_to_issue(issue)
394 output << '</span>'
395 else
396 ActiveRecord::Base.logger.debug "Gantt#subject_for_issue was not given an issue"
397 ''
398 end
399 output << "</small></div>"
400
401 # Tooltip
402 if issue.is_a? Issue
403 output << "<span class='tip' style='position: absolute;top:#{ options[:top].to_i + 16 }px;left:#{ options[:indent].to_i + 20 }px;'>"
404 output << view.render_issue_tooltip(issue)
405 output << "</span>"
337 css_classes = ''
338 css_classes << ' issue-overdue' if issue.overdue?
339 css_classes << ' issue-behind-schedule' if issue.behind_schedule?
340 css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to
341
342 subject = "<span class='#{css_classes}'>"
343 if issue.assigned_to.present?
344 assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
345 subject << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string)
406 346 end
407
408 output << "</div>"
409 @subjects << output
410 output
347 subject << view.link_to_issue(issue)
348 subject << '</span>'
349 html_subject(options, subject, :css => "issue-subject")
411 350 when :image
412 options[:image].fill('black')
413 options[:image].stroke('transparent')
414 options[:image].stroke_width(1)
415 options[:image].text(options[:indent], options[:top] + 2, issue.subject)
351 image_subject(options, issue.subject)
416 352 when :pdf
417 353 pdf_new_page?(options)
418 options[:pdf].SetY(options[:top])
419 options[:pdf].SetX(15)
420
421 char_limit = PDF::MaxCharactorsForSubject - options[:indent]
422 options[:pdf].Cell(options[:subject_width]-15, 5, (" " * options[:indent]) +"#{issue.tracker} #{issue.id}: #{issue.subject}".sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
423
424 options[:pdf].SetY(options[:top])
425 options[:pdf].SetX(options[:subject_width])
426 options[:pdf].Cell(options[:g_width], 5, "", "LR")
354 pdf_subject(options, issue.subject)
427 355 end
428 356 end
429 357
430 358 def line_for_issue(issue, options)
431 359 # Skip issues that don't have a due_before (due_date or version's due_date)
432 360 if issue.is_a?(Issue) && issue.due_before
433 361 coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom])
434 362 label = "#{ issue.status.name } #{ issue.done_ratio }%"
435 363
436 364 case options[:format]
437 365 when :html
438 366 html_task(options, coords, :css => "task " + (issue.leaf? ? 'leaf' : 'parent'), :label => label, :issue => issue)
439 367 when :image
440 368 image_task(options, coords, :label => label)
441 369 when :pdf
442 370 pdf_task(options, coords, :label => label)
443 371 end
444 372 else
445 373 ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before"
446 374 ''
447 375 end
448 376 end
449 377
450 378 # Generates a gantt image
451 379 # Only defined if RMagick is avalaible
452 380 def to_image(format='PNG')
453 381 date_to = (@date_from >> @months)-1
454 382 show_weeks = @zoom > 1
455 383 show_days = @zoom > 2
456 384
457 385 subject_width = 400
458 386 header_heigth = 18
459 387 # width of one day in pixels
460 388 zoom = @zoom*2
461 389 g_width = (@date_to - @date_from + 1)*zoom
462 390 g_height = 20 * number_of_rows + 30
463 391 headers_heigth = (show_weeks ? 2*header_heigth : header_heigth)
464 392 height = g_height + headers_heigth
465 393
466 394 imgl = Magick::ImageList.new
467 395 imgl.new_image(subject_width+g_width+1, height)
468 396 gc = Magick::Draw.new
469 397
470 398 # Subjects
471 399 gc.stroke('transparent')
472 400 subjects(:image => gc, :top => (headers_heigth + 20), :indent => 4, :format => :image)
473 401
474 402 # Months headers
475 403 month_f = @date_from
476 404 left = subject_width
477 405 @months.times do
478 406 width = ((month_f >> 1) - month_f) * zoom
479 407 gc.fill('white')
480 408 gc.stroke('grey')
481 409 gc.stroke_width(1)
482 410 gc.rectangle(left, 0, left + width, height)
483 411 gc.fill('black')
484 412 gc.stroke('transparent')
485 413 gc.stroke_width(1)
486 414 gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}")
487 415 left = left + width
488 416 month_f = month_f >> 1
489 417 end
490 418
491 419 # Weeks headers
492 420 if show_weeks
493 421 left = subject_width
494 422 height = header_heigth
495 423 if @date_from.cwday == 1
496 424 # date_from is monday
497 425 week_f = date_from
498 426 else
499 427 # find next monday after date_from
500 428 week_f = @date_from + (7 - @date_from.cwday + 1)
501 429 width = (7 - @date_from.cwday + 1) * zoom
502 430 gc.fill('white')
503 431 gc.stroke('grey')
504 432 gc.stroke_width(1)
505 433 gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1)
506 434 left = left + width
507 435 end
508 436 while week_f <= date_to
509 437 width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom
510 438 gc.fill('white')
511 439 gc.stroke('grey')
512 440 gc.stroke_width(1)
513 441 gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1)
514 442 gc.fill('black')
515 443 gc.stroke('transparent')
516 444 gc.stroke_width(1)
517 445 gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s)
518 446 left = left + width
519 447 week_f = week_f+7
520 448 end
521 449 end
522 450
523 451 # Days details (week-end in grey)
524 452 if show_days
525 453 left = subject_width
526 454 height = g_height + header_heigth - 1
527 455 wday = @date_from.cwday
528 456 (date_to - @date_from + 1).to_i.times do
529 457 width = zoom
530 458 gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white')
531 459 gc.stroke('grey')
532 460 gc.stroke_width(1)
533 461 gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1)
534 462 left = left + width
535 463 wday = wday + 1
536 464 wday = 1 if wday > 7
537 465 end
538 466 end
539 467
540 468 # border
541 469 gc.fill('transparent')
542 470 gc.stroke('grey')
543 471 gc.stroke_width(1)
544 472 gc.rectangle(0, 0, subject_width+g_width, headers_heigth)
545 473 gc.stroke('black')
546 474 gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1)
547 475
548 476 # content
549 477 top = headers_heigth + 20
550 478
551 479 gc.stroke('transparent')
552 480 lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image)
553 481
554 482 # today red line
555 483 if Date.today >= @date_from and Date.today <= date_to
556 484 gc.stroke('red')
557 485 x = (Date.today-@date_from+1)*zoom + subject_width
558 486 gc.line(x, headers_heigth, x, headers_heigth + g_height-1)
559 487 end
560 488
561 489 gc.draw(imgl)
562 490 imgl.format = format
563 491 imgl.to_blob
564 492 end if Object.const_defined?(:Magick)
565 493
566 494 def to_pdf
567 495 pdf = ::Redmine::Export::PDF::IFPDF.new(current_language)
568 496 pdf.SetTitle("#{l(:label_gantt)} #{project}")
569 497 pdf.AliasNbPages
570 498 pdf.footer_date = format_date(Date.today)
571 499 pdf.AddPage("L")
572 500 pdf.SetFontStyle('B',12)
573 501 pdf.SetX(15)
574 502 pdf.Cell(PDF::LeftPaneWidth, 20, project.to_s)
575 503 pdf.Ln
576 504 pdf.SetFontStyle('B',9)
577 505
578 506 subject_width = PDF::LeftPaneWidth
579 507 header_heigth = 5
580 508
581 509 headers_heigth = header_heigth
582 510 show_weeks = false
583 511 show_days = false
584 512
585 513 if self.months < 7
586 514 show_weeks = true
587 515 headers_heigth = 2*header_heigth
588 516 if self.months < 3
589 517 show_days = true
590 518 headers_heigth = 3*header_heigth
591 519 end
592 520 end
593 521
594 522 g_width = PDF.right_pane_width
595 523 zoom = (g_width) / (self.date_to - self.date_from + 1)
596 524 g_height = 120
597 525 t_height = g_height + headers_heigth
598 526
599 527 y_start = pdf.GetY
600 528
601 529 # Months headers
602 530 month_f = self.date_from
603 531 left = subject_width
604 532 height = header_heigth
605 533 self.months.times do
606 534 width = ((month_f >> 1) - month_f) * zoom
607 535 pdf.SetY(y_start)
608 536 pdf.SetX(left)
609 537 pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C")
610 538 left = left + width
611 539 month_f = month_f >> 1
612 540 end
613 541
614 542 # Weeks headers
615 543 if show_weeks
616 544 left = subject_width
617 545 height = header_heigth
618 546 if self.date_from.cwday == 1
619 547 # self.date_from is monday
620 548 week_f = self.date_from
621 549 else
622 550 # find next monday after self.date_from
623 551 week_f = self.date_from + (7 - self.date_from.cwday + 1)
624 552 width = (7 - self.date_from.cwday + 1) * zoom-1
625 553 pdf.SetY(y_start + header_heigth)
626 554 pdf.SetX(left)
627 555 pdf.Cell(width + 1, height, "", "LTR")
628 556 left = left + width+1
629 557 end
630 558 while week_f <= self.date_to
631 559 width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom
632 560 pdf.SetY(y_start + header_heigth)
633 561 pdf.SetX(left)
634 562 pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C")
635 563 left = left + width
636 564 week_f = week_f+7
637 565 end
638 566 end
639 567
640 568 # Days headers
641 569 if show_days
642 570 left = subject_width
643 571 height = header_heigth
644 572 wday = self.date_from.cwday
645 573 pdf.SetFontStyle('B',7)
646 574 (self.date_to - self.date_from + 1).to_i.times do
647 575 width = zoom
648 576 pdf.SetY(y_start + 2 * header_heigth)
649 577 pdf.SetX(left)
650 578 pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C")
651 579 left = left + width
652 580 wday = wday + 1
653 581 wday = 1 if wday > 7
654 582 end
655 583 end
656 584
657 585 pdf.SetY(y_start)
658 586 pdf.SetX(15)
659 587 pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1)
660 588
661 589 # Tasks
662 590 top = headers_heigth + y_start
663 591 options = {
664 592 :top => top,
665 593 :zoom => zoom,
666 594 :subject_width => subject_width,
667 595 :g_width => g_width,
668 596 :indent => 0,
669 597 :indent_increment => 5,
670 598 :top_increment => 5,
671 599 :format => :pdf,
672 600 :pdf => pdf
673 601 }
674 602 render(options)
675 603 pdf.Output
676 604 end
677 605
678 606 private
679 607
680 608 def coordinates(start_date, end_date, progress, zoom=nil)
681 609 zoom ||= @zoom
682 610
683 611 coords = {}
684 612 if start_date && end_date && start_date < self.date_to && end_date > self.date_from
685 613 if start_date > self.date_from
686 614 coords[:start] = start_date - self.date_from
687 615 coords[:bar_start] = start_date - self.date_from
688 616 else
689 617 coords[:bar_start] = 0
690 618 end
691 619 if end_date < self.date_to
692 620 coords[:end] = end_date - self.date_from
693 621 coords[:bar_end] = end_date - self.date_from + 1
694 622 else
695 623 coords[:bar_end] = self.date_to - self.date_from + 1
696 624 end
697 625
698 626 if progress
699 627 progress_date = start_date + (end_date - start_date) * (progress / 100.0)
700 628 if progress_date > self.date_from && progress_date > start_date
701 629 if progress_date < self.date_to
702 630 coords[:bar_progress_end] = progress_date - self.date_from + 1
703 631 else
704 632 coords[:bar_progress_end] = self.date_to - self.date_from + 1
705 633 end
706 634 end
707 635
708 636 if progress_date < Date.today
709 637 late_date = [Date.today, end_date].min
710 638 if late_date > self.date_from && late_date > start_date
711 639 if late_date < self.date_to
712 640 coords[:bar_late_end] = late_date - self.date_from + 1
713 641 else
714 642 coords[:bar_late_end] = self.date_to - self.date_from + 1
715 643 end
716 644 end
717 645 end
718 646 end
719 647 end
720 648
721 649 # Transforms dates into pixels witdh
722 650 coords.keys.each do |key|
723 651 coords[key] = (coords[key] * zoom).floor
724 652 end
725 653 coords
726 654 end
727 655
728 656 # Sorts a collection of issues by start_date, due_date, id for gantt rendering
729 657 def sort_issues!(issues)
730 658 issues.sort! do |a, b|
731 659 cmp = 0
732 660 cmp = (a.start_date <=> b.start_date) if a.start_date? && b.start_date?
733 661 cmp = (a.due_date <=> b.due_date) if cmp == 0 && a.due_date? && b.due_date?
734 662 cmp = (a.id <=> b.id) if cmp == 0
735 663 cmp
736 664 end
737 665 end
738 666
739 667 def current_limit
740 668 if @max_rows
741 669 @max_rows - @number_of_rows
742 670 else
743 671 nil
744 672 end
745 673 end
746 674
747 675 def abort?
748 676 if @max_rows && @number_of_rows >= @max_rows
749 677 @truncated = true
750 678 end
751 679 end
752 680
753 681 def pdf_new_page?(options)
754 682 if options[:top] > 180
755 683 options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top])
756 684 options[:pdf].AddPage("L")
757 685 options[:top] = 15
758 686 options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1)
759 687 end
760 688 end
761 689
690 def html_subject(params, subject, options={})
691 output = "<div class=' #{options[:css] }' style='position: absolute;line-height:1.2em;height:16px;top:#{params[:top]}px;left:#{params[:indent]}px;overflow:hidden;'>"
692 output << subject
693 output << "</div>"
694 @subjects << output
695 output
696 end
697
698 def pdf_subject(params, subject, options={})
699 params[:pdf].SetY(params[:top])
700 params[:pdf].SetX(15)
701
702 char_limit = PDF::MaxCharactorsForSubject - params[:indent]
703 params[:pdf].Cell(params[:subject_width]-15, 5, (" " * params[:indent]) + subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR")
704
705 params[:pdf].SetY(params[:top])
706 params[:pdf].SetX(params[:subject_width])
707 params[:pdf].Cell(params[:g_width], 5, "", "LR")
708 end
709
710 def image_subject(params, subject, options={})
711 params[:image].fill('black')
712 params[:image].stroke('transparent')
713 params[:image].stroke_width(1)
714 params[:image].text(params[:indent], params[:top] + 2, subject)
715 end
716
762 717 def html_task(params, coords, options={})
763 718 output = ''
764 719 # Renders the task bar, with progress and late
765 720 if coords[:bar_start] && coords[:bar_end]
766 721 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_end] - coords[:bar_start] - 2}px;' class='#{options[:css]} task_todo'>&nbsp;</div>"
767 722
768 723 if coords[:bar_late_end]
769 724 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_late_end] - coords[:bar_start] - 2}px;' class='#{options[:css]} task_late'>&nbsp;</div>"
770 725 end
771 726 if coords[:bar_progress_end]
772 727 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_progress_end] - coords[:bar_start] - 2}px;' class='#{options[:css]} task_done'>&nbsp;</div>"
773 728 end
774 729 end
775 730 # Renders the markers
776 731 if options[:markers]
777 732 if coords[:start]
778 733 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:start] }px;width:15px;' class='#{options[:css]} marker starting'>&nbsp;</div>"
779 734 end
780 735 if coords[:end]
781 736 output << "<div style='top:#{ params[:top] }px;left:#{ coords[:end] }px;width:15px;' class='#{options[:css]} marker ending'>&nbsp;</div>"
782 737 end
783 738 end
784 739 # Renders the label on the right
785 740 if options[:label]
786 741 output << "<div style='top:#{ params[:top] }px;left:#{ (coords[:bar_end] || 0) + 5 }px;' class='#{options[:css]} label'>"
787 742 output << options[:label]
788 743 output << "</div>"
789 744 end
790 745 # Renders the tooltip
791 746 if options[:issue] && coords[:bar_start] && coords[:bar_end]
792 747 output << "<div class='tooltip' style='position: absolute;top:#{ params[:top] }px;left:#{ coords[:bar_start] }px;width:#{ coords[:bar_end] - coords[:bar_start] }px;height:12px;'>"
793 748 output << '<span class="tip">'
794 749 output << view.render_issue_tooltip(options[:issue])
795 750 output << "</span></div>"
796 751 end
797 752 @lines << output
798 753 output
799 754 end
800 755
801 756 def pdf_task(params, coords, options={})
802 757 height = options[:height] || 2
803 758
804 759 # Renders the task bar, with progress and late
805 760 if coords[:bar_start] && coords[:bar_end]
806 761 params[:pdf].SetY(params[:top]+1.5)
807 762 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
808 763 params[:pdf].SetFillColor(200,200,200)
809 764 params[:pdf].Cell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1)
810 765
811 766 if coords[:bar_late_end]
812 767 params[:pdf].SetY(params[:top]+1.5)
813 768 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
814 769 params[:pdf].SetFillColor(255,100,100)
815 770 params[:pdf].Cell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1)
816 771 end
817 772 if coords[:bar_progress_end]
818 773 params[:pdf].SetY(params[:top]+1.5)
819 774 params[:pdf].SetX(params[:subject_width] + coords[:bar_start])
820 775 params[:pdf].SetFillColor(90,200,90)
821 776 params[:pdf].Cell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1)
822 777 end
823 778 end
824 779 # Renders the markers
825 780 if options[:markers]
826 781 if coords[:start]
827 782 params[:pdf].SetY(params[:top] + 1)
828 783 params[:pdf].SetX(params[:subject_width] + coords[:start] - 1)
829 784 params[:pdf].SetFillColor(50,50,200)
830 785 params[:pdf].Cell(2, 2, "", 0, 0, "", 1)
831 786 end
832 787 if coords[:end]
833 788 params[:pdf].SetY(params[:top] + 1)
834 789 params[:pdf].SetX(params[:subject_width] + coords[:end] - 1)
835 790 params[:pdf].SetFillColor(50,50,200)
836 791 params[:pdf].Cell(2, 2, "", 0, 0, "", 1)
837 792 end
838 793 end
839 794 # Renders the label on the right
840 795 if options[:label]
841 796 params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5)
842 797 params[:pdf].Cell(30, 2, options[:label])
843 798 end
844 799 end
845 800
846 801 def image_task(params, coords, options={})
847 802 height = options[:height] || 6
848 803
849 804 # Renders the task bar, with progress and late
850 805 if coords[:bar_start] && coords[:bar_end]
851 806 params[:image].fill('grey')
852 807 params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_end], params[:top] - height)
853 808
854 809 if coords[:bar_late_end]
855 810 params[:image].fill('red')
856 811 params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_late_end], params[:top] - height)
857 812 end
858 813 if coords[:bar_progress_end]
859 814 params[:image].fill('green')
860 815 params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_progress_end], params[:top] - height)
861 816 end
862 817 end
863 818 # Renders the markers
864 819 if options[:markers]
865 820 if coords[:start]
866 821 params[:image].fill('blue')
867 822 params[:image].rectangle(params[:subject_width] + coords[:start], params[:top] + 1, params[:subject_width] + coords[:start] + 4, params[:top] - 4)
868 823 end
869 824 if coords[:end]
870 825 params[:image].fill('blue')
871 826 params[:image].rectangle(params[:subject_width] + coords[:end], params[:top] + 1, params[:subject_width] + coords[:end] + 4, params[:top] - 4)
872 827 end
873 828 end
874 829 # Renders the label on the right
875 830 if options[:label]
876 831 params[:image].fill('black')
877 832 params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5,params[:top] + 1, options[:label])
878 833 end
879 834 end
880 835 end
881 836 end
882 837 end
General Comments 0
You need to be logged in to leave comments. Login now