class Etti::Window

Attributes

dataPage[R]
labelLayoutPage[R]
labelSelectionPage[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/etti/ui/window.rb, line 9
def initialize
    super()
    signal_connect(:delete_event) {on_quit false}
    signal_connect(:destroy) {on_quit true}
    self.border_width = 3
    self.title = 'Etti'
    set_default_size 1200, 900
    self.icon =  Etti::PIXMAP_PATH + 'win-logo.png'
    @logoPixbuf = Gdk::Pixbuf.new Etti::PIXMAP_PATH + 'logo.png'

    # the current document, if is read from a file or already saved
    @document = nil
    @documentFileFilter = Gtk::FileFilter.new 
    @documentFileFilter.add_pattern '*\.' + DOCUMENT_SUFFIX

    @printPageSetup = nil
    @saveButtonActivated = false

    @accelGroup = Gtk::AccelGroup.new
    add_accel_group @accelGroup

    create_data_page
    create_label_selection_page
    create_label_layout_page
    create_about_page

    @labelSelectionPage.signal_connect(:page_changed) do |widget, pageData|
        @labelLayoutPage.update_page_data pageData
    end
    
    @labelLayoutPage.layoutPreview.signal_connect(:element_count_changed) do |widget, count|
        @saveButton.sensitive = (count > 0 and @saveButtonActivated)
        @saveAsButton.sensitive = count > 0
    end

    @labelLayoutPage.update_page_data @labelSelectionPage.pageData

    pile = Rgw::Pile.new
    self.add pile
    @notebook = Gtk::Notebook.new
    pile.set_child @notebook
    pile.set_pile_child create_toolbar, -24, 6

    notebook_append_page @dataPage, _("<b>  Data  \n  Input  </b>")
    notebook_append_page @labelSelectionPage, _("<b>  Label  \n  Selection  </b>")
    notebook_append_page @labelLayoutPage, _("<b>  Label  \n  Formatting  </b>")
    notebook_append_page @aboutPage, _("<b>  About Etti  </b>")
end

Public Instance Methods

activate_save(state) click to toggle source
# File lib/etti/ui/window.rb, line 284
def activate_save state
    @saveButtonActivate = state
    @saveButton.sensitive = (state and @labelLayoutPage.layoutPreview.elements.length() > 0)
end
create_about_page() click to toggle source
# File lib/etti/ui/window.rb, line 89
def create_about_page
    @aboutPage = Gtk::Box.new :vertical
    img = Gtk::Image.new :pixbuf => @logoPixbuf
    @aboutPage.pack_start img, :expand => false, :fill => false, :padding => 36

    label = Gtk::Label.new
    @aboutPage.pack_start label, :expand => false, :fill => false, :padding => 6
    label.selectable = true
    label.can_focus = false
    label.markup = _("Copyright © 2014 Detlef Reichl   detlef!reichl()gmx!org\n\n\n" +
    
        "QR Code is a registered trademark of DENSO WAVE INCORPORATED.\n\n") +
    
        "This program is free software; you can redistribute it and/or modify\n" +
        "it under the terms of the GNU General Public License as published by\n" +
        "the Free Software Foundation; either version 2 of the License, or\n" +
        "(at your option) any later version.\n\n" +

        "This program is distributed in the hope that it will be useful,\n" +
        "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
        "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" +
        "GNU General Public License for more details.\n\n" +

        "You should have received a copy of the GNU General Public License along\n" +
        "with this program; if not, write to the Free Software Foundation, Inc.,\n" +
        "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."
end
create_data_page() click to toggle source
# File lib/etti/ui/window.rb, line 73
def create_data_page
    @dataPage = Etti::DataPage.new
    @dataPage.signal_connect(:data_changed) {|widget, head, data| @labelLayoutPage.set_element_data head, data}
end
create_label_layout_page() click to toggle source
# File lib/etti/ui/window.rb, line 84
def create_label_layout_page
    @labelLayoutPage = Etti::LabelLayoutPage.new
end
create_label_selection_page() click to toggle source
# File lib/etti/ui/window.rb, line 79
def create_label_selection_page
    @labelSelectionPage = Etti::LabelSelectionPage.new
end
create_toolbar() click to toggle source
# File lib/etti/ui/window.rb, line 118
def create_toolbar
    vbox = Gtk::Box.new :vertical
    toolbar = Gtk::Box.new :horizontal
    
    logo = Gtk::Image.new :pixbuf => @logoPixbuf #   Etti::PIXMAP_PATH + 'logo.png'
    toolbar.pack_end logo, :expand => false, :fill => false, :padding => 32
    
    
    vbox.pack_start toolbar, :expand => false, :fill => false, :padding => 6
    toolbarData = [{:stock => Gtk::Stock::OPEN,
                    :method => proc{on_open},
                    :tooltip => _('open a new document')},
                   {:stock => Gtk::Stock::SAVE,
                    :method => proc{on_save true},
                    :tooltip => _('save the document')},
                   {:stock => Gtk::Stock::SAVE_AS,
                    :method => proc{on_save false},
                    :tooltip => _('save the document under a new name')},
                   {:stock => Gtk::Stock::PRINT,
                    :method => proc{on_print},
                    :tooltip => _('print the labels')},
                    :separator,
                   {:stock => Gtk::Stock::ZOOM_IN,
                    :method => proc{@labelLayoutPage.layoutPreview.zoom_in},
                    :tooltip => _('zoom in')},
                   {:stock => Gtk::Stock::ZOOM_OUT,
                    :method => proc{@labelLayoutPage.layoutPreview.zoom_out},
                    :tooltip => _('zoom out')},
                   {:stock => Gtk::Stock::ZOOM_FIT,
                    :method => proc{@labelLayoutPage.layoutPreview.zoom_fit},
                    :tooltip => _('zoom that all is displayed')}]
    toolbarData.each_with_index do |buttonData, idx|
        if buttonData.is_a? Hash
            button = Gtk::Button.new
            if idx == 1
                @saveButton = button
                button.sensitive = false
            elsif idx == 2
                @saveAsButton = button
                button.sensitive = false
            end
            button.tooltip_text = buttonData[:tooltip]
            button.signal_connect(:clicked) {buttonData[:method].call}
            img = Gtk::Image.new :stock => buttonData[:stock], :size => :large_toolbar
            button << img
            toolbar.pack_start button, :expand => false, :fill => false, :padding => 0
        elsif buttonData.is_a? Symbol
            sep = Gtk::Separator.new :vertical
            toolbar.pack_start sep, :expand => false, :fill => false, :padding => 8
        end
    end
    vbox
end
notebook_append_page(page, label) click to toggle source
# File lib/etti/ui/window.rb, line 60
def notebook_append_page page, label
    ali = Gtk::Alignment.new 0, 0, 1, 1
    ali.set_padding 8, 8, 8, 8
    ali.add page
    @notebook.append_page ali, Gtk::Label.new.set_markup(label)

    pg = @notebook.n_pages
    return if pg > 9
    @accelGroup.connect(Gdk::Keyval.from_name(pg.to_s), :mod1_mask, :visible) {@notebook.page = pg - 1}
    @accelGroup.connect(Gdk::Keyval.from_name('KP_' + pg.to_s), :mod1_mask, :visible) {@notebook.page = pg - 1}
end
on_open() click to toggle source
# File lib/etti/ui/window.rb, line 173
def on_open
    fileChooser = Gtk::FileChooserDialog.new :title => _('Open File'),
                                             :parent => self.toplevel,
                                             :action => :open,
                                             :buttons => [[:cancel, :cancel],
                                                          [:ok, :ok]]
                                            
    fileChooser.filter = @documentFileFilter
    ret = fileChooser.run
    path = fileChooser.filename.clone
    fileChooser.destroy
    
    return if ret == Gtk::ResponseType::CANCEL
    return if path.nil?
    
    @document = path
    activate_save true

    data = File.new(path).read
    
    begin            
        json = JSON.parse data
    rescue JSON::ParserError => additional
        message = _("The document contains errors\nMessage from the file reader:")
        
        dialog = Etti::MessageDialog.new self.toplevel, :buttons => [:close],
                                          :type => :info, :message => message,
                                          :title => _('Invalid Entries'),
                                          :additional => additional.to_s
        dialog.run
        return
    end 
    
    
    unless json['Etti'] == 'document file'
        message = _("The document file\n\"%s\"\nis not a valid etti document") % path
        dialog = Etti::MessageDialog.new self.toplevel, :buttons => [:close],
                                          :type => :info, :message => message,
                                          :title => _('Invalid Entries')
        dialog.run
        return
    end

    # restore all settings
    sdat = [[@dataPage,           json['UserData']],
            [@labelLayoutPage,    json['LabelLayout']],
            [@labelSelectionPage, json['PageData']]]
    return if validate_settings sdat
    sdat.each {|section| section[0].send :set_user_settings, section[1]}
end
on_print() click to toggle source
# File lib/etti/ui/window.rb, line 290
        def on_print
            # set the borders from the selected label sheet
            pd = @labelSelectionPage.pageData
            lp = @labelLayoutPage.layoutPreview
            pageSetup = Gtk::PageSetup.new
            pageSetup.set_left_margin pd[:borders][0], :mm
            pageSetup.set_top_margin pd[:borders][1], :mm
            pageSetup.set_right_margin pd[:borders][0], :mm
            pageSetup.set_bottom_margin pd[:borders][1], :mm
            
            
            printOperation = Gtk::PrintOperation.new
            printOperation.show_progress = true 
            printOperation.default_page_setup = pageSetup
            printOperation.n_pages = (lp.elementDataData.length().to_f /
                                      (pd[:cols] * pd[:rows]).to_f).ceil
            
            printOperation.signal_connect('draw-page') do |widget, printContext, pageNumber|
                cc = printContext.cairo_context
                lp.print_labels cc, pageNumber
            end

            ret = printOperation.run :print_dialog, self.toplevel

=begin
            # only for debugging purpose
            printOperation.signal_connect('end-print') {print "-=== printing ended\n"}

            printOperation.signal_connect('status-changed') do
                case printOperation.status
                    when Gtk::PrintOperation::Status::INITIAL
                        p 'initial'
                    when Gtk::PrintOperation::Status::PREPARING
                        p 'preparing'
                    when Gtk::PrintOperation::Status::GENERATING_DATA
                        p 'GENERATING_DATA'
                    when Gtk::PrintOperation::Status::SENDING_DATA
                        p 'SENDING_DATA'
                    when Gtk::PrintOperation::Status::PENDING
                        p 'PENDING'
                    when Gtk::PrintOperation::Status::PRINTING
                        p 'PRINTING'
                    when Gtk::PrintOperation::Status::FINISHED
                        p 'FINISHED'
                    when Gtk::PrintOperation::Status::FINISHED_ABORTED
                        p 'FINISHED_ABORTED'
                    else
                        'p unknown'
                end
            end

            case ret
                when Gtk::PrintOperation::Result::ERROR
                  print "error\n"
                when Gtk::PrintOperation::Result::CANCEL
                  print "cancelled\n"
                when Gtk::PrintOperation::Result::APPLY
                  print "applied\n"
                when Gtk::PrintOperation::Result::IN_PROGRESS
                  print "in progress\n"
                else
                  print "unknown result: ",printr.inspect,"\n"
            end
=end
        end
on_quit(destroy) click to toggle source
# File lib/etti/ui/window.rb, line 357
        def on_quit destroy
#            @project.save_project
            Gtk.main_quit
        end
on_save(same) click to toggle source
# File lib/etti/ui/window.rb, line 242
def on_save same
    path = @document
    unless same
        fileChooser = Gtk::FileChooserDialog.new :title => _('Save File'),
                                                 :parent => self.toplevel,
                                                 :action => :save,
                                                 :buttons => [[:cancel, :cancel],
                                                              [:ok, :ok]]
                                                
        fileChooser.filter = @documentFileFilter
        ret = fileChooser.run
        path = fileChooser.filename.clone
        fileChooser.destroy
        
        return if ret == Gtk::ResponseType::CANCEL
        return if path.nil?

        path += '.' + DOCUMENT_SUFFIX unless path.end_with? '.' + DOCUMENT_SUFFIX
        
        if File.exist? path
            message = _("Shall the document\n\"%s\"\nbe replaced?") % File.basename(path)
            dialog = Etti::MessageDialog.new self.toplevel, :buttons => [:yes, :cancel],
                         :type => :info, :message => message
            ret = dialog.run
            return unless ret == 'yes'
        end
    end
    
    @document = path
    activate_save true

    json = {'Etti' => 'document file', 'Version' => [1, 0]}
    json['UserData'] = @dataPage.get_user_settings
    json['PageData'] = @labelSelectionPage.get_user_settings
    json['LabelLayout'] = @labelLayoutPage.get_user_settings
    
    fd = File.new path, File::CREAT|File::TRUNC|File::RDWR, 0644
    fd.write JSON.pretty_generate(json, :indent => '    ')
    fd.flush
end
validate_settings(data) click to toggle source
# File lib/etti/ui/window.rb, line 225
def validate_settings data
    data.each do |section|
        ret = section[0].send :validate_user_settings, section[1]
        if ret
            message = _('The file could not be loaded.')
            dialog = Etti::MessageDialog.new self.toplevel, :buttons => [:close],
                                              :type => :error, :message => message,
                                              :title => _('File load error'),
                                              :additional => ret
            dialog.run
            return true
        end
    end
    false
end