Class: Clevic::Browser

Inherits:
Qt::Widget
  • Object
show all
Defined in:
lib/clevic/qt/browser.rb,
lib/clevic/swing/browser.rb

Overview

The main application class. Display one tabs for each descendant of Clevic::View in Clevic::View.order. DefaultView classes created by Clevic::Record are included.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBrowser

Returns a new instance of Browser.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/clevic/qt/browser.rb', line 18

def initialize( main_window )
  super( main_window )

  # do menus and widgets
  @layout = Ui::Browser.new
  @layout.setup_ui( main_window )

  # set icon. MUST come after call to setup_ui
  icon_path = Pathname.new( __FILE__ ).parent + "../icons/icon.png"
  Kernel::raise "icon.png not found" unless icon_path.file?
  main_window.window_icon = Qt::Icon.new( icon_path.realpath.to_s )

  # add the tables tab
  @tables_tab = Qt::TabWidget.new( @layout.main_widget )
  @layout.main_widget.layout.add_widget @tables_tab
  @tables_tab.tab_bar.focus_policy = Qt::NoFocus

  # hide the file menu, for now
  @layout.menubar.remove_action( @layout.menu_file.menu_action )

  # tab navigation
  @layout.action_next.connect       SIGNAL( 'triggered()' ),          &method( :next_tab )
  @layout.action_previous.connect   SIGNAL( 'triggered()' ),          &method( :previous_tab )

  # dump model for current tab
  @layout.action_dump.visible = $options[:debug]
  @layout.action_dump.connect       SIGNAL( 'triggered()' ),          &method( :dump )

  tables_tab.connect                SIGNAL( 'currentChanged(int)' ),  &method( :current_changed )

  load_views
  update_menus
  main_window.window_title = [database_name, 'Clevic'].compact.join ' '
end

Instance Attribute Details

Returns the value of attribute menu_edit.



12
13
14
# File 'lib/clevic/swing/browser.rb', line 12

def menu_edit
  @menu_edit
end

Returns the value of attribute menu_search.



12
13
14
# File 'lib/clevic/swing/browser.rb', line 12

def menu_search
  @menu_search
end

Returns the value of attribute menu_table.



12
13
14
# File 'lib/clevic/swing/browser.rb', line 12

def menu_table
  @menu_table
end

#tables_tabObject (readonly)

Returns the value of attribute tables_tab.



16
17
18
# File 'lib/clevic/qt/browser.rb', line 16

def tables_tab
  @tables_tab
end

Class Method Details

.run(args) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/clevic/qt/browser.rb', line 175

def self.run( args )
  # load model files
  raise "no model definition file specified" if args.empty?
  args.each { |arg| load_models( Pathname.new( arg ) ) }

  app = Qt::Application.new( args )

  # show UI
  main_window = Qt::MainWindow.new
  browser = Clevic::Browser.new( main_window )
  # this must come after Clevic::Browser.new
  main_window.show
  # make sure any partially edited records are saved when the window is closed
  app.connect( SIGNAL('lastWindowClosed()') ) { browser.save_all }
  begin
    app.exec
  rescue
    puts $!.message
    puts $!.backtrace
  end
end

Instance Method Details

#current_changedObject

slot to handle the currentChanged signal from tables_tab, and set focus on the grid



112
113
114
115
# File 'lib/clevic/qt/browser.rb', line 112

def current_changed( current_tab_index )
  update_menus
  tables_tab.current_widget.set_focus
end

#database_nameObject

Set the main window title to the name of the database, if we can find it.



54
55
56
# File 'lib/clevic/qt/browser.rb', line 54

def database_name
  table_view.model.entity_class.db.url rescue ''
end

#dumpObject

activated by Ctrl-Shift-D for debugging



80
81
82
83
# File 'lib/clevic/qt/browser.rb', line 80

def dump
  puts "table_view.model: #{table_view.model.inspect}"
  puts "table_view.model.entity_class: #{table_view.model.entity_class.inspect}"
end

#iconObject



89
90
91
92
93
94
95
96
# File 'lib/clevic/swing/browser.rb', line 89

def icon
  @icon ||=
  begin
    icon_path = Pathname.new( __FILE__ ).parent.parent + "icons/icon.png"
    raise "icon.png not found" unless icon_path.file?
    javax.swing.ImageIcon.new( icon_path.realpath.to_s ).image
  end
end

#init_connections(tab) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/clevic/swing/browser.rb', line 234

def init_connections( tab )
  tab.emit_status_text do |msg|
    status_bar.text = msg
    # hide the message after a while.
    status_bar_timer.start
  end

  # handle filter status changed, so we can provide a visual indication
  tab.emit_filter_status do |status|
    # update the tab, so there's a visual indication of filtering
    filter_title = ( tab.filtered? ? '| ' : '' ) + tab.title
    tables_tab.set_title_at( tables_tab.selected_index, filter_title )
    tables_tab.set_tool_tip_text_at( tables_tab.selected_index, tab.filter_message )
  end
end

#load_viewsObject

Create the tabs, each with a collection for a particular entity class. views come from Clevic::View.order



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/clevic/qt/browser.rb', line 124

def load_views
  views = Clevic::View.order.uniq
  Kernel.raise "no views to display" if views.empty?

  # Add all existing model objects as tabs, one each
  views.each do |view_class|
    begin
      view = view_class.new

      # This will raise an exception if we can't talk to the
      # table. Returns nil if there is an empty table, which is
      # also fine.
      view.entity_class.first

      # create the the table_view and the table_model for the entity_class
      tab = Clevic::TableView.new( view )

      # show status messages
      tab.connect( SIGNAL( 'status_text_signal(QString)' ) ) { |msg| @layout.statusbar.show_message( msg, 10000 ) }

      # add a new tab
      tables_tab.add_tab( tab, translate( tab.title ) )

      # add the table to the Table menu
      action = Qt::Action.new( @layout.menu_model )
      action.text = translate( tab.title )
      action.connect SIGNAL( 'triggered()' ) do
        tables_tab.current_widget = tab
      end
      @layout.menu_model.add_action( action )

      # handle filter status changed, so we can provide a visual indication
      tab.connect SIGNAL( 'filter_status_signal(bool)' ) do |status|
        # update the tab, so there's a visual indication of filtering
        filter_title = ( tab.filtered? ? '| ' : '' ) + translate( tab.title )
        tables_tab.set_tab_text( tables_tab.current_index, filter_title )
      end
    rescue Exception => e
      puts
      puts "UI from #{view} will not be available: #{e.message}"
      puts e.backtrace #if $options[:debug]
      puts
    end
  end
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/clevic/swing/browser.rb', line 38

def menu_bar
  javax.swing.JMenuBar.new.tap do |menu_bar|
    menu_bar << javax.swing.JMenu.new( 'File' ).tap do |menu|
      menu.mnemonic = java.awt.event.KeyEvent::VK_F
      menu << "Open"
      menu << "Close"
    end

    @menu_edit = javax.swing.JMenu.new( 'Edit' ).tap {|m| m.mnemonic = java.awt.event.KeyEvent::VK_E}
    menu_bar << menu_edit

    @menu_search = javax.swing.JMenu.new( 'Search' ).tap {|m| m.mnemonic = java.awt.event.KeyEvent::VK_S}
    menu_bar << menu_search

    @menu_table = javax.swing.JMenu.new( 'Table' ).tap do |menu|
      menu.mnemonic = java.awt.event.KeyEvent::VK_T
      menu << Action.new( self ) do |action|
        action.name = :next_tab
        action.text = "&Next"
        action.shortcut = "Ctrl+Tab"
        action.handler do |event|
          puts "next tab"
          next_tab
        end
      end

      menu << Action.new( self ) do |action|
        action.name = :previous_tab
        action.text = "&Previous"
        action.shortcut = "Shift+Ctrl+Tab"
        action.handler do |event|
          puts "previous tab"
          previous_tab
        end
      end

      if $options[:debug]
        menu << Action.new( self ) do |action|
          action.name = :dump
          action.text = "&Dump"
          action.shortcut = "Ctrl+D"
          action.handler do |event|
            dump
          end
        end
      end
    end
    menu_bar << @menu_table
  end
end

#next_tabObject

slot to handle Ctrl-Tab and move to next tab, or wrap around



91
92
93
94
95
96
97
98
# File 'lib/clevic/qt/browser.rb', line 91

def next_tab
  tables_tab.current_index = 
  if tables_tab.current_index >= tables_tab.count - 1
    0
  else
    tables_tab.current_index + 1
  end
end

#previous_tabObject

slot to handle Ctrl-Backtab and move to previous tab, or wrap around



101
102
103
104
105
106
107
108
# File 'lib/clevic/qt/browser.rb', line 101

def previous_tab
  tables_tab.current_index = 
  if tables_tab.current_index <= 0
    tables_tab.count - 1
  else
    tables_tab.current_index - 1
  end
end

#save_allObject

make sure all outstanding records are saved



171
172
173
# File 'lib/clevic/qt/browser.rb', line 171

def save_all
  tables_tab.tabs.each {|x| x.save_row( x.current_index ) }
end

#status_barObject



111
112
113
114
115
116
117
# File 'lib/clevic/swing/browser.rb', line 111

def status_bar
  @status_bar ||= javax.swing.JLabel.new.tap do |status_bar|
    status_bar.horizontal_alignment = javax.swing.SwingConstants::RIGHT
    # just so the bar actually displays
    status_bar.text = "Welcome to Clevic"
  end
end

#status_bar_timerObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/clevic/swing/browser.rb', line 119

def status_bar_timer
  @status_bar_timer ||= javax.swing.Timer.new( 15000, nil ).tap do |timer|
    timer.repeats = false
    # This is only for 1.6
    #~ timer.action_command = 'hide_status_message'
    timer.add_action_listener do |event|
      status_bar.text = nil
      timer.stop
    end
  end
end

#table_viewObject

return the Clevic::TableView object in the currently displayed tab



86
87
88
# File 'lib/clevic/qt/browser.rb', line 86

def table_view
  tables_tab.current_widget
end

#title_stringObject



34
35
36
# File 'lib/clevic/swing/browser.rb', line 34

def title_string
  [database_name, 'Clevic'].compact.join ' '
end

#translate(st) ⇒ Object

shortcut for the Qt translate call



118
119
120
# File 'lib/clevic/qt/browser.rb', line 118

def translate( st )
  Qt::Application.translate("Browser", st, nil, Qt::Application::UnicodeUTF8)
end

#update_menusObject

called by current_changed to update the Edit menu to the menus defined by the currently selected view



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/clevic/swing/browser.rb', line 138

def update_menus
  # update edit menu
  @layout.menu_edit.clear

  # do the model-specific menu items first
  table_view.model_actions.each do |action|
    @layout.menu_edit.add_action( action )
  end

  # now do the generic edit items
  table_view.edit_actions.each do |action|
    @layout.menu_edit.add_action( action )
  end

  # update search menu
  @layout.menu_search.clear
  table_view.search_actions.each do |action|
    @layout.menu_search.add_action( action )
  end
end