diff --git a/src/SeerPlainTextEdit.cpp b/src/SeerPlainTextEdit.cpp index 6c37be2..9fb2462 100644 --- a/src/SeerPlainTextEdit.cpp +++ b/src/SeerPlainTextEdit.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include SeerPlainTextEdit::SeerPlainTextEdit(const QString& text, QWidget* parent) : QPlainTextEdit(text, parent) { @@ -14,6 +16,7 @@ SeerPlainTextEdit::SeerPlainTextEdit(const QString& text, QWidget* parent) : QPl _cursorTimer = new QTimer(this); QObject::connect(_cursorTimer, &QTimer::timeout, this, &SeerPlainTextEdit::blinkCursor); + QObject::connect(this, &QPlainTextEdit::cursorPositionChanged, this, &SeerPlainTextEdit::handleCursorPositionChanged); _cursorTimer->start(500); } @@ -24,6 +27,7 @@ SeerPlainTextEdit::SeerPlainTextEdit(QWidget* parent) : QPlainTextEdit(parent) { _cursorTimer = new QTimer(this); QObject::connect(_cursorTimer, &QTimer::timeout, this, &SeerPlainTextEdit::blinkCursor); + QObject::connect(this, &QPlainTextEdit::cursorPositionChanged, this, &SeerPlainTextEdit::handleCursorPositionChanged); _cursorTimer->start(500); } @@ -49,13 +53,37 @@ void SeerPlainTextEdit::paintEvent(QPaintEvent* event) { QPlainTextEdit::paintEvent(event); // If the widget does not have focus, draw the cursor manually. - if (!hasFocus() && _cursorVisible) { + if (_cursorVisible) { + setCursorWidth(CURSOR_WIDTH); QRect r = cursorRect(); - if (r.isValid()) { - QPainter p(viewport()); - p.fillRect(r, palette().text().color()); - } + QPainter p(viewport()); + p.fillRect(r, palette().text().color()); } + else + { + setCursorWidth(0); + } + + // Add margin highlight for current line + QPainter painter(viewport()); + QTextCursor cursor = textCursor(); + QTextBlock block = cursor.block(); + + // Get the rectangle of the current line (block) + QRectF rect = blockBoundingGeometry(block).translated(contentOffset()); + + // Optional: add a few pixels margin around the block + rect.adjust(-2, 0, 2, 0); + + // Draw a visible border + QPen pen(QColor(200, 200, 200), 0.5); + painter.setPen(pen); + painter.drawRect(rect); +} + +// Add margin highlight for current line +void SeerPlainTextEdit::handleCursorPositionChanged() { + viewport()->update(); } // diff --git a/src/SeerPlainTextEdit.h b/src/SeerPlainTextEdit.h index 362dfd1..d609f32 100644 --- a/src/SeerPlainTextEdit.h +++ b/src/SeerPlainTextEdit.h @@ -27,8 +27,10 @@ class SeerPlainTextEdit : public QPlainTextEdit { private slots: void blinkCursor (); + void handleCursorPositionChanged (); private: + constexpr static int CURSOR_WIDTH = 2; QTimer* _cursorTimer; bool _cursorVisible; };