-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjkcharacterscreenkeyboard.cpp
125 lines (106 loc) · 3.78 KB
/
jkcharacterscreenkeyboard.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Copyright (c) 2013-2015 Jan W. Krieger (<jan@jkrieger.de>)
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License (GPL) as published by
the Free Software Foundation, either version 3.0 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jkcharacterscreenkeyboard.h"
#include <QKeyEvent>
#include <QApplication>
#include <QPushButton>
JKCharacterScreenKeyboard::JKCharacterScreenKeyboard(QWidget* parent):
QWidget(parent)
{
m_characters="ABC";
m_generateApplicationEvent=true;
QFontMetrics fm(font());
int s=(double)fm.height()*1.5;
m_buttonSize=QSize(s,s);
m_focus=parent;
setFocusPolicy(Qt::NoFocus);
lay=new FlowLayout(this, 2, 0, 0);
setLayout(lay);
populateLayout();
}
JKCharacterScreenKeyboard::~JKCharacterScreenKeyboard()
{
//dtor
}
QSize JKCharacterScreenKeyboard::buttonSize() const {
return m_buttonSize;
}
void JKCharacterScreenKeyboard::setButtonSize(QSize s) {
m_buttonSize=s;
for (int i=0; i<m_buttons.size(); i++) {
QAbstractButton* but=m_buttons[i];
but->setMaximumSize(s);
but->setMinimumSize(s);
}
}
void JKCharacterScreenKeyboard::setKBFocusWidget(QWidget* focus) {
m_focus=focus;
}
QWidget* JKCharacterScreenKeyboard::KBFocusWidget() {
return m_focus;
}
void JKCharacterScreenKeyboard::setCharacters(const QString& characters) {
m_characters=characters;
populateLayout();
}
QString JKCharacterScreenKeyboard::characters() const {
return m_characters;
}
void JKCharacterScreenKeyboard::setGenerateFocusEvent(bool enable) {
m_generateApplicationEvent=enable;
}
bool JKCharacterScreenKeyboard::generateFocusEvent() const {
return m_generateApplicationEvent;
}
void JKCharacterScreenKeyboard::buttonClicked() {
int idx=-1;
if (sender()) {
QAbstractButton* but=qobject_cast<QAbstractButton*>(sender());
if (but) {
idx=m_buttons.indexOf(but);
}
}
if ((idx>-1)&&(idx<m_characters.size())) {
QChar character=m_characters[idx];
emit characterClicked(character);
if (m_generateApplicationEvent && m_focus) {
QKeyEvent keyEvent(QEvent::KeyPress, 0, Qt::NoModifier, QString(character));
QApplication::sendEvent(m_focus->focusWidget(), &keyEvent);
}
}
}
void JKCharacterScreenKeyboard::populateLayout() {
bool upEn=updatesEnabled();
if (upEn) setUpdatesEnabled(false);
// remove all buttons from layout and delete them
for (int i=0; i<m_buttons.size(); i++) {
disconnect(m_buttons[i], SIGNAL(clicked()), this, SLOT(buttonClicked()));
lay->removeWidget(m_buttons[i]);
delete m_buttons[i];
}
m_buttons.clear();
for (int i=0; i<m_characters.size(); i++) {
QChar character=m_characters[i];
if (character.isPrint()){
QPushButton* but=new QPushButton(QString(character), this);
m_buttons.append(but);
but->setFocusPolicy(Qt::NoFocus);
but->setMaximumSize(m_buttonSize);
but->setMinimumSize(m_buttonSize);
connect(but, SIGNAL(clicked()), this, SLOT(buttonClicked()));
lay->addWidget(but);
}
}
if (upEn) setUpdatesEnabled(true);
}