mixer/src/qt/qtclasses.cpp

1369 lines
53 KiB
C++
Raw Blame History

#include "qtclasses.h"
#include "meterslider.h"
#define POLLING_RATE 2
bool DarkModeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *) {
if (eventType == "windows_generic_MSG") {
ProcessedNativeEvent event = osh->processTopLevelWindowMessage(message);
switch(event) {
case COLORS:
StylingHelper::setBackgroundColor(osh->isLightMode());
//Update accent color across entire app
{
uint32_t color = osh->getAccentColor();
uint8_t r, g, b, a;
if (StylingHelper::argbToDiscreteValues(osh->getAccentColor(), &r, &g, &b, &a)) {
const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
for (QWidget *widget : topLevelWidgets) {
if(qobject_cast<MainWindow*>(widget)) {
((MainWindow*)widget)->updateColor(r, g, b, a);
}
}
}
}
return true;
break;
default:
break;
}
}
return false;
}
template <typename T>
CustomWidgetEvent<T>::CustomWidgetEvent(QEvent::Type type, T payload) : QEvent(type){
this->payload = payload;
}
/*
* MeterSlider::MeterSlider(Qt::Orientation orientation, QWidget* parent) {
* //style = new MixerStyle();
* //this->setStyle(style);
* }
*/
MeterSlider::~MeterSlider() {
//delete style;
}
void MeterSlider::setPeakValue(float peakValue) {
this->peakValue = peakValue;
}
bool MeterSlider::event(QEvent* ev) {
if (ev->type() == QEvent::Wheel) {
ev->setAccepted(false);
return false;
}
return QSlider::event(ev);
}
void MeterSlider::paintEvent(QPaintEvent *event) {
QStyleOptionSlider sliderComplex = QStyleOptionSlider();
sliderComplex.initFrom(this);
/*
* sliderComplex.initFrom(this);
* sliderComplex.subControls = QStyle::SC_None;
* sliderComplex.activeSubControls = QStyle::SC_None;
* sliderComplex.orientation = this->orientation();
* sliderComplex.maximum = this->maximum();
* sliderComplex.minimum = this->minimum();
* sliderComplex.tickPosition = (QSlider::TickPosition)this->tickPosition();
* sliderComplex.tickInterval = this->tickInterval();
* sliderComplex.upsideDown = (this->orientation() == Qt::Horizontal) ?
* (this->invertedAppearance() != (sliderComplex.direction == Qt::RightToLeft))
* : (!this->invertedAppearance());
* sliderComplex.direction = Qt::LeftToRight; // we use the upsideDown option instead
* sliderComplex.sliderPosition = this->sliderPosition();
* sliderComplex.sliderValue = this->value();
* sliderComplex.singleStep = this->singleStep();
* sliderComplex.pageStep = this->pageStep();
* if (this->orientation() == Qt::Horizontal)
* sliderComplex.state |= QStyle::State_Horizontal;
*
* if (this->isSliderDown()) {
* sliderComplex.activeSubControls = QStyle::SC_SliderHandle;
* sliderComplex.state |= QStyle::State_Sunken;
* } else {
* sliderComplex.activeSubControls = QStyle::SC_SliderHandle;
* }
*
* //sliderComplex.subControls = QStyle::SC_SliderGroove;
* if (this->tickPosition() != NoTicks) sliderComplex.subControls |= QStyle::SC_SliderTickmarks;
* QStylePainter p(this);
*/
//p.drawComplexControl(QStyle::CC_Slider, sliderComplex);
QStyleOptionSlider sliderComplex2 = QStyleOptionSlider();
sliderComplex2.initFrom(this);
sliderComplex2.orientation = this->orientation();
sliderComplex2.maximum = this->maximum();
sliderComplex2.minimum = this->minimum();
sliderComplex2.tickPosition = (QSlider::TickPosition)this->tickPosition();
sliderComplex2.tickInterval = this->tickInterval();
sliderComplex2.upsideDown = (this->orientation() == Qt::Horizontal) ?
(this->invertedAppearance() != (sliderComplex2.direction == Qt::RightToLeft))
: (!this->invertedAppearance());
sliderComplex2.subControls = QStyle::SC_SliderHandle;
sliderComplex2.direction = Qt::LeftToRight; // we use the upsideDown option instead
sliderComplex2.sliderPosition = this->sliderPosition();
sliderComplex2.sliderValue = this->value();
sliderComplex2.singleStep = this->singleStep();
sliderComplex2.pageStep = this->pageStep();
if (this->orientation() == Qt::Horizontal)
sliderComplex2.state |= QStyle::State_Horizontal;
if (this->isSliderDown()) {
sliderComplex2.activeSubControls = QStyle::SC_SliderHandle;
sliderComplex2.state |= QStyle::State_Sunken;
} else {
sliderComplex2.activeSubControls = QStyle::SC_SliderHandle;
}
sliderComplex2.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
if ((QSlider::TickPosition)this->tickPosition() != NoTicks)
sliderComplex2.subControls |= QStyle::SC_SliderTickmarks;
QPainter painter(this);
QStyle* style = QApplication::style();
style->drawComplexControl((QStyle::ComplexControl)CC_MeterSlider, &sliderComplex2, &painter, this);
//Q_D(QSlider);
/*
* QStylePainter p(this);
* QStyleOptionSlider opt;
* initStyleOption(&opt);
*
* opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
* //if (d->tickPosition != NoTicks)
* // opt.subControls |= QStyle::SC_SliderTickmarks;
*
* //p.drawComplexControl(QStyle::CC_Slider, opt);
*/
//QSlider::paintEvent(event);
//IL CODE WENO lbockomet
/*
* int left = 0, top = 0, right = 0, bottom = 0;
* ((QWidget*)parent())->layout()->getContentsMargins(&left, &top, &right, &bottom);
*
* QStyle *style = QApplication::style();
* //int lol = style->pixelMetric(QStyle::PM_SliderSpaceAvailable);
* QPainter painter(this);
* //painter.setPen(Qt::blue);
* painter.setOpacity(1.0);
* painter.setClipping(false);
* painter.setCompositionMode(QPainter::CompositionMode::CompositionMode_Source);
* float peakLength = (this->width() * (this->peakValue));
* double stepWidth = (double)this->width() * ((double)this->singleStep() / (this->maximum() - this->minimum()));
* //Fusion seems to fuck around with bar's height and width
* //const qreal dpr = painter->device()->devicePixelRatio();
* //QStyleOptionSlider sliderComplex = QStyleOptionSlider(); //slider.initFrom(this);
* QRect sliderSize = style->subControlRect(QStyle::CC_Slider, (QStyleOptionComplex*)&sliderComplex, QStyle::SC_SliderHandle);
* int handleCenterPos = QStyle::sliderPositionFromValue(this->minimum(), this->maximum(), this->value(), this->width());
* int unattenuatedPeakMeter = ((this->width() * this->peakValue) >= handleCenterPos - (sliderSize.width() / 2))
* ? this->width() - (sliderSize.width() / 2)
* : this->width() * this->peakValue;
* //QApplication::style()->subControlRect(QStyle::CC_Slider, (QStyleOptionComplex*)&slider, QStyle::SC_SliderHandle);
* painter.fillRect(0, (this->height() / 2) - 3, this->width(),
* 4, Qt::white);
* painter.fillRect(0, (this->height() / 2) - 3, this->width() * this->peakValue,
* 4, Qt::gray);
* painter.fillRect(0, (this->height() / 2) - 3, (this->width() - ((this->maximum() - this->value()) * stepWidth)) * this->peakValue,
* 4, Qt::green);
* // - ((this->maximum() - this->value()) * stepWidth))
* //double ratio = ;
* double handleShift = (double)((sliderSize.width() * ((double)(this->maximum() - this->value()) / 100)));
* painter.fillRect((this->width() - ((this->maximum() - this->value()) * stepWidth)) - (sliderSize.width()) + handleShift, top / 2, sliderSize.width(), sliderSize.height() - bottom, Qt::magenta);
* //sliderComplex.subControls = QStyle::SC_SliderHandle;
* //p.drawComplexControl(QStyle::CC_Slider, sliderComplex);
*/
}
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 + 1), ary1, width, height);
break;
case SpawnPos::DOWN | SpawnPos::LEFT:
this->addToolBar(Qt::TopToolBarArea, mainMenuBar);
return QRect(arx1, (ary2 - height + 1), width, height);
break;
case SpawnPos::DOWN | SpawnPos::RIGHT:
this->addToolBar(Qt::TopToolBarArea, mainMenuBar);
return QRect((arx2 - width + 1), (ary2 - height + 1), width, height);
break;
default:
this->addToolBar(Qt::BottomToolBarArea, mainMenuBar);
log_debugcpp("Failed positioning window");
return QRect(500, 400, width, height);
break;
}
}
void MainWindow::updateColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
QColor color(r, g, b, a);
QPalette pal = QPalette(color);
pal.setColor(QPalette::Highlight, color);
scrollArea->verticalScrollBar()->setPalette(pal);
}
void MainWindow::compose(bool isVisible) {
//We need dynamically added child widgets to expand so that we know their height
this->setUpdatesEnabled(false);
uint64_t windowWidth;
uint64_t screenHeight;
uint64_t windowHeight;
/*
* 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));
windowWidth = (((uint64_t)std::abs(screenRes.width())) * widthRatio);
screenHeight = (uint64_t)std::abs(screenRes.height());
log_debugcpp("Window Width: " + std::to_string(windowWidth));
log_to_file("Window Width: %d \n", windowWidth);
this->scrollArea->setMaximumWidth((int)windowWidth);
this->scrollArea->setMinimumWidth((int)windowWidth);
this->mainMenuBar->setMinimumWidth((int)windowWidth);
this->mainMenuBar->setMaximumWidth((int)windowWidth);
this->createLayout(new QGridLayout());
for (auto *epw : ews) {
if (!epw) continue;
epw->updateChannelsVisibility();
epw->calculateSize(windowWidth - scrollArea->verticalScrollBar()->sizeHint().width()
- widgetLayout->contentsMargins().left()
, screenHeight);
log_debugcpp("epw loop");
log_debugcpp("epw roles: " + print_as_binary((epw->getEndpointHandler()->getRoles())));
//std::bitset<sizeof(uint8_t)> content =
//content);
//varToBitset<sizeof(uint8_t), uint8_t>(epw->getEndpointHandler()->getRoles());
}
/*
* Calculating window height
*/
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 * 3;
log_debugcpp("windowHeight loop: " + std::to_string(windowHeight));
}
windowHeight += mainMenuBar->sizeHint().height();
log_debugcpp("windowHeight final value: " + std::to_string(windowHeight));
this->setUpdatesEnabled(true);
if (!isVisible) {
/*
* 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);
mainLabel->setToolTip(QString::fromStdWString(sh->getName()));
mainSlider = new MeterSlider(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()));
mainLabel->setToolTip(QString::fromStdWString(sh->getName()));
sh->getVolumeInfo()->isNameChanged = false;
}
const float roundingFactor = 0.005;
mainSlider->blockSignals(true);
muteButton->blockSignals(true);
mainSlider->setValue((int)((sh->getVolumeInfo()->mainVolume + roundingFactor) * 100));
mainSlider->setPeakValue(sh->getPeakVolume());
mainSlider->update();
//log_wdebugcpp(L"Session: " + sh->getName() + L" peak value: " + std::to_wstring(sh->getPeakVolume()));
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(POLLING_RATE);
}
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("\tSlider Maximum width: %d \n", mainSlider->maximumWidth());
log_to_file("\tSpacer Minimum width: %d \n", widthSpacer->minimumSize().width());
log_to_file("\tSpacer Maximum width: %d \n\n", widthSpacer->maximumSize().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++){
MeterSlider* tmp = new MeterSlider(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);
tmp->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
//mainSlider->setFocusPolicy(Qt::StrongFocus);
tmpLb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
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);
}
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;
eph->createSessionHandlers();
//todo: sussy
this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ACTIVE, idx);
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
widgetLayout = new QGridLayout(this);
//this->setContentsMargins(0, 0, 0, 0);
//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 MeterSlider(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::Expanding);
widgetLayout->addWidget(cw, row++, 0, 1, 4 /*colmax*/, Qt::AlignTop);
cw->setVisible(false);
char* const channelSettings = set->getValue("show_channels");
if(channelSettings && !(strcmp(channelSettings, "true"))){
cw->setVisible(true);
}
}
/*
* 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] { \
defaultRolesCheckBoxes.at(role)->setChecked(!(defaultRolesCheckBoxes.at(role)->isChecked())); \
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));
mainSlider->setPeakValue(eph->getPeakVolume());
mainSlider->update();
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(POLLING_RATE);
/* 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");
eph->createSessionHandlersCallback();
}
void EndpointWidget::addSessionWidget(CustomWidgetEvent<SessionHandler*>* ev){
this->setUpdatesEnabled(false);
uint64_t index = this->sessionWidgets.size();
SessionWidget* sw = new SessionWidget(index, ev->payload, this);
ev->payload->setFrontIndex(index);
//MainWindow* mw = dynamic_cast<MainWindow*>(parent());
//TODO: change mainwindow's widget name and subclass qwidget
const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
for (QWidget *widget : topLevelWidgets) {
if (qobject_cast<MainWindow*>(widget)) {
QCoreApplication::instance()->postEvent
(widget, new QEvent((QEvent::Type)CustomQEvent::RecomposeMainWindow));
}
}
this->widgetLayout->addWidget(sw, row, 0, 1, 4);
row++;
sessionWidgets.push_back(sw);
this->setUpdatesEnabled(true);
return;
}
void EndpointWidget::removeSessionWidget(CustomWidgetEvent<SessionHandler*>* ev){
this->setUpdatesEnabled(false);
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->setUpdatesEnabled(true);
//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);
this->eph->lockSessionCollections();
this->eph->deleteSessions();
this->eph->unlockSessionCollections();
for(auto sw : sessionWidgets) {
delete sw;
}
}
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);
} else if (ev->type() == (QEvent::Type)CustomQEvent::RecomposeMainWindow) {
ev->setAccepted(true);
if (this->isVisible()) this->compose(true);
} else if (ev->type() == (QEvent::Type)CustomQEvent::EndpointRoleChange) {
ev->setAccepted(true);
this->flushRoleChanges();
}
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;
//TODO: is a flattener really necessary?
//this->ewsUpdateTimer->start();
return;
}
void MainWindow::addEndpointWidget(CustomWidgetEvent<EndpointHandler*>* ev) {
EndpointWidget* epw = new EndpointWidget(ev->payload, containerWidget, this->ews.size());
//epw->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
epw->setParent(this);
if(this->widgetLayout)
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: {WWidth: %u SHeight: %u}\n", width, height);
//this->setMaximumWidth(width);
/*
* this->mainLabel->setMaximumWidth((int)(width * 0.50) /\* 1080p 120%*\/);
* this->mainLabel->setMinimumWidth((int)(width * 0.50) /\* 1080p 120%*\/);
*/
this->mainLabel->setMaximumSize((int)(width * 0.50), height /* 1080p 120%*/);
this->mainLabel->setMinimumSize((int)(width * 0.50), 1 /* 1080p 120%*/);
this->muteButton->setMaximumSize((int)(width * 0.10), height /* 1080p 120%*/);
this->mainVolumeLabel->setMaximumSize((int)(width * 0.10), height /* 1080p 120%*/);
for (auto roleCheckbox : defaultRolesCheckBoxes) {
roleCheckbox.second->setMaximumWidth((int)(width * 0.20) /* 1080p 120%*/);
log_to_file("Role %d width: %d \n", roleCheckbox.first, roleCheckbox.second->maximumWidth());
}
log_to_file("Main label width: %d \n", this->mainLabel->maximumWidth());
log_to_file("Mute button width: %d \n", this->muteButton->maximumWidth());
log_to_file("Volume label width: %d \n", this->mainVolumeLabel->maximumWidth());
if (cw) {
this->cw->setMinimumSize(QSize(1, (height * 0.06) * (int)((cw->getChannelCount() / 2) + 0.5)));
//this->cw->setMaximumSize(QSize(width, (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);
* }
*/
void EndpointWidget::updateChannelsVisibility() {
if (!cw) return;
char* const channelSettings = set->getValue("show_channels");
if(channelSettings && !(strcmp(channelSettings, "true"))){
cw->setVisible(true);
} else cw->setVisible(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_CHANNELS;
channels = new QCheckBox(text, this);
char* const channelSettings = set->getValue("show_channels");
if(channelSettings && !(strcmp(channelSettings, "true"))){
channels->setChecked(true);
}
connect(channels, &QCheckBox::stateChanged, [this, parent](){
//TODO: Find a better way to auto no-op when there's no settings file
if(set) {
set->setValue("show_channels", channels->isChecked(), sizeof("show_channels"));
if(!OverseerHandler::settingsPath.empty()){
set->save(OverseerHandler::settingsPath.c_str());
}
}
if(parent) {
QEvent explosion = QEvent((QEvent::Type)CustomQEvent::RecomposeMainWindow);
QCoreApplication::instance()->sendEvent
(parent, &explosion);
}
});
text = "&" STRING_STARTUP;
startup = new QCheckBox(text, this);
if(osh->isToRunAtStartup()) {
startup->setChecked(true);
}
connect(startup, &QCheckBox::stateChanged, [this](){
osh->setStartupConfig(startup->isChecked());
});
widgetLayout->addWidget(openCP , 0, 0, 2, 2);
widgetLayout->addWidget(channels, 0, 2, 1, 2);
widgetLayout->addWidget(startup , 1, 2, 1, 2);
#endif
//widgetLayout->addWidget(about , 0, 2);
this->setLayout(widgetLayout);
}
void MainWindow::createLayout(QGridLayout *newLayout) {
log_debugcpp("createLayout");
widgetLayout->removeItem(lastRowSpacer);
QGridLayout *tempStore = this->widgetLayout;
this->widgetLayout = 0;
delete tempStore;
containerWidget->setLayout(newLayout);
this->widgetLayout = newLayout;
bool areEndpoints = false;
uint64_t i = 0;
for (EndpointWidget *epw : ews) {
if (!epw) continue;
else areEndpoints = true;
log_wdebugcpp(L"epw name: " + epw->getEndpointHandler()->getName());
//epw->setIndex(i);
this->widgetLayout->addWidget(epw, i++, 0);
}
if(areEndpoints) {
if (noEndpoints) { delete noEndpoints; noEndpoints = nullptr; }
widgetLayout->addItem(lastRowSpacer, i, 0);
} else {
if (!noEndpoints) noEndpoints = new QLabel(STRING_NOENDPOINT, this);
widgetLayout->addWidget(noEndpoints, i, 0);
}
}
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: transparent; ");
//setStyleSheet("background-color: rgba(255,182,193);");
setWindowTitle(STRING_TITLE);
/*
* Font setup
*/
int id = QFontDatabase::addApplicationFont(":/assets/selawk.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
font = QFont(family);
font.setKerning(true);
font.setPointSize(12);
this->setFont(font);
//qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
/*
* 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);
QEvent::registerEventType(CustomQEvent::EndpointRoleChange);
/* This spacer provides proper spacing when window vertically > widgets. */
lastRowSpacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
ewsUpdateTimer = new QTimer(this);
recentlyClosedTimer = new QTimer(this);
containerWidget = new QWidget();
//widget->setContentsMargins(0, 0, 0, 0);
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);
recentlyClosedTimer->setSingleShot(true);
recentlyClosedTimer->setInterval(recentlyClosedTimerFrequency);
connect(recentlyClosedTimer, &QTimer::timeout, this, [=]() { this->recentlyClosed = false; });
//widget->setMinimumSize(QSize(300,300));
//widget->setLayout(widgetLayout);
/*
* Scroll bar code
*/
scrollArea = new QScrollArea(this);
//widget->setAttribute(Qt::WA_TranslucentBackground);
scrollArea->setWidget(containerWidget);
scrollArea->setWidgetResizable(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setContentsMargins(QMargins(0, 0, 0, 0));
//ScrollBarFocusFilter focusFilter = new ScrollBarFocusFilter(this);
scrollArea->verticalScrollBar()->installEventFilter(this);
scrollArea->horizontalScrollBar()->installEventFilter(this);
//scrollArea->verticalScrollBar()->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
//scrollArea->verticalScrollBar()->setSingleStep(1);
setCentralWidget(scrollArea);
/*
* Menu bar code
*/
mainMenuBar = new QToolBar(this);
hw = new HeaderWidget(this);
mainMenuBar->addWidget(hw);
mainMenuBar->setMovable(false);
this->addToolBar(Qt::BottomToolBarArea, mainMenuBar);
/*
* Create initial endpoint widgets batch
*/
reloadEndpointWidgets();
/*
* 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);
connect(qApp, &QGuiApplication::applicationStateChanged, this, [=](Qt::ApplicationState state){
if(state == Qt::ApplicationState::ApplicationInactive) {
this->hide();
//This recentlyClosed... kinda campy.
this->recentlyClosed = true;
this->recentlyClosedTimer->start();
}
});
/*
* Set accent color
*/
uint8_t a, r, g, b;
if (StylingHelper::argbToDiscreteValues(osh->getAccentColor(), &r, &g, &b, &a))
this->updateColor(r, g, b, a);
/*
* Set of function callback definitons for EndpointSituationCallback
*/
osh->setRoleBucketEntryFunction([this](Roles role, std::wstring endpointId) {
std::pair<Roles, std::wstring> entry = { role, endpointId };
this->roleBucketList.push_back(entry);
QCoreApplication::instance()->postEvent(this, new QEvent((QEvent::Type)CustomQEvent::EndpointRoleChange),Qt::LowEventPriority);
});
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));
});
}
bool MainWindow::eventFilter(QObject *object, QEvent *event) {
//QScrollBar* widgetCast = qobject_cast<QScrollBar*>(object);
if (object == scrollArea->verticalScrollBar()) {
[[maybe_unused]] QEvent::Type tipo = event->type();
if (event->type() == QHoverEvent::HoverEnter) {
log_debugcpp("Hover event: ");
log_debugcpp("\tGlobal pos: "
+ std::to_string(((QHoverEvent*)event)->globalPosition().x())
+ ", "
+ std::to_string(((QHoverEvent*)event)->globalPosition().y()));
log_debugcpp("\tNewWid pos: "
+ std::to_string(((QHoverEvent*)event)->position().x())
+ ", "
+ std::to_string(((QHoverEvent*)event)->position().y()));
log_debugcpp("\tOldWid pos: "
+ std::to_string(((QHoverEvent*)event)->oldPos().x())
+ ", "
+ std::to_string(((QHoverEvent*)event)->oldPos().y()));
}
if (event->type() == QScrollEvent::ScrollFinished) {
QHoverEvent* hoverEvent = new QHoverEvent(QEvent::HoverEnter,
scrollArea->verticalScrollBar()->mapFromGlobal(QCursor::pos()),
QCursor::pos(),
scrollArea->verticalScrollBar()->mapFromGlobal(QCursor::pos()));
log_debugcpp("ScrollFinished event: ");
log_debugcpp("\tGlobal pos: "
+ std::to_string(QCursor::pos().x())
+ ", "
+ std::to_string(QCursor::pos().y()));
log_debugcpp("\tWidget pos: "
+ std::to_string(scrollArea->verticalScrollBar()->mapFromGlobal(QCursor::pos()).x())
+ ", "
+ std::to_string(scrollArea->verticalScrollBar()->mapFromGlobal(QCursor::pos()).y()));
QCoreApplication::instance()->postEvent(scrollArea->verticalScrollBar(), hoverEvent);
/*
* QCoreApplication::instance()->postEvent(this, new CustomWidgetEvent<SessionHandler*>((QEvent::Type)CustomQEvent::SessionWidgetObsolete, sessionHandler));
*/
//scrollArea->setFocus(Qt::MouseFocusReason);
return false;
}}
return false;
}
void MainWindow::flushRoleChanges() {
std::pair<Roles, std::wstring> change = roleBucketList.front();
roleBucketList.pop_front();
EndpointWidget *newDef = nullptr, *oldDef = nullptr;
for (uint64_t i = 0; i < ews.size(); i++) {
if(newDef && oldDef) break;
auto epw = this->ews.at(i);
if (!epw) continue;
if (epw->getEndpointHandler()->getId() == change.second) {
newDef = epw;
ews.at(i) = nullptr;
continue;
}
if (epw->getEndpointHandler()->getRoles() & change.first) {
oldDef = epw;
ews.at(i) = nullptr;
continue;
}
}
this->changeFrontDefaults(change.first, newDef, oldDef);
}
void MainWindow::changeFrontDefaults(Roles role, EndpointWidget* newDef, EndpointWidget* oldDef) {
//Sigh... MS's naive (non)API didn't help, but...
//Since widgets are removed previously, they must be added back.
//This produces unneeded vector size increases.
//Also, there's no freaking way this must be this illegible.
//TODO: Rewrite this method. Seriously. You'll have to get to, someday.
if (newDef && !oldDef) {
newDef->getDefaultRolesWidgets().at(role)->blockSignals(true);
uint8_t newDefCurRolesReversed = ~(newDef->getEndpointHandler()->getRoles());
if(newDefCurRolesReversed & role) {
newDef->getEndpointHandler()->assignRoles(role);
QCoreApplication::instance()->sendEvent(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;
if(newDefCurRolesReversed & role) {
newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL)->blockSignals(true);
QCoreApplication::instance()->sendEvent(newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL)->blockSignals(false);
}
} 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);
}
else if (oldDef && newDef) {
newDef->getDefaultRolesWidgets().at(role)->blockSignals(true);
newDef->getEndpointHandler()->assignRoles(role);
QCoreApplication::instance()->sendEvent(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;
newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL)->blockSignals(true);
QCoreApplication::instance()->sendEvent(newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
newDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL)->blockSignals(false);
} 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) {
oldDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL)->blockSignals(true);
QCoreApplication::instance()->sendEvent(oldDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL), new QEvent((QEvent::Type)CustomQEvent::EndpointDefaultChange));
oldDef->getDefaultRolesWidgets().at(Roles::ROLE_ALL)->blockSignals(false);
}
oldDef->getEndpointHandler()->removeRoles(role);
QCoreApplication::instance()->sendEvent(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);
}
QCoreApplication::instance()->sendEvent
(this, new QEvent((QEvent::Type)CustomQEvent::RecomposeMainWindow));
}
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:
if (!this->isVisible() && !recentlyClosed) {
log_to_file("Recently Closed: %d \n", recentlyClosed);
this->compose(false);
this->showNormal();
this->activateWindow();
}
break;
default:
break;
}
}
void MainWindow::reloadEndpointWidgets() {
size_t i = 0;
ews.resize(2);
osh->handlersPlaybackMutex.lock();
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), containerWidget);
log_wdebugcpp(L"epw name: " + epw->getEndpointHandler()->getName());
if ((epw->getEndpointHandler()->getRoles() == Roles::ROLE_ALL) ||
(epw->getEndpointHandler()->getRoles() & Roles::ROLE_MULTIMEDIA))
{ ews[0] = epw; epw->setIndex(0); }
else if (epw->getEndpointHandler()->getRoles() & Roles::ROLE_COMMUNICATIONS)
{ ews[1] = epw; epw->setIndex(1); }
else
{ ews.push_back(epw); epw->setIndex(ews.size() - 1); }
}
}
osh->handlersPlaybackMutex.unlock();
//todo: ya no est<73> aqu<71> tirao como tal, y de hecho, no ha fallado de la manera arriba descrita
//pero ahora lo a<>ado porque s<> solo para garantizar que est<73> y poder reusarlo luego lmao
widgetLayout->addItem(lastRowSpacer, i, 0);
}