-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
92 lines (81 loc) · 2 KB
/
window.cpp
File metadata and controls
92 lines (81 loc) · 2 KB
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
#include "window.h"
#include <QPainter>
#include <QFont>
#include <QFontMetrics>
Window::Window(QWidget *parent) : QWidget(parent)
{
cursorTimer = new QTimer(this);
connect(cursorTimer, &QTimer::timeout, this, [this]() {
cursorVisible = !cursorVisible;
update();
});
cursorTimer->start(500);
}
void Window::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QFont font;
font.setPointSize(20);
painter.setFont(font);
int baseX = 10;
int baseY = 20;
pos = (char)baseX;
painter.drawText(baseX, baseY, text);
QFontMetrics fm(font);
int cursorX = baseX + fm.horizontalAdvance(text.left(cursorPos));
painter.drawText(200,300,pos);
if (cursorVisible)
{
painter.drawLine(
cursorX,
baseY - fm.ascent(),
cursorX,
baseY + fm.descent()
);
}
}
void Window::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Backspace &&
(event->modifiers() & Qt::ControlModifier))
{
if (cursorPos > 0){
int start = cursorPos;
while (start > 0 && text[start - 1] == ' ')
start--;
while (start > 0 && text[start - 1] != ' ')
start--;
text.remove(start, cursorPos - start);
cursorPos = start;
}
}
else if (event->key() == Qt::Key_Backspace)
{
if (cursorPos > 0)
{
text.remove(cursorPos - 1, 1);
cursorPos--;
}
}
else if (event->key() == Qt::Key_Left)
{
if (cursorPos > 0)
cursorPos--;
}
// Move Right
else if (event->key() == Qt::Key_Right)
{
if (cursorPos < text.length())
cursorPos++;
}
// Insert character
else if (!event->text().isEmpty())
{
text.insert(cursorPos, event->text());
cursorPos++;
}
// Reset blink on key press
cursorVisible = true;
cursorTimer->start(500);
update();
}