758 lines
28 KiB
C++
758 lines
28 KiB
C++
#include "qtclasses.h"
|
||
|
||
template <typename T>
|
||
CustomWidgetEvent<T>::CustomWidgetEvent(QEvent::Type type, T payload) : QEvent(type){
|
||
this->payload = payload;
|
||
}
|
||
|
||
void ExtendedCheckBox::customEvent(QEvent* ev) {
|
||
//QEvent::Type tipo = ev->type();
|
||
if (ev->type() == (QEvent::Type)CustomQEvent::EndpointDefaultChange) {
|
||
//todo: still prone to bugs; whack-a-mole to come
|
||
ev->setAccepted(true);
|
||
this->blockSignals(true);
|
||
if (this->isEnabled()) {
|
||
this->setCheckState(Qt::Checked);
|
||
this->setDisabled(true);
|
||
} else {
|
||
this->setDisabled(false);
|
||
this->setCheckState(Qt::Unchecked);
|
||
}
|
||
this->blockSignals(false);
|
||
return;
|
||
}
|
||
// Make sure the rest of events are handled
|
||
QCheckBox::customEvent(ev);
|
||
}
|
||
|
||
QRect MainWindow::setSizePosition(int width, int height) {
|
||
//setGeometry ignores decoration size xdddd
|
||
QRect trayIconPos = this->trayIcon->geometry();
|
||
int tix1, tix2, tiy1, tiy2;
|
||
trayIconPos.getCoords(&tix1, &tiy1, &tix2, &tiy2);
|
||
|
||
screen = QGuiApplication::primaryScreen();
|
||
QRect screenRes = screen->geometry();
|
||
int srx1, srx2, sry1, sry2;
|
||
screenRes.getCoords(&srx1, &sry1, &srx2, &sry2);
|
||
QRect availableRes = screen->availableGeometry();
|
||
int arx1, arx2, ary1, ary2;
|
||
availableRes.getCoords(&arx1, &ary1, &arx2, &ary2);
|
||
|
||
uint8_t pos = 0;
|
||
pos = tiy2 < (sry2 / 2) ? SpawnPos::UP : SpawnPos::DOWN;
|
||
pos = tix2 < (srx2 / 2) ? pos | SpawnPos::LEFT : pos | SpawnPos::RIGHT;
|
||
|
||
switch (pos) {
|
||
case SpawnPos::UP | SpawnPos::RIGHT:
|
||
return QRect((arx2 - width), ary1, width, height);
|
||
break;
|
||
case SpawnPos::DOWN | SpawnPos::LEFT:
|
||
return QRect(arx1, (ary2-height), width, height);
|
||
break;
|
||
case SpawnPos::DOWN | SpawnPos::RIGHT:
|
||
return QRect((arx2 - width), (ary2-height), width, height);
|
||
break;
|
||
default:
|
||
return QRect(500, 400, width, height);
|
||
break;
|
||
}
|
||
}
|
||
|
||
void MainWindow::calculateChildWidgetsSize() {
|
||
//We need dynamically added child widgets to expand so that we know their height
|
||
//TODO: more heights
|
||
this->setAttribute(Qt::WA_DontShowOnScreen, true);
|
||
this->show();
|
||
this->layout()->invalidate();
|
||
this->hide();
|
||
this->setAttribute(Qt::WA_DontShowOnScreen, false);
|
||
|
||
int height = 0, width = 0;
|
||
for (auto *epw : this->ews) {
|
||
height += epw->height();
|
||
//width = (epw->width() > width) ? epw->width() : width;
|
||
}
|
||
width = scrollArea->width();
|
||
height += mainMenuBar->height();
|
||
height += hw->height();
|
||
|
||
/*
|
||
* Establishing initial window size and position
|
||
*/
|
||
//TODO: test. hardcode. var.
|
||
setGeometry(setSizePosition(width, height));
|
||
}
|
||
|
||
SessionWidget::SessionWidget(uint64_t idx, SessionHandler* sh, QWidget *parent) : QWidget(parent){
|
||
//todo: based on qgridlayout, name+mute should be its own widget, same with channels
|
||
this->idx = idx;
|
||
this->sh = sh;
|
||
|
||
widgetLayout = new QHBoxLayout(this);
|
||
//widgetLayout->setSizeConstraint(QLayout::SetFixedSize);
|
||
widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
||
//widgetLayout->setMaximumSize(minimumSize());
|
||
//this->setLayout(
|
||
|
||
muteButton = new QCheckBox(this);
|
||
mainLabel = new QLabel(QString::fromStdWString(sh->getName()), this);
|
||
mainSlider = new QSlider(Qt::Horizontal, this);
|
||
|
||
mainLabel->setMaximumWidth(150 /*1/16ish 1080p*/);
|
||
mainLabel->setMinimumWidth(150 /*1/16ish 1080p*/);
|
||
mainLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
|
||
|
||
mainSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||
mainSlider->setMinimumWidth(120 /*1/16 1080p*/);
|
||
mainSlider->setFocusPolicy(Qt::StrongFocus);
|
||
mainSlider->setTickPosition(QSlider::TicksBothSides);
|
||
mainSlider->setTickInterval(5);
|
||
mainSlider->setSingleStep(1);
|
||
mainSlider->setRange(0,100);
|
||
|
||
muteButton->setCheckState((sh->getMute() == false ? Qt::Unchecked : Qt::Checked));
|
||
muteButton->setText(sh->getMute() ? STRING_UNMUTE : STRING_MUTE);
|
||
muteButton->setMaximumWidth(60 /*1/32th 1080p*/);
|
||
muteButton->setMinimumWidth(60 /*1/32th 1080p*/);
|
||
float volume = sh->getVolume(AudioChannel::CHANNEL_MAIN) * 100;
|
||
mainSlider->setValue((int)volume);
|
||
log_debugcpp("SESSION SET WITH VOLUME " + std::to_string(volume));
|
||
|
||
//tip: would need to be new widget with layout in it
|
||
//mainMuteLayout = new QGridLayout();
|
||
/*
|
||
* layout->addWidget(mainLabel, 0, 0, Qt::AlignLeft | Qt::AlignBottom);
|
||
* layout->addWidget(muteButton, 0, 1, Qt::AlignLeft | Qt::AlignBottom);
|
||
* layout->addWidget(mainSlider, 0, 2, 1, 2);
|
||
*
|
||
* layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
||
*/
|
||
widgetLayout->addItem(new QSpacerItem(200, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
|
||
widgetLayout->addWidget(mainLabel, Qt::AlignLeft | Qt::AlignBottom);
|
||
widgetLayout->addWidget(muteButton, Qt::AlignRight | Qt::AlignBottom);
|
||
widgetLayout->addWidget(mainSlider, Qt::AlignRight | Qt::AlignBottom);
|
||
|
||
//widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
||
|
||
//TODO:0 = mute and muted, change volume = unmuted are client side tricks = 2 callbacks, one for volume, one for mute state. Implement as an user selectable option?
|
||
connect<void(QSlider::*)(int), void(SessionWidget::*)(int)>(mainSlider, &QSlider::valueChanged, this,&SessionWidget::updateMainVolume);
|
||
connect<void(QCheckBox::*)(int), void(SessionWidget::*)(int)>(muteButton, &QCheckBox::stateChanged, this, (&SessionWidget::updateMute));
|
||
|
||
/*
|
||
* Session Volume Polling
|
||
*/
|
||
volumePoller = new QTimer();
|
||
connect(volumePoller, &QTimer::timeout, [this, sh](){
|
||
//if (memcmp(osh->callbackInfo[idx]->caller, osh->getGuid(), sizeof(NGuid)) == 0) return; CHECK IF THIS PROGRAM GENERATED THE FUNSIES IS NO LONGER IN USE FOR NOW.
|
||
//todo: global + constexpr + ratio
|
||
const float roundingFactor = 0.005;
|
||
mainSlider->blockSignals(true);
|
||
muteButton->blockSignals(true);
|
||
mainSlider->setValue((int)((sh->getVolumeInfo()->mainVolume + roundingFactor) * 100));
|
||
muteButton->setCheckState((sh->getVolumeInfo()->muted == false ? Qt::Unchecked : Qt::Checked));
|
||
muteButton->setText(sh->getVolumeInfo()->muted ? STRING_UNMUTE : STRING_MUTE);
|
||
|
||
//memcpy(osh->callbackInfo[idx]->caller, osh->getGuid(), sizeof(NGuid));
|
||
|
||
//TODO: el default = objcopy frees?
|
||
//Todo: like fr pregunta
|
||
sh->getVolumeInfo()->caller = osh->getGuid();
|
||
mainSlider->blockSignals(false);
|
||
muteButton->blockSignals(false);
|
||
});
|
||
volumePoller->start(10);
|
||
}
|
||
|
||
void SessionWidget::updateMute(int checked){
|
||
bool muted = (checked == 2 ? true : false);
|
||
this->sh->setMute(osh->getGuid(), muted);
|
||
this->muteButton->setText(this->sh->getMute() ? STRING_UNMUTE : STRING_MUTE);
|
||
}
|
||
|
||
void SessionWidget::updateMainVolume(int newValue){
|
||
this->sh->setVolume(osh->getGuid(), AudioChannel::CHANNEL_MAIN, newValue);
|
||
}
|
||
|
||
SessionWidget::~SessionWidget() {
|
||
volumePoller->stop();
|
||
delete volumePoller;
|
||
}
|
||
|
||
ChannelWidget::ChannelWidget(uint32_t channelCount, EndpointHandler* eph, QWidget *parent) : QWidget(parent){
|
||
this->eph = eph;
|
||
this->channelCount = channelCount;
|
||
widgetLayout = new QGridLayout(this);
|
||
float volume = 100;
|
||
|
||
/*
|
||
* Channel sliders setup
|
||
*/
|
||
//uint32_t epChannelCount = eph->getChannelCount();
|
||
for(uint32_t i = 0; i < channelCount && channelCount > 1; i++){
|
||
QSlider* tmp = new QSlider(Qt::Horizontal);
|
||
QLabel* tmpLb = new QLabel("");
|
||
//tmp->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||
tmp->setTickInterval(5);
|
||
tmp->setSingleStep(1);
|
||
tmp->setRange(0,100);
|
||
|
||
volume = eph->getVolume(i) * 100;
|
||
tmp->setValue((int) volume);
|
||
tmpLb->setText(QString::number(volume));
|
||
//tmpLb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||
this->channelSliders.push_back(tmp);
|
||
this->channelLabels.push_back(tmpLb);
|
||
widgetLayout->addWidget(tmp, 0, i);
|
||
widgetLayout->addWidget(tmpLb, 1, i);
|
||
|
||
//TODO: check if there's a need to prevent deadlocks; probably this will eventually turn into its own func
|
||
//this causes channel bar desync when back -> front. blocksignals below fix it. huh.
|
||
connect(tmp, &QSlider::valueChanged, [this, i](int newValue){
|
||
this->eph->setVolume(osh->getGuid(), i, newValue);
|
||
this->channelLabels.at(i)->setText(QString::number(newValue));
|
||
});
|
||
}
|
||
this->setLayout(widgetLayout);
|
||
|
||
}
|
||
|
||
void ChannelWidget::updateChannel(int channel) {
|
||
this->channelSliders.at(channel)->blockSignals(true);
|
||
this->channelSliders.at(channel)->setValue((int)((eph->getCallbackInfo()->channelVolumes[channel] + roundingFactor) * 100));
|
||
this->channelLabels.at(channel)->setText(QString::number((int)((eph->getCallbackInfo()->channelVolumes[channel] + roundingFactor) * 100)));
|
||
this->channelSliders.at(channel)->blockSignals(false);
|
||
}
|
||
|
||
EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *parent) : QWidget(parent) {
|
||
//todo: based on qgridlayout, name+mute should be its own widget, same with channels
|
||
row = 0;
|
||
this->idx = idx;
|
||
this->eph = eph;
|
||
//todo: sussy
|
||
this->eph->setState(EndpointState::ENDPOINT_ACTIVE, idx);
|
||
|
||
//setAttribute(Qt::WA_TranslucentBackground);
|
||
widgetLayout = new QGridLayout(this);
|
||
//this->setLayout(widgetLayout);
|
||
log_debugcpp("epw main layout parent: " + std::to_string((intptr_t)(widgetLayout->parent())));
|
||
if (parent == nullptr) { log_debugcpp("ayooooo?"); }
|
||
|
||
defaultRolesCheckBoxes = {
|
||
{Roles::ROLE_ALL, new ExtendedCheckBox(this)},
|
||
{Roles::ROLE_CONSOLE, new ExtendedCheckBox(this)},
|
||
{Roles::ROLE_MULTIMEDIA, new ExtendedCheckBox(this)},
|
||
{Roles::ROLE_COMMUNICATIONS, new ExtendedCheckBox(this)}
|
||
};
|
||
|
||
/*
|
||
* Mute, main slider and label setup
|
||
*/
|
||
muteButton = new QCheckBox(this);
|
||
mainLabel = new QLabel(QString::fromStdWString(eph->getName()), this);
|
||
mainSlider = new QSlider(Qt::Horizontal, this);
|
||
|
||
if (this->eph->getState() != EndpointState::ENDPOINT_ACTIVE) {
|
||
widgetLayout->addWidget(mainLabel, row, 0);
|
||
//widgetLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Minimum), 1, 0);
|
||
return;
|
||
}
|
||
|
||
mainLabel->setMaximumWidth(240 /*1/8 1080p*/);
|
||
mainLabel->setMinimumWidth(240 /*1/8 1080p*/);
|
||
//mainLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
|
||
|
||
//muteButton->setStyleSheet("background-color: #A3C1DA; color: red");
|
||
mainSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
|
||
mainSlider->setFocusPolicy(Qt::StrongFocus);
|
||
mainSlider->setTickPosition(QSlider::TicksBothSides);
|
||
mainSlider->setTickInterval(5);
|
||
mainSlider->setSingleStep(1);
|
||
mainSlider->setRange(0,100);
|
||
|
||
muteButton->setCheckState((eph->getMute() == false ? Qt::Unchecked : Qt::Checked));
|
||
muteButton->setText(eph->getMute() ? STRING_UNMUTE : STRING_MUTE);
|
||
float volume = eph->getVolume(AudioChannel::CHANNEL_MAIN) * 100;
|
||
mainSlider->setValue((int)volume);
|
||
log_debugcpp("ENDPOINT SET WITH VOLUME " + std::to_string(volume));
|
||
|
||
//tip: would need to be new widget with layout in it
|
||
//mainMuteLayout = new QGridLayout();
|
||
widgetLayout->addWidget(mainLabel, row, 0, Qt::AlignLeft | Qt::AlignVCenter);
|
||
widgetLayout->addWidget(muteButton, row, 1, Qt::AlignLeft | Qt::AlignVCenter);
|
||
widgetLayout->addWidget(mainSlider, row, 2, 1, 2, Qt::AlignLeft | Qt::AlignVCenter);
|
||
//widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
||
//int debug2 = this->minimumWidth();
|
||
row++;
|
||
|
||
//TODO:0 = mute and muted, change volume = unmuted are client side tricks = 2 callbacks, one for volume, one for mute state. Implement as an user selectable option?
|
||
connect<void(QSlider::*)(int), void(EndpointWidget::*)(int)>(mainSlider, &QSlider::valueChanged, this,&EndpointWidget::updateMainVolume);
|
||
connect<void(QCheckBox::*)(int), void(EndpointWidget::*)(int)>(muteButton, &QCheckBox::stateChanged, this, (&EndpointWidget::updateMute));
|
||
|
||
|
||
/*
|
||
* Channel sliders setup
|
||
*/
|
||
|
||
uint32_t epChannelCount = eph->getChannelCount();
|
||
if(epChannelCount) {
|
||
cw = new ChannelWidget(epChannelCount, eph, this);
|
||
//cw->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||
widgetLayout->addWidget(cw, row++, 0, 1, 4 /*colmax*/);
|
||
}
|
||
|
||
/*
|
||
* Role ExtendedCheckBoxes setup
|
||
*/
|
||
|
||
uint8_t assignedRoles = eph->getRoles();
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_ALL)->setCheckState(assignedRoles == Roles::ROLE_ALL ? Qt::Checked : Qt::Unchecked);
|
||
//todo duditas de &
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_ALL)->setDisabled(assignedRoles == Roles::ROLE_ALL ? true : false);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_ALL)->setText(STRING_ROLE_ALL);
|
||
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE)->setCheckState(assignedRoles & Roles::ROLE_CONSOLE ? Qt::Checked : Qt::Unchecked);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE)->setDisabled(assignedRoles & Roles::ROLE_CONSOLE ? true : false);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE)->setText(STRING_ROLE_CONSOLE);
|
||
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA)->setCheckState(assignedRoles & Roles::ROLE_MULTIMEDIA ? Qt::Checked : Qt::Unchecked);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA)->setDisabled(assignedRoles & Roles::ROLE_MULTIMEDIA ? true : false);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA)->setText(STRING_ROLE_MULTIMEDIA);
|
||
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS)->setCheckState(assignedRoles & Roles::ROLE_COMMUNICATIONS ? Qt::Checked : Qt::Unchecked);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS)->setDisabled(assignedRoles & Roles::ROLE_COMMUNICATIONS ? true : false);
|
||
defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS)->setText(STRING_ROLE_COMMUNICATIONS);
|
||
|
||
connect(defaultRolesCheckBoxes.at(Roles::ROLE_ALL), &QCheckBox::stateChanged,[this] {
|
||
this->eph->setRoles(Roles::ROLE_ALL);
|
||
});
|
||
connect(defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE), &QCheckBox::stateChanged,[this] {
|
||
this->eph->setRoles(Roles::ROLE_CONSOLE);
|
||
});
|
||
connect(defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA), &QCheckBox::stateChanged,[this] {
|
||
this->eph->setRoles(Roles::ROLE_MULTIMEDIA);
|
||
});
|
||
|
||
connect(defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS), &QCheckBox::stateChanged,[this] {
|
||
this->eph->setRoles(Roles::ROLE_COMMUNICATIONS);
|
||
});
|
||
|
||
widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_ALL), row, 0);
|
||
widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_CONSOLE), row, 1);
|
||
widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_MULTIMEDIA), row, 2);
|
||
widgetLayout->addWidget(defaultRolesCheckBoxes.at(Roles::ROLE_COMMUNICATIONS), row, 3);
|
||
row++;
|
||
|
||
/* ----------------------------------------------------------- */
|
||
|
||
/*
|
||
* EndpointVolume Polling time
|
||
*/
|
||
|
||
timer = new QTimer();
|
||
connect(timer, &QTimer::timeout, [this, eph](){
|
||
//if (memcmp(osh->callbackInfo[idx]->caller, osh->getGuid(), sizeof(NGuid)) == 0) return; CHECK IF THIS PROGRAM GENERATED THE FUNSIES IS NO LONGER IN USE FOR NOW.
|
||
//todo: global + constexpr + ratio
|
||
const float roundingFactor = 0.005;
|
||
mainSlider->blockSignals(true);
|
||
muteButton->blockSignals(true);
|
||
mainSlider->setValue((int)((eph->getCallbackInfo()->mainVolume + roundingFactor) * 100));
|
||
muteButton->setCheckState((eph->getCallbackInfo()->muted == false ? Qt::Unchecked : Qt::Checked));
|
||
muteButton->setText(eph->getCallbackInfo()->muted ? STRING_UNMUTE : STRING_MUTE);
|
||
for(uint32_t i = 0; i < eph->getCallbackInfo()->channels && eph->getChannelCount() > 1; i++){
|
||
cw->updateChannel(i);
|
||
}
|
||
//memcpy(osh->callbackInfo[idx]->caller, osh->getGuid(), sizeof(NGuid));
|
||
|
||
//TODO: el default = objcopy frees?
|
||
//Todo: like fr pregunta
|
||
eph->getCallbackInfo()->caller = osh->getGuid();
|
||
mainSlider->blockSignals(false);
|
||
muteButton->blockSignals(false);
|
||
});
|
||
timer->start(10);
|
||
|
||
/* First SessionWidget batch */
|
||
for (size_t i = 0; i < eph->getSessionCount(); i++) {
|
||
SessionWidget* sessionWidget = new SessionWidget(i, eph->getSessionHandlers().at(i), this);
|
||
widgetLayout->addWidget(sessionWidget, row, 0, 1, 4 /* colmax */);
|
||
row++;
|
||
sessionWidgets.push_back(sessionWidget);
|
||
eph->getSessionHandlers().at(i)->setFrontIndex(i);
|
||
}
|
||
/* This spacer provides proper spacing when window vertically > widgets */
|
||
//widgetLayout->addItem(&lastRowSpacer, row, 0);
|
||
|
||
/* Add/Remove SessionWidget callback */
|
||
eph->setAddSessionWidgetFunction([this](SessionHandler* sessionHandler) {
|
||
QCoreApplication::instance()->postEvent(this, new CustomWidgetEvent<SessionHandler*>((QEvent::Type)CustomQEvent::SessionWidgetCreated, sessionHandler));
|
||
});
|
||
|
||
eph->setRemoveSessionWidgetFunction([this](SessionHandler* sessionHandler) {
|
||
QCoreApplication::instance()->postEvent(this, new CustomWidgetEvent<SessionHandler*>((QEvent::Type)CustomQEvent::SessionWidgetObsolete, sessionHandler));
|
||
});
|
||
|
||
log_debugcpp("ENDPOINT_WIDGETED");
|
||
}
|
||
|
||
void EndpointWidget::addSessionWidget(CustomWidgetEvent<SessionHandler*>* ev){
|
||
uint64_t index = this->sessionWidgets.size();
|
||
SessionWidget* sw = new SessionWidget(index, ev->payload, this);
|
||
ev->payload->setFrontIndex(index);
|
||
this->widgetLayout->addWidget(sw, row, 0, 1, 4);
|
||
row++;
|
||
sessionWidgets.push_back(sw);
|
||
return;
|
||
}
|
||
|
||
void EndpointWidget::removeSessionWidget(CustomWidgetEvent<SessionHandler*>* ev){
|
||
uint64_t i = ev->payload->getFrontIndex();
|
||
this->sessionWidgets.at(i)->setParent(nullptr);
|
||
this->widgetLayout->removeWidget(sessionWidgets.at(i));
|
||
delete sessionWidgets.at(i);
|
||
sessionWidgets.at(i) = nullptr;
|
||
ev->payload->setFrontIndex(INT_MAX);
|
||
//this->sessionWidgetsUpdateTimer->start();
|
||
return;
|
||
}
|
||
|
||
void EndpointWidget::customEvent(QEvent* ev) {
|
||
if (ev->type() == (QEvent::Type)CustomQEvent::SessionWidgetCreated) {
|
||
ev->setAccepted(true);
|
||
this->addSessionWidget((CustomWidgetEvent<SessionHandler*>*) ev);
|
||
} else if (ev->type() == (QEvent::Type)CustomQEvent::SessionWidgetObsolete) {
|
||
ev->setAccepted(true);
|
||
this->removeSessionWidget((CustomWidgetEvent<SessionHandler*>*) ev);
|
||
}
|
||
QWidget::customEvent(ev);
|
||
}
|
||
|
||
|
||
EndpointWidget::~EndpointWidget() {
|
||
timer->stop();
|
||
delete timer;
|
||
this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ALL, INT_MAX);
|
||
}
|
||
|
||
void MainWindow::customEvent(QEvent* ev) {
|
||
if (ev->type() == (QEvent::Type)CustomQEvent::EndpointWidgetObsolete) {
|
||
ev->setAccepted(true);
|
||
this->removeEndpointWidget((CustomWidgetEvent<uint64_t>*)ev);
|
||
} else if (ev->type() == (QEvent::Type)CustomQEvent::EndpointWidgetCreated) {
|
||
ev->setAccepted(true);
|
||
this->addEndpointWidget((CustomWidgetEvent<EndpointHandler*>*)ev);
|
||
}
|
||
QMainWindow::customEvent(ev);
|
||
}
|
||
|
||
void MainWindow::removeEndpointWidget(CustomWidgetEvent<uint64_t>* ev){
|
||
uint64_t i = ev->payload;
|
||
this->ews.at(i)->setParent(nullptr);
|
||
this->widgetLayout->removeWidget(ews.at(i));
|
||
//uint64_t saisu = ews.size();
|
||
//delete ews.at(index);
|
||
delete ews.at(i);
|
||
ews.at(i) = nullptr;
|
||
this->ewsUpdateTimer->start();
|
||
return;
|
||
}
|
||
|
||
void MainWindow::addEndpointWidget(CustomWidgetEvent<EndpointHandler*>* ev){
|
||
EndpointWidget* epw = new EndpointWidget(this->ews.size(), ev->payload, widget);
|
||
//epw->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||
this->widgetLayout->addWidget(epw);
|
||
ews.push_back(epw);
|
||
return;
|
||
}
|
||
|
||
void MainWindow::reorderEndpointWidgetCollection() {
|
||
/* Flatten */
|
||
size_t firstNullPosition = 0;
|
||
size_t ewsSize = ews.size();
|
||
bool breakSorting = false;
|
||
|
||
//todo: is all of gui really atomic by definition? im afraid of cutting through amazing add momentos, but I think I did my homework. Must check back.
|
||
for (size_t i = 0; i < ewsSize; i++) {
|
||
if (ews.at(i) == nullptr) {
|
||
for (size_t j = (i + 1); j < ewsSize; j++) {
|
||
|
||
if (ews.at(j) != nullptr) {
|
||
ews.at(i) = ews.at(j);
|
||
ews.at(i)->setIndex(i);
|
||
ews.at(j) = nullptr;
|
||
break;
|
||
}
|
||
if (j == ewsSize - 1) {
|
||
firstNullPosition = i;
|
||
breakSorting = true;
|
||
}
|
||
|
||
}
|
||
if (breakSorting) break;
|
||
}
|
||
}
|
||
ews.resize(firstNullPosition + 1);
|
||
}
|
||
|
||
void EndpointWidget::updateMute(int checked){
|
||
bool muted = (checked == 2 ? true : false);
|
||
this->eph->setMute(osh->getGuid(), muted);
|
||
this->muteButton->setText(this->eph->getMute() ? STRING_UNMUTE : STRING_MUTE);
|
||
}
|
||
|
||
void EndpointWidget::updateMainVolume(int newValue){
|
||
this->eph->setVolume(osh->getGuid(), AudioChannel::CHANNEL_MAIN, newValue);
|
||
}
|
||
|
||
/*
|
||
* void EndpointWidget::updateVolume(uint32_t channel, float newValue){
|
||
* //this->blockSignals(true);
|
||
* int newVal = newValue * 100;
|
||
* if (channel == (uint32_t)AudioChannel::CHANNEL_MAIN) {
|
||
* //TIP: Above
|
||
* //this->mainSlider->blockSignals(true);
|
||
*
|
||
* if(this->mainSlider->value() != newVal) {
|
||
* this->mainSlider->blockSignals(true);
|
||
* this->mainSlider->setValue(newVal);
|
||
* this->mainSlider->blockSignals(false);
|
||
* }
|
||
* return;
|
||
* }
|
||
*
|
||
* for (size_t i = 0; i < sizeof(uint32_t) * 8 && i < channelSliders.size(); ++i) {
|
||
* if (((channel >> i) & 1) && this->channelSliders.at(i)->value() != newVal) {
|
||
* //this->channelSliders.at(i)->blockSignals(true);
|
||
*
|
||
* this->channelSliders.at(i)->setValue(newVal);
|
||
* this->channelLabels.at(i)->setText(QString::number((int)(newValue * 100)));
|
||
*
|
||
* //this->channelSliders.at(i)->blockSignals(false);
|
||
* }
|
||
* }
|
||
*
|
||
* //this->blockSignals(false);
|
||
* }
|
||
*/
|
||
|
||
EndpointHandler* EndpointWidget::getEndpointHandler(){
|
||
return this->eph;
|
||
}
|
||
|
||
/*
|
||
* void EndpointWidget::updateFrontIndex(uint64_t index){
|
||
* this->idx = index;
|
||
* }
|
||
*/
|
||
|
||
void EndpointWidget::setIndex(uint64_t idx){
|
||
this->idx = idx;
|
||
this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ACTIVE, this->idx);
|
||
}
|
||
|
||
uint64_t EndpointWidget::getIndex(){
|
||
return idx;
|
||
}
|
||
|
||
std::map<Roles, ExtendedCheckBox*> EndpointWidget::getDefaultRolesWidgets() {
|
||
return defaultRolesCheckBoxes;
|
||
}
|
||
|
||
HeaderWidget::HeaderWidget(QWidget *parent) : QWidget(parent) {
|
||
widgetLayout = new QGridLayout(this);
|
||
|
||
QString text = "&" STRING_ABOUT;
|
||
about = new QPushButton(text, this);
|
||
#ifdef WIN32
|
||
text = "&" STRING_CP;
|
||
openCP = new QPushButton(text, this);
|
||
connect(openCP, &QPushButton::clicked, [](){ osh->openControlPanel(); });
|
||
text = "&" STRING_STARTUP;
|
||
startup = new QPushButton(text, this);
|
||
|
||
widgetLayout->addWidget(openCP , 0, 0);
|
||
widgetLayout->addWidget(startup, 0, 1);
|
||
#endif
|
||
widgetLayout->addWidget(about , 0, 2);
|
||
this->setLayout(widgetLayout);
|
||
}
|
||
|
||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), lastRowSpacer(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding) {
|
||
//setWindowState(Qt::WindowFullScreen);
|
||
//setCentralWidget(centralWidget);
|
||
//todo: ratio
|
||
setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint);
|
||
setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip);
|
||
setWindowTitle(STRING_TITLE);
|
||
connect(qApp, &QGuiApplication::applicationStateChanged, this, [=](Qt::ApplicationState state){
|
||
if(state == Qt::ApplicationState::ApplicationInactive) hide();
|
||
});
|
||
/*
|
||
* Registering needed custom events
|
||
*/
|
||
//| Qt::FramelessWindowHint
|
||
QEvent::registerEventType(CustomQEvent::EndpointWidgetObsolete);
|
||
QEvent::registerEventType(CustomQEvent::EndpointWidgetCreated);
|
||
QEvent::registerEventType(CustomQEvent::EndpointDefaultChange);
|
||
QEvent::registerEventType(CustomQEvent::SessionWidgetObsolete);
|
||
QEvent::registerEventType(CustomQEvent::SessionWidgetCreated);
|
||
|
||
//setWindowFlags(Qt::FramelessWindowHint);
|
||
//setParent(0); // Create TopLevel-Widget
|
||
//setAttribute(Qt::WA_NoSystemBackground, true);
|
||
//setAttribute(Qt::WA_TranslucentBackground, true);
|
||
ewsUpdateTimer = new QTimer(this);
|
||
widget = new QWidget();
|
||
widgetLayout = new QGridLayout();
|
||
trayIcon = new QSystemTrayIcon();
|
||
trayIconMenu = new QMenu();
|
||
trayIconMenuQuit = new QAction(STRING_QUIT);
|
||
trayIconMenuOpenCP = new QAction(STRING_CP);
|
||
|
||
ewsUpdateTimer->setSingleShot(true);
|
||
ewsUpdateTimer->setInterval(ewsUpdateTimerFrequency);
|
||
connect(ewsUpdateTimer, &QTimer::timeout, this, &MainWindow::reorderEndpointWidgetCollection);
|
||
//widget->setMinimumSize(QSize(300,300));
|
||
widget->setLayout(widgetLayout);
|
||
/*
|
||
* Scroll bar code
|
||
*/
|
||
scrollArea = new QScrollArea(this);
|
||
scrollArea->setWidget(widget);
|
||
scrollArea->setWidgetResizable(true);
|
||
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
scrollArea->setStyleSheet("QScrollBar:vertical { width: 4px; }");
|
||
//scrollArea->setMinimumWidth(500);
|
||
setCentralWidget(scrollArea);
|
||
|
||
/*
|
||
* Menu bar code
|
||
*/
|
||
mainMenuBar = this->menuBar();
|
||
hw = new HeaderWidget(this);
|
||
mainMenuBar->setCornerWidget(hw,Qt::TopLeftCorner);
|
||
mainMenuBar->show();
|
||
this->setMenuBar(mainMenuBar);
|
||
|
||
//setCentralWidget(widget);
|
||
//widgetLayout->addWidget(pintas, 0, 0);
|
||
|
||
reloadEndpointWidgets();
|
||
//scrollArea->setMinimumWidth(ews.at(0)->minimumWidth());
|
||
log_debugcpp(std::to_string(scrollArea->minimumWidth()));
|
||
|
||
/*
|
||
* Tray Icon code
|
||
*/
|
||
//trayIconMenu->addSeparator();
|
||
trayIconMenu->addAction(trayIconMenuOpenCP);
|
||
trayIconMenu->addSeparator();
|
||
trayIconMenu->addAction(trayIconMenuQuit);
|
||
connect(trayIconMenuOpenCP, &QAction::triggered, ([]() {osh->openControlPanel();}) );
|
||
connect(trayIconMenuQuit, &QAction::triggered, qApp, &QCoreApplication::quit);
|
||
trayIcon->setIcon(QIcon(":/assets/notificationAreaIcon.png"));
|
||
setWindowIcon(QIcon(":/assets/notificationAreaIcon.png"));
|
||
//TODO: Extend qsystemtrayicon to change mouse click?
|
||
//show before setting tooltip required; smells like bug to me!
|
||
trayIcon->show();
|
||
trayIcon->setToolTip(STRING_TITLE);
|
||
trayIcon->setContextMenu(trayIconMenu);
|
||
connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated);
|
||
|
||
/*
|
||
* Set of function callback definitons for EndpointSituationCallback
|
||
*/
|
||
osh->setChangeFrontDefaultsFunction([this](Roles role, std::wstring endpointId) {
|
||
for (auto epw : this->ews) {
|
||
/*
|
||
* Is this the new default endpoint?
|
||
*/
|
||
if (epw->getEndpointHandler()->getId() == endpointId) {
|
||
//not necessary to keep endpointState flags up to date right now, but updating it will allow for later config files / profiles
|
||
epw->getDefaultRolesWidgets().at(role)->blockSignals(true);
|
||
epw->getEndpointHandler()->assignRoles(role);
|
||
epw->getDefaultRolesWidgets().at(role)->blockSignals(false);
|
||
QCoreApplication::instance()->postEvent(epw->getDefaultRolesWidgets().at(role), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
|
||
//epw->defaultRolesCheckBoxes.at(role)->postEnableChange();
|
||
/*
|
||
* And were you THE default?
|
||
*/
|
||
if (epw->getEndpointHandler()->getRoles() == Roles::ROLE_ALL) {
|
||
QCoreApplication::instance()->postEvent(epw->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
|
||
}
|
||
/*
|
||
* Are you the dethroned king?
|
||
*/
|
||
} else if (epw->getEndpointHandler()->getRoles() & role) {
|
||
/*
|
||
* And were you THE default up until now?
|
||
*/
|
||
if (epw->getEndpointHandler()->getRoles() == Roles::ROLE_ALL) {
|
||
QCoreApplication::instance()->postEvent(epw->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
|
||
}
|
||
|
||
epw->getDefaultRolesWidgets().at(role)->blockSignals(true);
|
||
//Same as before. ini-san will come...
|
||
epw->getEndpointHandler()->removeRoles(role);
|
||
epw->getDefaultRolesWidgets().at(role)->blockSignals(false);
|
||
QCoreApplication::instance()->postEvent(epw->getDefaultRolesWidgets().at(role), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
|
||
|
||
}
|
||
}
|
||
});
|
||
|
||
osh->setRemoveEndpointWidgetFunction([this](uint64_t index) {
|
||
QCoreApplication::instance()->postEvent(this, new CustomWidgetEvent<uint64_t>((QEvent::Type)CustomQEvent::EndpointWidgetObsolete, index));
|
||
});
|
||
|
||
osh->setAddEndpointWidgetFunction([this](EndpointHandler* eph) {
|
||
QCoreApplication::instance()->postEvent(this, new CustomWidgetEvent<EndpointHandler*>((QEvent::Type)CustomQEvent::EndpointWidgetCreated, eph));
|
||
});
|
||
|
||
}
|
||
|
||
void MainWindow::closeEvent(QCloseEvent *event) {
|
||
if (!event->spontaneous() || !isVisible()) return;
|
||
|
||
if (trayIcon->isVisible()) {
|
||
//todo: would be nice to show this to 1st time users; ini-san will come...
|
||
//this->trayIcon->showMessage("ini file calling","tratarte como un gilipollas la primera vez", QSystemTrayIcon::Information);
|
||
|
||
hide();
|
||
event->ignore();
|
||
}
|
||
}
|
||
|
||
void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason) {
|
||
switch (reason) {
|
||
case QSystemTrayIcon::Trigger:
|
||
this->calculateChildWidgetsSize();
|
||
this->showNormal();
|
||
this->activateWindow();
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
void MainWindow::reloadEndpointWidgets() {
|
||
size_t i = 0;
|
||
//widgetLayout->addItem(&lastRowSpacer, i++, 0);
|
||
for (size_t epwIndex = 0; i < (osh->getPlaybackEndpointHandlers().size()); i++) {
|
||
if (osh->getPlaybackEndpointHandlers().at(i)->getState() == EndpointState::ENDPOINT_ACTIVE){
|
||
log_debugcpp("EPWidget creation");
|
||
//osh->getPlaybackEndpointHandlers().at(i)->getCallbackInfo()->caller = osh->getGuid();
|
||
EndpointWidget *epw = new EndpointWidget(epwIndex, osh->getPlaybackEndpointHandlers().at(i), widget);
|
||
epwIndex++;
|
||
//alfinal estoes solopara inicializarlmao
|
||
ews.push_back(epw);
|
||
widgetLayout->addWidget(epw, i, 0);
|
||
}
|
||
}
|
||
//todo:: tas aqui tirao, no me gustas y probablemente yo a ti tampoco
|
||
//seguramente falle al querer rematar esto con redimensionar la ventana s<>lo
|
||
//con los default endpoints en vista
|
||
widgetLayout->addItem(&lastRowSpacer, i, 0);
|
||
}
|
||
|