1
0
Fork 0
OpenLCP/platform/qt/sacn/universestatuswidget.cpp

156 lines
4.3 KiB
C++

/*
universestatuswidget.cpp
Copyright (c) 2022 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "universestatuswidget.h"
#include <QPainter>
#include <QTimer>
UniverseStatusWidget::UniverseStatusWidget(QWidget *parent,
std::shared_ptr<DMX::Universe> universe)
: QWidget{parent}
, mUniverse(universe)
, mBlinkState(true)
{
setMinimumWidth(16);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
// initial state
setToolTip(statusText());
statusChanged();
// status feedback
data_change_token = mUniverse->onDataChange([this] (DMX::Universe*) {
dataActivity();
});
status_change_token = mUniverse->onStatusChange([this] (DMX::Universe*) {
statusChanged();
setToolTip(statusText());
});
}
/**
* @brief StatusIndicatorWidget::statusChanged
*/
void UniverseStatusWidget::statusChanged()
{
switch (mUniverse->status()) {
case DMX::Universe::DMX_LOST:
{
mBlinkState = !mBlinkState;
QTimer::singleShot(250, this, [this]() {
statusChanged();
});
break;
}
default:
mBlinkState = true;;
}
update();
}
/**
* @brief StatusIndicatorWidget::dataActivity
*/
void UniverseStatusWidget::dataActivity()
{
if (!mBlinkState)
return; // already in a blink state
QTimer::singleShot(100, this, [this]() {
mBlinkState = true;
update();
});
mBlinkState = false;
update();
}
/**
* @brief StatusIndicatorWidget::statusText
* @return
*/
const QString UniverseStatusWidget::statusText() const
{
switch (mUniverse->status())
{
case DMX::Universe::DMX_NULL:
return tr("Pending");
case DMX::Universe::DMX_ACTIVE:
return tr("Active");
case DMX::Universe::DMX_LOST:
return tr("Lost");
case DMX::Universe::RX_TIMEOUT:
return tr("Missing");
case sACN::Universe::sACN_TERMINATED:
return tr("Terminated");
default:
return tr("Undefined");
}
}
void UniverseStatusWidget::paintEvent(QPaintEvent *)
{
QColor stroke(127,127,127); // neutral
QColor fill(64,64,64); // grey
switch (mUniverse->status()) {
case DMX::Universe::DMX_NULL:
fill.setRgb(250,250,250); // white
break;
case DMX::Universe::DMX_ACTIVE:
fill.setRgb(32,255,64); // green
break;
case DMX::Universe::DMX_LOST:
fill.setRgb(255,255,64); // yellow
break;
case DMX::Universe::RX_TIMEOUT:
fill.setRgb(255,128,32); // amber
break;
case sACN::Universe::sACN_TERMINATED:
fill.setRgb(255,32,32); // red
break;
default:
break;
}
fill.setAlpha(mBlinkState ? 255 : 42);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(stroke);
painter.setBrush(fill);
const int size = qMin(width(), height());
switch (mUniverse->status()) {
case DMX::Universe::DMX_NULL:
case DMX::Universe::DMX_ACTIVE:
case DMX::Universe::DMX_LOST:
case DMX::Universe::RX_TIMEOUT:
case sACN::Universe::sACN_TERMINATED:
default:
painter.drawEllipse((width()-size)/2, (height()-size)/2, size, size); // circle
}
}