Skip to content

Commit 9253249

Browse files
author
ThePBone
committed
Add devices.conf generator to settings dialog
1 parent 1b4aa97 commit 9253249

File tree

5 files changed

+217
-10
lines changed

5 files changed

+217
-10
lines changed

config/io.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
QString ConfigIO::writeString(QVariantMap map){
99
QString ret("");
1010
for(auto e : map.keys())
11-
ret += QString("%1=%2\n").arg(e).arg(map.value(e).toString());
11+
ret += QString("%1=%2\n").arg(e).arg(map.value(e).toString());
1212
return ret;
1313
}
1414
void ConfigIO::writeFile(QString path,QVariantMap map,QString prefix){
1515
std::ofstream myfile(path.toUtf8().constData());
1616
if (myfile.is_open())
1717
{
18-
myfile << prefix.toUtf8().constData() << std::endl;
18+
if(!prefix.isEmpty())
19+
myfile << prefix.toUtf8().constData() << std::endl;
1920
for(auto e : map.keys())
20-
myfile << e.toStdString() << "=" << map.value(e).toString().toStdString() << std::endl;
21+
myfile << e.toStdString() << "=" << map.value(e).toString().toStdString() << std::endl;
2122
myfile.close();
2223
}
2324
}

dialog/settings.ui

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
<string>Convolver</string>
7878
</property>
7979
</item>
80+
<item>
81+
<property name="text">
82+
<string>Devices</string>
83+
</property>
84+
</item>
8085
<item>
8186
<property name="text">
8287
<string>Spectrum Analyser</string>
@@ -97,7 +102,7 @@
97102
<item>
98103
<widget class="QStackedWidget" name="stackedWidget">
99104
<property name="currentIndex">
100-
<number>2</number>
105+
<number>4</number>
101106
</property>
102107
<widget class="QWidget" name="general">
103108
<layout class="QVBoxLayout" name="verticalLayout_2">
@@ -686,6 +691,109 @@
686691
</item>
687692
</layout>
688693
</widget>
694+
<widget class="QWidget" name="devices">
695+
<layout class="QVBoxLayout" name="verticalLayout_16">
696+
<item>
697+
<widget class="QGroupBox" name="groupBox_7">
698+
<property name="title">
699+
<string>Select sink/device to be processed</string>
700+
</property>
701+
<layout class="QVBoxLayout" name="verticalLayout_17">
702+
<item>
703+
<widget class="QRadioButton" name="dev_mode_auto">
704+
<property name="text">
705+
<string>Automatically detect device</string>
706+
</property>
707+
</widget>
708+
</item>
709+
<item>
710+
<widget class="QRadioButton" name="dev_mode_manual">
711+
<property name="text">
712+
<string>Manually select device</string>
713+
</property>
714+
</widget>
715+
</item>
716+
<item>
717+
<layout class="QHBoxLayout" name="horizontalLayout_9">
718+
<property name="topMargin">
719+
<number>0</number>
720+
</property>
721+
<item>
722+
<widget class="QLabel" name="label_12">
723+
<property name="text">
724+
<string>Device</string>
725+
</property>
726+
</widget>
727+
</item>
728+
<item>
729+
<widget class="QComboBox" name="dev_select">
730+
<property name="minimumSize">
731+
<size>
732+
<width>0</width>
733+
<height>0</height>
734+
</size>
735+
</property>
736+
<property name="maximumSize">
737+
<size>
738+
<width>230</width>
739+
<height>16777215</height>
740+
</size>
741+
</property>
742+
</widget>
743+
</item>
744+
</layout>
745+
</item>
746+
</layout>
747+
</widget>
748+
</item>
749+
<item>
750+
<layout class="QHBoxLayout" name="horizontalLayout_8">
751+
<property name="topMargin">
752+
<number>0</number>
753+
</property>
754+
<item>
755+
<spacer name="horizontalSpacer_3">
756+
<property name="orientation">
757+
<enum>Qt::Horizontal</enum>
758+
</property>
759+
<property name="sizeHint" stdset="0">
760+
<size>
761+
<width>40</width>
762+
<height>20</height>
763+
</size>
764+
</property>
765+
</spacer>
766+
</item>
767+
<item>
768+
<widget class="QPushButton" name="dev_reload_viper">
769+
<property name="minimumSize">
770+
<size>
771+
<width>100</width>
772+
<height>0</height>
773+
</size>
774+
</property>
775+
<property name="text">
776+
<string>Reload viper</string>
777+
</property>
778+
</widget>
779+
</item>
780+
</layout>
781+
</item>
782+
<item>
783+
<spacer name="verticalSpacer_8">
784+
<property name="orientation">
785+
<enum>Qt::Vertical</enum>
786+
</property>
787+
<property name="sizeHint" stdset="0">
788+
<size>
789+
<width>20</width>
790+
<height>40</height>
791+
</size>
792+
</property>
793+
</spacer>
794+
</item>
795+
</layout>
796+
</widget>
689797
<widget class="QWidget" name="spectrum">
690798
<layout class="QVBoxLayout" name="verticalLayout_9">
691799
<item>

dialog/settingsdlg.cpp

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "palettedlg.h"
66
#include "misc/autostartmanager.h"
77

8+
#include <QProcess>
89
#include <QCloseEvent>
910
#include <QDesktopServices>
1011
#include <QUrl>
@@ -15,6 +16,7 @@
1516

1617
using namespace std;
1718
static bool lockslot = false;
19+
1820
SettingsDlg::SettingsDlg(MainWindow* mainwin,QWidget *parent) :
1921
QDialog(parent),
2022
ui(new Ui::settings){
@@ -60,11 +62,11 @@ SettingsDlg::SettingsDlg(MainWindow* mainwin,QWidget *parent) :
6062
connect(ui->selector,static_cast<void (QTreeWidget::*)(QTreeWidgetItem*,QTreeWidgetItem*)>(&QTreeWidget::currentItemChanged),this,[this](QTreeWidgetItem* cur, QTreeWidgetItem* prev){
6163
int toplevel_index = ui->selector->indexOfTopLevelItem(cur);
6264
switch(toplevel_index){
63-
case -1:
65+
case -1:
6466
if(cur->text(0) == "Design")
65-
ui->stackedWidget->setCurrentIndex(5);
66-
if(cur->text(0) == "Advanced")
6767
ui->stackedWidget->setCurrentIndex(6);
68+
if(cur->text(0) == "Advanced")
69+
ui->stackedWidget->setCurrentIndex(7);
6870
break;
6971
default:
7072
ui->stackedWidget->setCurrentIndex(toplevel_index);
@@ -196,6 +198,8 @@ SettingsDlg::SettingsDlg(MainWindow* mainwin,QWidget *parent) :
196198
appconf->setEqualizerPermanentHandles(ui->eq_alwaysdrawhandles->isChecked());
197199
});
198200

201+
refreshDevices();
202+
199203
int bands = appconf->getSpectrumBands();
200204
int minfreq = appconf->getSpectrumMinFreq();
201205
int maxfreq = appconf->getSpectrumMaxFreq();
@@ -271,6 +275,28 @@ SettingsDlg::SettingsDlg(MainWindow* mainwin,QWidget *parent) :
271275
appconf->setSpectrumInput(str);
272276
});
273277

278+
auto deviceUpdated = [this](){
279+
if(lockslot) return;
280+
QString absolute =
281+
QFileInfo(appconf->getPath()).absoluteDir().absolutePath();
282+
QString devices(pathAppend(absolute,"devices.conf"));
283+
if(ui->dev_mode_auto->isChecked()){
284+
QFile(devices).remove();
285+
}else{
286+
if(ui->dev_select->currentData() == "---")
287+
return;
288+
289+
ConfigContainer* devconf = new ConfigContainer();
290+
devconf->setConfigMap(ConfigIO::readFile(devices));
291+
devconf->setValue("location",ui->dev_select->currentData());
292+
ConfigIO::writeFile(devices,devconf->getConfigMap());
293+
}
294+
};
295+
296+
connect(ui->dev_reload_viper,&QPushButton::clicked,mainwin,&MainWindow::Restart);
297+
connect(ui->dev_mode_auto,&QRadioButton::clicked,this,deviceUpdated);
298+
connect(ui->dev_mode_manual,&QRadioButton::clicked,this,deviceUpdated);
299+
connect(ui->dev_select,static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this, deviceUpdated);
274300

275301
#ifndef QT_NO_SYSTEMTRAYICON
276302
ui->systray_unsupported->hide();
@@ -288,6 +314,65 @@ SettingsDlg::SettingsDlg(MainWindow* mainwin,QWidget *parent) :
288314
SettingsDlg::~SettingsDlg(){
289315
delete ui;
290316
}
317+
void SettingsDlg::refreshDevices()
318+
{
319+
lockslot = true;
320+
ui->dev_select->clear();
321+
QString absolute =
322+
QFileInfo(appconf->getPath()).absoluteDir().absolutePath();
323+
QFile devices(pathAppend(absolute,"devices.conf"));
324+
bool devmode_auto = !devices.exists();
325+
ui->dev_mode_auto->setChecked(devmode_auto);
326+
ui->dev_mode_manual->setChecked(!devmode_auto);
327+
328+
QProcess process;
329+
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
330+
env.insert("LC_ALL", "C");
331+
process.setProcessEnvironment(env);
332+
process.start("sh", QStringList()<<"-c"<<"pactl list sinks | grep \'Name: \' -A1");
333+
process.waitForFinished(500);
334+
335+
ConfigContainer* devconf = new ConfigContainer();
336+
devconf->setConfigMap(ConfigIO::readFile(pathAppend(absolute,"devices.conf")));
337+
QString out = process.readAllStandardOutput();
338+
ui->dev_select->addItem("...","---");
339+
for(auto item : out.split("Name:")){
340+
item.prepend("Name:");
341+
QRegularExpression re("(?<=(Name:)\\s)(?<name>.+)[\\s\\S]+(?<=(Description:)\\s)(?<desc>.+)");
342+
QRegularExpressionMatch match = re.match(item, 0, QRegularExpression::PartialPreferCompleteMatch);
343+
if(match.hasMatch()){
344+
ui->dev_select->addItem(QString("%1 (%2)").arg(match.captured("desc")).arg(match.captured("name")),
345+
match.captured("name"));
346+
}
347+
}
348+
QString dev_location = devconf->getString("location");
349+
if(dev_location.isEmpty())
350+
ui->dev_select->setCurrentIndex(0);
351+
else{
352+
bool notFound = true;
353+
for(int i = 0; i < ui->dev_select->count(); i++){
354+
if(ui->dev_select->itemData(i) ==
355+
dev_location){
356+
notFound = false;
357+
ui->dev_select->setCurrentIndex(i);
358+
break;
359+
}
360+
}
361+
if(notFound){
362+
QString name = QString("Unknown (%1)").arg(dev_location);
363+
ui->dev_select->addItem(name,dev_location);
364+
ui->dev_select->setCurrentText(name);
365+
}
366+
}
367+
lockslot = false;
368+
}
369+
370+
void SettingsDlg::setVisible(bool visible)
371+
{
372+
refreshDevices();
373+
QWidget::setVisible(visible);
374+
}
375+
291376
void SettingsDlg::updateInputSinks(){
292377
lockslot = true;
293378
ui->sa_input->clear();

dialog/settingsdlg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class SettingsDlg : public QDialog
3030
Ui::settings *ui;
3131
~SettingsDlg();
3232
void updateInputSinks();
33+
void refreshDevices();
34+
void setVisible(bool visible) override;
3335
private slots:
3436
void openPalConfig();
3537
void github();

mainwindow.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,11 +1050,22 @@ QVariantMap MainWindow::readConfig(){
10501050
QVariantMap confmap = ConfigIO::readFile(m_appwrapper->getPath());
10511051
if(confmap.count() < 1){
10521052
OverlayMsgProxy *msg = new OverlayMsgProxy(this);
1053-
msg->openError(tr("Viper not properly installed"),
1054-
tr("Unable to find a configuration file for viper,\n"
1055-
"please make sure that viper has been installed correctly.\n"
1053+
connect(msg,&OverlayMsgProxy::buttonPressed,[this]{
1054+
std::filebuf fb;
1055+
fb.open (m_appwrapper->getPath().toUtf8().constData(),std::ios::out);
1056+
std::ostream os(&fb);
1057+
os << default_config;
1058+
fb.close();
1059+
conf->setConfigMap(readConfig());
1060+
LoadConfig();
1061+
m_irsNeedUpdate = true;
1062+
ApplyConfig();
1063+
});
1064+
msg->openError(tr("Configuration file missing"),
1065+
tr("Please make sure that viper has been installed correctly.\n"
10561066
"If you're sure that your setup is correct, no further actions\n"
10571067
"are required. This GUI will automatically generate a configuration."));
1068+
10581069
}
10591070
return confmap;
10601071
}

0 commit comments

Comments
 (0)