annotate .svn/pristine/f3/f3dbbbaddf62f83b48454d2fa4ac89eb045ea4e9.svn-base @ 1295:622f24f53b42 redmine-2.3

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