annotate lib/redmine/helpers/gantt.rb @ 1298:4f746d8966dd redmine_2.3_integration

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