class PrintEditorItem

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

dragging[R]
drawing[R]
editable[R]
font[R]
gtk_type[R]
height[R]
id[R]
page_layout[R]
pagenum[R]
text_alignment[R]
width[R]
x[R]
y[R]

Public Class Methods

new(page_layout,page_num,id,x,y,width,height,gtk_type,text,font,text_alignment="left") click to toggle source
Calls superclass method
# File lib/PrintEditor/PrintEditorItem.rb, line 7
def initialize(page_layout,page_num,id,x,y,width,height,gtk_type,text,font,text_alignment="left")
        @id=id
        @x=x
        @y=y
        @pagenum=page_num
        @page_layout=page_layout
        @gtk_type=gtk_type
        @editable=true
        @width=width
        @height=height
        @text=text
        @font=font
        @dragging=false
        @resizing=false
        super()
        @child=case gtk_type
                when 'line','rectange' then Gtk::DrawingArea.new
                when 'text' then Gtk::TextView.new#.set_editable(true).set_sensitive(true)
                when 'list' then Gtk::Frame.new("list")
                when 'image' then Gtk::Image.new(Gtk::Stock::MISSING_IMAGE,Gtk::IconSize::DIALOG)
                else Gtk::Label.new("unknown object")
        end
        modify_font
        @child.modify_base(Gtk::StateType::PRELIGHT,Gdk::Color.parse("#EFEFEF"))
        @child.modify_base(Gtk::StateType::NORMAL,Gdk::Color.parse("#FBFBFB"))
        set_text_alignment(text_alignment)
        add(@child)
        set_size_request(width,height)
        set_can_focus(true)
        set_visible_window(true)
        set_border_width(0)
        set_above_child(true)# unless @gtk_type == "text"
        show_all
        @child.signal_connect('expose-event'){|me,event|
                case gtk_type
                        when 'line'
                                cr=me.window.create_cairo_context
                                cr.scale(print_editor.zoom,print_editor.zoom)
                                cr.move_to(0,0).line_to(@width,@height)
                                cr.stroke
                        when 'rectangle'
                                cr=me.window.create_cairo_context
                                cr.scale(print_editor.zoom,print_editor.zoom)
                                cr.rectangle(0,0,@width,@height)
                                cr.stroke
                        when 'text',"list","image"
                       else
                                edebug("unknown print-type: #{gtk_type}","printing","warning")
                end
        }
        signal_connect('button-press-event'){|me,event|
                if event.event_type == Gdk::Event::BUTTON2_PRESS then
                        set_above_child(false) if @gtk_type == "text"
                else
                        print_editor.set_focused_item(self)
                        if event.state.control_mask?
                                @xshift=event.x
                                @yshift=event.y
                                @owidth=@width*zoom
                                @oheight=@height*zoom
                                @pX,@pY=parent.pointer
                                case event.button
                                        when 1 then @dragging=true
                                        when 2 then @resizing=true
                                end
                        end
                end
                true
        }
        signal_connect('motion-notify-event'){|me,event|
        snap=5
                if @dragging or @resizing
                        pe=print_editor
                        pe.set_focused_item(self)
                        px=parent.pointer[0]
                        py=parent.pointer[1]
                        if @dragging
                                info.item_x.set_value(((px-@xshift)/zoom/snap).round*snap)
                                info.item_y.set_value(((py-@yshift)/zoom/snap).round*snap)
                        end
                        if @resizing
                                info.item_width.set_value(((@owidth+px-@pX)/zoom/snap).round*snap)
                                info.item_height.set_value(((@oheight+py-@pY)/zoom/snap).round*snap)
                        end
                
                end
        }
        signal_connect('button-release-event'){|me,event| @dragging=false;@resizing=false;}

end

Public Instance Methods

info() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 100
def info
        print_editor.info
end
modify_font(new_font=@font) click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 158
def modify_font(new_font=@font)
if @gtk_type=="text"
                @font=new_font
                edebug("setting font :"+@font,"printing","info")
                fontdesc=Pango::FontDescription.new(@font)
                @child.modify_font(fontdesc) unless fontdesc.nil?
        end
  self
end
moditemid() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 219
def moditemid
        print_editor.moditemid
end
move_me(newx=@x,newy=@y,new_page=@page_num) click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 111
def move_me(newx=@x,newy=@y,new_page=@page_num)
        @x=newx
        @y=newy
        @page_num=new_page
        page_layout.move(self)
end
print_editor() click to toggle source
remove_from_mysql() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 214
def remove_from_mysql
        query("delete from gtkprintitems where id='#{id}'")
        edebug("deleted","printing","info")
end
set_editable(editable) click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 150
def set_editable(editable)
        if @gtk_type=="text"
                @child.set_editable(editable)
                @child.set_sensitive(editable)
        end
        self
end
set_size_request(new_width=@width,new_height=@height) click to toggle source
Calls superclass method
# File lib/PrintEditor/PrintEditorItem.rb, line 118
def set_size_request(new_width=@width,new_height=@height)
        if @gtk_type == "image" and !@image_iter.nil?
                #image keep aspect ratio
                im=DrbImages.instance.client_image_of_id(@image_iter[0])
                if @width != new_width then
                        @width=new_width
                        @height=im.height*@width / im.width
                        info.item_height.set_value(@height)
                        else
                        @height=new_height
                        @width=im.width*@height / im.height
                        info.item_width.set_value(@width)
                end
                super(width*zoom,height*zoom)
                @child.set_pixbuf(im.scale(@width*zoom,@height*zoom))
                page_layout.print_editor.tips.set_tip(self,"#{im.width}x#{im.height} (#{sprintf('%.2f',@width / im.width)})",'extra hint')
                else
                #not image
                @width=new_width
                @height=new_height
                super(width*zoom,height*zoom)
        end
end
set_text(newtext=@text) click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 180
def set_text(newtext=@text)
        @text=newtext
        case @gtk_type
                when "text" then text_from_mysql(@text)
                when "image" then 
                        @image_iter=nil
                        info.item_image.model.each{|model,path,iter|
                                        @image_iter=iter if @text.to_i == iter[0]
                        }
                        if @image_iter.nil?
                                @child=Gtk::Image.new(Gtk::Stock::MISSING_IMAGE,Gtk::IconSize::DIALOG)
                        else
                                @child.set_pixbuf(DrbImages.instance.client_image_of_id(@image_iter[0]).scale(@width,@height))
                        end
        end
        self
end
set_text_alignment(na="left") click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 167
def set_text_alignment(na="left")
        @text_alignment=na
        if @gtk_type == "text"
                @child.set_justification(
                        case na
                                when "right" then Gtk::Justification::RIGHT
                                when "center" then Gtk::Justification::CENTER
                                else Gtk::Justification::LEFT
                        end
                )
        end
end
text() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 142
def text
        case @gtk_type
                when "text" then @text=text_to_mysql
                when "list","image" then @text
                else ""
        end
end
text_from_mysql(nt=text) click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 227
def text_from_mysql(nt=text)
        if @gtk_type=="text"
                ont=nt.clone
                mydata=get_ancestor(WysiwygPrintEditor).mydata
                found=false
                while first=nt.index("{")
                        found=true
                        @child.buffer.insert_at_cursor(nt[0 .. first-1]) if first >= 1
                        if last=nt[first .. nt.length].index("}")
                                last=last+first
                                datak=nt[first+1 .. last-1]
                                if mydata.has_key?(datak)
                                        mydata[datak]="N/A" if mydata[datak].nil? || mydata[datak] == ""
                                        edebug("has key: [#{datak}] -> [#{mydata[datak]}]","printing","debug")
                                        unless tag = @child.buffer.tag_table.lookup(datak)
                                                tag=Gtk::TextTag.new(datak).set_foreground_gdk(Gdk::Color.new(65000,0,0))
                                                @child.buffer.tag_table.add(tag)
                                        end
                                        @child.buffer.insert_at_cursor(mydata[datak])
                                        bounds=@child.buffer.selection_bounds
                                        bounds[0].backward_cursor_positions(mydata[datak].length)
                                        @child.buffer.apply_tag(tag,bounds[0],bounds[1])
                                        nt=nt[last+1 .. nt.length+1]
                                else
                                        edebug("no key: #{datak}","printing","debug")
                                        @child.buffer.insert_at_cursor(datak)               
                                        nt=nt[last+1 .. nt.length+1]
                                end
                        else
                                nt=nt[first+1 .. nt.length+1]
                                @child.buffer.insert_at_cursor("{"+nt)
                        end
                end

                @child.buffer.insert_at_cursor(nt) if nt.length>0
                edebug("from_mysql: [#{ont.inspect}] -> buffer: [#{@child.buffer.text}]","printing","debug")
        end
end
text_to_mysql() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 266
def text_to_mysql
        if @gtk_type=="text"
                i=@child.buffer.bounds[0]
                tag="start"
                plast=nil
                while !tag.nil?
                        tag=nil
                        first=nil
                        if i.begins_tag?(nil) then 
                                first=i.offset;
                                tag=i.toggled_tags(true)[0]
                        else
                                if i.forward_to_tag_toggle(nil) then
                                        first=i.offset
                                        tag=i.toggled_tags(true)[0]
                                end
                        end
                        if tag and i.forward_to_tag_toggle(tag) then last=i.offset;end
                        unless tag.nil?
                                if plast.nil?
                                        ret=@child.buffer.get_text(@child.buffer.bounds[0],@child.buffer.get_iter_at_offset(first))
                                        else
                                        ret="#{ret}#{@child.buffer.get_text(@child.buffer.get_iter_at_offset(plast),@child.buffer.get_iter_at_offset(first))}"
                                end
                                ret="#{ret}{#{tag.name}}"
                                plast=last
                        end
                end
                if plast.nil?
                        ret=@child.buffer.text
                else
                        ret="#{ret}#{@child.buffer.get_text(@child.buffer.get_iter_at_offset(plast),@child.buffer.bounds[1])}"
                end
                edebug("to_mysql: [#{@child.buffer.text}] -> [#{ret}]","printing","debug")
        else
                ret=text
        end
        ret
end
to_mysql() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 205
def to_mysql
        if id.nil?
                query("insert into gtkprintitems set printid='#{moditemid}', gtktype='#{gtk_type}', page_number='#{pagenum}', x='#{x}', y='#{y}',  width='#{width}', height='#{height}', txt='#{text_to_mysql}', font='#{font}', text_alignment='#{text_alignment}'")
        else
                query("update gtkprintitems set printid='#{moditemid}', gtktype='#{gtk_type}', page_number='#{pagenum}', x='#{x}', y='#{y}',  width='#{width}', height='#{height}', txt='#{text_to_mysql}', font='#{font}', text_alignment='#{text_alignment}' where id='#{id}'")
        end
        edebug("saved","printing","info")
end
to_s() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 306
def to_s
        "PrintItem{#{@gtk_type},id:#{id}}"
end
unfocus() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 198
def unfocus
        @dragging=false
        set_state(Gtk::STATE_NORMAL)
        set_above_child(true)
        edebug("deselected","printing","info")
end
zoom() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 107
def zoom
        print_editor.zoom
end
zoomed() click to toggle source
# File lib/PrintEditor/PrintEditorItem.rb, line 103
def zoomed
        move_me
        set_size_request
end