annotate lib/redmine/helpers/gantt.rb @ 1459:cf78a7ade302 luisf

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