#include "mainwindow.h"
#include "slxoutput.h"
#include <PulseAudioQt/Sink>
#include <PulseAudioQt/Port>
#include <PulseAudioQt/Card>
#include <PulseAudioQt/Profile>
MainWindow::MainWindow() : QDialog()
{
setupUi(this);
setWindowFlags(Qt::WindowStaysOnTopHint | windowFlags());
// Make heading 20% larger than system default
QFont f = inputLabel->font();
f.setPointSize(f.pointSize() * 6 / 5);
inputLabel->setFont(f);
outputLabel->setFont(f);
cardLabel->setFont(f);
_originalSize = this->size();
}
MainWindow::~MainWindow()
{
}
SlxOutput* MainWindow::getDevice(const PulseAudioQt::Card *card, const PulseAudioQt::Device *device, const PulseAudioQt::Port *port, bool output)
{
PulseAudioQt::Profile *profile = nullptr;
QString cardId;
QString id = device->name() + port->name();
SlxOutput* w = _widgets.value(id, nullptr);
QString heading = port->description();
if (card != nullptr) {
cardId = card->name();
if (card->activeProfileIndex() < card->profiles().size()) {
profile = card->profiles().at(card->activeProfileIndex());
heading.append(QLatin1String(" (")).append(profile->description()).append(QLatin1String(")"));
}
}
if (w != nullptr) {
w->headingLabel->setText(heading);
return w;
}
w = new SlxOutput(output ? SlxOutput::SinkPortItem : SlxOutput::SourcePortItem, id, cardId, QLatin1String(), device->name(), port->name(),
heading, device->description());
_widgets.insert(id, w);
insertItemWidget(w, true);
return w;
}
SlxOutput* MainWindow::getCardProfile(const PulseAudioQt::Card *card, const PulseAudioQt::Profile *profile)
{
QString id = card->name() + profile->name();
SlxOutput* w = _widgets.value(id, nullptr);
if (w != nullptr)
return w;
w = new SlxOutput(SlxOutput::CardProfileItem, id, card->name(), profile->name(), QLatin1String(), QLatin1String(),
profile->description(), card->description());
_widgets.insert(id, w);
insertItemWidget(w, false);
return w;
}
SlxOutput* MainWindow::getCardPort(const PulseAudioQt::Card *card, const PulseAudioQt::Port *port)
{
QString id = card->name() + port->name();
SlxOutput* w = _widgets.value(id, nullptr);
if (w != nullptr)
return w;
w = new SlxOutput(SlxOutput::CardProfileItem, id, card->name(), port->name(), QLatin1String(), QLatin1String(),
port->description(), card->description());
_widgets.insert(id, w);
insertItemWidget(w, false);
return w;
}
void MainWindow::insertItemWidget(SlxOutput* w, bool isDevice)
{
bool done = false;
int idx;
QBoxLayout *container;
if (w->type() == SlxOutput::CardProfileItem) {
container = cards;
} else if (w->type() == SlxOutput::SinkPortItem) {
container = outputListLayout;
} else {
container = inputListLayout;
}
for (idx = 0; idx < container->count(); ++idx) {
auto *l = container->itemAt(idx);
SlxOutput *other = qobject_cast<SlxOutput*>(l->widget());
if (other == nullptr)
continue;
printf(" Comparing %s - %s\n", other->isDevice() ? "SINK" : "PROF", other->nameLabel->text().toLocal8Bit().constData());
if (w->compareTo(other) < 0) {
done = true;
printf("Inserting before that (%d)!\n", idx);
container->insertWidget(idx, w);
break;
}
}
if (!done) {
if (idx == -1) {
idx = 0;
}
printf("Inserting before that (%d) 2!\n", idx);
container->insertWidget(idx, w);
}
}
/**
* Mark all entries as unused.
*/
void MainWindow::mark()
{
setUpdatesEnabled(false);
this->resize(_originalSize);
outputLabel->setMinimumWidth(0);
inputLabel->setMinimumWidth(0);
for (auto *w : _widgets) {
w->unused = true;
}
}
/**
* Remove all entries marked as unused.
*/
void MainWindow::sweep()
{
for (QMutableMapIterator<QString, SlxOutput*> it(_widgets); it.hasNext(); ) {
it.next();
auto *w = it.value();
if (w->unused) {
printf("Killing %p\n", w);
it.remove();
w->deleteLater();
}
}
setUpdatesEnabled(true);
int width = qMax(outputLabel->width(), inputLabel->width());
this->resize(_originalSize);
outputLabel->setMinimumWidth(width);
inputLabel->setMinimumWidth(width);
}