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

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.



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

def initialize(parent)
  super(parent)
  if Kernel.is_windows?
    setFont(Cosmos.getFont("Courier", 10))
    @fontMetrics = Cosmos.getFontMetrics(Cosmos.getFont("Courier", 10))
  else
    setFont(Cosmos.getFont("Courier", 14))
    @fontMetrics = Cosmos.getFontMetrics(Cosmos.getFont("Courier", 14))
  end

  # 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.



25
26
27
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 25

def enable_breakpoints
  @enable_breakpoints
end

Instance Method Details

#breakpoint(line) ⇒ Object



228
229
230
231
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 228

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

#breakpoint_click(point, add_breakpoint = true) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 213

def breakpoint_click(point, add_breakpoint = true)
  line = point.y / @fontMetrics.height() + 1 + firstVisibleBlock().blockNumber()
  if add_breakpoint
    if line <= document.blockCount()
      breakpoint(line)
      emit breakpoint_set(line)
    end
  else
    if line <= document.blockCount()
      clear_breakpoint(line)
      emit breakpoint_cleared(line)
    end
  end
end

#clear_breakpoint(line) ⇒ Object



233
234
235
236
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 233

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

#clear_breakpointsObject



238
239
240
241
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 238

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

#comment_or_uncomment_linesObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 243

def comment_or_uncomment_lines
  cursor = textCursor
  # Figure out if the cursor has a selection
  no_selection = true
  no_selection = false if cursor.hasSelection

  # 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 and result == true) or (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 we're working with a single line and we can break
    break if no_selection
  end
  cursor.endEditBlock
end

#context_menu(point) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 187

def context_menu(point)
  menu = createStandardContextMenu()
  if @enable_breakpoints
    menu.addSeparator()

    add_breakpoint = Qt::Action.new(tr("Add Breakpoint"), self)
    add_breakpoint.statusTip = tr("Add a breakpoint at this line")
    add_breakpoint.connect(SIGNAL('triggered()')) { breakpoint_click(point) }
    menu.addAction(add_breakpoint)

    clear_breakpoint = Qt::Action.new(tr("Clear Breakpoint"), self)
    clear_breakpoint.statusTip = tr("Clear an existing breakpoint at this line")
    clear_breakpoint.connect(SIGNAL('triggered()')) { breakpoint_click(point, false) }
    menu.addAction(clear_breakpoint)

    clear_all_breakpoints = Qt::Action.new(tr("Clear All Breakpoints"), self)
    clear_all_breakpoints.statusTip = tr("Clear all existing breakpoints")
    clear_all_breakpoints.connect(SIGNAL('triggered()')) do
      clear_breakpoints()
      emit breakpoints_cleared
    end
    menu.addAction(clear_all_breakpoints)
  end
  return menu
end

#disposeObject



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

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

#line_count_changedObject



297
298
299
300
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 297

def line_count_changed
  setViewportMargins(lineNumberAreaWidth(), 0, 0, 0)
  update
end

#lineNumberAreaPaintEvent(event) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 326

def lineNumberAreaPaintEvent(event)
  painter = Qt::Painter.new(@lineNumberArea)
  # On initialization sometimes we get some weird bad conditions so check for them
  if painter.isActive and not painter.paintEngine.nil?
    event_rect = event.rect()
    painter.fillRect(event_rect, Qt::lightGray)

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

    offset = contentOffset()
    rect = blockBoundingGeometry(block)
    rect2 = rect.translated(offset)
    top = rect2.top()
    rect3 = blockBoundingRect(block)
    bottom = top + rect3.height()
    offset.dispose
    offset = nil
    rect.dispose
    rect = nil
    rect2.dispose
    rect2 = nil
    rect3.dispose
    rect3 = nil

    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.getBrush(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

#lineNumberAreaWidthObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 278

def lineNumberAreaWidth
  digits = 1
  my_document = document()
  max = [1, my_document.blockCount()].max

  # Figure the line number power of ten to determine how much space we need
  while (max >= 10)
    max /= 10
    digits += 1
  end
  # We'll always display space for 5 digits so the line number area
  # isn't constantly expanding and contracting
  digits = 5 if digits < 5
  digits += 1 # always allow room for a breakpoint symbol
  # Get the font width of the character 57 ('9')
  # times the number of digits to display
  return (3 + @fontMetrics.width(CHAR_57) * digits)
end

#resizeEvent(e) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 314

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

#update_line_number_area(rect, dy) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 302

def update_line_number_area(rect, dy)
  if (dy)
    @lineNumberArea.scroll(0, dy)
  else
    @lineNumberArea.update(0, rect.y(), @lineNumberArea.width(), rect.height())
  end
  my_viewport = viewport()
  viewport_rect = my_viewport.rect()
  line_count_changed() if (rect.contains(viewport_rect))
  viewport_rect.dispose
end