Class | Ui::Qt4::Widgets::CommandLine |
In: |
ui/qt/widgets/commandline.rb
|
Parent: | Qt::LineEdit |
CommandLine is a custom Qt widget that behaves as a standard command line. It keeps a buffer of typed in commands that can be accessed by means of the Up and Down keys.
When the widget signals a returnPressed the last command inserted by the user is accessible using the last_command method.
Create the widget and initialize the internal data structures that will hold the command history.
# File ui/qt/widgets/commandline.rb, line 33 def initialize(parent=nil) super(parent) #initialize internal history buffer @history = [] @pointer = 0 @up = true end
Clears the internal command history buffer.
# File ui/qt/widgets/commandline.rb, line 100 def clear_history() @history.clear end
override some event handlers
# File ui/qt/widgets/commandline.rb, line 59 def keyPressEvent(event) #print_history('in') if (event.key == Qt::Key_Up || event.key == Qt::Key_Down || event.key == Qt::Key_Return) #p 'beb' case event.key when Qt::Key_Up: if ((@history.size > 0) && (@pointer >= 0) ) if (@pointer == @history.size) @history << self.text end @pointer -= 1 if @pointer > 0 self.text = @history[@pointer] end return when Qt::Key_Down: if ((@history.size > 0) && (@pointer < @history.size) ) @pointer += 1 if @pointer < @history.size - 1 self.text = @history[@pointer] end return #add a new element to the local @history when Qt::Key_Return: #keep an eye on the last entry to avoid empty entries in the list if ((@history.size > 0) && (@history.last.strip.size == 0) ) @history.pop end if self.text.strip.size > 0 @history << self.text @pointer = @history.size self.clear end end #print_history('out') if (event.key == Qt::Key_Up || event.key == Qt::Key_Down || event.key == Qt::Key_Return) super end