annotate .svn/pristine/f3/f3dbbbaddf62f83b48454d2fa4ac89eb045ea4e9.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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