1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 116 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 148 149 150 151 152 153 154 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
package CodeEditor;
use strict;
use warnings;
use List::Util qw( max );
use QtCore4;
use QtGui4;
#[codeeditordefinition]
use QtCore4::isa qw( Qt::PlainTextEdit );
use QtCore4::slots
updateLineNumberAreaWidth => ['int'],
highlightCurrentLine => [],
updateLineNumberArea => ['const QRect &', 'int'];
sub lineNumberArea() {
return this->{lineNumberArea};
}
sub setLineNumberArea() {
return this->{lineNumberArea} = shift;
}
#[codeeditordefinition]
#[extraarea]
package LineNumberArea;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Widget );
sub codeEditor() {
return this->{codeEditor};
}
sub setCodeEditor() {
return this->{codeEditor} = shift;
}
sub NEW {
my ( $class, $editor ) = @_;
$class->SUPER::NEW( $editor );
this->setCodeEditor( $editor );
}
sub sizeHint {
return Qt::Size(this->codeEditor->lineNumberAreaWidth(), 0);
}
sub paintEvent {
my ($event) = @_;
this->codeEditor->lineNumberAreaPaintEvent($event);
}
1;
#[extraarea]
package CodeEditor;
use LineNumberArea;
#[constructor]
sub NEW {
my ($class, $parent) = @_;
$class->SUPER::NEW( $parent );
this->setLineNumberArea( LineNumberArea(this) );
this->connect(this, SIGNAL 'blockCountChanged(int)', this, SLOT 'updateLineNumberAreaWidth(int)');
this->connect(this, SIGNAL 'updateRequest(const QRect &, int)', this, SLOT 'updateLineNumberArea(const QRect &, int)');
this->connect(this, SIGNAL 'cursorPositionChanged()', this, SLOT 'highlightCurrentLine()');
this->updateLineNumberAreaWidth(0);
this->highlightCurrentLine();
}
#[constructor]
#[extraAreaWidth]
sub lineNumberAreaWidth {
my $digits = 1;
my $max = max(1, this->blockCount());
while ($max >= 10) {
$max /= 10;
++$digits;
}
my $space = 3 + this->fontMetrics()->width(Qt::Char(Qt::Int(9))) * $digits;
return $space;
}
#[extraAreaWidth]
#[slotUpdateExtraAreaWidth]
sub updateLineNumberAreaWidth {
this->setViewportMargins(this->lineNumberAreaWidth(), 0, 0, 0);
}
#[slotUpdateExtraAreaWidth]
#[slotUpdateRequest]
sub updateLineNumberArea {
my ($rect, $dy) = @_;
if ($dy) {
this->lineNumberArea->scroll(0, $dy);
}
else {
this->lineNumberArea->update(0, $rect->y(), this->lineNumberArea->width(), $rect->height());
}
if ($rect->contains(this->viewport()->rect())) {
this->updateLineNumberAreaWidth(0);
}
}
#[slotUpdateRequest]
#[resizeEvent]
sub resizeEvent {
my ($e) = @_;
this->SUPER::resizeEvent($e);
my $cr = this->contentsRect();
this->lineNumberArea->setGeometry(Qt::Rect($cr->left(), $cr->top(), this->lineNumberAreaWidth(), $cr->height()));
}
#[resizeEvent]
#[cursorPositionChanged]
sub highlightCurrentLine {
my $extraSelections = [];
if (!this->isReadOnly()) {
my $selection = Qt::TextEdit::ExtraSelection();
my $lineColor = Qt::Color(Qt::yellow())->lighter(160);
$selection->format->setBackground( Qt::Brush( $lineColor ) );
$selection->format->setProperty(Qt::TextFormat::FullWidthSelection(), Qt::Variant(Qt::Bool(1)));
$selection->setCursor( this->textCursor() );
$selection->cursor->clearSelection();
push @{$extraSelections}, $selection;
}
this->setExtraSelections($extraSelections);
}
#[cursorPositionChanged]
#[extraAreaPaintEvent_0]
sub lineNumberAreaPaintEvent {
my ($event) = @_;
my $painter = Qt::Painter(this->lineNumberArea);
$painter->fillRect($event->rect(), Qt::Brush(Qt::Color(Qt::lightGray())));
#[extraAreaPaintEvent_0]
#[extraAreaPaintEvent_1]
my $block = this->firstVisibleBlock();
my $blockNumber = $block->blockNumber();
my $top = int this->blockBoundingGeometry($block)->translated(this->contentOffset())->top();
my $bottom = $top + int this->blockBoundingRect($block)->height();
#[extraAreaPaintEvent_1]
#[extraAreaPaintEvent_2]
while ($block->isValid() && $top <= $event->rect()->bottom()) {
if ($block->isVisible() && $bottom >= $event->rect()->top()) {
my $number = $blockNumber + 1;
$painter->setPen(Qt::Color(Qt::black()));
$painter->drawText(0, $top, this->lineNumberArea->width(), this->fontMetrics()->height(),
Qt::AlignRight(), $number);
}
$block = $block->next();
$top = $bottom;
$bottom = $top + int this->blockBoundingRect($block)->height();
++$blockNumber;
}
$painter->end();
}
#[extraAreaPaintEvent_2]
|