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

141 lines
4.5 KiB
C++

/*
universewindow.h
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 "universewindow.h"
#include "ui_universewindow.h"
#include "universemodel.h"
#include "universestatuswidget.h"
#include <QActionGroup>
#include <QComboBox>
#include <QLabel>
#include <QMetaEnum>
/**
* @brief UniverseWindow::UniverseWindow
* @param parent
*/
UniverseWindow::UniverseWindow(QWidget *parent, std::shared_ptr<DMX::Universe> universe)
: QMainWindow(parent)
, ui(new Ui::UniverseWindow())
, universe_(universe)
, status_watchdog_(new QTimer(this))
{
ui->setupUi(this);
// set the status watchdog to update the status if the universe
// isn't showing frequent activity
status_watchdog_->callOnTimeout([this](){update_status_();});
status_watchdog_->setInterval(DMX::E111_DATA_LOSS_TIMEOUT / 10);
status_watchdog_->start();
auto model = new UniverseModel(this, universe);
ui->tableView->setModel(model);
// update the status bar whenever the universe refreshes
connect(model, &UniverseModel::dataChanged, this, [this]() {
update_status_();
status_watchdog_->start();
});
// add data format combobox to toolbar
auto formatLabel = new QLabel(ui->toolBarDMX);
ui->toolBarDMX->addWidget(formatLabel);
formatLabel->setText(tr("Data Format") + ": ");
auto dataModes = new QComboBox(ui->toolBarDMX);
ui->toolBarDMX->addWidget(dataModes);
const QMetaEnum types = QMetaEnum::fromType<UniverseModel::data_modes>();
for (int i=0; i < types.keyCount(); ++i)
dataModes->addItem(types.key(i));
auto formatGroup = new QActionGroup(ui->centralwidget);
formatGroup->addAction(ui->actionViewDecimal);
formatGroup->addAction(ui->actionViewHex);
formatGroup->addAction(ui->actionViewPercent);
connect(dataModes, &QComboBox::currentTextChanged,
this, [this, model, types, formatGroup](QString mode) {
auto data_mode = static_cast<UniverseModel::data_modes>(types.keyToValue(mode.toLocal8Bit()));
model->setDataMode(data_mode);
formatGroup->blockSignals(true);
switch (data_mode) {
case UniverseModel::Decimal:
ui->actionViewDecimal->setChecked(true);
break;
case UniverseModel::Hex:
ui->actionViewHex->setChecked(true);
break;
case UniverseModel::Percent:
ui->actionViewPercent->setChecked(true);
break;
default:
break;
}
formatGroup->blockSignals(false);
});
connect(ui->actionViewDecimal, &QAction::toggled, this, [dataModes](bool state) {
if (state)
dataModes->setCurrentIndex((int)UniverseModel::Decimal);
});
connect(ui->actionViewHex, &QAction::toggled, this, [dataModes](bool state) {
if (state)
dataModes->setCurrentIndex((int)UniverseModel::Hex);
});
connect(ui->actionViewPercent, &QAction::toggled, this, [dataModes](bool state) {
if (state)
dataModes->setCurrentIndex((int)UniverseModel::Percent);
});
// status indicator
auto indicator = new UniverseStatusWidget(this, universe_);
ui->statusbar->addPermanentWidget(indicator);
update_status_();
}
/**
* @brief UniverseWindow::~UniverseWindow
*/
UniverseWindow::~UniverseWindow()
{
delete ui;
}
/**
* @brief UniverseWindow::updateStatusBar
*/
void UniverseWindow::update_status_()
{
if (!universe_->isEditable())
{
// status bar
ui->statusbar->clearMessage();
QString message = QString("%1 " + tr("Hz")).arg(
QString::number(universe_->rxRate()));
ui->statusbar->showMessage(message, 2500);
}
}