Sample Hotkey

Demonstrates the usage of IKeyboard

refs #83
This commit is contained in:
Roland Winklmeier
2014-03-05 15:55:30 +01:00
parent a7465fa6a3
commit f09c77ebd0
7 changed files with 345 additions and 1 deletions

93
samples/hotkey/hotkey.cpp Normal file
View File

@@ -0,0 +1,93 @@
/* Copyright (C) 2013 VATSIM Community / contributors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "hotkey.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
HotkeyDialog::HotkeyDialog(QWidget *parent) :
QDialog(parent),
m_keyboard(nullptr),
m_pbSelect(nullptr),
m_lblHotkey(nullptr),
m_led(nullptr)
{
m_keyboard = BlackCore::IKeyboard::getInstance();
setupUi();
setupConnections();
}
HotkeyDialog::~HotkeyDialog()
{
}
void HotkeyDialog::selectHotKey()
{
m_lblHotkey->setText("Press any key...");
m_keyboard->startCapture(false);
}
void HotkeyDialog::keySelectionChanged(BlackMisc::Hardware::CKeyboardKey key)
{
m_lblHotkey->setText(key.toFormattedQString());
}
void HotkeyDialog::keySelectionFinished(BlackMisc::Hardware::CKeyboardKey key)
{
m_lblHotkey->setText(key.toFormattedQString());
m_keyboard->unregisterAllHotkeys();
m_keyboard->registerHotkey(key, this, &HotkeyDialog::setPressed);
}
void HotkeyDialog::setPressed(bool isPressed)
{
m_led->setChecked(isPressed);
update();
}
void HotkeyDialog::setupUi()
{
m_pbSelect = new QPushButton(this);
m_pbSelect->setText("Select");
QFont font;
font.setPointSize(15);
m_lblHotkey = new QLabel(this);
m_lblHotkey->setFont(font);
m_lblHotkey->setAlignment(Qt::AlignCenter);
m_led = new CLed(this);
// PTT group box
QGroupBox *upperGroup = new QGroupBox;
upperGroup->setTitle("PTT");
QHBoxLayout *upperLine = new QHBoxLayout;
upperLine->addWidget(m_led);
upperGroup->setLayout(upperLine);
// Setting group box
QGroupBox *lowerGroup = new QGroupBox;
lowerGroup->setTitle("Setting");
QVBoxLayout *lowerLine = new QVBoxLayout;
lowerLine->addWidget(m_lblHotkey);
lowerLine->addWidget(m_pbSelect);
lowerGroup->setLayout(lowerLine);
// Main Layout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(upperGroup);
mainLayout->addWidget(lowerGroup);
setLayout(mainLayout);
setWindowTitle("Sample Hotkey");
}
void HotkeyDialog::setupConnections()
{
connect(m_keyboard, &BlackCore::IKeyboard::keySelectionChanged, this, &HotkeyDialog::keySelectionChanged);
connect(m_keyboard, &BlackCore::IKeyboard::keySelectionFinished, this, &HotkeyDialog::keySelectionFinished);
connect(m_pbSelect, &QPushButton::clicked, this, &HotkeyDialog::selectHotKey);
}

66
samples/hotkey/hotkey.h Normal file
View File

@@ -0,0 +1,66 @@
/* Copyright (C) 2013 VATSIM Community / contributors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BLACKSAMPLE_HOTKEY_H
#define BLACKSAMPLE_HOTKEY_H
#include "led.h"
#include "blackcore/keyboard.h"
#include "blackmisc/hwkeyboardkey.h"
#include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QScopedPointer>
class HotkeyDialog : public QDialog
{
Q_OBJECT
public:
/*!
* \brief Constructor
* \param parent
*/
explicit HotkeyDialog(QWidget *parent = 0);
//! Destructor
~HotkeyDialog();
public slots:
//! \brief Slot to select a new hot key
void selectHotKey();
private slots:
/*!
* \brief Slot when the selected key set has changed
* \param keySet
*/
void keySelectionChanged(BlackMisc::Hardware::CKeyboardKey key);
/*!
* \brief Slot when the key selection is finished
* \param keySet
*/
void keySelectionFinished(BlackMisc::Hardware::CKeyboardKey key);
private:
/*!
* \brief Set the key status
* \param isPressed
*/
void setPressed(bool isPressed);
private:
void setupUi();
void setupConnections();
BlackCore::IKeyboard *m_keyboard;
QPushButton *m_pbSelect;
QLabel *m_lblHotkey;
CLed *m_led;
};
#endif // BLACKSAMPLE_HOTKEY_H

67
samples/hotkey/led.cpp Normal file
View File

@@ -0,0 +1,67 @@
/* Copyright (C) 2013 VATSIM Community / contributors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "led.h"
#include <QPen>
#include <QPainter>
#include <QPointF>
#include <QRadialGradient>
CLed::CLed(QWidget *parent) :
QWidget(parent),
m_isChecked(false)
{
}
void CLed::paintEvent(QPaintEvent * /* event */)
{
qint32 diameter = qMin(width(), height());
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(diameter / 50.0, diameter / 50.0);
drawLed(painter);
drawShine(painter);
}
QSize CLed::minimumSizeHint() const
{
return QSize(50, 50);
}
void CLed::drawLed(QPainter &painter)
{
// Draw circle
QColor backgroundColor;
if (isChecked())
backgroundColor = Qt::red;
else
backgroundColor = Qt::gray;
// Black border color
// Fill color depends on the state
QPen penCircle;
penCircle.setColor(Qt::black);
penCircle.setWidthF(10.0);
painter.setBrush(backgroundColor);
painter.drawEllipse(QPointF(0.0, 0.0), 24.5, 24.5);
}
void CLed::drawShine(QPainter &painter)
{
// Draw shine
QColor white1(255,255,255,200);
QColor white0(255,255,255,0);
QRadialGradient shine(QPointF(-10.0,-10.0),30.0,QPointF(-10,-10));
shine.setColorAt(0.0, white1);
shine.setColorAt(1.0, white0);
painter.setBrush(shine);
painter.drawEllipse(QPointF(0.0, 0.0), 24.0, 24.0);
}

76
samples/hotkey/led.h Normal file
View File

@@ -0,0 +1,76 @@
/* Copyright (C) 2013 VATSIM Community / authors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BLACKSAMPLE_LED_H
#define BLACKSAMPLE_LED_H
#include <QWidget>
#include <QPainter>
//! \brief LED widget
class CLed : public QWidget
{
Q_OBJECT
public:
CLed(QWidget *parent = 0);
/*!
* \brief LED color
* \return
*/
QColor color() const { return m_color; }
/*!
* \brief Set LED color
* \param color
*/
void setColor(QColor color) { m_color = color; }
/*!
* \brief isChecked
* \return
*/
bool isChecked () const { return m_isChecked; }
//! \copydoc QWidget::minimumSizeHint()
virtual QSize minimumSizeHint () const override;
protected:
//! \copydoc QWidget::minimumSizeHint()
virtual void paintEvent(QPaintEvent * event) override;
signals:
/*!
* \brief Check value has changed
* \param value
*/
void checkChanged(bool value);
public slots:
/*!
* \brief setChecked
* \param value
*/
void setChecked(bool value) {m_isChecked = value;}
private:
/*!
* \brief drawLed
* \param painter
*/
void drawLed(QPainter & painter);
/*!
* \brief drawShine
* \param painter
*/
void drawShine(QPainter & painter);
bool m_isChecked;
QColor m_color;
};
#endif // BLACKSAMPLE_LED_H

16
samples/hotkey/main.cpp Normal file
View File

@@ -0,0 +1,16 @@
/* Copyright (C) 2013 VATSIM Community / contributors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "hotkey.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HotkeyDialog w;
w.show();
return a.exec();
}

View File

@@ -0,0 +1,25 @@
include (../../externals.pri)
QT += core gui dbus network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = sample_hotkey
CONFIG += c++11
CONFIG -= app_bundle
TEMPLATE = app
DEPENDPATH += . ../../src
INCLUDEPATH += . ../../src
SOURCES += *.cpp
HEADERS += *.h
LIBS += -L../../lib -lblackcore -lblackmisc
win32:!win32-g++*: PRE_TARGETDEPS += ../../lib/blackmisc.lib \
../../lib/blackcore.lib
else: PRE_TARGETDEPS += ../../lib/libblackmisc.a \
../../lib/libblackcore.a
DESTDIR = ../../bin