Class: Ruber::AnnotationModel

Inherits:
KTextEditor::AnnotationModel
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruber/editor/document.rb

Defined Under Namespace

Classes: Annotation

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#find!

Constructor Details

#initialize(doc) ⇒ AnnotationModel

Returns a new instance of AnnotationModel.



510
511
512
513
514
515
# File 'lib/ruber/editor/document.rb', line 510

def initialize doc
  super()
  @doc = doc
  @annotations = Dictionary.alpha
  connect self, SIGNAL('annotation_changed(int)'), self, SIGNAL('lineChanged(int)')
end

Class Attribute Details

.annotation_typesObject (readonly)

Returns the value of attribute annotation_types.



501
502
503
# File 'lib/ruber/editor/document.rb', line 501

def annotation_types
  @annotation_types
end

Class Method Details

.register_annotation_type(type, back = nil, fore = nil) ⇒ Object

Raises:

  • (ArgumentError)


504
505
506
507
508
# File 'lib/ruber/editor/document.rb', line 504

def self.register_annotation_type type, back = nil, fore = nil
  raise ArgumentError, "Annotation type #{type} has already been added" if 
  @annotation_types.has_key?(type)
  @annotation_types[type] = [back, fore].map{|i| i ? Qt::Variant.fromValue(i) : Qt::Variant.new}
end

Instance Method Details

#add_annotation(*args) ⇒ Object



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/ruber/editor/document.rb', line 517

def add_annotation *args
  a = args.size == 1 ? args[0] : Annotation.new( *args )
  # TODO: see why this sometimes gives extremely weird errors
  #The begin/rescue clause is there to find out why sometimes I get a crash saying:
  # `<': comparison of Fixnum with Qt::Variant failed (ArgumentError)
  #       begin
  
  #         raise IndexError, "Invalid line: #{a.line}" unless a.line < @doc.lines
  #       rescue ArgumentError
  #         puts "a.line: #{a.line}(#{a.line.class})"
  #         puts "@doc.lines: #{@doc.lines}(#{@doc.lines.class})"
  #       end
  @annotations[a.line] = a
  emit annotations_changed
  emit annotation_changed( a.line)
end

#annotation(line) ⇒ Object Also known as: []



546
547
548
# File 'lib/ruber/editor/document.rb', line 546

def annotation line
  @annotations[line]
end

#clearObject



563
564
565
566
567
# File 'lib/ruber/editor/document.rb', line 563

def clear
  @annotations.clear
  emit annotations_changed
  emit reset
end

#data(line, role) ⇒ Object



534
535
536
537
538
539
540
541
542
543
544
# File 'lib/ruber/editor/document.rb', line 534

def data line, role
  a = @annotations[line]
  return Qt::Variant.new unless a
  case role
  when Qt::DisplayRole then Qt::Variant.new a.msg
  when Qt::ToolTipRole then Qt::Variant.new a.tool_tip
  when Qt::ForegroundRole then self.class.annotation_types[a.type][1]
  when Qt::BackgroundRole then self.class.annotation_types[a.type][0]
  else Qt::Variant.new
  end
end

#eachObject



576
577
578
# File 'lib/ruber/editor/document.rb', line 576

def each
  @annotations.each_pair{|k, v| yield v}
end

#empty?Boolean

Returns:

  • (Boolean)


559
560
561
# File 'lib/ruber/editor/document.rb', line 559

def empty?
  @annotations.empty?
end

#has_annotation?(line) ⇒ Boolean

Returns:

  • (Boolean)


551
552
553
# File 'lib/ruber/editor/document.rb', line 551

def has_annotation? line
  @annotations.has_key? line
end

#has_annotations?Boolean

Returns:

  • (Boolean)


555
556
557
# File 'lib/ruber/editor/document.rb', line 555

def has_annotations?
  !@annotations.empty?
end

#remove_annotation(line) ⇒ Object



569
570
571
572
573
574
# File 'lib/ruber/editor/document.rb', line 569

def remove_annotation line
  if @annotations.delete line
    emit annotations_changed 
    emit annotation_changed( line )
  end
end