-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsafelineedit.cpp
More file actions
45 lines (35 loc) · 1.18 KB
/
safelineedit.cpp
File metadata and controls
45 lines (35 loc) · 1.18 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
#include "safelineedit.h"
#include <QKeyEvent>
#include <QCursor>
#include <QApplication>
#include "../Lib/alertsound.h"
SafeLineEdit::SafeLineEdit(QWidget *parent) :
QLineEdit(parent)
{
}
void SafeLineEdit::keyPressEvent(QKeyEvent *event)
{
bool allowed=true ;
bool selected=false ;
int key = event->key() ;
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_Delete || key==Qt::Key_Backspace ||
key==Qt::Key_Control || key==Qt::Key_Shift || key==Qt::Key_Alt) ;
allowed |= event->matches(QKeySequence::Copy) ;
allowed |= (key==Qt::Key_Delete && event->matches(QKeySequence::Cut)) ;
selected = this->hasSelectedText() ;
}
if (selected && !allowed) {
int pos = this->cursorPosition() ;
this->setCursorPosition(pos) ;
play(Disabled) ;
}
QLineEdit::keyPressEvent(event);
}
void SafeLineEdit::focusInEvent ( QFocusEvent * event)
{
QLineEdit::focusInEvent(event) ;
this->setCursorPosition(0) ;
}