44
55#include < QPainter>
66#include < QTextBlock>
7+ #include < QStatusBar>
78
8- CodeEditor::CodeEditor (QWidget *parent) : QPlainTextEdit(parent)
9+ CodeEditor::CodeEditor (QWidget *parent)
10+ : QPlainTextEdit(parent),
11+ m_lineNumberArea(new LineNumberArea(this ))
912{
10- lineNumberArea = new LineNumberArea (this );
11-
1213 connect (this , &CodeEditor::blockCountChanged, this , &CodeEditor::updateLineNumberAreaWidth);
1314 connect (this , &CodeEditor::updateRequest, this , &CodeEditor::updateLineNumberArea);
1415 connect (this , &CodeEditor::cursorPositionChanged, this , &CodeEditor::highlightCurrentLine);
@@ -33,6 +34,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
3334 {
3435 case Qt::Key_I:
3536 mode = INSERT;
37+ emit statusMessageChanged (" Insert mode activated" );
3638 break ;
3739 case Qt::Key_A:
3840 moveCursor (QTextCursor::Left);
@@ -46,14 +48,22 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
4648 case Qt::Key_W:
4749 moveCursor (QTextCursor::Up);
4850 break ;
49- case Qt::Key_Escape :
50- mode = NORMAL ;
51+ default :
52+ emit statusMessageChanged ( " Insert mode is not active. Press 'i' to enter insert mode. " ) ;
5153 break ;
5254 }
5355 }
54- else
56+ else if (mode == INSERT)
5557 {
56- QPlainTextEdit::keyPressEvent (event);
58+ if (event->key () == Qt::Key_Escape)
59+ {
60+ mode = NORMAL;
61+ emit statusMessageChanged (" Normal mode activated. Press 'escape' to return to normal mode." );
62+ }
63+ else
64+ {
65+ QPlainTextEdit::keyPressEvent (event);
66+ }
5767 }
5868}
5969
@@ -82,11 +92,11 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
8292{
8393 if (dy)
8494 {
85- lineNumberArea ->scroll (0 , dy);
95+ m_lineNumberArea ->scroll (0 , dy);
8696 }
8797 else
8898 {
89- lineNumberArea ->update (0 , rect.y (), lineNumberArea ->width (), rect.height ());
99+ m_lineNumberArea ->update (0 , rect.y (), m_lineNumberArea ->width (), rect.height ());
90100 }
91101
92102 if (rect.contains (viewport ()->rect ()))
@@ -100,7 +110,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
100110 QPlainTextEdit::resizeEvent (e);
101111
102112 QRect cr = contentsRect ();
103- lineNumberArea ->setGeometry (QRect (cr.left (), cr.top (), lineNumberAreaWidth (), cr.height ()));
113+ m_lineNumberArea ->setGeometry (QRect (cr.left (), cr.top (), lineNumberAreaWidth (), cr.height ()));
104114}
105115
106116void CodeEditor::highlightCurrentLine ()
@@ -128,13 +138,13 @@ void CodeEditor::highlightCurrentLine()
128138
129139void CodeEditor::lineNumberAreaPaintEvent (QPaintEvent *event)
130140{
131- QPainter painter (lineNumberArea );
141+ QPainter painter (m_lineNumberArea );
132142
133143 // Match the background color of the editor
134144 painter.fillRect (event->rect (), palette ().color (QPalette::Base));
135145
136146 // Draw a separating line between the number area and the text editor
137- int separatorX = lineNumberArea ->width () - 4 ;
147+ int separatorX = m_lineNumberArea ->width () - 4 ;
138148 painter.drawLine (separatorX, event->rect ().top (), separatorX, event->rect ().bottom ());
139149
140150 QTextBlock block = firstVisibleBlock ();
@@ -152,7 +162,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
152162 QString number = QString::number (blockNumber + 1 );
153163 painter.setPen (Qt::darkGray);
154164
155- painter.drawText (0 , top + padding, lineNumberArea ->width (), lineHeight,
165+ painter.drawText (0 , top + padding, m_lineNumberArea ->width (), lineHeight,
156166 Qt::AlignCenter, number);
157167 }
158168
0 commit comments