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)
BREAKPOINT_SET =
1
BREAKPOINT_CLEAR =
-1
MINIMUM_POINT_SIZE =
4

Constants inherited from CompletionTextEdit

CompletionTextEdit::SELECTION_DETAILS_POOL, CompletionTextEdit::TRUE_VARIANT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CompletionTextEdit

#center_line, #column_number, #current_line, #get_line, #highlight_line, #indent_selection, #keyPressEvent, #line_number, #previous_line, #stop_highlight, #unindent_selection

Constructor Details

#initialize(parent, font = Cosmos.get_default_font) ⇒ RubyEditor

Returns a new instance of RubyEditor.



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 158

def initialize(parent, font = Cosmos.get_default_font)
  super(parent)
  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(int)'))
  connect(self, SIGNAL('updateRequest(const QRect &, int)'), self, SLOT('update_line_number_area(const QRect &, int)'))

  line_count_changed(-1)
end

Instance Attribute Details

#enable_breakpointsObject

Returns the value of attribute enable_breakpoints.



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

def enable_breakpoints
  @enable_breakpoints
end

#filenameObject

Returns the value of attribute filename.



30
31
32
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 30

def filename
  @filename
end

Instance Method Details

#add_breakpoint(line) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 209

def add_breakpoint(line)
  @breakpoints << line
  block = document.findBlockByNumber(line-1)
  block.setUserState(BREAKPOINT_SET)
  block.dispose
  block = nil
  @lineNumberArea.repaint
end

#clear_breakpoint(line) ⇒ Object



218
219
220
221
222
223
224
225
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 218

def clear_breakpoint(line)
  @breakpoints.delete(line)
  block = document.findBlockByNumber(line-1)
  block.setUserState(BREAKPOINT_CLEAR)
  block.dispose
  block = nil
  @lineNumberArea.repaint
end

#clear_breakpointsObject



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 227

def clear_breakpoints
  @breakpoints = []
  block = document.firstBlock()
  while (block.isValid())
    block.setUserState(BREAKPOINT_CLEAR)
    next_block = block.next()
    block.dispose
    block = next_block
  end
  block.dispose
  block = nil
  @lineNumberArea.repaint
end

#comment_or_uncomment_linesObject



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
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 266

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



198
199
200
201
202
203
204
205
206
207
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 198

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



192
193
194
195
196
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 192

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

#line_number_area_paint_event(event) ⇒ Object



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
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 337

def line_number_area_paint_event(event)
  painter = Qt::Painter.new(@lineNumberArea)
  painter.setFont(font())
  # 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 block.userState() == BREAKPOINT_SET
          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



325
326
327
328
329
330
331
332
333
334
335
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 325

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

#update_breakpointsObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 241

def update_breakpoints
  return if @breakpoints.empty?

  breakpoints = []
  block = document.firstBlock()
  while (block.isValid())
    if block.userState() == BREAKPOINT_SET
      line = block.firstLineNumber() + 1
      breakpoints << line
    end
    next_block = block.next()
    block.dispose
    block = next_block
  end
  block.dispose
  block = nil

  # Only emit signals if the breakpoints have changed.
  if @breakpoints.sort != breakpoints.sort
    emit breakpoints_cleared
    breakpoints.each {|line| emit breakpoint_set(line)}
    @breakpoints = breakpoints
  end
end

#wheelEvent(event) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 180

def wheelEvent(event)
  if event.modifiers() == Qt::ControlModifier && event.delta > 0
    event.setAccepted(true)
    zoom_in()
  elsif event.modifiers() == Qt::ControlModifier && event.delta < 0
    event.setAccepted(true)
    zoom_out()
  else
    super(event)
  end
end

#zoom_defaultObject



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

def zoom_default
  font = Cosmos.get_default_font
  setFont(font)
  @fontMetrics = Cosmos.getFontMetrics(font)
  # Force a repaint of the number area by doing a small scroll
  verticalScrollBar.setValue(verticalScrollBar.minimum+1)
  verticalScrollBar.setValue(verticalScrollBar.minimum)
  emit font_changed(font)
end

#zoom_inObject



300
301
302
303
304
305
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 300

def zoom_in
  font = Cosmos.getFont(font().family, font().pointSize + 1)
  setFont(font)
  @fontMetrics = Cosmos.getFontMetrics(font)
  emit font_changed(font)
end

#zoom_outObject



307
308
309
310
311
312
313
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 307

def zoom_out
  return if font().pointSize <= MINIMUM_POINT_SIZE
  font = Cosmos.getFont(font().family, font().pointSize - 1)
  setFont(font)
  @fontMetrics = Cosmos.getFontMetrics(font)
  emit font_changed(font)
end