Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

lint(rubocop): fix & refactoring

as-opd67585f76274
diff --git a/app/models/work_package/pdf_export/gantt.rb b/app/models/work_package/pdf_export/gantt.rb
index 8d795b0f7b7..2bbde8837eb 100644
--- a/app/models/work_package/pdf_export/gantt.rb
+++ b/app/models/work_package/pdf_export/gantt.rb
@@ -27,7 +27,15 @@
#++
module WorkPackage::PDFExport::Gantt
+ GANTT_ROW_HEIGHT = 20
+ GANTT_GRID_COLOR = "9b9ea3".freeze
+ GANTT_TEXT_CELL_PADDING = 2
+ GANTT_BAR_CELL_PADDING = 5
+
def write_work_packages_gantt!(work_packages, _)
+ wps = work_packages.reject { |work_package| work_package.start_date.nil? }
+ return if wps.empty?
+
zoom_levels = [
[:day, 48],
[:day, 24],
@@ -41,398 +49,567 @@ module WorkPackage::PDFExport::Gantt
[:quarter, 24]
]
zoom = options[:zoom] || 1
- @gantt_mode, @gant_column_width = zoom_levels[zoom.to_i - 1].nil? ? zoom_levels[1] : zoom_levels[zoom.to_i - 1]
- @gantt_header_row_height = 30
- @gant_row_height = 20
- @gant_text_cell_padding = 2
- @gant_bar_cell_padding = 5
- @gantt_filter_empty = false
- @gantt_grid_color = "9b9ea3"
- @gantt_text_column_width = pdf.bounds.width / 4
-
- @gantt_columns_per_next_page = (pdf.bounds.width / @gant_column_width).floor
- gantt_columns_space_next_page = pdf.bounds.width - @gantt_columns_per_next_page * @gant_column_width
- # distribute space to the default column widths
- @gant_column_width += gantt_columns_space_next_page / @gantt_columns_per_next_page
- gantt_columns_width_first_page = pdf.bounds.width - @gantt_text_column_width
- @gantt_columns_per_first_page = (gantt_columns_width_first_page / @gant_column_width).floor
- @gantt_columns_per_next_page = (pdf.bounds.width / @gant_column_width).floor
- # distribute space to the first column
- @gantt_text_column_width = pdf.bounds.width - (@gantt_columns_per_first_page * @gant_column_width)
- gant_rows_height = pdf.bounds.height - @gantt_header_row_height
- @gantt_work_packages_per_page = (gant_rows_height / @gant_row_height).floor
- # distribute space bottom to the first row
- @gantt_header_row_height = pdf.bounds.height - (@gantt_work_packages_per_page * @gant_row_height)
-
- pages = build_gantt_pages(work_packages)
-
- if pages.find { |page| !page[:text_column] }.nil?
- # if there are not enough columns for even the first page of horizontal pages => distribute space to all columns
- nr_of_columns = pages.first[:columns].length
- @gantt_text_column_width = pdf.bounds.width / 4
- @gant_column_width = (pdf.bounds.width - @gantt_text_column_width) / nr_of_columns
- @gantt_columns_per_first_page = nr_of_columns
- @gantt_columns_per_next_page = nr_of_columns
- pages = build_gantt_pages(work_packages)
- end
-
- pages = pages.filter { |page| page[:columns].map { |c| c[:work_packages] }.flatten.any? } if @gantt_filter_empty
- paint_pages(pages)
- end
+ mode, column_width = zoom_levels[zoom.to_i - 1].nil? ? zoom_levels[1] : zoom_levels[zoom.to_i - 1]
+ builder = case mode
+ when :month
+ GanttBuilderMonths.new(pdf, heading, column_width)
+ when :quarter
+ GanttBuilderQuarters.new(pdf, heading, column_width)
+ else
+ # when :day
+ GanttBuilderDays.new(pdf, heading, column_width)
+ end
+ pages = builder.build(wps)
+ pages = pages.filter { |page| page[:columns].pluck(:work_packages).flatten.any? } if options[:filter_empty]
+ painter = GanttPainter.new(pdf)
+ painter.paint(pages)
+ end
+
+ class GanttUtils
+ def self.day_in_quarter(date)
+ date.yday - start_of_quarter(date.year, date.month).yday + 1
+ end
- private
+ def self.quarter_of_date(date)
+ (date.month / 3.0).ceil
+ end
- def build_gantt_variables
+ def self.start_of_quarter(year, month)
+ Date.new(year, (quarter_of_date(Date.new(year, month, 1)) * 3) - 2, 1)
+ end
- end
+ def self.end_of_quarter(year, month)
+ Date.new(year, (quarter_of_date(Date.new(year, month, 1)) * 3), -1)
+ end
- def build_gantt_pages(work_packages)
- wps = work_packages.select { |work_package| !work_package.start_date.nil? }
- wp_dates = wps
- .map { |work_package| [work_package.start_date, work_package.due_date.nil? ? Date.today : work_package.due_date] }
- .flatten.uniq.sort
- start_date = wp_dates.first
- end_date = wp_dates.last
-
- dates = case @gantt_mode
- when :day
- (start_date..end_date).to_a
- when :quarter
- (start_date..end_date).map { |d| [d.year, quarter_of_date(d)] }.uniq.map { |year, quarter| Date.new(year, quarter * 3, -1) }
- else
- # when :month
- (start_date..end_date).map { |d| Date.new(d.year, d.month, -1) }.uniq
- end
- vertical_pages_needed = (wps.size.to_f / @gantt_work_packages_per_page.to_f).ceil
- (0..vertical_pages_needed - 1)
- .map { |v_index| build_gantt_horizontal_pages(wps.slice(v_index * @gantt_work_packages_per_page, @gantt_work_packages_per_page), dates) }
- .flatten
- end
+ def self.days_of_month(date)
+ Date.new(date.year, date.month, -1).day
+ end
- def build_gantt_header_span_cell(text, columns)
- { text: text, x: columns.first[:x], width: columns.last[:x] + columns.last[:width] - columns.first[:x] }
+ def self.days_of_quarter(date)
+ quarter = quarter_of_date(date)
+ days = 0
+ (1..3).each do |q|
+ days += days_of_month(Date.new(date.year, (quarter * 3) - 2 + q, 1))
+ end
+ days
+ end
end
- def build_gantt_header_row(columns)
- result = []
-
- years = columns.map { |column| column[:date].year }.uniq
- result << years.map do |year|
- year_columns = columns.select { |column| column[:date].year == year }
- build_gantt_header_span_cell(year.to_s, year_columns)
+ class GanttBuilder
+ def initialize(pdf, title, column_width)
+ @pdf = pdf
+ @title = title
+ @column_width = column_width
end
- if [:quarter, :month].include?(@gantt_mode)
- quarters = columns.map { |column| [quarter_of_date(column[:date]), column[:date].year] }.uniq
- result << quarters.map do |quarter_tuple|
- quarter, year = quarter_tuple
- quarter_columns = columns.select { |column| column[:date].year == year && quarter_of_date(column[:date]) == quarter }
- build_gantt_header_span_cell("Q#{quarter}", quarter_columns)
+ def build(work_packages)
+ @header_row_height = 30
+ @text_column_width = @pdf.bounds.width / 4
+
+ @nr_columns = (@pdf.bounds.width / @column_width).floor
+ gantt_columns_space_next_page = @pdf.bounds.width - (@nr_columns * @column_width)
+ # distribute space to the default column widths
+ @column_width += gantt_columns_space_next_page / @nr_columns
+ gantt_columns_width_first_page = @pdf.bounds.width - @text_column_width
+ @nr_columns_first_page = (gantt_columns_width_first_page / @column_width).floor
+ @nr_columns = (@pdf.bounds.width / @column_width).floor
+ # distribute space to the first column
+ @text_column_width = @pdf.bounds.width - (@nr_columns_first_page * @column_width)
+ gant_rows_height = @pdf.bounds.height - @header_row_height
+ @rows_per_page = (gant_rows_height / GANTT_ROW_HEIGHT).floor
+ # distribute space bottom to the first row
+ @header_row_height = @pdf.bounds.height - (@rows_per_page * GANTT_ROW_HEIGHT)
+
+ pages = build_pages(work_packages)
+ if pages.find { |page| page[:text_column].nil? }.nil?
+ # if there are not enough columns for even the first page of horizontal pages => distribute space to all columns
+ nr_of_columns = pages.first[:columns].length
+ @text_column_width = @pdf.bounds.width / 4
+ @column_width = (@pdf.bounds.width - @text_column_width) / nr_of_columns
+ @nr_columns_first_page = nr_of_columns
+ @nr_columns = nr_of_columns
+ pages = build_pages(work_packages)
end
+ pages
end
- if [:day, :month].include?(@gantt_mode)
- months = columns.map { |column| [column[:date].month, column[:date].year] }.uniq
- result << months.map do |month_tuple|
- month, year = month_tuple
- month_columns = columns.select { |column| column[:date].year == year && column[:date].month == month }
- build_gantt_header_span_cell(month_columns.first[:date].strftime("%b"), month_columns)
- end
+ private
+
+ def build_pages(work_packages)
+ dates = build_column_dates(work_packages)
+ vertical_pages_needed = (work_packages.size / @rows_per_page.to_f).ceil
+ horizontal_pages_needed = [((dates.size - @nr_columns_first_page) / @nr_columns.to_f).ceil, 0].max + 1
+ (0..vertical_pages_needed - 1)
+ .map do |v_index|
+ build_horizontal_pages(work_packages.slice(v_index * @rows_per_page, @rows_per_page), dates, horizontal_pages_needed)
+ end.flatten
+ end
+
+ def build_column_dates(work_packages)
+ wp_dates = collect_work_packages_dates(work_packages)
+ build_column_dates_range(wp_dates.first..wp_dates.last)
end
- if @gantt_mode == :day
- result << columns.map do |column|
- build_gantt_header_span_cell(column[:date].day.to_s, [column])
+ def collect_work_packages_dates(work_packages)
+ work_packages.map do |work_package|
+ [work_package.start_date, work_package.due_date.nil? ? Time.zone.today : work_package.due_date]
+ end.flatten.uniq.sort
+ end
+
+ def build_header_span_cell(text, columns)
+ # top: & height: will be added later
+ left = columns.first[:left]
+ width = columns.last[:left] + columns.last[:width] - columns.first[:left]
+ { text:, left:, width:, right: left + width }
+ end
+
+ def set_header_row_y_positions(columns)
+ cell_height = @header_row_height / columns.length
+ columns.each_with_index do |cell_row, index|
+ y = index * cell_height
+ cell_row.each do |cell|
+ cell[:top] = y
+ cell[:height] = cell_height
+ cell[:bottom] = y + cell_height
+ end
end
end
- cell_height = @gantt_header_row_height / result.length
- result.each_with_index do |cell_row, index|
- y = index * cell_height
- cell_row.each do |cell|
- cell[:y] = y
- cell[:height] = cell_height
+ def build_header_row(columns)
+ parts = header_row_parts
+ result = parts.map do |part|
+ case part
+ when :years
+ build_header_row_years(columns)
+ when :quarters
+ build_header_row_quarters(columns)
+ when :months
+ build_header_row_months(columns)
+ when :days
+ build_header_row_days(columns)
+ else
+ []
+ end
end
+ set_header_row_y_positions(result)
+ result.flatten
end
- result.flatten
- end
+ def build_header_row_part(columns, mapping_lambda, compare_lambda, title_lambda)
+ columns
+ .map { |column| mapping_lambda.call(column[:date]) }
+ .uniq
+ .map do |entry|
+ part_columns = columns.select { |column| compare_lambda.call(column[:date], entry) }
+ build_header_span_cell(title_lambda.call(entry), part_columns)
+ end
+ end
- def build_gantt_horizontal_pages(work_packages, dates)
- horizontal_pages_needed = [((dates.size - @gantt_columns_per_first_page).to_f / @gantt_columns_per_next_page.to_f).ceil, 0].max + 1
- result = []
- dates_on_page = dates.slice(0, @gantt_columns_per_first_page)
- result << build_gantt_page(dates_on_page, 0, work_packages)
+ def build_header_row_years(columns)
+ build_header_row_part(columns,
+ ->(date) { date.year },
+ ->(date, year) { date.year == year },
+ ->(year) { year.to_s })
+ end
- list = (0..horizontal_pages_needed - 2)
- list.each do |index|
- dates_on_page = dates.slice(@gantt_columns_per_first_page + index * @gantt_columns_per_next_page, @gantt_columns_per_next_page)
- result << build_gantt_page(dates_on_page, index + 1, work_packages)
+ def build_header_row_quarters(columns)
+ build_header_row_part(columns,
+ ->(date) { [date.year, GanttUtils::quarter_of_date(date)] },
+ ->(date, quarter_tuple) {
+ date.year == quarter_tuple[0] && GanttUtils::quarter_of_date(date) == quarter_tuple[1]
+ },
+ ->(quarter_tuple) { "Q#{quarter_tuple[1]}" })
end
- result
- end
- def build_gantt_page(dates, index, work_packages)
- x = index == 0 ? @gantt_text_column_width : 0
- columns = dates.each_with_index.map { |date, col_index| build_gantt_column(x + (col_index * @gant_column_width), date, work_packages) }
- y = @gantt_header_row_height
- shapes = work_packages.each_with_index.map do |work_package, row_index|
- paint_columns = columns.filter { |column| column[:work_packages].include?(work_package) }
- build_gantt_shape(y + (row_index * @gant_row_height), paint_columns, work_package) unless paint_columns.empty?
- end.compact
- header = build_gantt_header_row(columns)
- {
- text_column: index == 0,
- width: x + (dates.size * @gant_column_width),
- height: @gantt_header_row_height + (@gantt_work_packages_per_page * @gant_row_height),
- columns: columns,
- header: header,
- shapes: shapes,
- work_packages: work_packages
- }
- end
+ def build_header_row_months(columns)
+ build_header_row_part(columns,
+ ->(date) { [date.year, date.month] },
+ ->(date, month_tuple) { date.year == month_tuple[0] && date.month == month_tuple[1] },
+ ->(month_tuple) { Date.new(month_tuple[0], month_tuple[1], 1).strftime("%b") })
+ end
- def build_gantt_shape(y, paint_columns, work_package)
- paint_column_first = paint_columns.first
- paint_column_last = paint_columns.last
- x = paint_column_first[:x]
- width = paint_column_last[:x] + paint_column_last[:width] - paint_column_first[:x]
- {
- x: x,
- y: y,
- width: width,
- work_package: work_package,
- column_first: paint_column_first,
- column_last: paint_column_last,
- x1: x + calc_start_offset(work_package, paint_column_first[:date]),
- x2: x + width - calc_end_offset(work_package, paint_columns.last[:date]),
- y1: y + @gant_bar_cell_padding,
- y2: y + @gant_row_height - @gant_bar_cell_padding,
- type: work_package.milestone? ? :milestone : :bar,
- color: gantt_wp_color(work_package)
- }
- end
+ def build_header_row_days(columns)
+ columns.map { |column| build_header_span_cell(column[:date].day.to_s, [column]) }
+ end
- def build_gantt_column(x, date, work_packages)
- work_packages_on_date =
- case @gantt_mode
- when :day
- work_packages.select { |work_package| wp_on_day?(work_package, date) }
- when :quarter
- work_packages.select { |work_package| wp_on_quarter?(work_package, date) }
- else
- # when :month
- work_packages.select { |work_package| wp_on_month?(work_package, date) }
+ def build_horizontal_pages(work_packages, dates, horizontal_pages_needed)
+ result = [build_page(dates.slice(0, @nr_columns_first_page), 0, work_packages)]
+ (0..horizontal_pages_needed - 2).each do |index|
+ result << build_page(
+ dates.slice(@nr_columns_first_page + (index * @nr_columns), @nr_columns),
+ index + 1, work_packages
+ )
end
- {
- date: date,
- x: x,
- width: @gant_column_width,
- work_packages: work_packages_on_date
- }
- end
+ result
+ end
+ def build_page(dates, index, work_packages)
+ x = index == 0 ? @text_column_width : 0
+ columns = dates.each_with_index.map { |date, col_index| build_column(x + (col_index * @column_width), date, work_packages) }
+ header = build_header_row(columns)
+ {
+ text_column: index == 0 ? { width: @text_column_width, title: @title } : nil,
+ width: x + (dates.size * @column_width),
+ height: @header_row_height + (@rows_per_page * GANTT_ROW_HEIGHT),
+ rows: work_packages.each_with_index.map { |work_package, row_index| build_row(work_package, row_index, columns) },
+ columns:,
+ header:,
+ work_packages:
+ }
+ end
- def paint_pages(pages)
- pages.each_with_index do |page, page_index|
- paint_page(page)
- # start a new page if not last
- @pdf.start_new_page if page_index != pages.size - 1
+ def build_row(work_package, row_index, columns)
+ paint_columns = columns.filter { |column| column[:work_packages].include?(work_package) }
+ top = @header_row_height + (row_index * GANTT_ROW_HEIGHT)
+ shape = build_shape(top, paint_columns, work_package) unless paint_columns.empty?
+ {
+ top:,
+ left: 0,
+ height: GANTT_ROW_HEIGHT,
+ bottom: top + GANTT_ROW_HEIGHT,
+ work_package:,
+ shape:
+ }
end
- end
- def paint_page(page)
- # paint grid lines
- paint_gantt_grid(page)
- # paint header row
- paint_gantt_header_row(page)
- # paint work packages titles if first of horizontal pages
- page[:work_packages].each_with_index { |wp, index| paint_gantt_wp(wp, index) } if page[:text_column]
- # paint work packages shapes
- page[:shapes].each { |shape| paint_gantt_shape(shape) }
- end
+ def bar_layout(paint_columns, work_package)
+ x1 = calc_start_offset(work_package, paint_columns.first[:date])
+ x2 = paint_columns.last[:right] - paint_columns.first[:left] -
+ calc_end_offset(work_package, paint_columns.last[:date])
+ [x1, x2, GANTT_BAR_CELL_PADDING, GANTT_ROW_HEIGHT - GANTT_BAR_CELL_PADDING]
+ end
- def paint_gantt_grid(page)
- paint_gantt_line_v(0, page[:height], 0)
- paint_gantt_line_v(0, page[:height], page[:width])
- paint_gantt_line_h(0, page[:width], page[:height])
- paint_gantt_line_v(0, page[:height], @gantt_text_column_width) if page[:text_column]
- page[:columns].each { |column| paint_gantt_line_v(@gantt_header_row_height, page[:height], column[:x] + column[:width]) }
- (0..@gantt_work_packages_per_page).each do |index|
- paint_gantt_line_h(0, page[:width], @gantt_header_row_height + index * @gant_row_height)
+ def build_shape_bar(top, paint_columns, work_package)
+ left = paint_columns.first[:left]
+ x1, x2, y1, y2 = bar_layout(paint_columns, work_package)
+ {
+ type: :bar,
+ left: left + x1, right: left + x2,
+ top: top + y1, bottom: top + y2,
+ width: [x2 - x1, 0.1].max, height: y2 - y1
+ }
end
- end
- def paint_gantt_header_row(page)
- if page[:text_column]
- paint_gantt_text_box(heading, 0, 0, @gantt_text_column_width, @gantt_header_row_height,
- @gant_text_cell_padding * 2, 0, { size: 10, style: :bold })
- paint_gantt_line_h(0, @gantt_text_column_width, 0)
+ def milestone_layout(top, paint_columns, work_package)
+ diamond_size = [@column_width, GANTT_ROW_HEIGHT].min / 2
+ x1 = if milestone_position_centered?
+ width = Math.sqrt((diamond_size.to_f **2) + (diamond_size.to_f **2))
+ (@column_width - width) / 2
+ else
+ calc_start_offset(work_package, paint_columns.first[:date])
+ end
+ y1 = top + (GANTT_ROW_HEIGHT / 2)
+ [x1, y1, diamond_size]
end
- page[:header].each do |cell|
- paint_gantt_text_box(cell[:text], cell[:x], cell[:y], cell[:width], cell[:height], 0, 0, { size: 10, style: :bold, align: :center })
- paint_gantt_line_h(cell[:x], cell[:x] + cell[:width], cell[:y])
- paint_gantt_line_v(cell[:y], cell[:y] + cell[:height], cell[:x])
+
+ def build_shape_milestone(top, paint_columns, work_package)
+ x1, y1, diamond_size = milestone_layout(top, paint_columns, work_package)
+ {
+ type: :milestone,
+ left: paint_columns.first[:left] + x1,
+ right: paint_columns.first[:left] + x1 + diamond_size,
+ top: y1, bottom: y1 + diamond_size,
+ width: diamond_size, height: diamond_size
+ }
end
- end
- def paint_gantt_header_cell(text, columns, y, height)
- x1 = columns.first[:x]
- x2 = columns.last[:x] + columns.last[:width]
- paint_gantt_text_box(text, x1, y, x2 - x1, height, 0, 0, { size: 8, style: :bold, align: :center })
- paint_gantt_line_h(x1, x2, y)
- paint_gantt_line_v(y, y + height, x1)
- end
+ def build_shape(top, paint_columns, work_package)
+ shape = {
+ work_package:,
+ columns: paint_columns,
+ color: work_package.type.color.hexcode.sub("#", "")
+ }
+ if work_package.milestone?
+ shape.merge(build_shape_milestone(top, paint_columns, work_package))
+ else
+ shape.merge(build_shape_bar(top, paint_columns, work_package))
+ end
+ end
- def paint_gantt_shape_bar(shape)
- paint_gant_rect(shape[:x1], shape[:y1], [shape[:x2] - shape[:x1], 0.1].max, shape[:y2] - shape[:y1], shape[:color])
- end
+ def build_column(left, date, work_packages)
+ {
+ date:,
+ left:,
+ right: left + @column_width,
+ width: @column_width,
+ header_row_height: @header_row_height,
+ work_packages: work_packages_on_date(date, work_packages)
+ }
+ end
- def paint_gantt_shape_milestone(shape)
- diamond_size = [@gant_column_width, @gant_row_height].min / 2
- diamond_half_size = diamond_size / 2
- center_x = @gantt_mode == :day ? shape[:x] + @gant_column_width / 2 : shape[:x1] + diamond_half_size
- center_y = shape[:y] + @gant_row_height / 2
- pdf.rotate(45, origin: [@pdf.bounds.left + center_x, @pdf.bounds.top - center_y]) do
- paint_gant_rect(center_x - diamond_half_size, center_y - diamond_half_size, diamond_size, diamond_size, shape[:color])
+ def build_column_dates_range(_range)
+ [] # to be overwritten
end
- end
- def paint_gantt_shape(shape)
- if shape[:type] == :milestone
- paint_gantt_shape_milestone(shape)
- else
- paint_gantt_shape_bar(shape)
+ def header_row_parts
+ [] # to be overwritten
end
- end
- def paint_gantt_wp(wp, index)
- paint_gantt_text_box(
- "#{wp.type} ##{wp.id} - #{wp.subject}",
- 0, @gantt_header_row_height + (index * @gant_row_height),
- @gantt_text_column_width, @gant_row_height,
- @gant_text_cell_padding * 2, @gant_text_cell_padding)
- end
+ def work_packages_on_date(_date, _work_packages)
+ [] # to be overwritten
+ end
- def paint_gantt_line(x, y, x2, y2)
- @pdf.stroke do
- @pdf.line_width = 0.5
- @pdf.stroke_color @gantt_grid_color
- @pdf.line x, y, x2, y2
+ def milestone_position_centered?
+ false # to be overwritten
end
- end
- def paint_gantt_line_h(x1, x2, y)
- paint_gantt_line(@pdf.bounds.left + x1, @pdf.bounds.top - y, @pdf.bounds.left + x2, @pdf.bounds.top - y)
- end
+ def calc_end_offset(_work_package, _date)
+ 0 # to be overwritten
+ end
- def paint_gantt_line_v(y1, y2, x)
- paint_gantt_line(@pdf.bounds.left + x, @pdf.bounds.top - y1, @pdf.bounds.left + x, @pdf.bounds.top - y2)
+ def calc_start_offset(_work_package, _date)
+ 0 # to be overwritten
+ end
end
- def paint_gant_rect(x, y, width, height, color)
- current_color = @pdf.fill_color
- @pdf.fill_color color
- @pdf.fill_rectangle([@pdf.bounds.left + x, @pdf.bounds.top - y], width, height)
- @pdf.fill_color = current_color
- end
+ class GanttBuilderMonths < GanttBuilder
+ def build_column_dates_range(range)
+ range
+ .map { |d| Date.new(d.year, d.month, -1) }
+ .uniq
+ end
- def paint_gantt_text_box(text, x_offset, y_offset, width, height, padding_h, padding_v, additional_options = {})
- @pdf.text_box(text, at: [@pdf.bounds.left + x_offset + padding_h, @pdf.bounds.top - padding_v - y_offset],
- width: width - padding_h * 2,
- height: height - 2 - padding_v * 2,
- overflow: :shrink_to_fit, min_font_size: 5, valign: :center, size: 8, leading: 0, **additional_options)
- end
+ def header_row_parts
+ %i[years quarters months]
+ end
+ def work_packages_on_date(date, work_packages)
+ work_packages.select { |work_package| wp_on_month?(work_package, date) }
+ end
- def gantt_wp_color(wp)
- wp.type.color.hexcode.sub("#", "")
- end
+ def calc_start_offset(work_package, date)
+ test_date = Date.new(date.year, date.month, 1)
+ return 0 if work_package.start_date <= test_date
- def calc_end_offset(wp, date)
- case @gantt_mode
- when :quarter
- wp_date = wp.due_date.nil? ? Date.today : wp.due_date
- quarter = quarter_of_date(date)
- test_date = Date.new(date.year, (quarter * 3), -1)
- return 0 if wp_date >= test_date
+ width_per_day = @column_width.to_f / GanttUtils::days_of_month(date)
+ day_in_month = work_package.start_date.day - 1
+ day_in_month * width_per_day
+ end
- width_per_day = @gant_column_width.to_f / days_of_quarter(date)
- day_in_quarter = day_in_quarter(wp_date)
- @gant_column_width - day_in_quarter * width_per_day
- when :month
- wp_date = wp.due_date.nil? ? Date.today : wp.due_date
+ def calc_end_offset(work_package, date)
+ wp_date = work_package.due_date || Time.zone.today
test_date = Date.new(date.year, date.month, -1)
return 0 if wp_date >= test_date
- width_per_day = @gant_column_width.to_f / days_of_month(test_date)
+ width_per_day = @column_width.to_f / GanttUtils::days_of_month(test_date)
day_in_month = wp_date.day
- @gant_column_width - day_in_month * width_per_day
- else
+ @column_width - (day_in_month * width_per_day)
+ end
+
+ def wp_on_month?(work_package, date)
+ start_date = work_package.start_date
+ end_date = work_package.due_date.nil? ? Time.zone.today : work_package.due_date
+ Range.new(Date.new(start_date.year, start_date.month, 1), Date.new(end_date.year, end_date.month, -1))
+ .include?(date)
+ end
+ end
+
+ class GanttBuilderDays < GanttBuilder
+ def build_column_dates_range(range)
+ range.to_a
+ end
+
+ def header_row_parts
+ %i[years months days]
+ end
+
+ def work_packages_on_date(date, work_packages)
+ work_packages.select { |work_package| wp_on_day?(work_package, date) }
+ end
+
+ def calc_start_offset(_work_package, _date)
+ 0
+ end
+
+ def calc_end_offset(_work_package, _date)
0
end
+
+ def milestone_position_centered?
+ true
+ end
+
+ def wp_on_day?(work_package, date)
+ start_date = work_package.start_date
+ end_date = work_package.due_date || Time.zone.today
+ Range.new(start_date, end_date).include?(date)
+ end
end
- def calc_start_offset(wp, date)
- case @gantt_mode
- when :quarter
- quarter = quarter_of_date(date)
- test_date = Date.new(date.year, (quarter * 3) - 2, 1)
- return 0 if wp.start_date <= test_date
+ class GanttBuilderQuarters < GanttBuilder
+ def build_column_dates_range(range)
+ range
+ .map { |d| [d.year, GanttUtils::quarter_of_date(d)] }
+ .uniq
+ .map { |year, quarter| Date.new(year, quarter * 3, -1) }
+ end
+
+ def header_row_parts
+ %i[years quarters months]
+ end
+
+ def work_packages_on_date(date, work_packages)
+ work_packages.select { |work_package| wp_on_quarter?(work_package, date) }
+ end
- width_per_day = @gant_column_width.to_f / days_of_quarter(date)
- day_in_quarter = day_in_quarter(wp.start_date) - 1
+ def calc_start_offset(work_package, date)
+ test_date = GanttUtils::start_of_quarter(date.year, date.month)
+ return 0 if work_package.start_date <= test_date
+
+ width_per_day = @column_width.to_f / GanttUtils::days_of_quarter(date)
+ day_in_quarter = GanttUtils::day_in_quarter(work_package.start_date) - 1
day_in_quarter * width_per_day
- when :month
- test_date = Date.new(date.year, date.month, 1)
- return 0 if wp.start_date <= test_date
+ end
- width_per_day = @gant_column_width.to_f / days_of_month(date)
- day_in_month = wp.start_date.day - 1
- day_in_month * width_per_day
- else
- return 0
+ def calc_end_offset(work_package, date)
+ wp_date = work_package.due_date.nil? ? Time.zone.today : work_package.due_date
+ test_date = GanttUtils::end_of_quarter(date.year, date.month)
+ return 0 if wp_date >= test_date
+
+ width_per_day = @column_width.to_f / GanttUtils::days_of_quarter(date)
+ day_in_quarter = GanttUtils::day_in_quarter(wp_date)
+ @column_width - (day_in_quarter * width_per_day)
end
- end
- def quarter_of_date(date)
- (date.month / 3.0).ceil
+ def wp_on_quarter?(work_package, date)
+ end_date = work_package.due_date || Time.zone.today
+ Range.new(
+ GanttUtils::start_of_quarter(work_package.start_date.year, work_package.start_date.month),
+ GanttUtils::end_of_quarter(end_date.year, end_date.month)
+ ).include?(date)
+ end
end
- def days_of_month(date)
- Date.new(date.year, date.month, -1).day
- end
+ class GanttPainter
+ def initialize(pdf)
+ @pdf = pdf
+ end
- def day_in_quarter(date)
- date.yday - Date.new(date.year, (quarter_of_date(date) * 3) - 2, 1).yday + 1
- end
+ def paint(pages)
+ paint_pages(pages)
+ end
+
+ private
- def days_of_quarter(date)
- quarter = quarter_of_date(date)
- days = 0
- (1..3).each do |q|
- days += days_of_month(Date.new(date.year, (quarter * 3) - 2 + q, 1))
+ def paint_pages(pages)
+ pages.each_with_index do |page, page_index|
+ paint_page(page)
+ # start a new page if not last
+ @pdf.start_new_page if page_index != pages.size - 1
+ end
end
- days
- end
- def wp_on_month?(work_package, date)
- start_date = work_package.start_date
- end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
- Range.new(Date.new(start_date.year, start_date.month, 1), Date.new(end_date.year, end_date.month, -1))
- .include?(date)
- end
+ def paint_page(page)
+ paint_grid(page)
+ paint_header_row(page)
+ page[:columns].each { |column| paint_line_v(column[:header_row_height], page[:height], column[:right]) }
+ page[:rows].each { |row| paint_row(page, row) }
+ paint_line_h(0, page[:width], page[:rows].last[:bottom])
+ end
- def wp_on_day?(work_package, date)
- start_date = work_package.start_date
- end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
- Range.new(start_date, end_date).include?(date)
- end
+ def paint_grid(page)
+ paint_line_v(0, page[:height], 0)
+ paint_line_v(0, page[:height], page[:width])
+ paint_line_v(0, page[:height], page[:text_column][:width]) unless page[:text_column].nil?
+ paint_line_h(0, page[:width], page[:height])
+ end
+
+ def paint_header_text_column(page)
+ paint_text_box(page[:text_column][:title], 0, 0, page[:text_column][:width], page[:columns].first[:header_row_height],
+ GANTT_TEXT_CELL_PADDING * 2, 0, { size: 10, style: :bold })
+ paint_line_h(0, page[:text_column][:width], 0)
+ end
+
+ def paint_header_column_cell(cell)
+ paint_text_box(cell[:text],
+ cell[:left], cell[:top], cell[:width], cell[:height],
+ 0, 0,
+ { size: 10, style: :bold, align: :center })
+ paint_line_h(cell[:left], cell[:right], cell[:top])
+ paint_line_v(cell[:top], cell[:bottom], cell[:left])
+ end
+
+ def paint_work_package_title(page, row)
+ work_package = row[:work_package]
+ paint_text_box(
+ "#{work_package.type} ##{work_package.id} - #{work_package.subject}",
+ row[:left], row[:top], page[:text_column][:width], GANTT_ROW_HEIGHT,
+ GANTT_TEXT_CELL_PADDING * 2, GANTT_TEXT_CELL_PADDING
+ )
+ end
+
+ def paint_row(page, row)
+ paint_line_h(0, page[:width], row[:top])
+ paint_work_package_title(page, row) unless page[:text_column].nil?
+ paint_shape(row[:shape]) if row[:shape]
+ end
+
+ def paint_header_row(page)
+ paint_header_text_column(page) unless page[:text_column].nil?
+ page[:header].each { |cell| paint_header_column_cell(cell) }
+ end
+
+ def paint_header_cell(text, columns, top, height)
+ left = columns.first[:left]
+ right = columns.last[:right]
+ paint_text_box(text, left, top, right - left, height, 0, 0, { size: 8, style: :bold, align: :center })
+ paint_line_h(left, right, top)
+ paint_line_v(top, top + height, left)
+ end
+
+ def paint_shape_bar(shape)
+ paint_rect(shape[:left], shape[:top], shape[:width], shape[:height], shape[:color])
+ end
- def wp_on_quarter?(work_package, date)
- start_date = work_package.start_date
- end_date = work_package.due_date.nil? ? Date.today : work_package.due_date
- Range.new(Date.new(start_date.year, (quarter_of_date(start_date) * 3) - 2, 1), Date.new(end_date.year, (quarter_of_date(end_date) * 3), -1))
- .include?(date)
+ def paint_shape_milestone(shape)
+ @pdf.rotate(45, origin: [@pdf.bounds.left + shape[:left], @pdf.bounds.top - shape[:top]]) do
+ paint_rect(shape[:left], shape[:top], shape[:width], shape[:height], shape[:color])
+ end
+ end
+
+ def paint_shape(shape)
+ if shape[:type] == :milestone
+ paint_shape_milestone(shape)
+ else
+ paint_shape_bar(shape)
+ end
+ end
+
+ def paint_line(line_x1, line_y1, line_x2, line_y2)
+ @pdf.stroke do
+ @pdf.line_width = 0.5
+ @pdf.stroke_color GANTT_GRID_COLOR
+ @pdf.line line_x1, line_y1, line_x2, line_y2
+ end
+ end
+
+ def paint_line_h(left, right, top)
+ paint_line(@pdf.bounds.left + left, @pdf.bounds.top - top, @pdf.bounds.left + right, @pdf.bounds.top - top)
+ end
+
+ def paint_line_v(top, bottom, left)
+ paint_line(@pdf.bounds.left + left, @pdf.bounds.top - top, @pdf.bounds.left + left, @pdf.bounds.top - bottom)
+ end
+
+ def paint_rect(left, top, width, height, color)
+ current_color = @pdf.fill_color
+ @pdf.fill_color color
+ @pdf.fill_rectangle([@pdf.bounds.left + left, @pdf.bounds.top - top], width, height)
+ @pdf.fill_color = current_color
+ end
+
+ def paint_text_box(text, left, top, width, height, padding_h, padding_v, additional_options = {})
+ @pdf.text_box(text,
+ at: [@pdf.bounds.left + left + padding_h, @pdf.bounds.top - padding_v - top],
+ width: width - (padding_h * 2),
+ height: height - 2 - (padding_v * 2),
+ overflow: :shrink_to_fit,
+ min_font_size: 5,
+ valign: :center,
+ size: 8,
+ leading: 0,
+ **additional_options)
+ end
end
end