comparison lib/redmine/helpers/gantt.rb @ 514:7eba09d624db live

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