| 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 36
36: def initialize(parent=nil)
37: super(parent)
38: #initialize internal history buffer
39: @history = []
40: @pointer = 0
41: @up = true
42: end
Clears the internal command history buffer.
# File ui/qt/widgets/commandline.rb, line 103
103: def clear_history()
104: @history.clear
105: end
override some event handlers
# File ui/qt/widgets/commandline.rb, line 62
62: def keyPressEvent(event)
63: #print_history('in') if (event.key == Qt::Key_Up || event.key == Qt::Key_Down || event.key == Qt::Key_Return)
64: #p 'beb'
65: case event.key
66: when Qt::Key_Up:
67: if ((@history.size > 0) && (@pointer >= 0) )
68: if (@pointer == @history.size)
69: @history << self.text
70: end
71: @pointer -= 1 if @pointer > 0
72: self.text = @history[@pointer]
73: end
74: return
75:
76: when Qt::Key_Down:
77: if ((@history.size > 0) && (@pointer < @history.size) )
78: @pointer += 1 if @pointer < @history.size - 1
79: self.text = @history[@pointer]
80: end
81: return
82:
83: #add a new element to the local @history
84: when Qt::Key_Return:
85:
86: #keep an eye on the last entry to avoid empty entries in the list
87: if ((@history.size > 0) && (@history.last.strip.size == 0) )
88: @history.pop
89: end
90:
91: if self.text.strip.size > 0
92: @history << self.text
93: @pointer = @history.size
94: self.clear
95: end
96: end
97:
98: #print_history('out') if (event.key == Qt::Key_Up || event.key == Qt::Key_Down || event.key == Qt::Key_Return)
99: super
100: end