Chris@0: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 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@441: # 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@441: # 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@1115: include Redmine::Utils::DateCalculation 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@119: 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@441: Chris@0: def initialize(options={}) Chris@0: options = options.dup 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: zoom = (options[:zoom] || User.current.pref[:gantt_zoom]).to_i Chris@441: @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: # Save gantt parameters as user preference (zoom and months count) Chris@1115: if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || Chris@1115: @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: @date_from = Date.civil(@year_from, @month_from, 1) Chris@0: @date_to = (@date_from >> @months) - 1 Chris@119: @subjects = '' Chris@119: @lines = '' Chris@119: @number_of_rows = nil Chris@119: @issue_ancestors = [] Chris@119: @truncated = false Chris@119: if options.has_key?(:max_rows) Chris@119: @max_rows = options[:max_rows] Chris@119: else Chris@119: @max_rows = Setting.gantt_items_limit.blank? ? nil : Setting.gantt_items_limit.to_i Chris@119: end Chris@0: end chris@22: chris@22: def common_params chris@22: { :controller => 'gantts', :action => 'show', :project_id => @project } Chris@0: end Chris@441: Chris@0: def params Chris@1115: common_params.merge({:zoom => zoom, :year => year_from, Chris@1115: :month => month_from, :months => months}) Chris@0: end Chris@441: Chris@0: def params_previous Chris@1115: common_params.merge({:year => (date_from << months).year, Chris@1115: :month => (date_from << months).month, Chris@1115: :zoom => zoom, :months => months}) Chris@0: end Chris@441: Chris@0: def params_next Chris@1115: common_params.merge({:year => (date_from >> months).year, Chris@1115: :month => (date_from >> months).month, Chris@1115: :zoom => zoom, :months => months}) Chris@0: end chris@22: chris@22: # Returns the number of rows that will be rendered on the Gantt chart chris@22: def number_of_rows Chris@119: return @number_of_rows if @number_of_rows Chris@441: rows = projects.inject(0) {|total, p| total += number_of_rows_on_project(p)} Chris@119: 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@441: return 0 unless projects.include?(project) chris@22: count = 1 Chris@441: count += project_issues(project).size Chris@441: count += project_versions(project).size 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@119: render(options.merge(:only => :subjects)) unless @subjects_rendered Chris@119: @subjects chris@22: end chris@22: chris@22: # Renders the lines of the Gantt chart, the right side chris@22: def lines(options={}) Chris@119: render(options.merge(:only => :lines)) unless @lines_rendered Chris@119: @lines Chris@119: end Chris@441: Chris@441: # Returns issues that will be rendered Chris@441: def issues Chris@441: @issues ||= @query.issues( Chris@441: :include => [:assigned_to, :tracker, :priority, :category, :fixed_version], Chris@441: :order => "#{Project.table_name}.lft ASC, #{Issue.table_name}.id ASC", Chris@441: :limit => @max_rows Chris@441: ) Chris@441: end Chris@441: Chris@441: # Return all the project nodes that will be displayed Chris@441: def projects Chris@441: return @projects if @projects Chris@441: ids = issues.collect(&:project).uniq.collect(&:id) Chris@441: if ids.any? Chris@441: # All issues projects and their visible ancestors Chris@441: @projects = Project.visible.all( Chris@441: :joins => "LEFT JOIN #{Project.table_name} child ON #{Project.table_name}.lft <= child.lft AND #{Project.table_name}.rgt >= child.rgt", Chris@441: :conditions => ["child.id IN (?)", ids], Chris@441: :order => "#{Project.table_name}.lft ASC" Chris@441: ).uniq Chris@441: else Chris@441: @projects = [] Chris@441: end Chris@441: end Chris@441: Chris@441: # Returns the issues that belong to +project+ Chris@441: def project_issues(project) Chris@441: @issues_by_project ||= issues.group_by(&:project) Chris@441: @issues_by_project[project] || [] Chris@441: end Chris@441: Chris@441: # Returns the distinct versions of the issues that belong to +project+ Chris@441: def project_versions(project) Chris@441: project_issues(project).collect(&:fixed_version).compact.uniq Chris@441: end Chris@441: Chris@441: # Returns the issues that belong to +project+ and are assigned to +version+ Chris@441: def version_issues(project, version) Chris@441: project_issues(project).select {|issue| issue.fixed_version == version} Chris@441: end Chris@441: Chris@119: def render(options={}) Chris@1115: options = {:top => 0, :top_increment => 20, Chris@1115: :indent_increment => 20, :render => :subject, Chris@1115: :format => :html}.merge(options) Chris@441: indent = options[:indent] || 4 Chris@119: @subjects = '' unless options[:only] == :lines Chris@119: @lines = '' unless options[:only] == :subjects Chris@119: @number_of_rows = 0 Chris@441: Project.project_tree(projects) do |project, level| Chris@441: options[:indent] = indent + level * options[:indent_increment] Chris@441: render_project(project, options) Chris@441: break if abort? chris@22: end Chris@119: @subjects_rendered = true unless options[:only] == :lines Chris@119: @lines_rendered = true unless options[:only] == :subjects Chris@119: render_end(options) chris@22: end chris@22: chris@22: def render_project(project, options={}) Chris@119: subject_for_project(project, options) unless options[:only] == :lines Chris@119: line_for_project(project, options) unless options[:only] == :subjects chris@22: options[:top] += options[:top_increment] chris@22: options[:indent] += options[:indent_increment] Chris@119: @number_of_rows += 1 Chris@119: return if abort? Chris@441: issues = project_issues(project).select {|i| i.fixed_version.nil?} Chris@119: sort_issues!(issues) chris@22: if issues Chris@119: render_issues(issues, options) Chris@119: return if abort? chris@22: end Chris@441: versions = project_versions(project) Chris@441: versions.each do |version| Chris@441: render_version(project, version, options) chris@22: end 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@119: @issue_ancestors = [] chris@22: issues.each do |i| Chris@119: subject_for_issue(i, options) unless options[:only] == :lines Chris@119: line_for_issue(i, options) unless options[:only] == :subjects chris@22: options[:top] += options[:top_increment] Chris@119: @number_of_rows += 1 Chris@119: break if abort? chris@22: end Chris@119: options[:indent] -= (options[:indent_increment] * @issue_ancestors.size) chris@22: end chris@22: Chris@441: def render_version(project, version, options={}) chris@22: # Version header Chris@119: subject_for_version(version, options) unless options[:only] == :lines Chris@119: line_for_version(version, options) unless options[:only] == :subjects chris@22: options[:top] += options[:top_increment] Chris@119: @number_of_rows += 1 Chris@119: return if abort? Chris@441: issues = version_issues(project, version) chris@22: if issues Chris@119: sort_issues!(issues) chris@22: # Indent issues chris@22: options[:indent] += options[:indent_increment] Chris@119: render_issues(issues, options) chris@22: options[:indent] -= options[:indent_increment] chris@22: end Chris@119: end Chris@441: Chris@119: def render_end(options={}) Chris@119: case options[:format] Chris@441: when :pdf Chris@119: options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top]) Chris@119: end chris@22: end chris@22: chris@22: def subject_for_project(project, options) chris@22: case options[:format] chris@22: when :html Chris@1115: html_class = "" Chris@1115: html_class << 'icon icon-projects ' Chris@1115: html_class << (project.overdue? ? 'project-overdue' : '') Chris@1115: s = view.link_to_project(project).html_safe Chris@1115: subject = view.content_tag(:span, s, Chris@1115: :class => html_class).html_safe Chris@119: html_subject(options, subject, :css => "project-name") chris@22: when :image Chris@119: image_subject(options, project.name) chris@22: when :pdf Chris@119: pdf_new_page?(options) Chris@119: 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@119: coords = coordinates(project.start_date, project.due_date, nil, options[:zoom]) Chris@119: label = h(project) chris@22: case options[:format] chris@22: when :html Chris@119: html_task(options, coords, :css => "project task", :label => label, :markers => true) chris@22: when :image Chris@119: image_task(options, coords, :label => label, :markers => true, :height => 3) chris@22: when :pdf Chris@119: 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@1115: html_class = "" Chris@1115: html_class << 'icon icon-package ' Chris@1115: html_class << (version.behind_schedule? ? 'version-behind-schedule' : '') << " " Chris@1115: html_class << (version.overdue? ? 'version-overdue' : '') Chris@1115: s = view.link_to_version(version).html_safe Chris@1115: subject = view.content_tag(:span, s, Chris@1115: :class => html_class).html_safe Chris@119: html_subject(options, subject, :css => "version-name") chris@22: when :image Chris@119: image_subject(options, version.to_s_with_project) chris@22: when :pdf Chris@119: pdf_new_page?(options) Chris@119: 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@1115: coords = coordinates(version.start_date, Chris@1115: version.due_date, version.completed_pourcent, Chris@1115: options[:zoom]) Chris@1115: label = "#{h version} #{h version.completed_pourcent.to_i.to_s}%" Chris@119: label = h("#{version.project} -") + label unless @project && @project == version.project chris@22: case options[:format] chris@22: when :html Chris@119: html_task(options, coords, :css => "version task", :label => label, :markers => true) chris@22: when :image Chris@119: image_task(options, coords, :label => label, :markers => true, :height => 3) chris@22: when :pdf Chris@119: 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@119: while @issue_ancestors.any? && !issue.is_descendant_of?(@issue_ancestors.last) Chris@119: @issue_ancestors.pop Chris@119: options[:indent] -= options[:indent_increment] Chris@119: end Chris@119: output = case options[:format] chris@22: when :html Chris@119: css_classes = '' Chris@119: css_classes << ' issue-overdue' if issue.overdue? Chris@119: css_classes << ' issue-behind-schedule' if issue.behind_schedule? Chris@119: css_classes << ' icon icon-issue' unless Setting.gravatar_enabled? && issue.assigned_to Chris@1115: s = "".html_safe Chris@119: if issue.assigned_to.present? Chris@119: assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name Chris@1115: s << view.avatar(issue.assigned_to, Chris@1115: :class => 'gravatar icon-gravatar', Chris@1115: :size => 10, Chris@1115: :title => assigned_string).to_s.html_safe Chris@119: end Chris@1115: s << view.link_to_issue(issue).html_safe Chris@1115: subject = view.content_tag(:span, s, :class => css_classes).html_safe Chris@1115: html_subject(options, subject, :css => "issue-subject", Chris@1115: :title => issue.subject) + "\n" Chris@119: when :image Chris@119: image_subject(options, issue.subject) Chris@119: when :pdf Chris@119: pdf_new_page?(options) Chris@119: pdf_subject(options, issue.subject) Chris@119: end Chris@119: unless issue.leaf? Chris@119: @issue_ancestors << issue Chris@119: options[:indent] += options[:indent_increment] Chris@119: end Chris@119: 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@119: coords = coordinates(issue.start_date, issue.due_before, issue.done_ratio, options[:zoom]) Chris@1115: label = "#{issue.status.name} #{issue.done_ratio}%" chris@22: case options[:format] chris@22: when :html Chris@1115: html_task(options, coords, Chris@1115: :css => "task " + (issue.leaf? ? 'leaf' : 'parent'), Chris@1115: :label => label, :issue => issue, Chris@1115: :markers => !issue.leaf?) chris@22: when :image Chris@119: image_task(options, coords, :label => label) chris@22: when :pdf Chris@119: pdf_task(options, coords, :label => label) Chris@119: 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@1115: date_to = (@date_from >> @months) - 1 Chris@0: show_weeks = @zoom > 1 Chris@0: show_days = @zoom > 2 Chris@1: subject_width = 400 Chris@441: header_height = 18 Chris@0: # width of one day in pixels Chris@1115: zoom = @zoom * 2 Chris@1115: g_width = (@date_to - @date_from + 1) * zoom chris@22: g_height = 20 * number_of_rows + 30 Chris@1115: headers_height = (show_weeks ? 2 * header_height : header_height) Chris@441: height = g_height + headers_height Chris@0: imgl = Magick::ImageList.new Chris@1115: imgl.new_image(subject_width + g_width + 1, height) Chris@0: gc = Magick::Draw.new Chris@1115: gc.font = Redmine::Configuration['rmagick_font_path'] || "" Chris@0: # Subjects Chris@119: gc.stroke('transparent') Chris@441: subjects(:image => gc, :top => (headers_height + 20), :indent => 4, :format => :image) Chris@0: # Months headers Chris@0: month_f = @date_from Chris@0: left = subject_width Chris@441: @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: # Weeks headers Chris@0: if show_weeks Chris@1115: left = subject_width Chris@1115: height = header_height Chris@1115: if @date_from.cwday == 1 Chris@1115: # date_from is monday Chris@1115: week_f = date_from Chris@1115: else Chris@1115: # find next monday after date_from Chris@1115: week_f = @date_from + (7 - @date_from.cwday + 1) Chris@1115: width = (7 - @date_from.cwday + 1) * zoom Chris@1115: gc.fill('white') Chris@1115: gc.stroke('grey') Chris@1115: gc.stroke_width(1) Chris@1115: gc.rectangle(left, header_height, left + width, 2 * header_height + g_height - 1) Chris@1115: left = left + width Chris@1115: end Chris@1115: while week_f <= date_to Chris@1115: width = (week_f + 6 <= date_to) ? 7 * zoom : (date_to - week_f + 1) * zoom Chris@1115: gc.fill('white') Chris@1115: gc.stroke('grey') Chris@1115: gc.stroke_width(1) Chris@1115: gc.rectangle(left.round, header_height, left.round + width, 2 * header_height + g_height - 1) Chris@1115: gc.fill('black') Chris@1115: gc.stroke('transparent') Chris@1115: gc.stroke_width(1) Chris@1115: gc.text(left.round + 2, header_height + 14, week_f.cweek.to_s) Chris@1115: left = left + width Chris@1115: week_f = week_f + 7 Chris@1115: end Chris@0: end Chris@0: # Days details (week-end in grey) Chris@0: if show_days Chris@1115: left = subject_width Chris@1115: height = g_height + header_height - 1 Chris@1115: wday = @date_from.cwday Chris@1115: (date_to - @date_from + 1).to_i.times do Chris@1115: width = zoom Chris@1115: gc.fill(non_working_week_days.include?(wday) ? '#eee' : 'white') Chris@1115: gc.stroke('#ddd') Chris@1115: gc.stroke_width(1) Chris@1115: gc.rectangle(left, 2 * header_height, left + width, 2 * header_height + g_height - 1) Chris@1115: left = left + width Chris@1115: wday = wday + 1 Chris@1115: wday = 1 if wday > 7 Chris@1115: end Chris@0: end Chris@0: # border Chris@0: gc.fill('transparent') Chris@0: gc.stroke('grey') Chris@0: gc.stroke_width(1) Chris@1115: gc.rectangle(0, 0, subject_width + g_width, headers_height) Chris@0: gc.stroke('black') Chris@1115: gc.rectangle(0, 0, subject_width + g_width, g_height + headers_height - 1) Chris@0: # content Chris@441: top = headers_height + 20 Chris@119: gc.stroke('transparent') Chris@1115: lines(:image => gc, :top => top, :zoom => zoom, Chris@1115: :subject_width => subject_width, :format => :image) Chris@0: # today red line Chris@0: if Date.today >= @date_from and Date.today <= date_to Chris@0: gc.stroke('red') Chris@1115: x = (Date.today - @date_from + 1) * zoom + subject_width Chris@1115: gc.line(x, headers_height, x, headers_height + g_height - 1) Chris@441: end 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@441: pdf = ::Redmine::Export::PDF::ITCPDF.new(current_language) chris@22: pdf.SetTitle("#{l(:label_gantt)} #{project}") Chris@441: pdf.alias_nb_pages chris@22: pdf.footer_date = format_date(Date.today) chris@22: pdf.AddPage("L") Chris@1115: pdf.SetFontStyle('B', 12) chris@22: pdf.SetX(15) Chris@441: pdf.RDMCell(PDF::LeftPaneWidth, 20, project.to_s) chris@22: pdf.Ln Chris@1115: pdf.SetFontStyle('B', 9) chris@22: subject_width = PDF::LeftPaneWidth Chris@441: header_height = 5 Chris@441: headers_height = header_height chris@22: show_weeks = false chris@22: show_days = false chris@22: if self.months < 7 chris@22: show_weeks = true Chris@1115: headers_height = 2 * header_height chris@22: if self.months < 3 chris@22: show_days = true Chris@1115: headers_height = 3 * header_height chris@22: end chris@22: end 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@441: t_height = g_height + headers_height chris@22: y_start = pdf.GetY chris@22: # Months headers chris@22: month_f = self.date_from chris@22: left = subject_width Chris@441: height = header_height Chris@441: self.months.times do Chris@441: width = ((month_f >> 1) - month_f) * zoom chris@22: pdf.SetY(y_start) chris@22: pdf.SetX(left) Chris@441: pdf.RDMCell(width, height, "#{month_f.year}-#{month_f.month}", "LTR", 0, "C") chris@22: left = left + width chris@22: month_f = month_f >> 1 Chris@441: end chris@22: # Weeks headers chris@22: if show_weeks chris@22: left = subject_width Chris@441: height = header_height 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@441: pdf.SetY(y_start + header_height) chris@22: pdf.SetX(left) Chris@441: pdf.RDMCell(width + 1, height, "", "LTR") Chris@1115: 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@441: pdf.SetY(y_start + header_height) chris@22: pdf.SetX(left) Chris@441: pdf.RDMCell(width, height, (width >= 5 ? week_f.cweek.to_s : ""), "LTR", 0, "C") chris@22: left = left + width Chris@1115: week_f = week_f + 7 chris@22: end chris@22: end chris@22: # Days headers chris@22: if show_days chris@22: left = subject_width Chris@441: height = header_height chris@22: wday = self.date_from.cwday Chris@1115: pdf.SetFontStyle('B', 7) Chris@441: (self.date_to - self.date_from + 1).to_i.times do chris@22: width = zoom Chris@441: pdf.SetY(y_start + 2 * header_height) chris@22: pdf.SetX(left) Chris@441: pdf.RDMCell(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: pdf.SetY(y_start) chris@22: pdf.SetX(15) Chris@1115: pdf.RDMCell(subject_width + g_width - 15, headers_height, "", 1) chris@22: # Tasks Chris@441: top = headers_height + y_start Chris@119: options = { Chris@119: :top => top, Chris@119: :zoom => zoom, Chris@119: :subject_width => subject_width, Chris@119: :g_width => g_width, Chris@119: :indent => 0, Chris@119: :indent_increment => 5, Chris@119: :top_increment => 5, Chris@119: :format => :pdf, Chris@119: :pdf => pdf Chris@119: } Chris@119: render(options) chris@22: pdf.Output chris@22: end Chris@441: Chris@0: private Chris@441: Chris@119: def coordinates(start_date, end_date, progress, zoom=nil) Chris@119: zoom ||= @zoom Chris@119: coords = {} Chris@119: if start_date && end_date && start_date < self.date_to && end_date > self.date_from Chris@119: if start_date > self.date_from Chris@119: coords[:start] = start_date - self.date_from Chris@119: coords[:bar_start] = start_date - self.date_from Chris@119: else Chris@119: coords[:bar_start] = 0 Chris@119: end Chris@119: if end_date < self.date_to Chris@119: coords[:end] = end_date - self.date_from Chris@119: coords[:bar_end] = end_date - self.date_from + 1 Chris@119: else Chris@119: coords[:bar_end] = self.date_to - self.date_from + 1 Chris@119: end Chris@119: if progress Chris@441: progress_date = start_date + (end_date - start_date + 1) * (progress / 100.0) Chris@119: if progress_date > self.date_from && progress_date > start_date Chris@119: if progress_date < self.date_to Chris@441: coords[:bar_progress_end] = progress_date - self.date_from Chris@119: else Chris@119: coords[:bar_progress_end] = self.date_to - self.date_from + 1 Chris@119: end Chris@119: end Chris@119: if progress_date < Date.today Chris@119: late_date = [Date.today, end_date].min Chris@119: if late_date > self.date_from && late_date > start_date Chris@119: if late_date < self.date_to Chris@119: coords[:bar_late_end] = late_date - self.date_from + 1 Chris@119: else Chris@119: coords[:bar_late_end] = self.date_to - self.date_from + 1 Chris@119: end Chris@119: end Chris@119: end Chris@119: end Chris@119: end Chris@119: # Transforms dates into pixels witdh Chris@119: coords.keys.each do |key| Chris@119: coords[key] = (coords[key] * zoom).floor Chris@119: end Chris@119: coords Chris@119: end chris@22: Chris@119: # Sorts a collection of issues by start_date, due_date, id for gantt rendering Chris@119: def sort_issues!(issues) Chris@1115: issues.sort! { |a, b| gantt_issue_compare(a, b) } Chris@119: end Chris@441: Chris@119: # TODO: top level issues should be sorted by start date Chris@1115: def gantt_issue_compare(x, y) Chris@119: if x.root_id == y.root_id Chris@119: x.lft <=> y.lft Chris@0: else Chris@119: x.root_id <=> y.root_id Chris@119: end Chris@119: end Chris@441: Chris@119: def current_limit Chris@119: if @max_rows Chris@119: @max_rows - @number_of_rows Chris@119: else Chris@119: nil Chris@119: end Chris@119: end Chris@441: Chris@119: def abort? Chris@119: if @max_rows && @number_of_rows >= @max_rows Chris@119: @truncated = true Chris@119: end Chris@119: end Chris@441: Chris@119: def pdf_new_page?(options) Chris@119: if options[:top] > 180 Chris@119: options[:pdf].Line(15, options[:top], PDF::TotalWidth, options[:top]) Chris@119: options[:pdf].AddPage("L") Chris@119: options[:top] = 15 Chris@119: options[:pdf].Line(15, options[:top] - 0.1, PDF::TotalWidth, options[:top] - 0.1) Chris@119: end Chris@119: end Chris@441: Chris@119: def html_subject(params, subject, options={}) Chris@245: style = "position: absolute;top:#{params[:top]}px;left:#{params[:indent]}px;" Chris@245: style << "width:#{params[:subject_width] - params[:indent]}px;" if params[:subject_width] Chris@1115: output = view.content_tag('div', subject, Chris@1115: :class => options[:css], :style => style, Chris@1115: :title => options[:title]) Chris@119: @subjects << output Chris@119: output Chris@119: end Chris@441: Chris@119: def pdf_subject(params, subject, options={}) Chris@119: params[:pdf].SetY(params[:top]) Chris@119: params[:pdf].SetX(15) Chris@119: char_limit = PDF::MaxCharactorsForSubject - params[:indent] Chris@1115: params[:pdf].RDMCell(params[:subject_width] - 15, 5, Chris@1115: (" " * params[:indent]) + Chris@1115: subject.to_s.sub(/^(.{#{char_limit}}[^\s]*\s).*$/, '\1 (...)'), Chris@1115: "LR") Chris@119: params[:pdf].SetY(params[:top]) Chris@119: params[:pdf].SetX(params[:subject_width]) Chris@441: params[:pdf].RDMCell(params[:g_width], 5, "", "LR") Chris@119: end Chris@441: Chris@119: def image_subject(params, subject, options={}) Chris@119: params[:image].fill('black') Chris@119: params[:image].stroke('transparent') Chris@119: params[:image].stroke_width(1) Chris@119: params[:image].text(params[:indent], params[:top] + 2, subject) Chris@119: end Chris@441: Chris@119: def html_task(params, coords, options={}) Chris@119: output = '' Chris@119: # Renders the task bar, with progress and late Chris@119: if coords[:bar_start] && coords[:bar_end] Chris@1115: width = coords[:bar_end] - coords[:bar_start] - 2 Chris@1115: style = "" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{coords[:bar_start]}px;" Chris@1115: style << "width:#{width}px;" Chris@1115: output << view.content_tag(:div, ' '.html_safe, Chris@1115: :style => style, Chris@1115: :class => "#{options[:css]} task_todo") Chris@119: if coords[:bar_late_end] Chris@1115: width = coords[:bar_late_end] - coords[:bar_start] - 2 Chris@1115: style = "" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{coords[:bar_start]}px;" Chris@1115: style << "width:#{width}px;" Chris@1115: output << view.content_tag(:div, ' '.html_safe, Chris@1115: :style => style, Chris@1115: :class => "#{options[:css]} task_late") Chris@0: end Chris@119: if coords[:bar_progress_end] Chris@1115: width = coords[:bar_progress_end] - coords[:bar_start] - 2 Chris@1115: style = "" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{coords[:bar_start]}px;" Chris@1115: style << "width:#{width}px;" Chris@1115: output << view.content_tag(:div, ' '.html_safe, Chris@1115: :style => style, Chris@1115: :class => "#{options[:css]} task_done") Chris@119: end Chris@119: end Chris@119: # Renders the markers Chris@119: if options[:markers] Chris@119: if coords[:start] Chris@1115: style = "" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{coords[:start]}px;" Chris@1115: style << "width:15px;" Chris@1115: output << view.content_tag(:div, ' '.html_safe, Chris@1115: :style => style, Chris@1115: :class => "#{options[:css]} marker starting") Chris@119: end Chris@119: if coords[:end] Chris@1115: style = "" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{coords[:end] + params[:zoom]}px;" Chris@1115: style << "width:15px;" Chris@1115: output << view.content_tag(:div, ' '.html_safe, Chris@1115: :style => style, Chris@1115: :class => "#{options[:css]} marker ending") Chris@119: end Chris@119: end Chris@119: # Renders the label on the right Chris@119: if options[:label] Chris@1115: style = "" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{(coords[:bar_end] || 0) + 8}px;" Chris@1115: style << "width:15px;" Chris@1115: output << view.content_tag(:div, options[:label], Chris@1115: :style => style, Chris@1115: :class => "#{options[:css]} label") Chris@119: end Chris@119: # Renders the tooltip Chris@119: if options[:issue] && coords[:bar_start] && coords[:bar_end] Chris@1115: s = view.content_tag(:span, Chris@1115: view.render_issue_tooltip(options[:issue]).html_safe, Chris@1115: :class => "tip") Chris@1115: style = "" Chris@1115: style << "position: absolute;" Chris@1115: style << "top:#{params[:top]}px;" Chris@1115: style << "left:#{coords[:bar_start]}px;" Chris@1115: style << "width:#{coords[:bar_end] - coords[:bar_start]}px;" Chris@1115: style << "height:12px;" Chris@1115: output << view.content_tag(:div, s.html_safe, Chris@1115: :style => style, Chris@1115: :class => "tooltip") Chris@119: end Chris@119: @lines << output Chris@119: output Chris@119: end Chris@441: Chris@119: def pdf_task(params, coords, options={}) Chris@119: height = options[:height] || 2 Chris@119: # Renders the task bar, with progress and late Chris@119: if coords[:bar_start] && coords[:bar_end] Chris@1115: params[:pdf].SetY(params[:top] + 1.5) Chris@119: params[:pdf].SetX(params[:subject_width] + coords[:bar_start]) Chris@1115: params[:pdf].SetFillColor(200, 200, 200) Chris@441: params[:pdf].RDMCell(coords[:bar_end] - coords[:bar_start], height, "", 0, 0, "", 1) Chris@119: if coords[:bar_late_end] Chris@1115: params[:pdf].SetY(params[:top] + 1.5) Chris@119: params[:pdf].SetX(params[:subject_width] + coords[:bar_start]) Chris@1115: params[:pdf].SetFillColor(255, 100, 100) Chris@441: params[:pdf].RDMCell(coords[:bar_late_end] - coords[:bar_start], height, "", 0, 0, "", 1) Chris@119: end Chris@119: if coords[:bar_progress_end] Chris@1115: params[:pdf].SetY(params[:top] + 1.5) Chris@119: params[:pdf].SetX(params[:subject_width] + coords[:bar_start]) Chris@1115: params[:pdf].SetFillColor(90, 200, 90) Chris@441: params[:pdf].RDMCell(coords[:bar_progress_end] - coords[:bar_start], height, "", 0, 0, "", 1) Chris@119: end Chris@119: end Chris@119: # Renders the markers Chris@119: if options[:markers] Chris@119: if coords[:start] Chris@119: params[:pdf].SetY(params[:top] + 1) Chris@119: params[:pdf].SetX(params[:subject_width] + coords[:start] - 1) Chris@1115: params[:pdf].SetFillColor(50, 50, 200) Chris@441: params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1) Chris@119: end Chris@119: if coords[:end] Chris@119: params[:pdf].SetY(params[:top] + 1) Chris@119: params[:pdf].SetX(params[:subject_width] + coords[:end] - 1) Chris@1115: params[:pdf].SetFillColor(50, 50, 200) Chris@441: params[:pdf].RDMCell(2, 2, "", 0, 0, "", 1) Chris@119: end Chris@119: end Chris@119: # Renders the label on the right Chris@119: if options[:label] Chris@119: params[:pdf].SetX(params[:subject_width] + (coords[:bar_end] || 0) + 5) Chris@441: params[:pdf].RDMCell(30, 2, options[:label]) Chris@0: end Chris@0: end chris@22: Chris@119: def image_task(params, coords, options={}) Chris@119: height = options[:height] || 6 Chris@119: # Renders the task bar, with progress and late Chris@119: if coords[:bar_start] && coords[:bar_end] Chris@119: params[:image].fill('#aaa') Chris@1115: params[:image].rectangle(params[:subject_width] + coords[:bar_start], Chris@1115: params[:top], Chris@1115: params[:subject_width] + coords[:bar_end], Chris@1115: params[:top] - height) Chris@119: if coords[:bar_late_end] Chris@119: params[:image].fill('#f66') Chris@1115: params[:image].rectangle(params[:subject_width] + coords[:bar_start], Chris@1115: params[:top], Chris@1115: params[:subject_width] + coords[:bar_late_end], Chris@1115: params[:top] - height) Chris@119: end Chris@119: if coords[:bar_progress_end] Chris@119: params[:image].fill('#00c600') Chris@1115: params[:image].rectangle(params[:subject_width] + coords[:bar_start], Chris@1115: params[:top], Chris@1115: params[:subject_width] + coords[:bar_progress_end], Chris@1115: params[:top] - height) Chris@119: end Chris@119: end Chris@119: # Renders the markers Chris@119: if options[:markers] Chris@119: if coords[:start] Chris@119: x = params[:subject_width] + coords[:start] Chris@119: y = params[:top] - height / 2 Chris@119: params[:image].fill('blue') Chris@1115: params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4) Chris@119: end Chris@119: if coords[:end] Chris@119: x = params[:subject_width] + coords[:end] + params[:zoom] Chris@119: y = params[:top] - height / 2 Chris@119: params[:image].fill('blue') Chris@1115: params[:image].polygon(x - 4, y, x, y - 4, x + 4, y, x, y + 4) Chris@119: end Chris@119: end Chris@119: # Renders the label on the right Chris@119: if options[:label] Chris@119: params[:image].fill('black') Chris@1115: params[:image].text(params[:subject_width] + (coords[:bar_end] || 0) + 5, Chris@1115: params[:top] + 1, Chris@1115: options[:label]) Chris@119: end Chris@119: end Chris@0: end Chris@0: end Chris@0: end