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

optional dependencies lines, part 3

as-op88062f6bf80d
diff --git a/app/models/work_package/pdf_export/gantt/builder.rb b/app/models/work_package/pdf_export/gantt/builder.rb
index b33cdf061d6..82e3ed5e0fc 100644
--- a/app/models/work_package/pdf_export/gantt/builder.rb
+++ b/app/models/work_package/pdf_export/gantt/builder.rb
@@ -53,7 +53,7 @@ class GanttBuilder
page_groups = build_pages(work_packages)
end
build_dep_lines(page_groups) if @draw_gantt_lines
- page_groups.flat_map { |page_group| page_group.pages }
+ page_groups.flat_map(&:pages)
end
private
@@ -104,10 +104,14 @@ class GanttBuilder
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_vertical_pages(work_packages, dates, vertical_pages_needed, horizontal_pages_needed)
+ end
+
+ def build_vertical_pages(work_packages, dates, vertical_pages_needed, horizontal_pages_needed)
+ (0..vertical_pages_needed - 1).map do |v_index|
group_work_packages = work_packages.slice(v_index * @rows_per_page, @rows_per_page)
- GanttPageGroup.new(v_index, group_work_packages, build_horizontal_pages(group_work_packages, dates, horizontal_pages_needed))
+ GanttPageGroup.new(v_index, group_work_packages,
+ build_horizontal_pages(group_work_packages, dates, horizontal_pages_needed))
end
end
@@ -211,17 +215,25 @@ class GanttBuilder
end
end
- def collect_line_infos(work_package, page_groups)
- rows = page_groups.map do |page_group|
- page_group.pages.filter_map { |page| page.rows.find { |r| r.work_package == work_package } }
- end.flatten
+ def rows_by_work_package(work_package, page_groups)
+ page_groups.map do |page_group|
+ page_group.pages.map do |page|
+ page.rows.find { |r| r.work_package == work_package }
+ end
+ end.flatten.compact
+ end
+
+ def start_and_finish_rows(rows)
draw_rows = rows.reject { |row| row.shape.nil? }
start = draw_rows.max_by { |row| row.page.index }
finish = draw_rows.max_by { |row| row.page.index }
- GanttLineInfo.new(rows[0].page.group, rows, draw_rows,
- start, start.shape.left, start.shape.top + (start.shape.height / 2),
- finish, finish.shape.right, finish.shape.top + (finish.shape.height / 2)
- )
+ [start, finish]
+ end
+
+ def collect_line_infos(work_package, page_groups)
+ rows = rows_by_work_package(work_package, page_groups)
+ start, finish = start_and_finish_rows(rows)
+ GanttLineInfo.new(rows[0].page.group, rows, start, finish)
end
def build_dep_line(work_package, target_work_package, page_groups)
@@ -236,55 +248,92 @@ class GanttBuilder
end
end
+ def build_same_page_dep_lines_step(line_source, line_target)
+ dep_lines_step(line_source.finish_row.bottom,
+ line_source.finish_left, line_source.finish_top,
+ line_target.start_left, line_target.start_top)
+ end
+
+ def build_same_page_dep_lines_straight(line_source, line_target)
+ dep_lines_straight(line_source.finish_left, line_source.finish_top, line_target.start_left, line_target.start_top)
+ end
+
def build_same_page_dep_lines(line_source, line_target)
- lines = if line_target.start_x - 10 <= line_source.finish_x
- dep_lines_step(line_source.finish_row.bottom, line_source.finish_x, line_source.finish_y, line_target.start_x, line_target.start_y)
+ lines = if line_target.start_left - 10 <= line_source.finish_left
+ build_same_page_dep_lines_step(line_source, line_target)
else
- dep_lines_straight(line_source.finish_x, line_source.finish_y, line_target.start_x, line_target.start_y)
+ build_same_page_dep_lines_straight(line_source, line_target)
end
- line_source.start_row.page.add_lines(lines)
- end
-
- def build_multi_page_dep_line(line_source, line_target)
- page_group = line_source.page_group
- i = line_source.page_group.pages.index(line_source.finish_row.page)
- j = line_target.page_group.pages.index(line_target.start_row.page)
- if i < j
- page = line_source.finish_row.page
- lines = [{ left: line_source.finish_x, right: page.columns.last.right, top: line_source.finish_y, bottom: line_source.finish_y }]
- page.add_lines(lines)
-
- ((i + 1)..(j - 1)).each do |index|
- page = page_group.pages[index]
- lines = [{ left: page.columns.first.left, right: page.columns.last.right, top: line_source.finish_y, bottom: line_source.finish_y }]
- page.add_lines(lines)
- end
+ line_source.start_row.page.add_lines lines
+ end
- page = line_target.start_row.page
- lines = dep_lines_straight(page.columns.first.left, line_source.finish_y, line_target.start_x, line_target.start_y)
- page.add_lines(lines)
- else
- y = line_source.finish_row.bottom
- page = line_source.finish_row.page
- lines = [
- { left: line_source.finish_x, right: line_source.finish_x + 5, top: line_source.finish_y, bottom: line_source.finish_y },
- { left: line_source.finish_x + 5, right: line_source.finish_x + 5, top: line_source.finish_y, bottom: y },
- { left: line_source.finish_row.left, right: line_source.finish_x + 5, top: y, bottom: y }
+ def build_multi_page_dep_line_middle(source, start_page_index, finish_page_index, top)
+ ((start_page_index + 1)..(finish_page_index - 1)).each do |index|
+ page = source.page_group.pages[index]
+ page.add_line(page.columns.first.left, page.columns.last.right, top, top)
+ end
+ end
+
+ def build_multi_page_dep_line_forward_start(source)
+ source.finish_row.page.add_line(source.finish_left, source.finish_row.page.columns.last.right,
+ source.finish_top, source.finish_top)
+ end
+
+ def build_multi_page_dep_line_forward_end(source, target)
+ source_left = target.start_row.page.columns.first.left - 10
+ source_top = source.finish_top
+ target_left = target.start_left
+ target_top = target.start_top
+ target.start_row.page.add_lines(
+ [
+ [source_left, target_left - 5, source_top, source_top],
+ [target_left - 5, target_left - 5, source_top, target_top],
+ [target_left - 5, target_left, target_top, target_top]
]
- page.add_lines(lines)
+ )
+ end
- ((j + 1)..(i - 1)).each do |index|
- page = page_group.pages[index]
- lines = [{ left: page.columns.first.left, right: page.columns.last.right, top: y, bottom: y }]
- page.add_lines(lines)
- end
- page = line_target.start_row.page
- lines = [
- { left: line_target.start_x - 5, right: page.columns.last.right, top: y, bottom: y },
- { left: line_target.start_x - 5, right: line_target.start_x - 5, top: y, bottom: line_target.start_y },
- { left: line_target.start_x - 5, right: line_target.start_x, top: line_target.start_y, bottom: line_target.start_y }
+ def build_multi_page_dep_line_forward(source, target, start_page_index, finish_page_index)
+ build_multi_page_dep_line_forward_start(source)
+ build_multi_page_dep_line_middle(source, start_page_index, finish_page_index, source.finish_top)
+ build_multi_page_dep_line_forward_end(source, target)
+ end
+
+ def build_multi_page_dep_line_backward_end(target, top)
+ left = target.start_left - 5
+ target.start_row.page.add_lines(
+ [
+ [left, target.start_row.page.columns.last.right, top, top],
+ [left, left, top, target.start_top],
+ [left, target.start_left, target.start_top, target.start_top]
]
- page.add_lines(lines)
+ )
+ end
+
+ def build_multi_page_dep_line_backward_start(source, top)
+ source.finish_row.page.add_lines(
+ [
+ [source.finish_left, source.finish_left + 5, source.finish_top, source.finish_top],
+ [source.finish_left + 5, source.finish_left + 5, source.finish_top, top],
+ [source.finish_row.left, source.finish_left + 5, top, top]
+ ]
+ )
+ end
+
+ def build_multi_page_dep_line_backward(source, target, start_page_index, finish_page_index)
+ y = source.finish_row.bottom
+ build_multi_page_dep_line_backward_start(source, y)
+ build_multi_page_dep_line_middle(source, finish_page_index, start_page_index, y)
+ build_multi_page_dep_line_backward_end(target, y)
+ end
+
+ def build_multi_page_dep_line(source, target)
+ i = source.page_group.pages.index(source.finish_row.page)
+ j = source.page_group.pages.index(target.start_row.page)
+ if i > j
+ build_multi_page_dep_line_backward(source, target, i, j)
+ else
+ build_multi_page_dep_line_forward(source, target, i, j)
end
end
@@ -292,36 +341,48 @@ class GanttBuilder
def dep_lines_straight(source_left, source_top, target_left, target_top)
[
- { left: source_left, right: target_left - 5, top: source_top, bottom: source_top },
- { left: target_left - 5, right: target_left - 5, top: source_top, bottom: target_top },
- { left: target_left - 5, right: target_left, top: target_top, bottom: target_top }
+ [source_left, target_left - 5, source_top, source_top],
+ [target_left - 5, target_left - 5, source_top, target_top],
+ [target_left - 5, target_left, target_top, target_top]
]
end
def dep_lines_step(source_row_bottom, source_left, source_top, target_left, target_top)
[
- { left: source_left, right: source_left + 5, top: source_top, bottom: source_top },
- { left: source_left + 5, right: source_left + 5, top: source_top, bottom: source_row_bottom },
- { left: target_left - 5, right: source_left + 5, top: source_row_bottom, bottom: source_row_bottom },
- { left: target_left - 5, right: target_left - 5, top: source_row_bottom, bottom: target_top },
- { left: target_left - 5, right: target_left, top: target_top, bottom: target_top }
+ [source_left, source_left + 5, source_top, source_top],
+ [source_left + 5, source_left + 5, source_top, source_row_bottom],
+ [target_left - 5, source_left + 5, source_row_bottom, source_row_bottom],
+ [target_left - 5, target_left - 5, source_row_bottom, target_top],
+ [target_left - 5, target_left, target_top, target_top]
]
end
+ def build_text_column(index)
+ if index == 0
+ GanttTextColumn.new(@title, 0, @text_column_width, 0, GANTT_ROW_HEIGHT,
+ GANTT_TEXT_CELL_PADDING * 2, GANTT_TEXT_CELL_PADDING)
+ end
+ end
+
+ def build_columns(left, dates, work_packages)
+ dates.each_with_index.map { |date, col_index| build_column(date, left + (col_index * @column_width), work_packages) }
+ end
+
+ def build_rows(columns, work_packages)
+ work_packages.each_with_index.map { |work_package, row_index| build_row(work_package, row_index, columns) }
+ 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(date, x + (col_index * @column_width), work_packages) }
- rows = work_packages.each_with_index.map { |work_package, row_index| build_row(work_package, row_index, columns) }
+ left = (index == 0 ? @text_column_width : 0)
+ columns = build_columns(left, dates, work_packages)
GanttPage.new(
index,
work_packages,
build_header_cells(columns),
- rows,
+ build_rows(columns, work_packages),
columns,
- index == 0 ? GanttTextColumn.new(@title,
- 0, @text_column_width, 0, GANTT_ROW_HEIGHT,
- GANTT_TEXT_CELL_PADDING * 2, GANTT_TEXT_CELL_PADDING) : nil,
- x + (dates.size * @column_width),
+ build_text_column(index),
+ left + (dates.size * @column_width),
@header_row_height + (@rows_per_page * GANTT_ROW_HEIGHT),
@header_row_height
)
@@ -521,7 +582,7 @@ class GanttBuilderQuarters < GanttBuilder
end
def days_of_quarter(date)
- (1..3).map { |q| Date.new(date.year, (date.quarter * 3) - 3 + q, -1).day }.sum
+ (1..3).sum { |q| Date.new(date.year, (date.quarter * 3) - 3 + q, -1).day }
end
def wp_on_quarter?(work_package, date)
@@ -530,4 +591,3 @@ class GanttBuilderQuarters < GanttBuilder
Range.new(start_date.beginning_of_quarter, end_date.end_of_quarter).include?(date)
end
end
-
diff --git a/app/models/work_package/pdf_export/gantt/data.rb b/app/models/work_package/pdf_export/gantt/data.rb
index c301ab1865e..c8672df7490 100644
--- a/app/models/work_package/pdf_export/gantt/data.rb
+++ b/app/models/work_package/pdf_export/gantt/data.rb
@@ -58,8 +58,12 @@ class GanttPage
columns.each { |column| column.page = self }
end
+ def add_line(left, right, top, bottom)
+ @lines.push({ left:, right:, top:, bottom: })
+ end
+
def add_lines(lines)
- @lines.concat(lines)
+ lines.each { |line| add_line(line[0], line[1], line[2], line[3]) }
end
end
@@ -92,18 +96,21 @@ class GanttColumn
end
class GanttLineInfo
- attr_accessor :page_group, :rows, :draw_rows, :start_row, :start_x, :start_y, :finish_row, :finish_x, :finish_y
+ attr_accessor :page_group, :rows, :start_row, :start_left, :start_top, :finish_row, :finish_left, :finish_top
- def initialize(page_group, rows, draw_rows, start_row, start_x, start_y, finish_row, finish_x, finish_y)
+ def initialize(page_group, rows, start_row, finish_row)
@page_group = page_group
@rows = rows
- @draw_rows = draw_rows
@start_row = start_row
- @start_x = start_x
- @start_y = start_y
@finish_row = finish_row
- @finish_x = finish_x
- @finish_y = finish_y
+ init_positions
+ end
+
+ def init_positions
+ @start_left = @start_row.shape.left
+ @start_top = @start_row.shape.top + (@start_row.shape.height / 2)
+ @finish_left = @finish_row.shape.right
+ @finish_top = @finish_row.shape.top + (@finish_row.shape.height / 2)
end
end
@@ -153,3 +160,14 @@ class GanttShape
@color = color
end
end
+
+class GanttLineFragment
+ attr_accessor :left, :right, :top, :bottom
+
+ def initialize(left, right, top, bottom)
+ @left = left
+ @right = right
+ @top = top
+ @bottom = bottom
+ end
+end
diff --git a/app/models/work_package/pdf_export/gantt/painter.rb b/app/models/work_package/pdf_export/gantt/painter.rb
index fe76f04f42f..1065d192cc7 100644
--- a/app/models/work_package/pdf_export/gantt/painter.rb
+++ b/app/models/work_package/pdf_export/gantt/painter.rb
@@ -30,7 +30,7 @@
class GanttPainter
GANTT_GRID_COLOR = "9b9ea3".freeze
- GANTT_LINE_COLOR = "0000ff".freeze
+ GANTT_LINE_COLOR = "2b8bd5".freeze
def initialize(pdf)
@pdf = pdf
@@ -53,45 +53,69 @@ class GanttPainter
def paint_page(page)
paint_grid(page)
paint_header_row(page)
- page.columns.each { |column| paint_grid_line_v(page.header_row_height, page.height, column.right) }
- paint_grid_line_h(0, page.width, page.rows.last.bottom)
- page.lines.each { |line| paint_gantt_line(line) }
+ paint_lines(page)
+ paint_rows(page)
+ end
+
+ def paint_rows(page)
page.rows.each { |row| paint_row(row) }
end
- def paint_grid(page)
- paint_grid_line_v(0, page.height, 0)
- paint_grid_line_v(0, page.height, page.width)
- paint_grid_line_v(0, page.height, page.text_column.width) unless page.text_column.nil?
- paint_grid_line_h(0, page.width, page.height)
- page.rows.each { |row| paint_grid_line_h(0, page.width, row.top) }
+ def paint_lines(page)
+ @pdf.stroke do
+ @pdf.line_width = 1
+ @pdf.stroke_color GANTT_LINE_COLOR
+ page.lines.each { |line| paint_line(line[:left], line[:top], line[:right], line[:bottom]) }
+ end
end
- def paint_header_text_column(page)
- paint_text_box(page.text_column.title, 0, 0, page.text_column.width, page.header_row_height,
- page.text_column.padding_h, 0, { size: 10, style: :bold })
- paint_grid_line_h(0, page.text_column.width, 0)
+ def grid_v(page)
+ page_height = page.height
+ [
+ [0, page_height, 0],
+ [0, page_height, page.width],
+ page.text_column.nil? ? nil : [0, page_height, page.text_column.width],
+ page.columns.map { |column| [page.header_row_height, page_height, column.right] },
+ page.header_cells.map { |cell| [cell.top, cell.bottom, cell.left] }
+ ]
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_grid_line_h(cell.left, cell.right, cell.top)
- paint_grid_line_v(cell.top, cell.bottom, cell.left)
+ def grid_h(page)
+ page_width = page.width
+ text_column = page.text_column
+ [
+ [0, page_width, page.height],
+ [0, page_width, page.rows.last.bottom],
+ text_column.nil? ? nil : [0, text_column.width, 0],
+ page.rows.map { |row| [0, page_width, row.top] },
+ page.header_cells.map { |cell| [cell.left, cell.right, cell.top] }
+ ]
end
- def paint_work_package_title(row)
- paint_text_box(
- "#{row.work_package.type} ##{row.work_package.id} - #{row.work_package.subject}",
- row.left, row.top, row.page.text_column.width, row.page.text_column.height,
- row.page.text_column.padding_h, row.page.text_column.padding_v
- )
+ def paint_grid_h(page)
+ grid_h(page).compact.flatten.each_slice(3) { |left, right, top| paint_line(left, top, right, top) }
+ end
+
+ def paint_grid_v(page)
+ grid_v(page).compact.flatten.each_slice(3) { |top, bottom, left| paint_line(left, top, left, bottom) }
+ end
+
+ def paint_grid(page)
+ @pdf.stroke do
+ @pdf.line_width = 0.5
+ @pdf.stroke_color GANTT_GRID_COLOR
+ paint_grid_v(page)
+ paint_grid_h(page)
+ end
end
def paint_row(row)
- paint_work_package_title(row) unless row.page.text_column.nil?
- paint_shape(row.shape) unless row.shape.nil?
+ unless row.page.text_column.nil?
+ paint_work_package_title(row, row.left, row.top, row.page.text_column.width, row.page.text_column.height)
+ end
+ unless row.shape.nil?
+ paint_shape(row.shape)
+ end
end
def paint_header_row(page)
@@ -99,14 +123,6 @@ class GanttPainter
page.header_cells.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_grid_line_h(left, right, top)
- paint_grid_line_v(top, top + height, left)
- end
-
def paint_shape(shape)
if shape.type == :milestone
paint_diamond(shape.left, shape.top, shape.width, shape.height, shape.color)
@@ -115,25 +131,9 @@ class GanttPainter
end
end
- def paint_line(line_x1, line_y1, line_x2, line_y2, color)
- @pdf.stroke do
- @pdf.line_width = 0.5
- @pdf.stroke_color color
- @pdf.line @pdf.bounds.left + line_x1, @pdf.bounds.top - line_y1,
- @pdf.bounds.left + line_x2, @pdf.bounds.top - line_y2
- end
- end
-
- def paint_gantt_line(line)
- paint_line(line[:left], line[:top], line[:right], line[:bottom], GANTT_LINE_COLOR)
- end
-
- def paint_grid_line_h(left, right, top)
- paint_line(left, top, right, top, GANTT_GRID_COLOR)
- end
-
- def paint_grid_line_v(top, bottom, left)
- paint_line(left, top, left, bottom, GANTT_GRID_COLOR)
+ def paint_line(line_x1, line_y1, line_x2, line_y2)
+ @pdf.line @pdf.bounds.left + line_x1, @pdf.bounds.top - line_y1,
+ @pdf.bounds.left + line_x2, @pdf.bounds.top - line_y2
end
def paint_diamond(left, top, width, height, color)
@@ -152,6 +152,23 @@ class GanttPainter
@pdf.fill_color = current_color
end
+ def paint_header_text_column(page)
+ paint_text_box(page.text_column.title, 0, 0, page.text_column.width, page.header_row_height,
+ page.text_column.padding_h, 0, { size: 10, style: :bold })
+ 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 })
+ end
+
+ def paint_work_package_title(row, left, top, width, height)
+ paint_text_box("#{row.work_package.type} ##{row.work_package.id} - #{row.work_package.subject}",
+ left, top, width, height,
+ row.page.text_column.padding_h, row.page.text_column.padding_v)
+ 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],