-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsafetextedit.cpp
63 lines (49 loc) · 1.93 KB
/
safetextedit.cpp
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
#include "safetextedit.h"
#include <QKeyEvent>
#include <QTextCursor>
#include <QApplication>
#include "../Lib/alertsound.h"
SafeTextEdit::SafeTextEdit(QWidget *parent) :
QPlainTextEdit(parent)
{
setAutoHomeEnabled(true) ;
setMouseEnabled(false) ;
}
void SafeTextEdit::setAutoHomeEnabled(bool state)
{
autohomeenabled=state ;
}
void SafeTextEdit::setMouseEnabled(bool state)
{
setAttribute(Qt::WA_TransparentForMouseEvents, !state) ;
}
void SafeTextEdit::keyPressEvent(QKeyEvent *event)
{
bool allowed=true ;
bool selected=false ;
int key = event->key() ;
QTextCursor cursor = this->textCursor() ;
if (event->type() == QKeyEvent::KeyPress) {
allowed = (key==Qt::Key_Left || key==Qt::Key_Right || key==Qt::Key_Up ||
key==Qt::Key_Down || key==Qt::Key_Home || key==Qt::Key_End ||
key==Qt::Key_Control || key==Qt::Key_Shift || key==Qt::Key_Alt ||
key==Qt::Key_PageDown || key==Qt::Key_PageUp) ;
allowed |= event->matches(QKeySequence::Copy) ;
allowed |= (key==Qt::Key_Delete && (event->modifiers()&Qt::ShiftModifier)!=0) ;
// move the cursor down and home or up and home
enum QTextCursor::MoveMode shifted = QTextCursor::MoveAnchor ;
if ((event->modifiers()&Qt::ShiftModifier)!=0) shifted=QTextCursor::KeepAnchor ;
if (autohomeenabled && (key==Qt::Key_Up || key==Qt::Key_Down || key==Qt::Key_PageDown || key==Qt::Key_PageUp)) {
cursor.movePosition(QTextCursor::StartOfLine, shifted) ;
this->setTextCursor(cursor) ;
}
// If the cursor start and end are not the same, there is a selection
selected = (cursor.selectionStart() != cursor.selectionEnd()) ;
}
if (selected && !allowed) {
play(Disabled) ;
cursor.setPosition(cursor.selectionEnd()) ;
this->setTextCursor(cursor) ;
}
QPlainTextEdit::keyPressEvent(event);
}