diff options
Diffstat (limited to 'src/cardwidget.cc')
-rw-r--r-- | src/cardwidget.cc | 137 |
1 files changed, 112 insertions, 25 deletions
diff --git a/src/cardwidget.cc b/src/cardwidget.cc index d5b4e63..3b4c8b3 100644 --- a/src/cardwidget.cc +++ b/src/cardwidget.cc @@ -24,46 +24,59 @@ #include "cardwidget.h" +#include <QDebug> +#include <QHash> +#include <QSet> + /*** CardWidget ***/ CardWidget::CardWidget(QWidget* parent) : QWidget(parent) { setupUi(this); connect(profileList, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &CardWidget::onProfileChange); - connect(profileCB, &QAbstractButton::toggled, this, &CardWidget::onProfileCheck); + connect(profileFlavorList, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &CardWidget::onProfileFlavorChange); } void CardWidget::prepareMenu() { int idx = 0; - const bool off = activeProfile == noInOutProfile; profileList->clear(); /* Fill the ComboBox */ - for (const auto & profile : profiles) { - QByteArray name = profile.first; - // skip the "off" profile - if (name == noInOutProfile) - continue; - QString desc = QString::fromUtf8(profile.second); - profileList->addItem(desc, name); - if (profile.first == activeProfile - || (off && profile.first == lastActiveProfile) - ) - { + for (auto k : profiles.keys()) { + auto & profile = profiles[k]; + auto desc = profile.getProfileName(); + profileList->addItem(desc, k); + if (profile.containsProfile(activeProfile)) { profileList->setCurrentIndex(idx); - lastActiveProfile = profile.first; + changeProfile(k); } ++idx; } - - profileCB->setChecked(!off); } -void CardWidget::changeProfile(const QByteArray & name) +void CardWidget::changeProfile(const QString & name) { + auto &profile = profiles[name]; + auto old = profileFlavorList->currentText(); + profileFlavorList->clear(); + int idx = 0; + for (auto & entry : profile.entries) { + auto name = entry.getName(); + profileFlavorList->addItem(name, entry.id); + if (name == old || entry.id == activeProfile) { + profileFlavorList->setCurrentIndex(idx); + } + ++idx; + } + if (profileFlavorList->currentIndex() == -1) { + profileFlavorList->setCurrentIndex(0); + } +} + +void CardWidget::changeProfileFlavor(const QByteArray & id) { pa_operation* o; - if (!(o = pa_context_set_card_profile_by_index(get_context(), index, name.constData(), nullptr, nullptr))) { + if (!(o = pa_context_set_card_profile_by_index(get_context(), index, id.constData(), nullptr, nullptr))) { show_error(tr("pa_context_set_card_profile_by_index() failed").toUtf8().constData()); return; } @@ -76,17 +89,91 @@ void CardWidget::onProfileChange(int active) { return; if (active != -1) - changeProfile(profileList->itemData(active).toByteArray()); + changeProfile(profileList->itemData(active).toString()); } -void CardWidget::onProfileCheck(bool on) -{ +void CardWidget::onProfileFlavorChange(int active) { if (updating) return; - if (on) - onProfileChange(profileList->currentIndex()); - else - changeProfile(noInOutProfile); + if (active != -1) + changeProfileFlavor(profileFlavorList->itemData(active).toByteArray()); +} + +QString ProfileGroup::getProfileName() { + if (!name.isEmpty()) + return name; + // TODO + QHash<QString, int> counts; + qDebug() << "Entries" << this; + for (auto n : entries) { + qDebug() << n.tokens; + QSet<QString> tmp = n.tokens.toSet(); //(n.tokens.begin(), n.tokens.end()); + for (auto t : tmp) { + counts[t]++; + } + } + qDebug() << counts; + QSet<QString> todo = counts.keys().toSet(); + for (auto &pe : entries) { + QMutableListIterator<QString> it(pe.tokens); + while (it.hasNext()) { + auto s = it.next(); + if (counts[s] == entries.size()) { + it.remove(); + if (todo.remove(s)) { + if (!name.isEmpty()) { + name.append(QLatin1Char(' ')); + } + name.append(s); + } + } + } + } + return name; +} + +void ProfileGroup::addEntry(const char *id, const char *name) { + auto *pe = new ProfileEntry; + bool paren = false; + const char *pos = name, *tokenStart = name; + qDebug() << name << "is" << id; + while (*pos != '\0') { + if (!paren && isspace(*pos)) { + if (pos > tokenStart) { + qDebug() << tokenStart; + pe->tokens.append(QString::fromUtf8(tokenStart, pos - tokenStart)); + } + tokenStart = pos + 1; + } + if (paren && *pos == ')') { + qDebug() << tokenStart; + pe->tokens.append(QString::fromUtf8(tokenStart, pos - tokenStart + 1)); + paren = false; + tokenStart = pos + 1; + } + if (tokenStart == pos && *pos == '(') { + paren = true; + } + pos++; + } + if (pos > tokenStart) { + qDebug() << tokenStart; + pe->tokens.append(QString::fromUtf8(tokenStart, pos - tokenStart)); + } + pe->id = id; + this->entries.append(*pe); + delete pe; +} + +QString ProfileEntry::getName() const { + return tokens.join(QLatin1String(" ")); +} +bool ProfileGroup::containsProfile(const QByteArray &pro) const { + for (const auto &e : this->entries) { + if (e.id == pro) + return true; + } + return false; } |