annotate lib/redmine/helpers/.svn/text-base/gantt.rb.svn-base @ 904:0a8317a50fa0 redmine-1.1

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