Virtual family keyboard in work.
This commit is contained in:
parent
7d3f0690e6
commit
db10fcb6bc
|
@ -107,6 +107,8 @@ FamilyKeyboardWidget::FamilyKeyboardWidget( QWidget *parent )
|
||||||
QFont font;
|
QFont font;
|
||||||
std::string fontString;
|
std::string fontString;
|
||||||
|
|
||||||
|
setMouseTracking(true);
|
||||||
|
|
||||||
g_config->getOption("SDL.FamilyKeyboardFont", &fontString);
|
g_config->getOption("SDL.FamilyKeyboardFont", &fontString);
|
||||||
|
|
||||||
if ( fontString.size() > 0 )
|
if ( fontString.size() > 0 )
|
||||||
|
@ -125,10 +127,19 @@ FamilyKeyboardWidget::FamilyKeyboardWidget( QWidget *parent )
|
||||||
|
|
||||||
calcFontData();
|
calcFontData();
|
||||||
|
|
||||||
|
keyPressed = -1;
|
||||||
|
keyUnderMouse = -1;
|
||||||
|
|
||||||
|
updateTimer = new QTimer(this);
|
||||||
|
|
||||||
|
connect(updateTimer, &QTimer::timeout, this, &FamilyKeyboardWidget::updatePeriodic);
|
||||||
|
|
||||||
|
updateTimer->start(50); // 20hz
|
||||||
}
|
}
|
||||||
//*********************************************************************************
|
//*********************************************************************************
|
||||||
FamilyKeyboardWidget::~FamilyKeyboardWidget(void)
|
FamilyKeyboardWidget::~FamilyKeyboardWidget(void)
|
||||||
{
|
{
|
||||||
|
updateTimer->stop();
|
||||||
|
|
||||||
}
|
}
|
||||||
//*********************************************************************************
|
//*********************************************************************************
|
||||||
|
@ -160,9 +171,104 @@ void FamilyKeyboardWidget::calcFontData(void)
|
||||||
setMinimumHeight( pxBtnGridY * 8 );
|
setMinimumHeight( pxBtnGridY * 8 );
|
||||||
}
|
}
|
||||||
//*********************************************************************************
|
//*********************************************************************************
|
||||||
|
void FamilyKeyboardWidget::updatePeriodic(void)
|
||||||
|
{
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
|
int FamilyKeyboardWidget::getKeyAtPoint( QPoint p )
|
||||||
|
{
|
||||||
|
for (int i=0; i<NUM_KEYS; i++)
|
||||||
|
{
|
||||||
|
if ( key[i].rect.contains(p) )
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
|
void FamilyKeyboardWidget::mousePressEvent(QMouseEvent * event)
|
||||||
|
{
|
||||||
|
keyPressed = keyUnderMouse = getKeyAtPoint(event->pos());
|
||||||
|
|
||||||
|
if ( keyPressed >= 0 )
|
||||||
|
{
|
||||||
|
key[ keyPressed ].pressed();
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
|
void FamilyKeyboardWidget::mouseReleaseEvent(QMouseEvent * event)
|
||||||
|
{
|
||||||
|
keyUnderMouse = getKeyAtPoint(event->pos());
|
||||||
|
|
||||||
|
if ( keyPressed >= 0 )
|
||||||
|
{
|
||||||
|
key[ keyPressed ].released();
|
||||||
|
|
||||||
|
keyPressed = -1;
|
||||||
|
}
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
|
void FamilyKeyboardWidget::mouseMoveEvent(QMouseEvent * event)
|
||||||
|
{
|
||||||
|
int tmpKeyUnderMouse = getKeyAtPoint(event->pos());
|
||||||
|
|
||||||
|
//printf("Mouse Move: Key:%i \n", keyUnderMouse );
|
||||||
|
|
||||||
|
if ( tmpKeyUnderMouse != keyUnderMouse )
|
||||||
|
{
|
||||||
|
keyUnderMouse = tmpKeyUnderMouse;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
|
void FamilyKeyboardWidget::mouseDoubleClickEvent(QMouseEvent * event)
|
||||||
|
{
|
||||||
|
keyUnderMouse = getKeyAtPoint(event->pos());
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
|
void FamilyKeyboardWidget::drawButton( QPainter &painter, int idx, int x, int y, int w, int h )
|
||||||
|
{
|
||||||
|
int i = idx;
|
||||||
|
QColor bgColor;
|
||||||
|
|
||||||
|
key[i].rect = QRect( x, y, w, h );
|
||||||
|
|
||||||
|
if ( key[i].isDown() )
|
||||||
|
{
|
||||||
|
if ( keyUnderMouse == idx )
|
||||||
|
{
|
||||||
|
bgColor = QColor( Qt::darkGreen );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bgColor = QColor( Qt::green );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( keyUnderMouse == idx )
|
||||||
|
{
|
||||||
|
bgColor = QColor( Qt::gray );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bgColor = QColor( Qt::lightGray );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
painter.fillRect( key[i].rect, bgColor );
|
||||||
|
painter.drawText( key[i].rect, Qt::AlignCenter, tr(keyNames[i]) );
|
||||||
|
painter.drawRect( key[i].rect );
|
||||||
|
|
||||||
|
}
|
||||||
|
//*********************************************************************************
|
||||||
void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
int i, j, x, y, w, h, xs, ys;
|
int i, x, y, w, h, xs, ys;
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
|
|
||||||
// Row 1
|
// Row 1
|
||||||
|
@ -177,12 +283,7 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
for (i=0; i<8; i++)
|
for (i=0; i<8; i++)
|
||||||
{
|
{
|
||||||
j = i;
|
drawButton( painter, i, x, y, w, h );
|
||||||
|
|
||||||
key[j].rect = QRect( x, y, w, h );
|
|
||||||
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += (w + xs);
|
x += (w + xs);
|
||||||
}
|
}
|
||||||
|
@ -197,13 +298,8 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
for (i=8; i<22; i++)
|
for (i=8; i<22; i++)
|
||||||
{
|
{
|
||||||
j = i;
|
drawButton( painter, i, x, y, w, h );
|
||||||
|
|
||||||
key[j].rect = QRect( x, y, w, h );
|
|
||||||
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += (w + xs);
|
x += (w + xs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,22 +313,14 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
for (i=22; i<35; i++)
|
for (i=22; i<35; i++)
|
||||||
{
|
{
|
||||||
j = i;
|
drawButton( painter, i, x, y, w, h );
|
||||||
|
|
||||||
key[j].rect = QRect( x, y, w, h );
|
|
||||||
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += (w + xs);
|
x += (w + xs);
|
||||||
}
|
}
|
||||||
|
|
||||||
x += (xs);
|
x += (xs);
|
||||||
|
|
||||||
j = 35;
|
drawButton( painter, 35, x, y, w*3, h );
|
||||||
key[j].rect = QRect( x, y, w*3, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
// Row 4
|
// Row 4
|
||||||
x = pxBtnGridX;
|
x = pxBtnGridX;
|
||||||
|
@ -245,20 +333,12 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
for (i=36; i<49; i++)
|
for (i=36; i<49; i++)
|
||||||
{
|
{
|
||||||
j = i;
|
drawButton( painter, i, x, y, w, h );
|
||||||
|
|
||||||
key[j].rect = QRect( x, y, w, h );
|
|
||||||
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += (w + xs);
|
x += (w + xs);
|
||||||
}
|
}
|
||||||
|
|
||||||
j = 49;
|
drawButton( painter, 49, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
// Row 5
|
// Row 5
|
||||||
x = pxBtnGridX / 2;
|
x = pxBtnGridX / 2;
|
||||||
|
@ -269,29 +349,18 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
xs = w / 4;
|
xs = w / 4;
|
||||||
//x -= xs;
|
//x -= xs;
|
||||||
|
|
||||||
j = 50;
|
drawButton( painter, 50, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += ((w*2) + xs);
|
x += ((w*2) + xs);
|
||||||
|
|
||||||
for (i=51; i<62; i++)
|
for (i=51; i<62; i++)
|
||||||
{
|
{
|
||||||
j = i;
|
drawButton( painter, i, x, y, w, h );
|
||||||
|
|
||||||
key[j].rect = QRect( x, y, w, h );
|
|
||||||
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += (w + xs);
|
x += (w + xs);
|
||||||
}
|
}
|
||||||
|
|
||||||
j = 62;
|
drawButton( painter, 62, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
// Row 6
|
// Row 6
|
||||||
x = (pxBtnGridX * 5) / 2;
|
x = (pxBtnGridX * 5) / 2;
|
||||||
|
@ -302,17 +371,11 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
xs = w / 4;
|
xs = w / 4;
|
||||||
x += (2*xs);
|
x += (2*xs);
|
||||||
|
|
||||||
j = 63;
|
drawButton( painter, 63, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += ((w*2) + xs);
|
x += ((w*2) + xs);
|
||||||
|
|
||||||
j = 64;
|
drawButton( painter, 64, x, y, (w*8) + (xs*7), h );
|
||||||
key[j].rect = QRect( x, y, (w*8) + (xs*7), h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
// Row 7
|
// Row 7
|
||||||
xs = pxBtnGridX / 4;
|
xs = pxBtnGridX / 4;
|
||||||
|
@ -327,12 +390,7 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
for (i=65; i<68; i++)
|
for (i=65; i<68; i++)
|
||||||
{
|
{
|
||||||
j = i;
|
drawButton( painter, i, x, y, w, h );
|
||||||
|
|
||||||
key[j].rect = QRect( x, y, w, h );
|
|
||||||
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += (w + xs);
|
x += (w + xs);
|
||||||
}
|
}
|
||||||
|
@ -341,32 +399,20 @@ void FamilyKeyboardWidget::paintEvent(QPaintEvent *event)
|
||||||
x = (pxBtnGridX+xs) * 17 + (pxBtnGridX)/2 + xs;
|
x = (pxBtnGridX+xs) * 17 + (pxBtnGridX)/2 + xs;
|
||||||
y += pxBtnGridY + ys;
|
y += pxBtnGridY + ys;
|
||||||
|
|
||||||
j = 68;
|
drawButton( painter, 68, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x = (pxBtnGridX+xs) * 17 - (pxBtnGridX)/2 + (xs/2);
|
x = (pxBtnGridX+xs) * 17 - (pxBtnGridX)/2 + (xs/2);
|
||||||
y += pxBtnGridY + ys;
|
y += pxBtnGridY + ys;
|
||||||
|
|
||||||
j = 69;
|
drawButton( painter, 69, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x += ((w*2) + xs);
|
x += ((w*2) + xs);
|
||||||
|
|
||||||
j = 70;
|
drawButton( painter, 70, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
|
|
||||||
x = (pxBtnGridX+xs) * 17 + (pxBtnGridX)/2 + xs;
|
x = (pxBtnGridX+xs) * 17 + (pxBtnGridX)/2 + xs;
|
||||||
y += pxBtnGridY + ys;
|
y += pxBtnGridY + ys;
|
||||||
|
|
||||||
j = 71;
|
drawButton( painter, 71, x, y, w*2, h );
|
||||||
key[j].rect = QRect( x, y, w*2, h );
|
|
||||||
painter.drawText( key[j].rect, Qt::AlignCenter, tr(keyNames[j]) );
|
|
||||||
painter.drawRect( key[j].rect );
|
|
||||||
}
|
}
|
||||||
//*********************************************************************************
|
//*********************************************************************************
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include <QRect>
|
#include <QRect>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QTimer>
|
||||||
|
#include <QMouseEvent>
|
||||||
#include <QFont>
|
#include <QFont>
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,8 +9,43 @@ class FKB_Key_t
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
FKB_Key_t(void)
|
||||||
|
{
|
||||||
|
vState = kState = 0;
|
||||||
|
toggleOnPress = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char isDown(void)
|
||||||
|
{
|
||||||
|
return vState || kState;
|
||||||
|
}
|
||||||
|
|
||||||
|
char pressed(void)
|
||||||
|
{
|
||||||
|
if ( toggleOnPress )
|
||||||
|
{
|
||||||
|
vState = !vState;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
vState = 1;
|
||||||
|
}
|
||||||
|
return vState;
|
||||||
|
}
|
||||||
|
|
||||||
|
char released(void)
|
||||||
|
{
|
||||||
|
if ( !toggleOnPress )
|
||||||
|
{
|
||||||
|
vState = 0;
|
||||||
|
}
|
||||||
|
return vState;
|
||||||
|
}
|
||||||
|
|
||||||
QRect rect;
|
QRect rect;
|
||||||
char name[16];
|
char vState;
|
||||||
|
char kState;
|
||||||
|
char toggleOnPress;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FamilyKeyboardWidget : public QWidget
|
class FamilyKeyboardWidget : public QWidget
|
||||||
|
@ -24,14 +61,27 @@ public:
|
||||||
protected:
|
protected:
|
||||||
//void keyPressEvent(QKeyEvent *event);
|
//void keyPressEvent(QKeyEvent *event);
|
||||||
//void kepaintEvent(QPaintEvent *event);
|
//void kepaintEvent(QPaintEvent *event);
|
||||||
void paintEvent(QPaintEvent *event);
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
void mousePressEvent(QMouseEvent * event) override;
|
||||||
|
void mouseReleaseEvent(QMouseEvent * event) override;
|
||||||
|
void mouseMoveEvent(QMouseEvent * event) override;
|
||||||
|
void mouseDoubleClickEvent(QMouseEvent * event) override;
|
||||||
|
|
||||||
|
int getKeyAtPoint( QPoint p );
|
||||||
void calcFontData(void);
|
void calcFontData(void);
|
||||||
|
void drawButton( QPainter &painter, int idx, int x, int y, int w, int h );
|
||||||
|
|
||||||
|
int keyUnderMouse;
|
||||||
|
int keyPressed;
|
||||||
int pxCharWidth;
|
int pxCharWidth;
|
||||||
int pxCharHeight;
|
int pxCharHeight;
|
||||||
int pxBtnGridX;
|
int pxBtnGridX;
|
||||||
int pxBtnGridY;
|
int pxBtnGridY;
|
||||||
|
|
||||||
FKB_Key_t key[NUM_KEYS];
|
FKB_Key_t key[NUM_KEYS];
|
||||||
|
|
||||||
|
QTimer *updateTimer;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void updatePeriodic(void);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue