Class: Cosmos::DataViewerComponent

Inherits:
Qt::Widget show all
Defined in:
lib/cosmos/tools/data_viewer/data_viewer_component.rb

Direct Known Subclasses

DumpComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, tab_name) ⇒ DataViewerComponent

Initialize the Data Viewer Component



20
21
22
23
24
25
26
27
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 20

def initialize(parent, tab_name)
  super(parent)
  @tab_name = tab_name
  @packets = []
  @processed_queue = Queue.new
  @log_file_directory = System.paths['LOGS']
  @timer = nil
end

Instance Attribute Details

#packetsObject (readonly)

Returns the value of attribute packets.



17
18
19
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 17

def packets
  @packets
end

#tab_nameObject (readonly)

Returns the value of attribute tab_name.



16
17
18
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 16

def tab_name
  @tab_name
end

Instance Method Details

#add_packet(target_name, packet_name) ⇒ Object

Adds a packet to the list of packets this components processes



30
31
32
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 30

def add_packet(target_name, packet_name)
  @packets << [target_name, packet_name]
end

#find(dialog) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 95

def find(dialog)
  found = @text.find(dialog.find_text, dialog.find_flags)
  if not found and dialog.wrap_around?
    cursor = @text.textCursor
    if dialog.find_up?
      cursor.movePosition(Qt::TextCursor::End)
    else
      cursor.movePosition(Qt::TextCursor::Start)
    end
    @text.setTextCursor(cursor)
    @text.find(dialog.find_text, dialog.find_flags)
  end
end

#find_next(dialog) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 109

def find_next(dialog)
  flags = dialog.find_flags
  flags &= ~Qt::TextDocument::FindBackward.to_i
  found = @text.find(dialog.find_text, flags)
  if not found and dialog.wrap_around?
    cursor = @text.textCursor
    cursor.movePosition(Qt::TextCursor::Start)
    @text.setTextCursor(cursor)
    @text.find(dialog.find_text, flags)
  end
end

#find_previous(dialog) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 121

def find_previous(dialog)
  flags = dialog.find_flags
  flags |= Qt::TextDocument::FindBackward.to_i
  found = @text.find(dialog.find_text, flags)
  if not found and dialog.wrap_around?
    cursor = @text.textCursor
    cursor.movePosition(Qt::TextCursor::End)
    @text.setTextCursor(cursor)
    @text.find(dialog.find_text, flags)
  end
end

#initialize_guiObject

Builds the gui for this component



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
69
70
71
72
73
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 35

def initialize_gui
  @top_layout = Qt::VBoxLayout.new

  @text = Qt::PlainTextEdit.new
  @text.setReadOnly(true)
  @text.setMaximumBlockCount(10000) # 10000 lines of history will be displayed
  if Kernel.is_windows?
    @text.font = Cosmos.getFont("courier", 9)
  else
    @text.font = Cosmos.getFont("courier", 12)
  end
  @text.setWordWrapMode(Qt::TextOption::NoWrap)
  @top_layout.addWidget(@text)

  @button = Qt::PushButton.new('Save Text to File')
  @button.connect(SIGNAL('clicked()')) do
    Qt.execute_in_main_thread(true) do
      filename = File.join(@log_file_directory, File.build_timestamped_filename(['dataviewer']))

      # Prompt user for filename
      filename = Qt::FileDialog::getSaveFileName(self, "Save As...", filename, "Text Files (*.txt);;All Files (*)")
      if not filename.nil? and not filename.empty?
        @log_file_directory = File.dirname(filename)
        @log_file_directory += '/' unless @log_file_directory[-1..-1] == '\\'

        File.open(filename, 'w') do |file|
          file.write(@text.toPlainText)
        end
      end
    end
  end
  @top_layout.addWidget(@button)

  @timer = Qt::Timer.new(self)
  @timer.connect(SIGNAL('timeout()')) { scroll_to_bottom() }
  @timer.setSingleShot(true)

  setLayout(@top_layout)
end

#process_packet(packet) ⇒ Object

Processes the given packet. No gui interaction should be done in this method. Override this method for other components.



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 147

def process_packet(packet)
  processed_text = ''
  processed_text << '*' * 80 << "\n"
  processed_text << "* #{packet.target_name} #{packet.packet_name}\n"
  processed_text << "* Received Time: #{packet.received_time.formatted}\n" if packet.received_time
  processed_text << "* Received Count: #{packet.received_count}\n"
  processed_text << '*' * 80 << "\n"
  processed_text << packet.formatted(:WITH_UNITS) << "\n"
  # Ensure that queue does not grow infinitely while paused
  if @processed_queue.length < 1000
    @processed_queue << processed_text
  end
end

#resetObject

Resets the gui and any intermediate processing



91
92
93
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 91

def reset
  @text.setPlainText("")
end

#scroll_to_bottomObject



141
142
143
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 141

def scroll_to_bottom
  @text.verticalScrollBar.value = @text.verticalScrollBar.maximum
end

#showEvent(event) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 133

def showEvent(event)
  # When the tab is shown we want to ensure the scroll bar is at the maximum to allow
  # the PlainTextArea to automatically hold the scroll at the bottom of the display while
  # appending things. If this is not done, switching tabs will cause the scroll bar to "stick"
  # and not stay at the bottom with the newest text.
  @timer.start(100)
end

#shutdownObject

Shutdown the Data Viewer Component. Called when program is closed.



162
163
164
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 162

def shutdown
  # Do nothing by default
end

#update_guiObject

Updates the gui with any changes if needed



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 76

def update_gui
  begin
    loop do
      # Get new processed text
      processed_text = @processed_queue.pop(true)

      # Add text to text widget
      @text.appendPlainText(processed_text)
    end
  rescue ThreadError
    # Nothing to do
  end
end