Skip to content

Commit

Permalink
yWIP Improve UX in controller mapping editing workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Nov 11, 2020
1 parent 67bb2c6 commit ab52a30
Show file tree
Hide file tree
Showing 5 changed files with 1,445 additions and 24 deletions.
112 changes: 88 additions & 24 deletions src/controllers/dlgprefcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QDesktopServices>
#include <QFileDialog>
#include <QFileInfo>
#include <QInputDialog>
#include <QStandardPaths>
#include <QTableWidget>
#include <QTableWidgetItem>
Expand All @@ -23,11 +24,13 @@
#include "preferences/usersettings.h"
#include "util/version.h"

DlgPrefController::DlgPrefController(QWidget* parent, Controller* controller,
ControllerManager* controllerManager,
UserSettingsPointer pConfig)
DlgPrefController::DlgPrefController(QWidget* parent,
Controller* controller,
ControllerManager* controllerManager,
UserSettingsPointer pConfig)
: DlgPreferencePage(parent),
m_pConfig(pConfig),
m_pUserDir(userPresetsPath(pConfig)),
m_pControllerManager(controllerManager),
m_pController(controller),
m_pDlgControllerLearning(NULL),
Expand Down Expand Up @@ -488,36 +491,97 @@ void DlgPrefController::savePreset() {
}

if (!m_pPreset->isDirty()) {
qDebug() << "Preset is not dirty, no need to save it.";
qDebug() << "Preset has not been edited, no need to save it.";
return;
}

QFileInfo fileInfo(m_pPreset->filePath());
QString filePath = m_pPreset->filePath();
QFileInfo fileInfo(filePath);
QString fileName = fileInfo.fileName();

// Add " (edited)" to preset name (if it's not already present)
QString editedSuffix = QStringLiteral(" (") + tr("edited") + QStringLiteral(")");
if (!m_pPreset->name().endsWith(editedSuffix)) {
m_pPreset->setName(m_pPreset->name() + editedSuffix);
qDebug() << "Renamed preset to " << m_pPreset->name();

// Add " (edited)" to file name (if it's not already present)
QString baseName = fileInfo.baseName();
if (baseName.endsWith(editedSuffix)) {
baseName.chop(editedSuffix.size());
QString baseName = fileInfo.baseName();
bool isUserPreset = fileInfo.absoluteDir() == m_pUserDir;

bool saveAsNewPreset = true;
// If this is a user preset, ask whether to overwrite or save with new name.
// This aims to ease the workflow of continouosly editing a mapping because
// presents one dialog instead of file dialog + optional file replace dialog).
// TODO(ronso0) read bool alwaysOverwrite from m_pOverwritePresets
if (isUserPreset) {
QMessageBox overwriteDialog(QMessageBox::Question,
tr("Preset already exists."),
tr("Overwrite <b>%1</b> or save with a new name?").arg(fileName),
QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes);
overwriteDialog.setDefaultButton(QMessageBox::No);
overwriteDialog.setButtonText(QMessageBox::Yes, tr("Overwrite"));
overwriteDialog.setButtonText(QMessageBox::No, tr("Save as new"));
QCheckBox alwaysOverwriteCheckBox(
QObject::tr("Always overwrite this mapping during this Mixxx session"),
&overwriteDialog);
overwriteDialog.addButton(&alwaysOverwriteCheckBox, QMessageBox::ActionRole);

switch (overwriteDialog.exec()) {
case QMessageBox::Yes:
saveAsNewPreset = false;
// if (alwaysOverwriteCheckBox.checkState() == Qt::Checked) {
// m_pOverwritePresets.insert(m_pPreset, true);
// }
case QMessageBox::Cancel:
// Quit dialog, preset changes still pending.
return;
}
fileName = baseName + editedSuffix + QStringLiteral(".") + fileInfo.completeSuffix();
}
QString filePath = QDir(userPresetsPath(m_pConfig)).absoluteFilePath(fileName);

if (!m_pPreset->savePreset(filePath)) {
qDebug() << "Failed to save preset!";
}
// Initially save a modified Mixxx preset to the user folder or save a user mapping
// with a new name.
QString newFilePath;

if (saveAsNewPreset) {
QString newBaseName;
bool validNameGiven = false;

while (!validNameGiven) {
// TODO QFileDialog
// * remember file base name for preset display name dialog
newFilePath = QFileDialog::getSaveFileName(
nullptr,
tr("Save Preset"),
m_pUserDir.append(fileName),
tr("Mixxx controller presets (*.xml)"),
nullptr);
if (newFilePath.isNull() || newFilePath.isEmpty()) {
return;
}
newBaseName = QFileInfo(newFilePath).baseName();
if (newBaseName == fileName ||
QFile::exists(m_pUserDir +
newBaseName + QStringLiteral(".") + fileInfo.completeSuffix())) {
QMessageBox::warning(nullptr,
tr("Renaming Preset Failed"),
tr("A preset by that name already exists."));
} else if (newBaseName.isEmpty()) {
QMessageBox::warning(nullptr,
tr("Renaming Preset Failed"),
tr("A preset cannot have a blank name."));
} else {
validNameGiven = true;
}
qDebug() << " newBaseName" << newBaseName;

m_pPreset->setFilePath(filePath);
newFilePath = m_pUserDir + newBaseName +
QStringLiteral(".") + fileInfo.completeSuffix();
qDebug() << " newFilePath" << newFilePath;
}
}
//
// if (!m_pPreset->savePreset(newFilePath)) {
// qDebug() << " Failed to save preset!";
// return;
// }
//
// m_pPreset->setFilePath(newFilePath);
m_pPreset->setDirty(false);

enumeratePresets(m_pPreset->filePath());
//
// enumeratePresets(m_pPreset->filePath());
}

void DlgPrefController::initTableView(QTableView* pTable) {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/dlgprefcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ class DlgPrefController : public DlgPreferencePage {

Ui::DlgPrefControllerDlg m_ui;
UserSettingsPointer m_pConfig;
QString m_pUserDir;
ControllerManager* m_pControllerManager;
Controller* m_pController;
DlgControllerLearning* m_pDlgControllerLearning;
ControllerPresetPointer m_pPreset;
QMap<ControllerPresetPointer, bool> m_pOverwritePresets;
ControllerInputMappingTableModel* m_pInputTableModel;
QSortFilterProxyModel* m_pInputProxyModel;
ControllerOutputMappingTableModel* m_pOutputTableModel;
Expand Down
Loading

0 comments on commit ab52a30

Please sign in to comment.