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.



540
541
542
543
544
545
# File 'lib/ruber/editor/document.rb', line 540

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.



531
532
533
# File 'lib/ruber/editor/document.rb', line 531

def annotation_types
  @annotation_types
end

Class Method Details

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

Raises:

  • (ArgumentError)


534
535
536
537
538
# File 'lib/ruber/editor/document.rb', line 534

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



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/ruber/editor/document.rb', line 547

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: []



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

def annotation line
  @annotations[line]
end

#clearObject



593
594
595
596
597
# File 'lib/ruber/editor/document.rb', line 593

def clear
  @annotations.clear
  emit annotations_changed
  emit reset
end

#data(line, role) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
# File 'lib/ruber/editor/document.rb', line 564

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



606
607
608
# File 'lib/ruber/editor/document.rb', line 606

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

#empty?Boolean

Returns:

  • (Boolean)


589
590
591
# File 'lib/ruber/editor/document.rb', line 589

def empty?
  @annotations.empty?
end

#has_annotation?(line) ⇒ Boolean

Returns:

  • (Boolean)


581
582
583
# File 'lib/ruber/editor/document.rb', line 581

def has_annotation? line
  @annotations.has_key? line
end

#has_annotations?Boolean

Returns:

  • (Boolean)


585
586
587
# File 'lib/ruber/editor/document.rb', line 585

def has_annotations?
  !@annotations.empty?
end

#remove_annotation(line) ⇒ Object



599
600
601
602
603
604
# File 'lib/ruber/editor/document.rb', line 599

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