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