Class: Wads::Table

Inherits:
Widget show all
Defined in:
lib/wads/widgets.rb

Overview

Displays a table of information at the given coordinates. The headers are an array of text labels to display at the top of each column. The max_visible_rows specifies how many rows are visible at once. If there are more data rows than the max, the arrow keys can be used to page up or down through the rows in the table.

Direct Known Subclasses

MultiSelectTable, SingleSelectTable

Instance Attribute Summary collapse

Attributes inherited from Widget

#base_z, #children, #gui_theme, #height, #is_selected, #layout, #overlay_widget, #override_color, #text_input_fields, #visible, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Widget

#add, #add_axis_lines, #add_button, #add_child, #add_delete_button, #add_document, #add_graph_display, #add_image, #add_multi_select_table, #add_overlay, #add_panel, #add_plot, #add_single_select_table, #add_table, #add_text, #border_color, #bottom_edge, #button_down, #button_up, #center_children, #center_x, #center_y, #clear_children, #contains_click, #debug, #disable_background, #disable_border, #draw, #draw_background, #draw_border, #enable_background, #enable_border, #error, #get_layout, #get_theme, #graphics_color, #handle_key_held_down, #handle_key_press, #handle_key_up, #handle_mouse_down, #handle_mouse_up, #handle_right_mouse, #info, #intercept_widget_event, #left_edge, #move_recursive_absolute, #move_recursive_delta, #overlaps_with, #pad, #relative_x, #relative_y, #relative_z_order, #remove_child, #remove_children, #remove_children_by_type, #right_edge, #selection_color, #set_absolute_position, #set_dimensions, #set_layout, #set_selected, #set_theme, #text_color, #top_edge, #unset_selected, #update, #warn, #x_pixel_to_screen, #y_pixel_to_screen, #z_order

Constructor Details

#initialize(x, y, width, height, headers, max_visible_rows = 10, args = {}) ⇒ Table

Returns a new instance of Table.



2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
# File 'lib/wads/widgets.rb', line 2281

def initialize(x, y, width, height, headers, max_visible_rows = 10, args = {}) 
    super(x, y)
    set_dimensions(width, height)
    if args[ARG_THEME]
        @gui_theme = args[ARG_THEME]
    end
    @headers = headers
    @current_row = 0
    @max_visible_rows = max_visible_rows
    clear_rows
    @can_delete_rows = false
    @delete_buttons = []
    @next_delete_button_y = 38
end

Instance Attribute Details

#can_delete_rowsObject

Returns the value of attribute can_delete_rows.



2279
2280
2281
# File 'lib/wads/widgets.rb', line 2279

def can_delete_rows
  @can_delete_rows
end

#current_rowObject

Returns the value of attribute current_row.



2278
2279
2280
# File 'lib/wads/widgets.rb', line 2278

def current_row
  @current_row
end

#data_rowsObject

Returns the value of attribute data_rows.



2274
2275
2276
# File 'lib/wads/widgets.rb', line 2274

def data_rows
  @data_rows
end

#headersObject

Returns the value of attribute headers.



2276
2277
2278
# File 'lib/wads/widgets.rb', line 2276

def headers
  @headers
end

#max_visible_rowsObject

Returns the value of attribute max_visible_rows.



2277
2278
2279
# File 'lib/wads/widgets.rb', line 2277

def max_visible_rows
  @max_visible_rows
end

#row_colorsObject

Returns the value of attribute row_colors.



2275
2276
2277
# File 'lib/wads/widgets.rb', line 2275

def row_colors
  @row_colors
end

Instance Method Details

#add_row(data_row, color = text_color) ⇒ Object



2313
2314
2315
2316
# File 'lib/wads/widgets.rb', line 2313

def add_row(data_row, color = text_color )
    @data_rows << data_row
    @row_colors << color
end

#add_table_delete_buttonObject



2318
2319
2320
2321
2322
2323
2324
2325
2326
# File 'lib/wads/widgets.rb', line 2318

def add_table_delete_button 
    if @delete_buttons.size < @max_visible_rows
        new_button = add_delete_button(@width - 18, @next_delete_button_y) do
            # nothing to do here, handled in parent widget by event
        end
        @delete_buttons << new_button
        @next_delete_button_y = @next_delete_button_y + 30
    end
end

#clear_rowsObject



2308
2309
2310
2311
# File 'lib/wads/widgets.rb', line 2308

def clear_rows 
    @data_rows = []
    @row_colors = []
end

#determine_row_number(mouse_y) ⇒ Object



2414
2415
2416
2417
2418
2419
2420
2421
# File 'lib/wads/widgets.rb', line 2414

def determine_row_number(mouse_y)
    relative_y = mouse_y - @y
    row_number = (relative_y / 30).floor - 1
    if row_number < 0 or row_number > data_rows.size - 1
        return nil 
    end 
    row_number
end

#handle_update(update_count, mouse_x, mouse_y) ⇒ Object



2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
# File 'lib/wads/widgets.rb', line 2336

def handle_update update_count, mouse_x, mouse_y
    # How many visible data rows are there
    if @can_delete_rows
        number_of_visible_rows = @data_rows.size - @current_row
        if number_of_visible_rows > @max_visible_rows
            number_of_visible_rows = @max_visible_rows
        end
        if number_of_visible_rows > @delete_buttons.size
            number_to_add = number_of_visible_rows - @delete_buttons.size 
            number_to_add.times do 
                add_table_delete_button 
            end 
        elsif number_of_visible_rows < @delete_buttons.size
            number_to_remove = @delete_buttons.size - number_of_visible_rows  
            number_to_remove.times do 
                remove_table_delete_button 
            end 
        end
    end
end

#number_of_rowsObject



2357
2358
2359
# File 'lib/wads/widgets.rb', line 2357

def number_of_rows 
    @data_rows.size 
end

#remove_table_delete_buttonObject



2328
2329
2330
2331
2332
2333
2334
# File 'lib/wads/widgets.rb', line 2328

def remove_table_delete_button 
    if not @delete_buttons.empty?
        @delete_buttons.pop
        @children.pop
        @next_delete_button_y = @next_delete_button_y - 30
    end
end

#renderObject



2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
# File 'lib/wads/widgets.rb', line 2361

def render
    draw_border
    return unless number_of_rows > 0

    column_widths = []
    number_of_columns = @data_rows[0].size 
    (0..number_of_columns-1).each do |c| 
        max_length = @gui_theme.font.text_width(headers[c])
        (0..number_of_rows-1).each do |r|
            text_pixel_width = @gui_theme.font.text_width(@data_rows[r][c])
            if text_pixel_width > max_length 
                max_length = text_pixel_width
            end 
        end 
        column_widths[c] = max_length
    end

    # Draw a horizontal line between header and data rows
    x = @x + 10
    if number_of_columns > 1
        (0..number_of_columns-2).each do |c| 
            x = x + column_widths[c] + 20
            Gosu::draw_line x, @y, graphics_color, x, @y + @height, graphics_color, z_order
        end 
    end

    # Draw the header row
    y = @y
    Gosu::draw_rect(@x + 1, y, @width - 3, 28, graphics_color, relative_z_order(Z_ORDER_SELECTION_BACKGROUND)) 

    x = @x + 20
    (0..number_of_columns-1).each do |c| 
        @gui_theme.font.draw_text(@headers[c], x, y + 3, z_order, 1, 1, text_color)
        x = x + column_widths[c] + 20
    end
    y = y + 30

    count = 0
    @data_rows.each do |row|
        if count < @current_row
            # skip
        elsif count < @current_row + @max_visible_rows
            x = @x + 20
            (0..number_of_columns-1).each do |c| 
                @gui_theme.font.draw_text(row[c], x, y + 2, z_order, 1, 1, @row_colors[count])
                x = x + column_widths[c] + 20
            end
            y = y + 30
        end
        count = count + 1
    end
end

#scroll_downObject



2302
2303
2304
2305
2306
# File 'lib/wads/widgets.rb', line 2302

def scroll_down
    if @current_row + @max_visible_rows < @data_rows.size
        @current_row = @current_row + @max_visible_rows 
    end 
end

#scroll_upObject



2296
2297
2298
2299
2300
# File 'lib/wads/widgets.rb', line 2296

def scroll_up 
    if @current_row > 0
        @current_row = @current_row - @max_visible_rows 
    end 
end

#uses_layoutObject



2427
2428
2429
# File 'lib/wads/widgets.rb', line 2427

def uses_layout
    false 
end

#widget_zObject



2423
2424
2425
# File 'lib/wads/widgets.rb', line 2423

def widget_z 
    Z_ORDER_TEXT
end