Sha256: 5c10047d1372540469765482a3390b6887335dad9656142a801ca45a6d0d0ec7

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

require 'clevic/qt/combo_delegate.rb'

module Clevic

# Edit a relation from an id and display a list of relevant entries.
#
# attribute is the method to call on the row entity to retrieve the related object.
#
# The ids of the model objects are stored in the item data
# and the item text is fetched from them using attribute_path.
class RelationalDelegate < ComboDelegate
  def item_to_editor( item )
    [ field.transform_attribute( item ), item.pk.to_variant ]
  end

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

  # called by Qt when it wants to give the delegate an index to edit
  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

  # return an entity object, given a text selection
  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

end

end

require 'clevic/delegates/relational_delegate.rb'

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
clevic-0.14.6 lib/clevic/qt/relational_delegate.rb
clevic-0.14.5 lib/clevic/qt/relational_delegate.rb
clevic-0.14.4 lib/clevic/qt/relational_delegate.rb
clevic-0.14.3 lib/clevic/qt/relational_delegate.rb
clevic-0.14.2 lib/clevic/qt/relational_delegate.rb
clevic-0.14.1 lib/clevic/qt/relational_delegate.rb
clevic-0.14.0 lib/clevic/qt/relational_delegate.rb
clevic-0.13.0.b12 lib/clevic/qt/relational_delegate.rb