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_press, #handle_mouse_down, #handle_mouse_up, #handle_right_mouse, #info, #intercept_widget_event, #left_edge, #move_recursive_absolute, #move_recursive_delta, #overlaps_with, #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.



2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
# File 'lib/wads/widgets.rb', line 2250

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.



2248
2249
2250
# File 'lib/wads/widgets.rb', line 2248

def can_delete_rows
  @can_delete_rows
end

#current_rowObject

Returns the value of attribute current_row.



2247
2248
2249
# File 'lib/wads/widgets.rb', line 2247

def current_row
  @current_row
end

#data_rowsObject

Returns the value of attribute data_rows.



2243
2244
2245
# File 'lib/wads/widgets.rb', line 2243

def data_rows
  @data_rows
end

#headersObject

Returns the value of attribute headers.



2245
2246
2247
# File 'lib/wads/widgets.rb', line 2245

def headers
  @headers
end

#max_visible_rowsObject

Returns the value of attribute max_visible_rows.



2246
2247
2248
# File 'lib/wads/widgets.rb', line 2246

def max_visible_rows
  @max_visible_rows
end

#row_colorsObject

Returns the value of attribute row_colors.



2244
2245
2246
# File 'lib/wads/widgets.rb', line 2244

def row_colors
  @row_colors
end

Instance Method Details

#add_row(data_row, color = text_color) ⇒ Object



2282
2283
2284
2285
# File 'lib/wads/widgets.rb', line 2282

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

#add_table_delete_buttonObject



2287
2288
2289
2290
2291
2292
2293
2294
2295
# File 'lib/wads/widgets.rb', line 2287

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



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

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

#determine_row_number(mouse_y) ⇒ Object



2383
2384
2385
2386
2387
2388
2389
2390
# File 'lib/wads/widgets.rb', line 2383

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



2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
# File 'lib/wads/widgets.rb', line 2305

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



2326
2327
2328
# File 'lib/wads/widgets.rb', line 2326

def number_of_rows 
    @data_rows.size 
end

#remove_table_delete_buttonObject



2297
2298
2299
2300
2301
2302
2303
# File 'lib/wads/widgets.rb', line 2297

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



2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
# File 'lib/wads/widgets.rb', line 2330

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



2271
2272
2273
2274
2275
# File 'lib/wads/widgets.rb', line 2271

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

#scroll_upObject



2265
2266
2267
2268
2269
# File 'lib/wads/widgets.rb', line 2265

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

#uses_layoutObject



2396
2397
2398
# File 'lib/wads/widgets.rb', line 2396

def uses_layout
    false 
end

#widget_zObject



2392
2393
2394
# File 'lib/wads/widgets.rb', line 2392

def widget_z 
    Z_ORDER_TEXT
end