diff --git a/client.pro b/client.pro index 641520c3d..77dca7523 100644 --- a/client.pro +++ b/client.pro @@ -16,7 +16,7 @@ WITH_UNITTESTS = ON equals(WITH_BLACKMISC, ON) { SUBDIRS += src/blackmisc - SUBDIRS += src/blackmisc_cpp2xml +# SUBDIRS += src/blackmisc_cpp2xml } equals(WITH_BLACKSOUND, ON) { @@ -48,6 +48,7 @@ equals(WITH_SAMPLES, ON) { SUBDIRS += samples/blackmisc/sample_blackmisc.pro SUBDIRS += samples/voiceclient/sample_voice_client.pro SUBDIRS += samples/blacksim/sample_blacksim.pro + SUBDIRS += samples/hotkey/sample_hotkey.pro } equals(WITH_UNITTESTS, ON) { diff --git a/samples/hotkey/hotkey.cpp b/samples/hotkey/hotkey.cpp new file mode 100644 index 000000000..ece4ea548 --- /dev/null +++ b/samples/hotkey/hotkey.cpp @@ -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 +#include +#include + +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); +} diff --git a/samples/hotkey/hotkey.h b/samples/hotkey/hotkey.h new file mode 100644 index 000000000..81b84dd43 --- /dev/null +++ b/samples/hotkey/hotkey.h @@ -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 +#include +#include +#include + +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 diff --git a/samples/hotkey/led.cpp b/samples/hotkey/led.cpp new file mode 100644 index 000000000..995a89512 --- /dev/null +++ b/samples/hotkey/led.cpp @@ -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 +#include +#include +#include + +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); +} + + diff --git a/samples/hotkey/led.h b/samples/hotkey/led.h new file mode 100644 index 000000000..76a4e951a --- /dev/null +++ b/samples/hotkey/led.h @@ -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 +#include + +//! \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 diff --git a/samples/hotkey/main.cpp b/samples/hotkey/main.cpp new file mode 100644 index 000000000..e5c3e66d1 --- /dev/null +++ b/samples/hotkey/main.cpp @@ -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 + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + HotkeyDialog w; + w.show(); + + return a.exec(); +} diff --git a/samples/hotkey/sample_hotkey.pro b/samples/hotkey/sample_hotkey.pro new file mode 100644 index 000000000..a11647927 --- /dev/null +++ b/samples/hotkey/sample_hotkey.pro @@ -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