Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat profiles #5

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 140 additions & 1 deletion src/mumble/ConfigDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QScrollArea>
#include <QtCore/QMutexLocker>
#include <QtGui/QScreen>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QPushButton>

Expand Down Expand Up @@ -84,6 +85,7 @@ ConfigDialog::ConfigDialog(QWidget *p) : QDialog(p) {
restoreGeometry(Global::get().s.qbaConfigGeometry);
}

updateProfileList();
updateTabOrder();
qlwIcons->setFocus();
}
Expand Down Expand Up @@ -205,6 +207,137 @@ void ConfigDialog::on_qlwIcons_currentItemChanged(QListWidgetItem *current, QLis
}
}

void ConfigDialog::updateProfileList() {
// Prevent changing the profile unintentionally while filling the ComboBox
const QSignalBlocker blocker(qcbProfiles);

qcbProfiles->clear();

// Always sort the default profile before anything else
qcbProfiles->addItem(Profiles::s_default_profile_name);

QStringList profiles = Global::get().profiles.allProfiles.keys();
profiles.sort();
for (const QString &profile : profiles) {
if (profile == Profiles::s_default_profile_name) {
continue;
}
qcbProfiles->addItem(profile);
}

qcbProfiles->setCurrentIndex(qcbProfiles->findText(Global::get().profiles.activeProfileName));

bool isDefault = qcbProfiles->currentText() == Profiles::s_default_profile_name;
qpbProfileRename->setEnabled(!isDefault);
qpbProfileDelete->setEnabled(!isDefault);
}

void ConfigDialog::switchProfile(const QString &newProfile, bool saveActiveProfile) {
Profiles &profiles = Global::get().profiles;

if (saveActiveProfile) {
profiles.allProfiles[profiles.activeProfileName] = Global::get().s;
}
Global::get().s.loadProfile(newProfile);
s = Global::get().s;
for (ConfigWidget *cw : s_existingWidgets.values()) {
cw->load(s);
}

updateProfileList();
}

void ConfigDialog::on_qcbProfiles_currentIndexChanged(int) {
QString selectedProfile = qcbProfiles->currentText();

Profiles &profiles = Global::get().profiles;

if (selectedProfile == profiles.activeProfileName) {
return;
}

if (!profiles.allProfiles.contains(selectedProfile)) {
return;
}

switchProfile(selectedProfile, true);
}

void ConfigDialog::on_qpbProfileAdd_clicked() {
bool ok;
QString profileName =
QInputDialog::getText(this, tr("Creating settings profile"), tr("Enter new settings profile name"),
QLineEdit::Normal, Global::get().profiles.activeProfileName, &ok);

if (!ok || profileName.isEmpty()) {
return;
}

if (Global::get().profiles.allProfiles.contains(profileName)) {
QMessageBox::critical(this, tr("Creating settings profile"),
tr("A settings profile with this name already exists"));
return;
}

// Instead of "resetting" when creating a new profile, use the currently
// (possibly not applied) settings for the new profile
Profiles &profiles = Global::get().profiles;
profiles.allProfiles[profiles.activeProfileName] = Global::get().s;
apply();
profiles.allProfiles.insert(profileName, Global::get().s);
switchProfile(profileName, false);
}

void ConfigDialog::on_qpbProfileRename_clicked() {
QString oldProfileName = qcbProfiles->currentText();

if (oldProfileName == Profiles::s_default_profile_name) {
return;
}

bool ok;
QString profileName =
QInputDialog::getText(this, tr("Renaming settings profile"), tr("Enter new settings profile name"),
QLineEdit::Normal, oldProfileName, &ok);

if (!ok || profileName.isEmpty()) {
return;
}

if (Global::get().profiles.allProfiles.contains(profileName)) {
QMessageBox::critical(this, tr("Renaming settings profile"),
tr("A settings profile with this name already exists"));
return;
}

Global::get().profiles.allProfiles.insert(profileName, Global::get().s);
Global::get().profiles.allProfiles.remove(oldProfileName);
switchProfile(profileName, false);
}

void ConfigDialog::on_qpbProfileDelete_clicked() {
QString oldProfileName = qcbProfiles->currentText();

if (oldProfileName == Profiles::s_default_profile_name) {
return;
}

if (!Global::get().profiles.allProfiles.contains(oldProfileName)) {
return;
}

QMessageBox::StandardButton confirmation = QMessageBox::question(
this, tr("Delete settings profile"),
tr("Are you sure you want to permanently delete settings profile '%1'").arg(oldProfileName));

if (confirmation != QMessageBox::Yes) {
return;
}

Global::get().profiles.allProfiles.remove(oldProfileName);
switchProfile(Profiles::s_default_profile_name, false);
}

void ConfigDialog::updateTabOrder() {
QPushButton *okButton = dialogButtonBox->button(QDialogButtonBox::Ok);
QPushButton *cancelButton = dialogButtonBox->button(QDialogButtonBox::Cancel);
Expand All @@ -228,7 +361,13 @@ void ConfigDialog::updateTabOrder() {
}

setTabOrder(cancelButton, okButton);
setTabOrder(okButton, qlwIcons);
setTabOrder(okButton, qcbProfiles);

setTabOrder(qcbProfiles, qpbProfileAdd);
setTabOrder(qpbProfileAdd, qpbProfileRename);
setTabOrder(qpbProfileRename, qpbProfileDelete);
setTabOrder(qpbProfileDelete, qlwIcons);

setTabOrder(qlwIcons, contentFocusWidget);
if (resetButton && restoreButton && restoreAllButton) {
setTabOrder(contentFocusWidget, resetButton);
Expand Down
6 changes: 6 additions & 0 deletions src/mumble/ConfigDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ConfigDialog : public QDialog, public Ui::ConfigDialog {
Q_DISABLE_COPY(ConfigDialog)

void updateTabOrder();
void updateProfileList();
void switchProfile(const QString &newProfile, bool saveActiveProfile);

protected:
static QMutex s_existingWidgetsMutex;
Expand Down Expand Up @@ -46,6 +48,10 @@ public slots:
void on_pageButtonBox_clicked(QAbstractButton *);
void on_dialogButtonBox_clicked(QAbstractButton *);
void on_qlwIcons_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
void on_qcbProfiles_currentIndexChanged(int);
void on_qpbProfileAdd_clicked();
void on_qpbProfileRename_clicked();
void on_qpbProfileDelete_clicked();
void apply();
void accept() Q_DECL_OVERRIDE;
};
Expand Down
67 changes: 56 additions & 11 deletions src/mumble/ConfigDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,96 @@
</size>
</property>
<property name="resizeMode">
<enum>QListView::Adjust</enum>
<enum>QListView::ResizeMode::Adjust</enum>
</property>
<property name="layoutMode">
<enum>QListView::Batched</enum>
<enum>QListView::LayoutMode::Batched</enum>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<item row="1" column="0">
<widget class="QGroupBox" name="qgbProfiles">
<property name="title">
<string>Profile</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QComboBox" name="qcbProfiles"/>
</item>
<item>
<widget class="QPushButton" name="qpbProfileAdd">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="qpbProfileRename">
<property name="text">
<string>Rename</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="qpbProfileDelete">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QDialogButtonBox" name="pageButtonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
</widget>
</item>
<item>
<spacer>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>474</width>
<height>22</height>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="dialogButtonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::StandardButton::Apply|QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<item row="0" column="1" rowspan="2" colspan="2">
<widget class="QStackedWidget" name="qswPages">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
Expand Down
1 change: 1 addition & 0 deletions src/mumble/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct Global Q_DECL_FINAL {

MainWindow *mw;
Settings s;
Profiles profiles;
boost::shared_ptr< ServerHandler > sh;
boost::shared_ptr< AudioInput > ai;
boost::shared_ptr< AudioOutput > ao;
Expand Down
15 changes: 15 additions & 0 deletions src/mumble/JSONSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ void from_json(const nlohmann::json &j, OverlaySettings &settings) {
}


void to_json(nlohmann::json &j, const Profiles &settings) {
#define PROCESS(category, key, variable) save(j, SettingsKeys::key, settings.variable);

PROCESS_ALL_PROFILE_SETTINGS

#undef PROCESS
}

void from_json(const nlohmann::json &j, Profiles &settings) {
#define PROCESS(category, key, variable) load(j, SettingsKeys::key, settings.variable, settings.variable, true);

PROCESS_ALL_PROFILE_SETTINGS

#undef PROCESS
}

void to_json(nlohmann::json &j, const QString &string) {
j = string.toStdString();
Expand Down
2 changes: 2 additions & 0 deletions src/mumble/JSONSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void to_json(nlohmann::json &j, const Settings &settings);
void from_json(const nlohmann::json &j, Settings &settings);
void to_json(nlohmann::json &j, const OverlaySettings &settings);
void from_json(const nlohmann::json &j, OverlaySettings &settings);
void to_json(nlohmann::json &j, const Profiles &settings);
void from_json(const nlohmann::json &j, Profiles &settings);

void to_json(nlohmann::json &j, const QString &string);
void from_json(const nlohmann::json &j, QString &string);
Expand Down
Loading
Loading