Class: Cosmos::RubyEditor
Defined Under Namespace
Classes: LineNumberArea, RubySyntax
Constant Summary
collapse
- CHAR_57 =
Qt::Char.new(57)
CompletionTextEdit::SELECTION_DETAILS_POOL, CompletionTextEdit::TRUE_VARIANT
Instance Attribute Summary collapse
Instance Method Summary
collapse
#highlight_line, #indent_selection, #keyPressCallback=, #keyPressEvent, #rehighlight, #stop_highlight, #unindent_selection
#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
setStyleSheet("selection-background-color: lightblue; selection-color: black;")
@breakpoints = []
@enable_breakpoints = false
@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_breakpoints ⇒ Object
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_breakpoints ⇒ Object
238
239
240
241
|
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 238
def clear_breakpoints
@breakpoints = []
@lineNumberArea.repaint
end
|
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
cursor = textCursor
no_selection = true
no_selection = false if cursor.hasSelection
cursor.beginEditBlock
selection_end = cursor.selectionEnd
cursor.setPosition(textCursor.selectionStart)
cursor.movePosition(Qt::TextCursor::StartOfLine)
result = true
while (cursor.position < selection_end and result == true) or (no_selection)
if cursor.block.text =~ /^\S*#~/
cursor.deleteChar
cursor.deleteChar
selection_end -= 2
else
cursor.insertText("#~")
selection_end += 2
end
cursor.movePosition(Qt::TextCursor::StartOfLine)
result = cursor.movePosition(Qt::TextCursor::Down)
break if no_selection
end
cursor.endEditBlock
end
|
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 (point)
= createStandardContextMenu()
if @enable_breakpoints
.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) }
.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) }
.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
.addAction(clear_all_breakpoints)
end
return
end
|
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_changed ⇒ Object
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)
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, top, width, height, Qt::AlignRight, number.to_s)
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
painter.dispose
painter = nil
end
|
#lineNumberAreaWidth ⇒ Object
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
while (max >= 10)
max /= 10
digits += 1
end
digits = 5 if digits < 5
digits += 1 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
|