class Rectangle

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint(dpblnt@gmail.com)

Attributes

dur[R]
group[R]
height[R]
iter[R]
iter_id[R]
level[RW]
percentage[R]
pheight[RW]
pwidth[RW]
px1[RW]
py1[RW]
start[R]
successors[R]
y1[R]

Public Class Methods

new(gantt,iter) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 6
def initialize(gantt,iter)
        @iter=iter
        @gantt=gantt
        @has_child=iter.has_child?
        @level=0
        @iter_id=iter[gantt.list.list_model.column_of_id].to_i
        @parent_r=@gantt.rectangles[iter.parent[gantt.list.list_model.column_of_id].to_i] unless iter.parent.nil?
        @successors=Hash.new
        if gantt.successors_column
                        iter[gantt.successors_column].split(',').each{|pid|
                                @successors[pid.to_i]=nil
                        }
        end
        @group=iter[gantt.group_column] if gantt.group_column
        @partial=if gantt.partial_column then iter[gantt.partial_column].to_i else 100 end
        @percentage=if gantt.percentage_column then iter[gantt.percentage_column].to_i else 0 end
        if gantt.group_color_column
                begin
                        @color=Gdk::Color.parse("##{iter[@gantt.group_color_column]}")
                rescue
                        @color=nil
                end
                else
                @color=nil
        end
        @color=Gdk::Color.parse("#1782E0") if @color.nil?
        #we assume it's mysql unix_timestamp
        @start=iter[gantt.start_column].to_i
        @dur=iter[gantt.dur_column].to_i
        
        @y1=0
        @height=10
        @gantt_min_x=0
        iter_coordinates
        @visible=iter[gantt.list.list_model.column_of_visible]
        @px1=nil
        @pwidth=nil
        @py=nil
        @pheight=nil
end

Public Instance Methods

draw(cr,hilight=false) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 130
def draw(cr,hilight=false)
        return self unless visible?
        #fill bar
        cr.set_source_rgba(@color.red.to_f/0x10000,@color.green.to_f/0x10000,@color.blue.to_f/0x10000,@has_child ? 0.05 : 0.7)
        cr.set_line_width(1).set_dash(100000)
        cr.rounded_rectangle(x1,y1,width,height,4)
        cr.fill
        cr.stroke
        #bar border
        if hilight
                cr.set_source_rgba(0.7,0.1,0.1,0.9)
        else
                cr.set_source_rgba(0,0,0,0.7)
        end
        cr.rounded_rectangle(x1,y1,width,height,4)
        cr.stroke
        #draw collisions
        cr.set_source_rgba(1,0,0,0.1)
        @gantt.collisions(self).each{|coo|
                cr.rectangle(to_gui(coo[0]),0,to_gui(coo[1])-to_gui(coo[0]),@gantt.widget.allocation.height)
                cr.fill
                cr.stroke
        }
        #draw percentage
        if @gantt.percentage_column
                cr.set_source_rgba((1-@color.red.to_f)/0x10000,(1-@color.green.to_f)/0x10000,(1-@color.blue.to_f)/0x10000,if @has_child then 0.05 else 0.3 end)
                cr.rounded_rectangle(x1,y1+1,@percentage*width/100,height-2,3)
                cr.fill
                cr.stroke
        end
        self
end
draw_arrow(cr) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 163
                def draw_arrow(cr)
                        #successors arrow
                        @successors.each_value{|pred|
                                if pred && visible? && pred.visible?
                                        cr.set_line_width(2).set_source_rgba(0.1,0.1,0.1,0.7).set_dash(100000)
                                        xx1=[x+10,x2].min
                                        yy4=if y<pred.y then pred.y1-1 else pred.y2+1 end
                                        yya=if y<pred.y then -3 else 3 end
                                        cr.move_to(xx1,y)
                                        cr.curve_to(pred.x,y,pred.x,y,pred.x,yy4)
#                                       cr.stroke
=begin                                  
                                        cr.set_source_rgba(1,0,0,0.5)
                                        cr.move_to(pred.x,y)
                                        cr.line_to(pred.x,yy4)

                                        cr.stroke

                                        cr.set_source_rgba(1,1,0,0.5)
                                        cr.move_to(xx1,y)
                                        cr.line_to(pred.x,y)
                                        cr.stroke
=end
                                        #arrow
                                        cr.move_to(pred.x,yy4).rel_line_to(-3,yya)
                                        cr.move_to(pred.x,yy4).rel_line_to(3,yya)
                                        cr.stroke
                                end
                        }
                        self
                end
draw_hilight(cr) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 221
def draw_hilight(cr)
        return self unless visible?
        cr.set_source_rgba(0.5,0.5,0.5,0.3).set_dash(100000).set_line_cap(Cairo::LineCap::BUTT)
        cr.set_line_width(height).move_to(0,y).line_to(x2,y).stroke
        cr.set_line_width(width).move_to(x,0).line_to(x,y2).stroke
        cr.set_line_cap(Cairo::LineCap::ROUND)
end
draw_verticals(cr) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 210
def draw_verticals(cr)
        return self unless visible?
        #verticals
        cr.set_source_rgba(48.0/255,88.0/255,116.0/255,0.4).set_line_width(0.7).set_dash(5)
        cr.move_to(x1,0)
        cr.rel_line_to(0,y1)
        cr.move_to(x2,0)
        cr.rel_line_to(0,y1)
        cr.stroke
        self
end
ffinish() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 125
def ffinish #finish formatted
        if @gantt.list.column_by_id(@gantt.list.list_model.column_of_gantt_start['id'])
                Time.at(finish || 0).strftime(@gantt.list.column_by_id(@gantt.list.list_model.column_of_gantt_start['id']).timestamp_format)
        end
end
finish() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 49
def finish
        @start+@dur
end
fstart() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 120
def fstart #start formatted
        if @gantt.list.column_by_id(@gantt.list.list_model.column_of_gantt_start['id'])
                Time.at(start || 0).strftime(@gantt.list.column_by_id(@gantt.list.list_model.column_of_gantt_start['id']).timestamp_format)
        end
end
has_child?() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 309
def has_child?
        @has_child
end
in_area?(x,y) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 96
def in_area?(x,y)
        x1<=x && x<=x2 && y1<y && y<y2
end
init_successors() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 265
def init_successors
        @successors.each_key{|k| @successors[k]=@gantt.rectangles[k]}
end
iter_coordinates() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 67
def iter_coordinates
        r=@gantt.list.get_background_area(@gantt.list.model.convert_child_path_to_path(@gantt.list.model.model.convert_child_path_to_path(iter.path)),nil)
        @y1=r.y+@gantt.vadjustment.value+r.height/3
        @height=r.height*2/3
end
move_finished() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 245
def move_finished
        set_start(@start,true)
        push_successors(true)
end
move_to(new_x,store=false) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 233
def move_to(new_x,store=false)
        return unless @gantt.start_editable
        new_start=@gantt.res*(@gantt_min_x+new_x)
        @store_start=true if @start!=new_start
        @start=new_start
        min=@gantt.min_for_rectangle(self)
        new_start=min if new_start<min
        @start=new_start
        @gantt.clear
        push_successors(store)
        set_start(@start,store)
end
partial_duration() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 87
def partial_duration
        @dur*@partial.to_f/100.0
end
partial_finish() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 90
def partial_finish
        @start+@dur*@partial.to_f/100.0
end
partial_width() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 81
def partial_width
        width*@partial.to_f/100.0
end
partial_x2() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 84
def partial_x2
        x1+partial_width
end
print_percentage(cr) click to toggle source
push_successors(store=false) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 295
def push_successors(store=false)
        successors.each_value{|pred|
                next if pred.nil?
                if store then 
                        pred.move_to(pred.x1,store)
                else 
                        pred.move_to(partial_x2,store)  if pred.start < @start+partial_duration
                end
                pred.push_successors(store)
        }
end
px() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 108
def px
        px1+pwidth/2 if px1 && pwidth
end
px2() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 114
def px2
        px1+pwidth if px1 && pwidth
end
py() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 111
def py
        py1+pheight/2 if py1 && pheight
end
py2() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 117
def py2
        py1+pheight if py1 && pheight
end
recursion?(starter=nil) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 250
def recursion?(starter=nil)
        ret=false
        successors.each_value{|pred|
                next if pred.nil?
                #avoid recursion
                if starter == pred
                        ret=true
                        ewarn("recursion")
                        return ret
                end
                ret=pred.recursion?(if starter.nil? then self else starter end)
        }
        ret
end
set_duration(ndur,store=false) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 53
def set_duration(ndur,store=false)
        @dur=ndur
        @gantt.list.list_model.change_value_of_id(iter_id,@gantt.list.gtk_attribute('gantt_duration'),@dur) if store
end
set_gantt_min_x(gmin_x) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 229
def set_gantt_min_x(gmin_x)
        @gantt_min_x=gmin_x
end
set_height(new_height) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 63
def set_height(new_height)
        @height=new_height
end
set_percentage(npercentage,store=false) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 58
def set_percentage(npercentage,store=false)
        @percentage=npercentage
        @gantt.list.list_model.change_value_of_id(iter_id,@gantt.list.gtk_attribute('gantt_percentage'),@percentage) if store && @gantt.percentage_column
end
set_start(new_start,store=false) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 288
def set_start(new_start,store=false)
        #save start
        @start=new_start
        if store
                @gantt.list.list_model.change_value_of_id(iter_id,@gantt.list.gtk_attribute('gantt_start'),@start)
        end
end
to_gui(x) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 75
def to_gui(x)
        x.to_f/@gantt.res-@gantt_min_x
end
to_s() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 312
def to_s
        "Rectangle(id:#{iter_id})"
end
toggle_successor(pr) click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 269
def toggle_successor(pr)
        return false if pr.nil?
        if ret=((@successors.has_key?(pr.iter_id)) || (pr==self))
                #this successor exists, removing
                @successors.delete(pr.iter_id)
        else
                #adding new successor
                @successors[pr.iter_id]=pr
                @successors.delete(pr.iter_id) if recursion?
        end

        #pr is successors rectangle, saving here
        pids=""
        @successors.each_key{|k| pids="#{pids}#{k},"}
        @gantt.list.list_model.change_value_of_id(iter_id,@gantt.list.gtk_attribute('gantt_successors'),pids.chomp(","))
        push_successors(true)
        ret
end
visible?() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 306
def visible?
        @visible && (@parent_r.nil? || y1!=@parent_r.y1)
end
width() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 99
def width
        @dur/@gantt.res
end
x() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 102
def x
        x1+width/2
end
x1() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 72
def x1
        @start/@gantt.res-@gantt_min_x
end
x2() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 78
def x2
        x1+width
end
y() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 105
def y
        y1+height/2
end
y2() click to toggle source
# File lib/ListHolder/GanttHolder/Gantt/Rectangle.rb, line 93
def y2
        @y1+@height
end