Files
bell-mqtt/window.cpp

423 lines
13 KiB
C++

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "window.h"
#ifndef QT_NO_SYSTEMTRAYICON
#include <QAction>
#include <QSound>
#include <QCheckBox>
#include <QComboBox>
#include <QCoreApplication>
#include <QCloseEvent>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
#include <QPushButton>
#include <QSpinBox>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QSettings>
#include <QHostAddress>
#include <QSettings>
//! [0]
Window::Window()
{
createIconGroupBox();
createSoundGroupBox();
createIpGroupBox();
createIconGroupBox();
createMessageGroupBox();
iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
createActions();
createTrayIcon();
connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage);
connect(saveSettingsButton, &QAbstractButton::clicked, this, &Window::writeSettings);
connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
connect(iconComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &Window::setIcon);
connect(soundComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &Window::playSound);
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(iconGroupBox);
mainLayout->addWidget(soundGroupBox);
mainLayout->addWidget(ipGroupBox);
mainLayout->addWidget(messageGroupBox);
setLayout(mainLayout);
iconComboBox->setCurrentIndex(1);
trayIcon->show();
setWindowTitle(tr("Systray"));
readSettings();
}
//! [0]
void Window::writeSettings()
{
qDebug() << "Write Settings";
QSettings settings(QSettings::IniFormat, QSettings::UserScope,"BAD Soft", "Bell");
settings.beginGroup("Window");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.setValue("sound",soundComboBox->currentIndex());
settings.setValue("icon",iconComboBox->currentIndex());
settings.setValue("type",typeComboBox->currentIndex());
settings.setValue("check",showIconCheckBox->checkState());
settings.setValue("title",titleEdit->text());
settings.setValue("body",bodyEdit->toPlainText());
settings.setValue("duration",durationSpinBox->value());
settings.endGroup();
settings.beginGroup("Subscriber");
settings.setValue("address", ipEdit->text());
settings.endGroup();
}
void Window::readSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope,"BAD Soft", "Bell");
settings.beginGroup("Window");
resize(settings.value("size", QSize(400, 300)).toSize());
move(settings.value("pos", QPoint(200, 200)).toPoint());
soundComboBox->setCurrentIndex(settings.value("sound",0).toUInt());
iconComboBox->setCurrentIndex(settings.value("icon",1).toUInt());
typeComboBox->setCurrentIndex(settings.value("type",0).toUInt());
if (settings.value("check",1).toUInt())
showIconCheckBox->setCheckState(Qt::Checked);
else
showIconCheckBox->setCheckState(Qt::Unchecked);
titleEdit->setText(settings.value("title",tr("Bell ringing")).toString());
bodyEdit->setPlainText(settings.value("body",tr("Detected Bell ringing.\nClick this balloon for details.")).toString());
durationSpinBox->setValue(settings.value("duration",15).toUInt());
settings.endGroup();
}
void Window::playSound(int index)
{
QSound::play(bellResourceNames.at(index));
}
void Window::setMulticast(QHostAddress address)
{
qDebug() << "Signal Mulitcast = " << address.toString();
ipEdit->setText(address.toString());
}
void Window::packetArrived(int bellNumber)
{
showMessage();
}
//! [1]
void Window::setVisible(bool visible)
{
minimizeAction->setEnabled(visible);
maximizeAction->setEnabled(!isMaximized());
restoreAction->setEnabled(isMaximized() || !visible);
QDialog::setVisible(visible);
}
//! [1]
//! [2]
void Window::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_MACOS
if (!event->spontaneous() || !isVisible()) {
return;
}
#endif
if (trayIcon->isVisible()) {
QMessageBox::information(this, tr("Systray"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
hide();
event->ignore();
}
}
//! [2]
//! [3]
void Window::setIcon(int index)
{
QIcon icon = iconComboBox->itemIcon(index);
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->setToolTip(iconComboBox->itemText(index));
}
//! [3]
//! [4]
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
break;
case QSystemTrayIcon::MiddleClick:
showMessage();
break;
default:
;
}
}
//! [4]
//! [5]
void Window::showMessage()
{
qDebug() << "SHOW MESSAGE";
showIconCheckBox->setChecked(true);
int selectedIcon = typeComboBox->itemData(typeComboBox->currentIndex()).toInt();
QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(selectedIcon);
QSound::play(bellResourceNames.at(soundComboBox->currentIndex()));
if (selectedIcon == -1) { // custom icon
QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
durationSpinBox->value() * 1000);
} else {
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon,
durationSpinBox->value() * 1000);
}
emit settingsChanged(QHostAddress(ipEdit->text()));
}
//! [5]
//! [6]
void Window::messageClicked()
{
QMessageBox::information(nullptr, tr("Systray"),
tr("Sorry, I already gave what help I could.\n"
"Maybe you should try asking a human?"));
}
//! [6]
void Window::createIconGroupBox()
{
iconGroupBox = new QGroupBox(tr("Tray Icon"));
iconLabel = new QLabel("Icon:");
iconComboBox = new QComboBox;
iconComboBox->addItem(QIcon(":/images/bad.png"), tr("Bad"));
iconComboBox->addItem(QIcon(":/images/bell-icon.png"), tr("Bell"));
iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart"));
iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash"));
showIconCheckBox = new QCheckBox(tr("Show icon"));
showIconCheckBox->setChecked(true);
QHBoxLayout *iconLayout = new QHBoxLayout;
iconLayout->addWidget(iconLabel);
iconLayout->addWidget(iconComboBox);
iconLayout->addStretch();
iconLayout->addWidget(showIconCheckBox);
iconGroupBox->setLayout(iconLayout);
}
void Window::createSoundGroupBox()
{
soundGroupBox = new QGroupBox(tr("Playing sound"));
soundLabel = new QLabel("Sound:");
soundComboBox = new QComboBox;
foreach (QString str, bellNames) {
soundComboBox->addItem(str);
}
QHBoxLayout *soundLayout = new QHBoxLayout;
soundLayout->addWidget(soundLabel);
soundLayout->addWidget(soundComboBox);
soundLayout->addStretch();
soundGroupBox->setLayout(soundLayout);
}
void Window::createIpGroupBox()
{
ipGroupBox = new QGroupBox(tr("Adress of Mqtt server"));
ipLabel = new QLabel(tr("Address:"));
ipEdit = new QLineEdit("172.16.1.254");
QHBoxLayout *ipLayout = new QHBoxLayout;
ipLayout->addWidget(ipLabel);
ipLayout->addWidget(ipEdit);
ipLayout->addStretch();
ipGroupBox->setLayout(ipLayout);
}
QString Window::getIpAddress()
{
return ipEdit->text();
}
void Window::createMessageGroupBox()
{
messageGroupBox = new QGroupBox(tr("Balloon Message"));
typeLabel = new QLabel(tr("Type:"));
typeComboBox = new QComboBox;
typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
typeComboBox->addItem(style()->standardIcon(
QStyle::SP_MessageBoxInformation), tr("Information"),
QSystemTrayIcon::Information);
typeComboBox->addItem(style()->standardIcon(
QStyle::SP_MessageBoxWarning), tr("Warning"),
QSystemTrayIcon::Warning);
typeComboBox->addItem(style()->standardIcon(
QStyle::SP_MessageBoxCritical), tr("Critical"),
QSystemTrayIcon::Critical);
typeComboBox->addItem(QIcon(), tr("Custom icon"),
-1);
typeComboBox->setCurrentIndex(1);
durationLabel = new QLabel(tr("Duration:"));
durationSpinBox = new QSpinBox;
durationSpinBox->setRange(5, 60);
durationSpinBox->setSuffix(" s");
durationSpinBox->setValue(15);
durationWarningLabel = new QLabel(tr("(some systems might ignore this "
"hint)"));
durationWarningLabel->setIndent(10);
titleLabel = new QLabel(tr("Title:"));
titleEdit = new QLineEdit();
bodyLabel = new QLabel(tr("Body:"));
bodyEdit = new QTextEdit;
bodyEdit->setPlainText("");
saveSettingsButton = new QPushButton(tr("Save Settings"));
showMessageButton = new QPushButton(tr("Show Message"));
showMessageButton->setDefault(true);
QGridLayout *messageLayout = new QGridLayout;
messageLayout->addWidget(typeLabel, 0, 0);
messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
messageLayout->addWidget(durationLabel, 1, 0);
messageLayout->addWidget(durationSpinBox, 1, 1);
messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
messageLayout->addWidget(titleLabel, 2, 0);
messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
messageLayout->addWidget(bodyLabel, 3, 0);
messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
messageLayout->addWidget(saveSettingsButton, 5, 3, Qt::AlignRight);
messageLayout->addWidget(showMessageButton, 5, 4);
messageLayout->setColumnStretch(3, 1);
messageLayout->setRowStretch(4, 1);
messageGroupBox->setLayout(messageLayout);
}
void Window::createActions()
{
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);
maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
}
void Window::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
#endif