Class: Cosmos::CmdSenderTextEdit

Inherits:
Qt::TextEdit show all
Defined in:
lib/cosmos/tools/cmd_sender/cmd_sender_text_edit.rb

Overview

CmdSenderTextEdit is a subclass of TextEdit that creates a CosmosCompletion class to allow for code completion when creating new commands in the command history window. It also monitors for the Enter key to allow for commands in the command history to be executed.

Instance Method Summary collapse

Methods inherited from Qt::TextEdit

#setColors

Constructor Details

#initialize(status_bar) ⇒ CmdSenderTextEdit

Returns a new instance of CmdSenderTextEdit.



17
18
19
20
21
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_text_edit.rb', line 17

def initialize(status_bar)
  super()
  @status_bar = status_bar
  @c = Completion.new(self)
end

Instance Method Details

#keyPressEvent(event) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_text_edit.rb', line 23

def keyPressEvent(event)
  # If the completion popup is visible then ignore the event and allow the popup to handle it
  if (@c and @c.popup.isVisible)
    case event.key
    when Qt::Key_Return, Qt::Key_Enter
      event.ignore
      return
    end
  end

  case event.key
  # The return key means we need to execute the current line
  when Qt::Key_Return, Qt::Key_Enter
    # If the cursor is at the beginning of the line allow the newline
    if textCursor.atBlockStart
      super(event)
    # If the line isn't blank then execute it
    elsif textCursor.block.text.strip != ""
      begin
        eval(textCursor.block.text, $eval_binding)
        CmdSender.send_count += 1
        @status_bar.showMessage("#{textCursor.block.text} sent. (#{CmdSender.send_count})")
      rescue Exception => error
        if error.class == DRb::DRbConnError
          message = "Error Connecting to Command and Telemetry Server"
        else
          message = "Error sending command due to #{error}"
        end
        @status_bar.showMessage(message)
        Qt::MessageBox.critical(self, 'Error', message)
      end
    end
  # Handle Key_Down to automatically create a newline if they Key_Down at
  # the bottom of the document
  when Qt::Key_Down
    # If the number of lines equals the current line (plus one since it is 0
    # based) then we append a newline to allow the Key_Down to go to a new line
    if self.toPlainText.split("\n").length == (textCursor.block.blockNumber + 1)
      self.append("")
    end
    super(event)
  else # All other keys are handled by this widget as well as being passed to the completion handler
    super(event)
    @c.handle_keypress(event)
  end
end