Class: Clevic::TextDelegate

Inherits:
Delegate show all
Defined in:
lib/clevic/qt/text_delegate.rb,
lib/clevic/swing/text_delegate.rb

Defined Under Namespace

Classes: TextEditor

Instance Attribute Summary

Attributes inherited from Delegate

#entity, #field, #parent

Instance Method Summary collapse

Methods inherited from Delegate

#attribute, #editorEvent, #entity_class, #initialize, #inspect, #is_combo?, #native, #show_message

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

This class inherits a constructor from Clevic::Delegate

Instance Method Details

#createEditor(parent_widget, style_option_view_item, model_index) ⇒ Object

Override the Qt method



42
43
44
45
46
47
48
49
50
51
# File 'lib/clevic/qt/text_delegate.rb', line 42

def createEditor( parent_widget, style_option_view_item, model_index )
  if false && model_index.edit_value.count("\n") == 0
    # futzing about here, really
    @editor = Qt::LineEdit.new( parent_widget )
  else
    @editor = TextEditor.new( parent_widget )
    @editor.install_event_filter( self )
  end
  @editor
end

#editorObject



12
13
14
15
16
# File 'lib/clevic/swing/text_delegate.rb', line 12

def editor
  @editor ||= javax.swing.JTextField.new.tap do |e|
    e.horizontal_alignment = field.swing_alignment
  end
end

#eventFilter(object, event) ⇒ Object

this is overridden in Qt::ItemDelegate, but that always catches the return key. Which we want for text editing. Instead, we use Ctrl-Enter to save the edited text. return true if event is handled, false otherwise



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/clevic/qt/text_delegate.rb', line 16

def eventFilter( object, event )
  if object.class == TextEditor && event.class == Qt::KeyEvent
    retval =
    case
      when event.ctrl? && ( event.enter? || event.return? )
        # close and save
        # copied from QItemDelegate.
        emit commitData( object )
        emit closeEditor( object )
        true

      # send an enter or return to the text editor
      when event.enter? || event.return?
        object.event( event )
        true
    end
  end
  retval || super
end

#full_editObject

maybe open in a separate window?



37
38
39
# File 'lib/clevic/qt/text_delegate.rb', line 37

def full_edit
  puts "#{self.class.name} full_edit"
end

#init_component(cell_editor) ⇒ Object

TODO check that VK_ENTER stops editing



7
8
9
10
# File 'lib/clevic/swing/text_delegate.rb', line 7

def init_component( cell_editor )
  editor.text = edit_value
  editor.select_all
end

#minimal_editObject



22
23
24
# File 'lib/clevic/swing/text_delegate.rb', line 22

def minimal_edit
  editor.select_all
end

#needs_pre_selection?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/clevic/swing/text_delegate.rb', line 26

def needs_pre_selection?
  true
end

#setEditorData(editor, model_index) ⇒ Object

Override the Qt method to send data to the editor from the model.



71
72
73
# File 'lib/clevic/qt/text_delegate.rb', line 71

def setEditorData( editor, model_index )
  editor.plain_text = model_index.edit_value
end

#setModelData(editor, abstract_item_model, model_index) ⇒ Object

Send the data from the editor to the model. The data will be translated by translate_from_editor_text,



77
78
79
80
81
82
# File 'lib/clevic/qt/text_delegate.rb', line 77

def setModelData( editor, abstract_item_model, model_index )
  model_index.edit_value = editor.to_plain_text
  abstract_item_model.data_changed( model_index )
rescue
  abstract_item_model.emit_data_error( model_index, editor.to_plain_text, $!.message )
end

#updateEditorGeometry(editor, style_option_view_item, model_index) ⇒ Object

Override the Qt::ItemDelegate method.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/clevic/qt/text_delegate.rb', line 54

def updateEditorGeometry( editor, style_option_view_item, model_index )
  rect = Qt::Rect.new( style_option_view_item.rect.top_left, style_option_view_item.rect.size ) 

  # ask the editor for how much space it wants, and set the editor
  # to that size when it displays in the table
  rect.set_width( [editor.size_hint.width,rect.width].max )
  rect.set_height( editor.size_hint.height )

  unless editor.parent.rect.contains( rect )
    # 46 because TableView returns an incorrect bottom.
    # And I can't find out how to get the correct value.
    rect.move_bottom( parent.contents_rect.bottom - 46 )
  end
  editor.set_geometry( rect )
end

#valueObject



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

def value
  editor.text
end