Class: Cosmos::RubyEditor

Inherits:
CompletionTextEdit show all
Defined in:
lib/cosmos/gui/text/ruby_editor.rb

Defined Under Namespace

Classes: LineNumberArea, RubySyntax

Constant Summary collapse

CHAR_57 =
Qt::Char.new(57)

Constants inherited from CompletionTextEdit

CompletionTextEdit::SELECTION_DETAILS_POOL, CompletionTextEdit::TRUE_VARIANT

Constants inherited from Qt::PlainTextEdit

Qt::PlainTextEdit::AMP, Qt::PlainTextEdit::BLANK, Qt::PlainTextEdit::BREAK, Qt::PlainTextEdit::GT, Qt::PlainTextEdit::LT, Qt::PlainTextEdit::NBSP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CompletionTextEdit

#highlight_line, #indent_selection, #keyPressCallback=, #keyPressEvent, #rehighlight, #stop_highlight, #unindent_selection

Methods inherited from Qt::PlainTextEdit

#addText, #add_formatted_text, #appendText, #flush, #selected_lines, #selection_end_line, #selection_start_line

Constructor Details

#initialize(parent) ⇒ RubyEditor

Returns a new instance of RubyEditor.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 157

def initialize(parent)
  super(parent)
  font = Cosmos.get_default_font
  setFont(font)
  @fontMetrics = Cosmos.getFontMetrics(font)

  # This is needed so text searching highlights correctly
  setStyleSheet("selection-background-color: lightblue; selection-color: black;")

  @breakpoints = []
  @enable_breakpoints = false

  # RubySyntax works but slows down the GUI significantly when
  # pasting a large (10k line) block of code into the editor
  @syntax = RubySyntax.new(document())
  @lineNumberArea = LineNumberArea.new(self)

  connect(self, SIGNAL('blockCountChanged(int)'), self, SLOT('line_count_changed()'))
  connect(self, SIGNAL('updateRequest(const QRect &, int)'), self, SLOT('update_line_number_area(const QRect &, int)'))

  line_count_changed()
end

Instance Attribute Details

#enable_breakpointsObject

Returns the value of attribute enable_breakpoints.



28
29
30
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 28

def enable_breakpoints
  @enable_breakpoints
end

Instance Method Details

#add_breakpoint(line) ⇒ Object



197
198
199
200
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 197

def add_breakpoint(line)
  @breakpoints << line
  @lineNumberArea.repaint
end

#clear_breakpoint(line) ⇒ Object



202
203
204
205
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 202

def clear_breakpoint(line)
  @breakpoints.delete(line)
  @lineNumberArea.repaint
end

#clear_breakpointsObject



207
208
209
210
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 207

def clear_breakpoints
  @breakpoints = []
  @lineNumberArea.repaint
end

#comment_or_uncomment_linesObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 212

def comment_or_uncomment_lines
  cursor = textCursor
  no_selection = cursor.hasSelection ? false : true

  # Start the edit block so this can be all undone with a single undo step
  cursor.beginEditBlock
  selection_end = cursor.selectionEnd
  # Initially place the cursor at the beginning of the selection
  # If nothing is selected this will just put the cursor at the beginning
  # of the current line
  cursor.setPosition(textCursor.selectionStart)
  cursor.movePosition(Qt::TextCursor::StartOfLine)
  result = true
  while (cursor.position < selection_end && result == true) || (no_selection)
    # Check for a special comment
    if cursor.block.text =~ /^\S*#~/
      cursor.deleteChar
      cursor.deleteChar
      # Since we deleted two spaces we need to move the end position by two
      selection_end -= 2
    else
      cursor.insertText("#~")
      # Since we deleted two spaces we need to move the end position by two
      selection_end += 2
    end
    # Move the cursor to the beginning of the next line
    cursor.movePosition(Qt::TextCursor::StartOfLine)
    result = cursor.movePosition(Qt::TextCursor::Down)
    # If nothing was selected then its a single line so break
    break if no_selection
  end
  cursor.endEditBlock
end

#context_menu(point) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 186

def context_menu(point)
  menu = createStandardContextMenu()
  return menu unless @enable_breakpoints

  menu.addSeparator()
  menu.addAction(create_add_breakpoint_action(point))
  menu.addAction(create_clear_breakpoint_action(point))
  menu.addAction(create_clear_all_breakpoints_action())
  menu
end

#disposeObject



180
181
182
183
184
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 180

def dispose
  super()
  @syntax.dispose
  @lineNumberArea.dispose
end

#line_number_area_paint_event(event) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 258

def line_number_area_paint_event(event)
  painter = Qt::Painter.new(@lineNumberArea)
  # Check for weird bad initialization conditions
  if painter.isActive and not painter.paintEngine.nil?
    event_rect = event.rect()
    painter.fillRect(event_rect, Qt::lightGray)

    block = firstVisibleBlock()
    blockNumber = block.blockNumber()

    top, bottom = block_top_and_bottom(block)

    width = @lineNumberArea.width()
    height = @fontMetrics.height()
    ellipse_width = @fontMetrics.width(CHAR_57)
    painter.setPen(Cosmos::BLACK)
    while (block.isValid() and top <= event_rect.bottom())
      if (block.isVisible() and bottom >= event_rect.top())
        number = blockNumber + 1
        painter.drawText(0,               # x
                         top,             # y
                         width,           # width
                         height,          # height
                         Qt::AlignRight,  # flags
                         number.to_s)     # text

        if @enable_breakpoints and @breakpoints.include?(number)
          painter.setBrush(Cosmos::RED)
          painter.drawEllipse(2,
                              top+2,
                              ellipse_width,
                              ellipse_width)
        end
      end

      next_block = block.next()
      block.dispose
      block = next_block
      top = bottom
      rect = blockBoundingRect(block)
      bottom = top + rect.height()
      rect.dispose
      blockNumber += 1
    end
    block.dispose
    block = nil
    event_rect.dispose
    event_rect = nil
  end
  # Ensure the painter is disposed in the paint function to avoid this:
  #   QPaintDevice: Cannot destroy paint device that is being painted
  painter.dispose
  painter = nil
end

#resizeEvent(e) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 246

def resizeEvent(e)
  super(e)
  cr = self.contentsRect()
  rect = Qt::Rect.new(cr.left(),
    cr.top(),
    line_number_area_width(),
    cr.height())
  @lineNumberArea.setGeometry(rect)
  cr.dispose
  rect.dispose
end