Class: Clevic::RelationalDelegate

Inherits:
ComboDelegate show all
Defined in:
lib/clevic/qt/relational_delegate.rb,
lib/clevic/swing/relational_delegate.rb,
lib/clevic/delegates/relational_delegate.rb

Overview

Display a collection of possible related entities in the combo box. TODO this should be a module

Instance Attribute Summary

Attributes inherited from ComboDelegate

#editor

Attributes inherited from Delegate

#entity, #field, #parent

Instance Method Summary collapse

Methods inherited from ComboDelegate

#allow_null?, #autocomplete, #configure_editable, #configure_prefix, #createEditor, #create_combo_box, #display_for, #dump_editor_state, #empty_set?, #filter_prefix, #framework_setup, #full_edit, #getListCellRendererComponent, #hint_string, #if_empty_message, #init_component, #is_combo?, #line_editor, #minimal_edit, #needs_pre_selection?, #repopulate, #setModelData, #updateEditorGeometry, #value

Methods inherited from Delegate

#attribute, #editorEvent, #entity_class, #full_edit, #inspect, #is_combo?, #minimal_edit, #native, #needs_pre_selection?, #show_message, #updateEditorGeometry

Methods included from FieldValuer

#attribute_value, #attribute_value=, #display_value, #edit_value, #edit_value=, #find_related, #raw_value, #text_value=, #tooltip, valuer, #valuer, #writer

Constructor Details

#initialize(field) ⇒ RelationalDelegate

Returns a new instance of RelationalDelegate.



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

def initialize( field )
  super
end

Instance Method Details

#combo_classObject

use the Clevic::ComboBox class because JCombobox is remarkably stupid about far too many things.



18
19
20
# File 'lib/clevic/swing/relational_delegate.rb', line 18

def combo_class
  ComboBox
end

#editor_to_item(data) ⇒ Object



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

def editor_to_item( data )
  entity.related_class[ data ]
end

#empty_set_messageObject



13
14
15
# File 'lib/clevic/delegates/relational_delegate.rb', line 13

def empty_set_message
  "There must be records in #{field.related_class} for this field to be editable."
end

#item_to_editor(item) ⇒ Object



12
13
14
# File 'lib/clevic/qt/relational_delegate.rb', line 12

def item_to_editor( item )
  [ field.transform_attribute( item ), item.pk.to_variant ]
end

#needs_combo?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/clevic/delegates/relational_delegate.rb', line 9

def needs_combo?
  dataset.count > 0
end

#populationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/clevic/delegates/relational_delegate.rb', line 17

def population
  # dataset contains the set of all possible related entities,

  # dataset is defined in Delegate
  # entity is set in init_component
  # field and entity are used by FieldValuer

  # including the current entity.
  # Could also use
  #  dataset.or( entity_class.primary_key => entity_key.pk )
  # but that would put current entity in the list somewhere
  # other than the top, which seems to be the most sensible
  # place for it. Could also create a special enumerator
  # which gives back the entity first, followed by the dataset.
  dataset.all.with do |values|
    # make sure there's only one instance of the current value,
    # and make sure it's at the top of the list
    values.delete( attribute_value )
    values.unshift( attribute_value )
  end
end

#restricted?Boolean

don’t allow new values

Returns:

  • (Boolean)


23
24
25
# File 'lib/clevic/swing/relational_delegate.rb', line 23

def restricted?
  true
end

#setEditorData(editor_widget, model_index) ⇒ Object

called by Qt when it wants to give the delegate an index to edit



21
22
23
24
25
26
27
28
# File 'lib/clevic/qt/relational_delegate.rb', line 21

def setEditorData( editor_widget, model_index )
  if is_combo?( editor_widget )
    unless model_index.attribute_value.nil?
      editor_widget.selected_item = model_index.attribute_value
    end
    editor_widget.line_edit.andand.select_all
  end
end

#translate_from_editor_text(editor_widget, text) ⇒ Object

return an entity object, given a text selection



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/clevic/qt/relational_delegate.rb', line 31

def translate_from_editor_text( editor_widget, text )
  item_index = editor_widget.find_text( text )

  # fetch record id from editor_widget item_data
  item_data = editor_widget.item_data( item_index )
  if item_data.valid?
    # get the entity it refers to, if there is one
    # return nil if nil was passed or the entity wasn't found
    field.related_class[ item_data.to_int ]
  end
end