mixer/src/qt/qtclasses.cpp
2024-04-27 17:51:29 +02:00

920 lines
35 KiB
C++
Raw Blame History

#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(QScreen* screen, int width, int height) {
//setGeometry ignores decoration size, theres others for that
QRect trayIconPos = this->trayIcon->geometry();
int tix1, tix2, tiy1, tiy2;
trayIconPos.getCoords(&tix1, &tiy1, &tix2, &tiy2);
log_debugcpp("Tray Icon Pos: " + std::to_string(tix1) + " " + std::to_string(tix2)+" " + std::to_string(tiy1) + " " + std::to_string(tiy2));
this->setScreen(screen);
QRect screenRes = screen->geometry();
int srx1, srx2, sry1, sry2;
screenRes.getCoords(&srx1, &sry1, &srx2, &sry2);
log_debugcpp("Screen Res: " + std::to_string(srx1) + " " + std::to_string(srx2)+" " + std::to_string(sry1) + " " + std::to_string(sry2));
QRect availableRes = screen->availableGeometry();
int arx1, arx2, ary1, ary2;
availableRes.getCoords(&arx1, &ary1, &arx2, &ary2);
log_debugcpp("Available res: " + std::to_string(arx1) + " " + std::to_string(arx2)+" " + std::to_string(ary1) + " " + std::to_string(ary2));
if(height > (ary2 - ary1)) height = (ary2 - ary1);
log_debugcpp("Height res: " + std::to_string(height));
uint8_t pos = 0;
int leftDistance = std::abs(srx1 - tix1);
int rightDistance = std::abs(srx2 - tix1);
int upDistance = std::abs(sry1 - tiy1);
int downDistance = std::abs(sry2 - tiy1);
pos = (upDistance < downDistance) ? SpawnPos::UP : SpawnPos::DOWN;
pos = (leftDistance < rightDistance) ? pos | SpawnPos::LEFT : pos | SpawnPos::RIGHT;
switch (pos) {
case SpawnPos::UP | SpawnPos::RIGHT:
this->addToolBar(Qt::BottomToolBarArea, mainMenuBar);
return QRect((arx2 - width), ary1, width, height);
break;
case SpawnPos::DOWN | SpawnPos::LEFT:
this->addToolBar(Qt::TopToolBarArea, mainMenuBar);
return QRect(arx1, (ary2-height), width, height);
break;
case SpawnPos::DOWN | SpawnPos::RIGHT:
this->addToolBar(Qt::TopToolBarArea, mainMenuBar);
return QRect((arx2 - width), (ary2-height), width, height);
break;
default:
this->addToolBar(Qt::BottomToolBarArea, mainMenuBar);
log_debugcpp("Failed positioning window");
return QRect(500, 400, width, height);
break;
}
}
void MainWindow::compose() {
//todo: invalidate layout when adding sessions with window open
//We need dynamically added child widgets to expand so that we know their height
/*
* Setting correct widget widths and heights
*/
log_to_file("[Compose]\n");
screen = this->getCurrentScreen();
log_debugcpp("Screen: " + screen->model().toStdString() + " " + screen->name().toStdString());
QRect screenRes = screen->geometry();
int srx1, srx2, sry1, sry2;
screenRes.getCoords(&srx1, &sry1, &srx2, &sry2);
log_debugcpp("(for Percentage) Screen Res: " + std::to_string(srx1) + " " + std::to_string(srx2)+" " + std::to_string(sry1) + " " + std::to_string(sry2));
uint64_t windowWidth = (uint64_t)std::abs(screenRes.width()) * this->widthRatio;
uint64_t screenHeight = (uint64_t)std::abs(screenRes.height());
log_debugcpp("Window Width: " + std::to_string(windowWidth));
this->setAttribute(Qt::WA_DontShowOnScreen, true);
this->show();
this->widget->layout()->update();
this->hide();
this->setAttribute(Qt::WA_DontShowOnScreen, false);
for (auto *epw : ews) {
if (!epw) continue;
epw->calculateSize(windowWidth, screenHeight);
log_debugcpp("epw loop");
log_debugcpp("epw roles: " + print_as_binary((epw->getEndpointHandler()->getRoles())).to_string());
//std::bitset<sizeof(uint8_t)> content =
//content);
//varToBitset<sizeof(uint8_t), uint8_t>(epw->getEndpointHandler()->getRoles());
}
/*
* Calculating window height
*/
uint64_t windowHeight = 0;
int left = 0, top = 0, right = 0, bottom = 0;
for (int i = 0; i < 2; i++) {
if (!ews[i]) continue;
ews[i]->layout()->getContentsMargins(&left, &top, &right, &bottom);
windowHeight += ews[i]->sizeHint().height();
windowHeight += top;
windowHeight += bottom;
log_debugcpp("windowHeight loop: " + std::to_string(windowHeight));
}
windowHeight += mainMenuBar->height();
log_debugcpp("windowHeight final value: " + std::to_string(windowHeight));
//Undoing scrolling
scrollArea->verticalScrollBar()->setValue(0);
/*
* Establishing initial window size and position
*/
setGeometry(setSizePosition(screen, windowWidth, windowHeight));
}
QScreen* MainWindow::getCurrentScreen() {
//todo: Using cursor pos as screen detector. Flawed.
QPoint cursorPos = QCursor::pos();
log_debugcpp("Cursor pos: " + std::to_string(cursorPos.ry()) + " " + std::to_string(cursorPos.rx()));
for (QScreen *screen : QGuiApplication::screens()) {
QRect screenRect = screen->geometry();
if (screenRect.contains(cursorPos)) {
return screen;
}
}
return QGuiApplication::primaryScreen();
}
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;
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
widgetLayout = new QHBoxLayout(this);
//widgetLayout->setSizeConstraint(QLayout::SetFixedSize);
widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
int left = 0, top = 0, right = 0, bottom = 0;
widgetLayout->getContentsMargins(&left, &top, &right, &bottom);
widgetLayout->setContentsMargins(0, top, 0, bottom);
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
widthSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
widgetLayout->addItem(widthSpacer);
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(this);
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
if (sh->getVolumeInfo()->isNameChanged)
mainLabel->setText(QString::fromStdWString(sh->getName()));
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::calculateSize(uint64_t width, uint64_t height) {
/* og 1080p 120% testing values */
this->mainLabel->setMaximumWidth((int)(width * 0.30) /*1/16ish 1080p*/);
this->mainLabel->setMinimumWidth((int)(width * 0.30) /*1/16ish 1080p*/);
this->mainLabel->setMaximumHeight((int)(height * 0.02));
this->mainLabel->setMinimumHeight((int)(height * 0.02));
this->muteButton->setMaximumWidth((int)(width * 0.10) /*1/32th 1080p*/);
this->muteButton->setMinimumWidth((int)(width * 0.10) /*1/32th 1080p*/);
//this->muteButton->setMinimumWidth((int)(width * 0.10) /*1/16 1080p*/);
this->mainSlider->setMinimumWidth((int)(width * 0.30) /*1/16 1080p*/);
widthSpacer->changeSize((int)(width * 0.20), 1, QSizePolicy::Expanding, QSizePolicy::Minimum /*200*/);
log_to_file("\t[Session %s sizes]\n", converter.to_bytes(this->getName()).c_str());
log_to_file("\tMain label Maximum size: %d, %d \n", mainLabel->maximumWidth(), mainLabel->maximumHeight());
log_to_file("\tMain label Minimum size: %d, %d \n", mainLabel->minimumWidth(), mainLabel->minimumHeight());
log_to_file("\tMute btn Maximum width: %d \n", muteButton->maximumWidth());
log_to_file("\tMute btn Minimum width: %d \n", muteButton->minimumWidth());
log_to_file("\tSlider Minimum width: %d \n", mainSlider->minimumWidth());
log_to_file("\tSpacer Minimum width: %d \n\n", widthSpacer->minimumSize().width());
}
std::wstring SessionWidget::getName() {
return sh->getName();
}
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();
// volumePoller;
}
ChannelWidget::ChannelWidget(uint32_t channelCount, EndpointHandler* eph, QWidget *parent) : QWidget(parent){
this->eph = eph;
this->channelCount = channelCount;
this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
widgetLayout = new QGridLayout(this);
float volume = 100;
int left = 0, top = 0, right = 0, bottom = 0;
widgetLayout->getContentsMargins(&left, &top, &right, &bottom);
widgetLayout->setContentsMargins(0, top, 0, bottom);
/*
* Channel sliders setup
*/
//uint32_t epChannelCount = eph->getChannelCount();
for(uint64_t channel = 0, col = 0, row = 0; channel < channelCount && channelCount > 1; channel++){
QSlider* tmp = new QSlider(Qt::Horizontal);
QLabel* tmpLb = new QLabel("");
tmp->setTickInterval(5);
tmp->setSingleStep(1);
tmp->setRange(0,100);
volume = eph->getVolume(channel) * 100;
tmp->setValue((int) volume);
tmpLb->setText(QString::number(volume));
//tmpLb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
tmp->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
tmpLb->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
this->channelSliders.push_back(tmp);
this->channelLabels.push_back(tmpLb);
widgetLayout->addWidget(tmp, row , col);
widgetLayout->addWidget(tmpLb, row + 1, col++);
if(channel % 2 != 0) { row += 2; col = 0; }
//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, channel](int newValue){
this->eph->setVolume(osh->getGuid(), channel, newValue);
this->channelLabels.at(channel)->setText(QString::number(newValue));
});
}
this->setLayout(widgetLayout);
}
/*
* QSize ChannelWidget::minimumSizeHint() const {
* return minimum;
* }
*
* void ChannelWidget::setMinimum(QSize minimum) {
* this->minimum = minimum;
* }
*/
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);
}
uint32_t ChannelWidget::getChannelCount() const {
return channelCount;
}
EndpointWidget::EndpointWidget(EndpointHandler* eph, QWidget *parent, uint64_t idx) : 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);
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
//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);
mainVolumeLabel = new QLabel(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(350 /* 1080p 120%*/);
//mainLabel->setMinimumWidth(350 /* 1080p 120%*/);
mainLabel->setWordWrap(true);
mainLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
//mainLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
//muteButton->setStyleSheet("background-color: #A3C1DA; color: red");
mainSlider->setSizePolicy(QSizePolicy::Expanding, 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);
mainVolumeLabel->setText(QString::number(volume));
log_debugcpp("ENDPOINT SET WITH VOLUME " + std::to_string(volume));
//mainMuteLayout = new QGridLayout();
widgetLayout->addWidget(mainLabel, row, 0, 1, 2, Qt::AlignLeft | Qt::AlignVCenter);
widgetLayout->addWidget(muteButton, row, 2, Qt::AlignRight | Qt::AlignVCenter);
widgetLayout->addWidget(mainVolumeLabel, row, 3, Qt::AlignRight | Qt::AlignVCenter);
row++;
widgetLayout->addWidget(mainSlider, row, 0, 1, 4, Qt::AlignVCenter);
//widgetLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
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 > 1) {
cw = new ChannelWidget(epChannelCount, eph, nullptr);
//cw->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
widgetLayout->addWidget(cw, row++, 0, 1, 4 /*colmax*/, Qt::AlignTop);
}
/*
* Role ExtendedCheckBoxes setup
*/
defaultRolesCheckBoxes.at(Roles::ROLE_ALL)->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
uint8_t assignedRoles = eph->getRoles();
uint8_t col = 0;
#define checkbox_setup(role, optor) do { \
defaultRolesCheckBoxes.at(role)->setCheckState(assignedRoles optor role ? Qt::Checked : Qt::Unchecked); \
defaultRolesCheckBoxes.at(role)->setDisabled(assignedRoles optor role ? true : false); \
defaultRolesCheckBoxes.at(role)->setText(STRING_##role); \
connect(defaultRolesCheckBoxes.at(role), &QCheckBox::stateChanged,[this] { \
this->eph->setRoles(role); \
}); \
widgetLayout->addWidget(defaultRolesCheckBoxes.at(role), row, col++); \
} while(0)
checkbox_setup(ROLE_ALL, ==);
checkbox_setup(ROLE_CONSOLE, &);
checkbox_setup(ROLE_MULTIMEDIA, &);
checkbox_setup(ROLE_COMMUNICATIONS, &);
#undef checkbox_setup
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;
if (eph->getCallbackInfo()->updateName) {
eph->getCallbackInfo()->updateName = false;
mainLabel->setText(QString::fromStdWString(eph->getName()));
mainLabel->setMinimumHeight(mainLabel->sizeHint().height());
}
mainSlider->blockSignals(true);
muteButton->blockSignals(true);
mainSlider->setValue((int)((eph->getCallbackInfo()->mainVolume + roundingFactor) * 100));
mainVolumeLabel->setText(QString::number(mainSlider->value()));
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 Widget 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);
}
/* 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();
SessionWidget* deceased = sessionWidgets.at(i);
deceased->setParent(nullptr);
deceased->hide();
this->widgetLayout->removeWidget(deceased);
delete deceased;
sessionWidgets.at(i) = nullptr;
//row--;
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);
}
//__attribute__((optimize("O0", "unroll-loops")))
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(ev->payload, widget, this->ews.size());
//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::calculateSize(uint64_t width, uint64_t height) {
/* og 1080p 120% testing values */
log_to_file("[EndpointWidget %s sizes]\n", converter.to_bytes(this->getEndpointHandler()->getName()).c_str());
log_to_file("Params: {Width: %u Height: %u}\n", width, height);
this->mainLabel->setMaximumWidth((int)(width * 0.50) /* 1080p 120%*/);
this->mainLabel->setMinimumWidth((int)(width * 0.50) /* 1080p 120%*/);
log_to_file("Main label width: %d \n", this->mainLabel->maximumWidth());
if (cw) {
this->cw->setMinimumSize(QSize(1, (height * 0.06) * (int)((cw->getChannelCount() / 2) + 0.5)));
this->cw->setMaximumSize(QSize(QWIDGETSIZE_MAX, (height * 0.06) * (int)((cw->getChannelCount() / 2) + 0.5)));
log_to_file("Channels Maximum size: %d, %d \n", cw->maximumWidth(), cw->maximumHeight());
log_to_file("Channels Minimum size: %d, %d \n", cw->minimumWidth(), cw->minimumHeight());
}
for (auto sw : sessionWidgets){
if (sw) sw->calculateSize(width, height);
}
}
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){
mainVolumeLabel->setText(QString::number(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->setState(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) {
//setWindowState(Qt::WindowFullScreen);
//setCentralWidget(centralWidget);
//todo: ratio
setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint);
setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip);
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background-color: rgba(255,182,193,90%);");
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);
QEvent::registerEventType(CustomQEvent::RecomposeMainWindow);
/* This spacer provides proper spacing when window vertically > widgets. */
lastRowSpacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
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 = new QToolBar(this);
hw = new HeaderWidget(this);
mainMenuBar->addWidget(hw);
mainMenuBar->setMovable(false);
this->addToolBar(Qt::BottomToolBarArea, mainMenuBar);
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) {
//Sigh... I hope to get this right when librole is done...
EndpointWidget *newDef = nullptr, *oldDef = nullptr;
for (uint64_t i = 0; i < ews.size(); i++) {
auto epw = this->ews.at(i);
if (!epw) continue;
if (epw->getEndpointHandler()->getId() == endpointId) {
newDef = epw;
continue; }
if (epw->getEndpointHandler()->getRoles() & role) {
oldDef = epw;
continue; }
}
//debug if (role != Roles::ROLE_COMMUNICATIONS) return;
if (oldDef && newDef) {
this->ews.at(oldDef->getIndex()) = nullptr;
this->ews.at(newDef->getIndex()) = nullptr;
newDef->getDefaultRolesWidgets().at(role)->blockSignals(true);
newDef->getEndpointHandler()->assignRoles(role);
QCoreApplication::instance()->postEvent(newDef->getDefaultRolesWidgets().at(role), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
uint8_t newDefIdx = newDef->getIndex();
uint8_t newDefRoles = newDef->getEndpointHandler()->getRoles();
if (newDefRoles == Roles::ROLE_ALL) {
newDef->setIndex(0);
this->ews[0] = newDef;
QCoreApplication::instance()->postEvent(newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
} else if ((newDefRoles & Roles::ROLE_MULTIMEDIA) ||
(newDefRoles & Roles::ROLE_CONSOLE)){
newDef->setIndex(0);
this->ews[0] = newDef;
} else if (newDefRoles & Roles::ROLE_COMMUNICATIONS) {
newDef->setIndex(1);
this->ews[1] = newDef;
}
log_debugcpp("newDef new idx: " + std::to_string(newDef->getIndex()));
newDef->getDefaultRolesWidgets().at(role)->blockSignals(false);
oldDef->getDefaultRolesWidgets().at(role)->blockSignals(true);
uint8_t oldDefRoles = oldDef->getEndpointHandler()->getRoles();
if (oldDefRoles == Roles::ROLE_ALL) QCoreApplication::instance()->postEvent(oldDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
oldDef->getEndpointHandler()->removeRoles(role);
QCoreApplication::instance()->postEvent(oldDef->getDefaultRolesWidgets().at(role), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
oldDefRoles = oldDef->getEndpointHandler()->getRoles();
//this->ews[oldDef->getIndex()] = nullptr;
if ((oldDefRoles & Roles::ROLE_MULTIMEDIA) &&
(oldDefRoles & Roles::ROLE_CONSOLE)) {
this-> ews[0] = oldDef;
oldDef->setIndex(0);
} else if (oldDefRoles & Roles::ROLE_COMMUNICATIONS) {
this-> ews[1] = oldDef;
oldDef->setIndex(1);
} else {
if (newDefIdx > 1) {
this->ews[newDefIdx] = oldDef;
oldDef->setIndex(newDefIdx);
} else {
this->ews.push_back(oldDef);
oldDef->setIndex(this->ews.size() - 1);
}
}
log_debugcpp("oldDef new idx: " + std::to_string(oldDef->getIndex()));
oldDef->getDefaultRolesWidgets().at(role)->blockSignals(false);
}
});
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","primerita vez", QSystemTrayIcon::Information);
hide();
event->ignore();
}
}
void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::Trigger:
this->compose();
this->showNormal();
this->activateWindow();
break;
default:
break;
}
}
void MainWindow::reloadEndpointWidgets() {
size_t i = 0;
ews.resize(2);
//widgetLayout->addItem(&lastRowSpacer, i++, 0);
//todo: -log flag
//std::wofstream log("log.txt");
for (size_t epwIndex = 2; 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(osh->getPlaybackEndpointHandlers().at(i), widget);
log_wdebugcpp(L"epw name: " + epw->getEndpointHandler()->getName());
if ((epw->getEndpointHandler()->getRoles() == Roles::ROLE_ALL) ||
(epw->getEndpointHandler()->getRoles() & Roles::ROLE_MULTIMEDIA))
{ ews[0] = epw; widgetLayout->addWidget(epw, 0, 0); epw->setIndex(0); }
else if (epw->getEndpointHandler()->getRoles() & Roles::ROLE_COMMUNICATIONS)
{ ews[1] = epw; widgetLayout->addWidget(epw, 1, 0); epw->setIndex(1); }
else {
epw->setIndex(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);
}