Class: Qt::PlainTextEdit

Inherits:
Object show all
Defined in:
lib/cosmos/gui/qt.rb

Direct Known Subclasses

Cosmos::CompletionTextEdit

Constant Summary collapse

BLANK =
''.freeze
BREAK =
'<br/>'.freeze
AMP =
'&amp;'.freeze
NBSP =
'&nbsp;'.freeze
GT =
'&gt;'.freeze
LT =
'&lt;'.freeze
@@color_cache =
{}
@@mapping =
{'&'=>AMP,"\n"=>BLANK,"\s"=>NBSP,'>'=>GT,'<'=>LT}
@@regex =
Regexp.union(@@mapping.keys)

Instance Method Summary collapse

Instance Method Details

#add_formatted_text(text, color = nil) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/cosmos/gui/qt.rb', line 473

def add_formatted_text(text, color = nil)
  if text =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F-\xFF]/
    text.chomp!
    text = text.inspect.remove_quotes
    text << "\n"
  end
  if text =~ /<G>/ or color == Cosmos::GREEN
    text.gsub!(/<G>/, BLANK)
    addText(text, Cosmos::GREEN)
  elsif text =~ /<Y>/ or color == Cosmos::YELLOW
    text.gsub!(/<Y>/, BLANK)
    addText(text, Cosmos::YELLOW)
  elsif text =~ /<R>/ or color == Cosmos::RED
    text.gsub!(/<R>/, BLANK)
    addText(text, Cosmos::RED)
  elsif text =~ /<B>/ or color == Cosmos::BLUE
    text.gsub!(/<B>/, BLANK)
    addText(text, Cosmos::BLUE)
  else
    addText(text) # default is Cosmos::BLACK
  end
end

#addText(text, color = Cosmos::BLACK) ⇒ Object



496
497
498
499
# File 'lib/cosmos/gui/qt.rb', line 496

def addText(text, color = Cosmos::BLACK)
  @current_text ||= ''
  @current_text << escape_text(text.chomp, color) << BREAK
end

#appendText(text, color = Cosmos::BLACK) ⇒ Object



506
507
508
# File 'lib/cosmos/gui/qt.rb', line 506

def appendText(text, color = Cosmos::BLACK)
  appendHtml(escape_text(text.chomp, color))
end

#flushObject



501
502
503
504
# File 'lib/cosmos/gui/qt.rb', line 501

def flush
  appendHtml(@current_text)
  @current_text.clear
end

#selected_linesObject

Return the selected lines. If a partial line is selected the entire line will be returned. If there is no selection the line with the cursor will be returned



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/cosmos/gui/qt.rb', line 512

def selected_lines
  cursor = textCursor
  selection_end = cursor.selectionEnd
  # Initially place the cursor at the beginning of the selection
  # If nothing is selected this will just put the cursor at the beginning of the current line
  cursor.setPosition(textCursor.selectionStart)
  cursor.movePosition(Qt::TextCursor::StartOfLine)
  # Move the cursor to the end of the selection while keeping the anchor
  cursor.setPosition(selection_end, Qt::TextCursor::KeepAnchor)
  # Normally we want to select the entire line where the end of selection is
  # However if the user selects an exact number of lines
  # the cursor will be at the beginning of the following line
  # Therefore if the cursor is at the beginning of the line and we have a selection
  # we'll skip moving to the end of the line
  unless (cursor.atBlockStart and textCursor.hasSelection)
    cursor.movePosition(Qt::TextCursor::EndOfLine, Qt::TextCursor::KeepAnchor)
  end
  # If there is no selection then just return nil
  return nil if cursor.selectedText.nil?

  cursor.selection.toPlainText

  # The selectedText function returns the Unicode U+2029 paragraph separator character
  # instead of newline \n. Thus we have to unpack as Unicode, covert to newlines, and then repack
  #text = cursor.selectedText.unpack("U*")
  #text.collect! {|letter| if letter == 63 then 10 else letter end }
  #text.pack("U*")
end

#selection_end_lineObject

Return the line number (0 based) of the selection end



549
550
551
552
553
# File 'lib/cosmos/gui/qt.rb', line 549

def selection_end_line
  cursor = textCursor
  cursor.setPosition(textCursor.selectionEnd)
  cursor.blockNumber
end

#selection_start_lineObject

Return the line number (0 based) of the selection start



542
543
544
545
546
# File 'lib/cosmos/gui/qt.rb', line 542

def selection_start_line
  cursor = textCursor
  cursor.setPosition(textCursor.selectionStart)
  cursor.blockNumber
end