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