Class: Cosmos::FullTextSearchLineEdit

Inherits:
Qt::LineEdit show all
Defined in:
lib/cosmos/gui/widgets/full_text_search_line_edit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Qt::LineEdit

#setColors, #text=

Constructor Details

#initialize(parent) ⇒ FullTextSearchLineEdit

Returns a new instance of FullTextSearchLineEdit.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 17

def initialize(parent)
  super(parent)
  @listView = Qt::ListWidget.new(parent)
  @listView.setWindowFlags(0xc | 0x1) # Qt::ToolTip
  @completion_list = []
  @filtered_list = []
  @callback = nil
  connect(self, SIGNAL("textChanged(const QString &)")) do |text|
    handleTextChanged()
  end
  connect(@listView, SIGNAL("itemClicked(QListWidgetItem*)")) do |item|
    text = item.text
    setText(text)
    @callback.call(text) if @callback
    @listView.hide
  end
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



15
16
17
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 15

def callback
  @callback
end

#completion_listObject

Returns the value of attribute completion_list.



14
15
16
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 14

def completion_list
  @completion_list
end

Instance Method Details

#focusOutEvent(e) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 35

def focusOutEvent(e)
  Qt.execute_in_main_thread(false, 0.001, true) do
    unless @listView.hasFocus
      @listView.hide
    end
  end
  super(e)
end

#handleTextChangedObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 117

def handleTextChanged
  text = self.text
  if text.empty?
    @listView.hide
    return
  end

  updateFilteredList(text)

  if @filtered_list.length == 0 or (@filtered_list.length ==1 and text == @filtered_list[0])
    @listView.hide
    return
  end

  maxVisibleRows = 10
  p = Qt::Point.new(0, height())
  x = mapToGlobal(p).x
  y = mapToGlobal(p).y + 1
  p.dispose
  @listView.move(x, y)
  @listView.setMinimumWidth(width())
  @listView.setMaximumWidth(width())
  if @filtered_list.length > maxVisibleRows
    @listView.setFixedHeight(maxVisibleRows * (@listView.fontMetrics.height + 2) + 2)
  else
    @listView.setFixedHeight((@filtered_list.length + 1) * (@listView.fontMetrics.height + 2) + 2)
  end

  @listView.show
  @listView.raise
end

#keyPressEvent(e) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 44

def keyPressEvent(e)
  key = e.key
  if !@listView.isHidden
    count = @filtered_list.length
    row = @listView.currentRow

    # Move through list
    if (key == Qt::Key_Down || key == Qt::Key_Up)
      case(key)
      when Qt::Key_Down
        row += 1
        row = 0 if row >= count
      when Qt::Key_Up
        row -= 1
        row = count - 1 if row < 0
      end

      if @listView.isEnabled
        @listView.setCurrentRow(row)
      end

    # Accept choice
    elsif ((Qt::Key_Enter == key || Qt::Key_Return == key) && @listView.isEnabled)
      if row >= 0
        text = @listView.item(row).text
        setText(text)
        @listView.hide
        handleTextChanged()
        @callback.call(text) if @callback
        @listView.hide
      else
        super(e)
      end

    # Cancel
    elsif Qt::Key_Escape == key
      @listView.hide

    # More data entry
    else
      @listView.hide
      super(e)
    end

  else
    # Up/down potentially after cancel
    if (key == Qt::Key_Down || key == Qt::Key_Up)
      handleTextChanged()

      if !@listView.isHidden
        case(key)
        when Qt::Key_Down
          row = 0
        when Qt::Key_Up
          row = @filtered_list.length - 1
        end

        if @listView.isEnabled
          @listView.setCurrentRow(row)
        end
      end

    # More data entry
    else
      if ((Qt::Key_Enter == key || Qt::Key_Return == key))
        @callback.call(self.text) if @callback
        @listView.hide
      end
      super(e)
    end
  end
end

#updateFilteredList(text) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/cosmos/gui/widgets/full_text_search_line_edit.rb', line 149

def updateFilteredList(text)
  @filtered_list = []
  regex = Regexp.new(Regexp.quote(text), Regexp::IGNORECASE)
  @completion_list.each do |string|
    @filtered_list << string if string =~ regex
  end
  @listView.clear
  @listView.addItems(@filtered_list)
  @listView.setCurrentRow(0) if @filtered_list.length > 0
end