Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2008 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module Redmine Chris@0: module Helpers Chris@0: # Simple class to handle gantt chart data Chris@0: class Gantt chris@22: include ERB::Util chris@22: include Redmine::I18n chris@22: chris@22: # :nodoc: chris@22: # Some utility methods for the PDF export chris@22: class PDF chris@22: MaxCharactorsForSubject = 45 chris@22: TotalWidth = 280 chris@22: LeftPaneWidth = 100 chris@22: chris@22: def self.right_pane_width chris@22: TotalWidth - LeftPaneWidth chris@22: end chris@22: end chris@22: Chris@117: attr_reader :year_from, :month_from, :date_from, :date_to, :zoom, :months, :truncated, :max_rows chris@22: attr_accessor :query chris@22: attr_accessor :project chris@22: attr_accessor :view chris@22: Chris@0: def initialize(options={}) Chris@0: options = options.dup Chris@0: Chris@0: if options[:year] && options[:year].to_i >0 Chris@0: @year_from = options[:year].to_i Chris@0: if options[:month] && options[:month].to_i >=1 && options[:month].to_i <= 12 Chris@0: @month_from = options[:month].to_i Chris@0: else Chris@0: @month_from = 1 Chris@0: end Chris@0: else Chris@0: @month_from ||= Date.today.month Chris@0: @year_from ||= Date.today.year Chris@0: end Chris@0: Chris@0: zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i Chris@0: @zoom = (zoom > 0 && zoom < 5) ? zoom : 2 Chris@0: months = (options[:months] || User.current.pref[:gantt_months]).to_i Chris@0: @months = (months > 0 && months < 25) ? months : 6 Chris@0: Chris@0: # Save gantt parameters as user preference (zoom and months count) Chris@0: if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months])) Chris@0: User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months Chris@0: User.current.preference.save Chris@0: end Chris@0: Chris@0: @date_from = Date.civil(@year_from, @month_from, 1) Chris@0: @date_to = (@date_from >> @months) - 1 Chris@117: Chris@117: @subjects = '' Chris@117: @lines = '' Chris@117: @number_of_rows = nil Chris@117: Chris@117: @issue_ancestors = [] Chris@117: Chris@117: @truncated = false Chris@117: if options.has_key?(:max_rows) Chris@117: @max_rows = options[:max_rows] Chris@117: else Chris@117: @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i Chris@117: end Chris@0: end chris@22: chris@22: def common_params chris@22: { :controller => 'gantts', :action => 'show', :project_id => @project } Chris@0: end Chris@0: Chris@0: def params chris@22: common_params.merge({ :zoom => zoom, :year => year_from, :month => month_from, :months => months }) Chris@0: end Chris@0: Chris@0: def params_previous chris@22: common_params.merge({:year => (date_from << months).year, :month => (date_from << months).month, :zoom => zoom, :months => months }) Chris@0: end Chris@0: Chris@0: def params_next chris@22: common_params.merge({:year => (date_from >> months).year, :month => (date_from >> months).month, :zoom => zoom, :months => months }) Chris@0: end chris@22: chris@22: ### Extracted from the HTML view/helpers chris@22: # Returns the number of rows that will be rendered on the Gantt chart chris@22: def number_of_rows Chris@117: return @number_of_rows if @number_of_rows Chris@117: Chris@117: rows = if @project Chris@117: number_of_rows_on_project(@project) chris@22: else Chris@117: Project.roots.visible.has_module('issue_tracking').inject(0) do |total, project| chris@22: total += number_of_rows_on_project(project) chris@22: end chris@22: end Chris@117: Chris@117: rows > @max_rows ? @max_rows : rows chris@22: end chris@22: chris@22: # Returns the number of rows that will be used to list a project on chris@22: # the Gantt chart. This will recurse for each subproject. chris@22: def number_of_rows_on_project(project) chris@22: # Remove the project requirement for Versions because it will chris@22: # restrict issues to only be on the current project. This chris@22: # ends up missing issues which are assigned to shared versions. chris@22: @query.project = nil if @query.project chris@22: chris@22: # One Root project chris@22: count = 1 chris@22: # Issues without a Version chris@22: count += project.issues.for_gantt.without_version.with_query(@query).count chris@22: chris@22: # Versions chris@22: count += project.versions.count chris@22: chris@22: # Issues on the Versions chris@22: project.versions.each do |version| chris@22: count += version.fixed_issues.for_gantt.with_query(@query).count chris@22: end chris@22: chris@22: # Subprojects Chris@117: project.children.visible.has_module('issue_tracking').each do |subproject| chris@22: count += number_of_rows_on_project(subproject) chris@22: end chris@22: chris@22: count chris@22: end chris@22: chris@22: # Renders the subjects of the Gantt chart, the left side. chris@22: def subjects(options={}) Chris@117: render(options.merge(:only => :subjects)) unless @subjects_rendered Chris@117: @subjects chris@22: end chris@22: chris@22: # Renders the lines of the Gantt chart, the right side chris@22: def lines(options={}) Chris@117: render(options.merge(:only => :lines)) unless @lines_rendered Chris@117: @lines Chris@117: end Chris@117: Chris@117: def render(options={}) Chris@117: options = {:indent => 4, :render => :subject, :format => :html}.merge(options) Chris@117: Chris@117: @subjects = '' unless options[:only] == :lines Chris@117: @lines = '' unless options[:only] == :subjects Chris@117: @number_of_rows = 0 Chris@117: chris@22: if @project Chris@117: render_project(@project, options) chris@22: else Chris@117: Project.roots.visible.has_module('issue_tracking').each do |project| Chris@117: render_project(project, options) Chris@117: break if abort? chris@22: end chris@22: end chris@22: Chris@117: @subjects_rendered = true unless options[:only] == :lines Chris@117: @lines_rendered = true unless options[:only] == :subjects Chris@117: Chris@117: render_end(options) chris@22: end chris@22: chris@22: def render_project(project, options={}) chris@22: options[:top] = 0 unless options.key? :top chris@22: options[:indent_increment] = 20 unless options.key? :indent_increment chris@22: options[:top_increment] = 20 unless options.key? :top_increment chris@22: Chris@117: subject_for_project(project, options) unless options[:only] == :lines Chris@117: line_for_project(project, options) unless options[:only] == :subjects chris@22: chris@22: options[:top] += options[:top_increment] chris@22: options[:indent] += options[:indent_increment] Chris@117: @number_of_rows += 1 Chris@117: return if abort? chris@22: chris@22: # Second, Issues without a version Chris@117: issues = project.issues.for_gantt.without_version.with_query(@query).all(:limit => current_limit) Chris@117: sort_issues!(issues) chris@22: if issues Chris@117: render_issues(issues, options) Chris@117: return if abort? chris@22: end chris@22: chris@22: # Third, Versions chris@22: project.versions.sort.each do |version| Chris@117: render_version(version, options) Chris@117: return if abort? chris@22: end chris@22: chris@22: # Fourth, subprojects Chris@117: project.children.visible.has_module('issue_tracking').each do |project| Chris@117: render_project(project, options) Chris@117: return if abort? Chris@117: end unless project.leaf? chris@22: chris@22: # Remove indent to hit the next sibling chris@22: options[:indent] -= options[:indent_increment] chris@22: end chris@22: chris@22: def render_issues(issues, options={}) Chris@117: @issue_ancestors = [] Chris@117: chris@22: issues.each do |i| Chris@117: subject_for_issue(i, options) unless options[:only] == :lines Chris@117: line_for_issue(i, options) unless options[:only] == :subjects Chris@117: chris@22: options[:top] += options[:top_increment] Chris@117: @number_of_rows += 1 Chris@117: break if abort? chris@22: end Chris@117: Chris@117: options[:indent] -= (options[:indent_increment] * @issue_ancestors.size) chris@22: end chris@22: chris@22: def render_version(version, options={}) chris@22: # Version header Chris@117: subject_for_version(version, options) unless options[:only] == :lines Chris@117: line_for_version(version, options) unless options[:only] == :subjects chris@22: chris@22: options[:top] += options[:top_increment] Chris@117: @number_of_rows += 1 Chris@117: return if abort? Chris@117: chris@22: # Remove the project requirement for Versions because it will chris@22: # restrict issues to only be on the current project. This chris@22: # ends up missing issues which are assigned to shared versions. chris@22: @query.project = nil if @query.project chris@22: Chris@117: issues = version.fixed_issues.for_gantt.with_query(@query).all(:limit => current_limit) chris@22: if issues Chris@117: sort_issues!(issues) chris@22: # Indent issues chris@22: options[:indent] += options[:indent_increment] Chris@117: render_issues(issues, options) chris@22: options[:indent] -= options[:indent_increment] chris@22: end Chris@117: end Chris@117: Chris@117: def render_end(options={}) Chris@117: case options[:format] Chris@117: when :pdf Chris@117: options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top]) Chris@117: end chris@22: end chris@22: chris@22: def subject_for_project(project, options) chris@22: case options[:format] chris@22: when :html Chris@117: subject = "" Chris@117: subject << view.link_to_project(project) Chris@117: subject << '' Chris@117: html_subject(options, subject, :css => "project-name") chris@22: when :image Chris@117: image_subject(options, project.name) chris@22: when :pdf Chris@117: pdf_new_page?(options) Chris@117: pdf_subject(options, project.name) chris@22: end chris@22: end chris@22: chris@22: def line_for_project(project, options) chris@37: # Skip versions that don't have a start_date or due date chris@37: if project.is_a?(Project) && project.start_date && project.due_date chris@22: options[:zoom] ||= 1 chris@22: options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom] Chris@117: Chris@117: coords = coordinates(project.start_date, project.due_date, nil, options[:zoom]) Chris@117: label = h(project) chris@22: chris@22: case options[:format] chris@22: when :html Chris@117: html_task(options, coords, :css => "project task", :label => label, :markers => true) chris@22: when :image Chris@117: image_task(options, coords, :label => label, :markers => true, :height => 3) chris@22: when :pdf Chris@117: pdf_task(options, coords, :label => label, :markers => true, :height => 0.8) chris@22: end chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#line_for_project was not given a project with a start_date" chris@22: '' chris@22: end chris@22: end chris@22: chris@22: def subject_for_version(version, options) chris@22: case options[:format] chris@22: when :html Chris@117: subject = "" Chris@117: subject << view.link_to_version(version) Chris@117: subject << '' Chris@117: html_subject(options, subject, :css => "version-name") chris@22: when :image Chris@117: image_subject(options, version.to_s_with_project) chris@22: when :pdf Chris@117: pdf_new_page?(options) Chris@117: pdf_subject(options, version.to_s_with_project) chris@22: end chris@22: end chris@22: chris@22: def line_for_version(version, options) chris@22: # Skip versions that don't have a start_date chris@37: if version.is_a?(Version) && version.start_date && version.due_date chris@22: options[:zoom] ||= 1 chris@22: options[:g_width] ||= (self.date_to - self.date_from + 1) * options[:zoom] Chris@117: Chris@117: coords = coordinates(version.start_date, version.due_date, version.completed_pourcent, options[:zoom]) Chris@117: label = "#{h version } #{h version.completed_pourcent.to_i.to_s}%" Chris@117: label = h("#{version.project} -") + label unless @project && @project == version.project chris@22: chris@22: case options[:format] chris@22: when :html Chris@117: html_task(options, coords, :css => "version task", :label => label, :markers => true) chris@22: when :image Chris@117: image_task(options, coords, :label => label, :markers => true, :height => 3) chris@22: when :pdf Chris@117: pdf_task(options, coords, :label => label, :markers => true, :height => 0.8) chris@22: end chris@22: else chris@22: ActiveRecord::Base.logger.debug "Gantt#line_for_version was not given a version with a start_date" chris@22: '' chris@22: end chris@22: end chris@22: chris@22: def subject_for_issue(issue, options) Chris@117: while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last) Chris@117: @issue_ancestors.pop Chris@117: options[:indent] -= options[:indent_increment] Chris@117: end Chris@117: Chris@117: output = case options[:format] chris@22: when :html Chris@117: css_classes = '' Chris@117: css_classes << ' issue-overdue' if issue.overdue? Chris@117: css_classes << ' issue-behind-schedule' if issue.behind_schedule? Chris@117: css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to Chris@117: Chris@117: subject = "" Chris@117: if issue.assigned_to.present? Chris@117: assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name Chris@117: subject << view.avatar(issue.assigned_to, :class => 'gravatar icon-gravatar', :size => 10, :title => assigned_string) Chris@117: end Chris@117: subject << view.link_to_issue(issue) Chris@117: subject << '' Chris@117: html_subject(options, subject, :css => "issue-subject") + "\n" Chris@117: when :image Chris@117: image_subject(options, issue.subject) Chris@117: when :pdf Chris@117: pdf_new_page?(options) Chris@117: pdf_subject(options, issue.subject) Chris@117: end chris@22: Chris@117: unless issue.leaf? Chris@117: @issue_ancestors << issue Chris@117: options[:indent] += options[:indent_increment] Chris@117: end chris@22: Chris@117: output chris@22: end chris@22: chris@22: def line_for_issue(issue, options) chris@22: # Skip issues that don't have a due_before (due_date or version's due_date) chris@22: if issue.is_a?(Issue) && issue.due_before Chris@117: coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom]) Chris@117: label = "#{ issue.status.name } #{ issue.done_ratio }%" Chris@117: chris@22: case options[:format] chris@22: when :html Chris@117: html_task(options, coords, :css => "task " + (issue.leaf? ? 'leaf' : 'parent'), :label => label, :issue => issue, :markers => !issue.leaf?) chris@22: when :image Chris@117: image_task(options, coords, :label => label) chris@22: when :pdf Chris@117: pdf_task(options, coords, :label => label) Chris@117: end chris@22: else chris@22: ActiveRecord::Base.logger.debug "GanttHelper#line_for_issue was not given an issue with a due_before" chris@22: '' chris@22: end chris@22: end chris@22: Chris@0: # Generates a gantt image Chris@0: # Only defined if RMagick is avalaible chris@22: def to_image(format='PNG') Chris@0: date_to = (@date_from >> @months)-1 Chris@0: show_weeks = @zoom > 1 Chris@0: show_days = @zoom > 2 Chris@0: Chris@1: subject_width = 400 Chris@0: header_heigth = 18 Chris@0: # width of one day in pixels Chris@0: zoom = @zoom*2 Chris@0: g_width = (@date_to - @date_from + 1)*zoom chris@22: g_height = 20 * number_of_rows + 30 Chris@0: headers_heigth = (show_weeks ? 2*header_heigth : header_heigth) Chris@0: height = g_height + headers_heigth Chris@0: Chris@0: imgl = Magick::ImageList.new Chris@0: imgl.new_image(subject_width+g_width+1, height) Chris@0: gc = Magick::Draw.new Chris@0: Chris@0: # Subjects Chris@117: gc.stroke('transparent') chris@22: subjects(:image => gc, :top => (headers_heigth + 20), :indent => 4, :format => :image) Chris@0: Chris@0: # Months headers Chris@0: month_f = @date_from Chris@0: left = subject_width Chris@0: @months.times do Chris@0: width = ((month_f >> 1) - month_f) * zoom Chris@0: gc.fill('white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left, 0, left + width, height) Chris@0: gc.fill('black') Chris@0: gc.stroke('transparent') Chris@0: gc.stroke_width(1) Chris@0: gc.text(left.round + 8, 14, "#{month_f.year}-#{month_f.month}") Chris@0: left = left + width Chris@0: month_f = month_f >> 1 Chris@0: end Chris@0: Chris@0: # Weeks headers Chris@0: if show_weeks Chris@0: left = subject_width Chris@0: height = header_heigth Chris@0: if @date_from.cwday == 1 Chris@0: # date_from is monday Chris@0: week_f = date_from Chris@0: else Chris@0: # find next monday after date_from Chris@0: week_f = @date_from + (7 - @date_from.cwday + 1) Chris@0: width = (7 - @date_from.cwday + 1) * zoom Chris@0: gc.fill('white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left, header_heigth, left + width, 2*header_heigth + g_height-1) Chris@0: left = left + width Chris@0: end Chris@0: while week_f <= date_to Chris@0: width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom Chris@0: gc.fill('white') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left.round, header_heigth, left.round + width, 2*header_heigth + g_height-1) Chris@0: gc.fill('black') Chris@0: gc.stroke('transparent') Chris@0: gc.stroke_width(1) Chris@0: gc.text(left.round + 2, header_heigth + 14, week_f.cweek.to_s) Chris@0: left = left + width Chris@0: week_f = week_f+7 Chris@0: end Chris@0: end Chris@0: Chris@0: # Days details (week-end in grey) Chris@0: if show_days Chris@0: left = subject_width Chris@0: height = g_height + header_heigth - 1 Chris@0: wday = @date_from.cwday Chris@0: (date_to - @date_from + 1).to_i.times do Chris@0: width = zoom Chris@0: gc.fill(wday == 6 || wday == 7 ? '#eee' : 'white') Chris@117: gc.stroke('#ddd') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(left, 2*header_heigth, left + width, 2*header_heigth + g_height-1) Chris@0: left = left + width Chris@0: wday = wday + 1 Chris@0: wday = 1 if wday > 7 Chris@0: end Chris@0: end Chris@0: Chris@0: # border Chris@0: gc.fill('transparent') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@0: gc.rectangle(0, 0, subject_width+g_width, headers_heigth) Chris@0: gc.stroke('black') Chris@0: gc.rectangle(0, 0, subject_width+g_width, g_height+ headers_heigth-1) Chris@0: Chris@0: # content Chris@0: top = headers_heigth + 20 Chris@117: Chris@117: gc.stroke('transparent') chris@22: lines(:image => gc, :top => top, :zoom => zoom, :subject_width => subject_width, :format => :image) Chris@0: Chris@0: # today red line Chris@0: if Date.today >= @date_from and Date.today <= date_to Chris@0: gc.stroke('red') Chris@0: x = (Date.today-@date_from+1)*zoom + subject_width Chris@0: gc.line(x, headers_heigth, x, headers_heigth + g_height-1) Chris@0: end Chris@0: Chris@0: gc.draw(imgl) Chris@0: imgl.format = format Chris@0: imgl.to_blob Chris@0: end if Object.const_defined?(:Magick) chris@22: chris@22: def to_pdf chris@22: pdf = ::Redmine::Export::PDF::IFPDF.new(current_language) chris@22: pdf.SetTitle("#{l(:label_gantt)} #{project}") chris@22: pdf.AliasNbPages chris@22: pdf.footer_date = format_date(Date.today) chris@22: pdf.AddPage("L") chris@22: pdf.SetFontStyle('B',12) chris@22: pdf.SetX(15) chris@22: pdf.Cell(PDF::LeftPaneWidth, 20, project.to_s) chris@22: pdf.Ln chris@22: pdf.SetFontStyle('B',9) chris@22: chris@22: subject_width = PDF::LeftPaneWidth chris@22: header_heigth = 5 chris@22: chris@22: headers_heigth = header_heigth chris@22: show_weeks = false chris@22: show_days = false chris@22: chris@22: if self.months < 7 chris@22: show_weeks = true chris@22: headers_heigth = 2*header_heigth chris@22: if self.months < 3 chris@22: show_days = true chris@22: headers_heigth = 3*header_heigth chris@22: end chris@22: end chris@22: chris@22: g_width = PDF.right_pane_width chris@22: zoom = (g_width) / (self.date_to - self.date_from + 1) chris@22: g_height = 120 chris@22: t_height = g_height + headers_heigth chris@22: chris@22: y_start = pdf.GetY chris@22: chris@22: # Months headers chris@22: month_f = self.date_from chris@22: left = subject_width chris@22: height = header_heigth chris@22: self.months.times do chris@22: width = ((month_f >> 1) - month_f) * zoom chris@22: pdf.SetY(y_start) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C") chris@22: left = left + width chris@22: month_f = month_f >> 1 chris@22: end chris@22: chris@22: # Weeks headers chris@22: if show_weeks chris@22: left = subject_width chris@22: height = header_heigth chris@22: if self.date_from.cwday == 1 chris@22: # self.date_from is monday chris@22: week_f = self.date_from chris@22: else chris@22: # find next monday after self.date_from chris@22: week_f = self.date_from + (7 - self.date_from.cwday + 1) chris@22: width = (7 - self.date_from.cwday + 1) * zoom-1 chris@22: pdf.SetY(y_start + header_heigth) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width + 1, height, "", "LTR") chris@22: left = left + width+1 chris@22: end chris@22: while week_f <= self.date_to chris@22: width = (week_f + 6 <= self.date_to) ? 7 * zoom : (self.date_to - week_f + 1) * zoom chris@22: pdf.SetY(y_start + header_heigth) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C") chris@22: left = left + width chris@22: week_f = week_f+7 chris@22: end chris@22: end chris@22: chris@22: # Days headers chris@22: if show_days chris@22: left = subject_width chris@22: height = header_heigth chris@22: wday = self.date_from.cwday chris@22: pdf.SetFontStyle('B',7) chris@22: (self.date_to - self.date_from + 1).to_i.times do chris@22: width = zoom chris@22: pdf.SetY(y_start + 2 * header_heigth) chris@22: pdf.SetX(left) chris@22: pdf.Cell(width, height, day_name(wday).first, "LTR", 0, "C") chris@22: left = left + width chris@22: wday = wday + 1 chris@22: wday = 1 if wday > 7 chris@22: end chris@22: end chris@22: chris@22: pdf.SetY(y_start) chris@22: pdf.SetX(15) chris@22: pdf.Cell(subject_width+g_width-15, headers_heigth, "", 1) chris@22: chris@22: # Tasks chris@22: top = headers_heigth + y_start Chris@117: options = { Chris@117: :top => top, Chris@117: :zoom => zoom, Chris@117: :subject_width => subject_width, Chris@117: :g_width => g_width, Chris@117: :indent => 0, Chris@117: :indent_increment => 5, Chris@117: :top_increment => 5, Chris@117: :format => :pdf, Chris@117: :pdf => pdf Chris@117: } Chris@117: render(options) chris@22: pdf.Output chris@22: end Chris@0: Chris@0: private Chris@117: Chris@117: def coordinates(start_date, end_date, progress, zoom=nil) Chris@117: zoom ||= @zoom Chris@117: Chris@117: coords = {} Chris@117: if start_date && end_date && start_date < self.date_to && end_date > self.date_from Chris@117: if start_date > self.date_from Chris@117: coords[:start] = start_date - self.date_from Chris@117: coords[:bar_start] = start_date - self.date_from Chris@117: else Chris@117: coords[:bar_start] = 0 Chris@117: end Chris@117: if end_date < self.date_to Chris@117: coords[:end] = end_date - self.date_from Chris@117: coords[:bar_end] = end_date - self.date_from + 1 Chris@117: else Chris@117: coords[:bar_end] = self.date_to - self.date_from + 1 Chris@117: end Chris@117: Chris@117: if progress Chris@117: progress_date = start_date + (end_date - start_date) * (progress / 100.0) Chris@117: if progress_date > self.date_from && progress_date > start_date Chris@117: if progress_date < self.date_to Chris@117: coords[:bar_progress_end] = progress_date - self.date_from + 1 Chris@117: else Chris@117: coords[:bar_progress_end] = self.date_to - self.date_from + 1 Chris@117: end Chris@117: end Chris@117: Chris@117: if progress_date < Date.today Chris@117: late_date = [Date.today, end_date].min Chris@117: if late_date > self.date_from && late_date > start_date Chris@117: if late_date < self.date_to Chris@117: coords[:bar_late_end] = late_date - self.date_from + 1 Chris@117: else Chris@117: coords[:bar_late_end] = self.date_to - self.date_from + 1 Chris@117: end Chris@117: end Chris@117: end Chris@117: end Chris@117: end Chris@117: Chris@117: # Transforms dates into pixels witdh Chris@117: coords.keys.each do |key| Chris@117: coords[key] = (coords[key] * zoom).floor Chris@117: end Chris@117: coords Chris@117: end chris@22: Chris@117: # Sorts a collection of issues by start_date, due_date, id for gantt rendering Chris@117: def sort_issues!(issues) Chris@117: issues.sort! { |a, b| gantt_issue_compare(a, b, issues) } Chris@117: end Chris@117: Chris@117: # TODO: top level issues should be sorted by start date Chris@117: def gantt_issue_compare(x, y, issues) Chris@117: if x.root_id == y.root_id Chris@117: x.lft <=> y.lft Chris@0: else Chris@117: x.root_id <=> y.root_id Chris@117: end Chris@117: end Chris@117: Chris@117: def current_limit Chris@117: if @max_rows Chris@117: @max_rows - @number_of_rows Chris@117: else Chris@117: nil Chris@117: end Chris@117: end Chris@117: Chris@117: def abort? Chris@117: if @max_rows && @number_of_rows >= @max_rows Chris@117: @truncated = true Chris@117: end Chris@117: end Chris@117: Chris@117: def pdf_new_page?(options) Chris@117: if options[:top] > 180 Chris@117: options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top]) Chris@117: options[:pdf].AddPage("L") Chris@117: options[:top] = 15 Chris@117: options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1) Chris@117: end Chris@117: end Chris@117: Chris@117: def html_subject(params, subject, options={}) Chris@117: output = "
" Chris@117: output << subject Chris@117: output << "
" Chris@117: @subjects << output Chris@117: output Chris@117: end Chris@117: Chris@117: def pdf_subject(params, subject, options={}) Chris@117: params[:pdf].SetY(params[:top]) Chris@117: params[:pdf].SetX(15) Chris@117: Chris@117: char_limit = PDF::MaxCharactorsForSubject - params[:indent] Chris@117: params[:pdf].Cell(params[:subject_width]-15, 5, (" " * params[:indent]) + subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), "LR") Chris@117: Chris@117: params[:pdf].SetY(params[:top]) Chris@117: params[:pdf].SetX(params[:subject_width]) Chris@117: params[:pdf].Cell(params[:g_width], 5, "", "LR") Chris@117: end Chris@117: Chris@117: def image_subject(params, subject, options={}) Chris@117: params[:image].fill('black') Chris@117: params[:image].stroke('transparent') Chris@117: params[:image].stroke_width(1) Chris@117: params[:image].text(params[:indent], params[:top] + 2, subject) Chris@117: end Chris@117: Chris@117: def html_task(params, coords, options={}) Chris@117: output = '' Chris@117: # Renders the task bar, with progress and late Chris@117: if coords[:bar_start] && coords[:bar_end] Chris@117: output << "
 
" Chris@117: Chris@117: if coords[:bar_late_end] Chris@117: output << "
 
" Chris@0: end Chris@117: if coords[:bar_progress_end] Chris@117: output << "
 
" Chris@117: end Chris@117: end Chris@117: # Renders the markers Chris@117: if options[:markers] Chris@117: if coords[:start] Chris@117: output << "
 
" Chris@117: end Chris@117: if coords[:end] Chris@117: output << "
 
" Chris@117: end Chris@117: end Chris@117: # Renders the label on the right Chris@117: if options[:label] Chris@117: output << "
" Chris@117: output << options[:label] Chris@117: output << "
" Chris@117: end Chris@117: # Renders the tooltip Chris@117: if options[:issue] && coords[:bar_start] && coords[:bar_end] Chris@117: output << "
" Chris@117: output << '' Chris@117: output << view.render_issue_tooltip(options[:issue]) Chris@117: output << "
" Chris@117: end Chris@117: @lines << output Chris@117: output Chris@117: end Chris@117: Chris@117: def pdf_task(params, coords, options={}) Chris@117: height = options[:height] || 2 Chris@117: Chris@117: # Renders the task bar, with progress and late Chris@117: if coords[:bar_start] && coords[:bar_end] Chris@117: params[:pdf].SetY(params[:top]+1.5) Chris@117: params[:pdf].SetX(params[:subject_width] + coords[:bar_start]) Chris@117: params[:pdf].SetFillColor(200,200,200) Chris@117: params[:pdf].Cell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1) Chris@117: Chris@117: if coords[:bar_late_end] Chris@117: params[:pdf].SetY(params[:top]+1.5) Chris@117: params[:pdf].SetX(params[:subject_width] + coords[:bar_start]) Chris@117: params[:pdf].SetFillColor(255,100,100) Chris@117: params[:pdf].Cell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1) Chris@117: end Chris@117: if coords[:bar_progress_end] Chris@117: params[:pdf].SetY(params[:top]+1.5) Chris@117: params[:pdf].SetX(params[:subject_width] + coords[:bar_start]) Chris@117: params[:pdf].SetFillColor(90,200,90) Chris@117: params[:pdf].Cell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1) Chris@117: end Chris@117: end Chris@117: # Renders the markers Chris@117: if options[:markers] Chris@117: if coords[:start] Chris@117: params[:pdf].SetY(params[:top] + 1) Chris@117: params[:pdf].SetX(params[:subject_width] + coords[:start] - 1) Chris@117: params[:pdf].SetFillColor(50,50,200) Chris@117: params[:pdf].Cell(2, 2, "", 0, 0, "", 1) Chris@117: end Chris@117: if coords[:end] Chris@117: params[:pdf].SetY(params[:top] + 1) Chris@117: params[:pdf].SetX(params[:subject_width] + coords[:end] - 1) Chris@117: params[:pdf].SetFillColor(50,50,200) Chris@117: params[:pdf].Cell(2, 2, "", 0, 0, "", 1) Chris@117: end Chris@117: end Chris@117: # Renders the label on the right Chris@117: if options[:label] Chris@117: params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5) Chris@117: params[:pdf].Cell(30, 2, options[:label]) Chris@0: end Chris@0: end chris@22: Chris@117: def image_task(params, coords, options={}) Chris@117: height = options[:height] || 6 Chris@117: Chris@117: # Renders the task bar, with progress and late Chris@117: if coords[:bar_start] && coords[:bar_end] Chris@117: params[:image].fill('#aaa') Chris@117: params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_end], params[:top] - height) Chris@117: Chris@117: if coords[:bar_late_end] Chris@117: params[:image].fill('#f66') Chris@117: params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_late_end], params[:top] - height) Chris@117: end Chris@117: if coords[:bar_progress_end] Chris@117: params[:image].fill('#00c600') Chris@117: params[:image].rectangle(params[:subject_width] + coords[:bar_start], params[:top], params[:subject_width] + coords[:bar_progress_end], params[:top] - height) Chris@117: end Chris@117: end Chris@117: # Renders the markers Chris@117: if options[:markers] Chris@117: if coords[:start] Chris@117: x = params[:subject_width] + coords[:start] Chris@117: y = params[:top] - height / 2 Chris@117: params[:image].fill('blue') Chris@117: params[:image].polygon(x-4, y, x, y-4, x+4, y, x, y+4) Chris@117: end Chris@117: if coords[:end] Chris@117: x = params[:subject_width] + coords[:end] + params[:zoom] Chris@117: y = params[:top] - height / 2 Chris@117: params[:image].fill('blue') Chris@117: params[:image].polygon(x-4, y, x, y-4, x+4, y, x, y+4) Chris@117: end Chris@117: end Chris@117: # Renders the label on the right Chris@117: if options[:label] Chris@117: params[:image].fill('black') Chris@117: params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5,params[:top] + 1, options[:label]) Chris@117: end Chris@117: end Chris@0: end Chris@0: end Chris@0: end