File indexing completed on 2024-04-28 04:48:21

0001 /* AUDEX CDDA EXTRACTOR
0002  * SPDX-FileCopyrightText: Copyright (C) 2007 Marco Nelles
0003  * <https://userbase.kde.org/Audex>
0004  *
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 #include "profiledatacuesheetdialog.h"
0009 
0010 #include <QDialogButtonBox>
0011 #include <QVBoxLayout>
0012 
0013 ProfileDataCueSheetDialog::ProfileDataCueSheetDialog(ProfileModel *profile_model, const int profile_row, const bool new_profile_mode, QWidget *parent)
0014     : QDialog(parent)
0015 {
0016     Q_UNUSED(parent);
0017 
0018     this->profile_model = profile_model;
0019     this->profile_row = profile_row;
0020     this->new_profile_mode = new_profile_mode;
0021 
0022     applyButton = nullptr;
0023 
0024     // profile data cue sheet data
0025     QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_CUE_NAME_INDEX)).toString();
0026     bool add_mcn_and_isrc = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_CUE_ADD_MCN_AND_ISRC_INDEX)).toBool();
0027 
0028     setWindowTitle(i18n("Cue Sheet Settings"));
0029 
0030     auto *mainLayout = new QVBoxLayout;
0031     setLayout(mainLayout);
0032 
0033     QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok | QDialogButtonBox::Cancel;
0034     if (!new_profile_mode)
0035         buttons |= QDialogButtonBox::Apply;
0036 
0037     QDialogButtonBox *buttonBox = new QDialogButtonBox(buttons);
0038     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0039     okButton->setDefault(true);
0040     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0041     if (!new_profile_mode)
0042         applyButton = buttonBox->button(QDialogButtonBox::Apply);
0043     connect(buttonBox, &QDialogButtonBox::accepted, this, &ProfileDataCueSheetDialog::slotAccepted);
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &ProfileDataCueSheetDialog::reject);
0045     if (!new_profile_mode)
0046         connect(applyButton, &QPushButton::clicked, this, &ProfileDataCueSheetDialog::slotApplied);
0047 
0048     QWidget *widget = new QWidget(this);
0049     mainLayout->addWidget(widget);
0050     mainLayout->addWidget(buttonBox);
0051     ui.setupUi(widget);
0052 
0053     connect(ui.kpushbutton_scheme, SIGNAL(clicked()), this, SLOT(scheme_wizard()));
0054     ui.kpushbutton_scheme->setIcon(QIcon::fromTheme("tools-wizard"));
0055 
0056     ui.qlineedit_scheme->setText(scheme);
0057     connect(ui.qlineedit_scheme, SIGNAL(textEdited(const QString &)), this, SLOT(trigger_changed()));
0058 
0059     ui.checkBox_add_mcn_and_isrc->setChecked(add_mcn_and_isrc);
0060     connect(ui.checkBox_add_mcn_and_isrc, SIGNAL(toggled(bool)), this, SLOT(trigger_changed()));
0061 
0062     if (applyButton)
0063         applyButton->setEnabled(false);
0064 }
0065 
0066 ProfileDataCueSheetDialog::~ProfileDataCueSheetDialog()
0067 {
0068 }
0069 
0070 void ProfileDataCueSheetDialog::slotAccepted()
0071 {
0072     if (save())
0073         accept();
0074     else
0075         ErrorDialog::show(this, error.message(), error.details());
0076 }
0077 
0078 void ProfileDataCueSheetDialog::slotApplied()
0079 {
0080     if (!save())
0081         ErrorDialog::show(this, error.message(), error.details());
0082 }
0083 
0084 void ProfileDataCueSheetDialog::scheme_wizard()
0085 {
0086     FilenameSchemeWizardDialog *dialog = new FilenameSchemeWizardDialog(ui.qlineedit_scheme->text(), "cue", this);
0087 
0088     if (dialog->exec() != QDialog::Accepted) {
0089         delete dialog;
0090         return;
0091     }
0092 
0093     ui.qlineedit_scheme->setText(dialog->scheme);
0094 
0095     delete dialog;
0096 
0097     trigger_changed();
0098 }
0099 
0100 void ProfileDataCueSheetDialog::trigger_changed()
0101 {
0102     if (applyButton) {
0103         QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_CUE_NAME_INDEX)).toString();
0104         bool add_mcn_and_isrc = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_CUE_ADD_MCN_AND_ISRC_INDEX)).toBool();
0105 
0106         if (ui.qlineedit_scheme->text() != scheme) {
0107             applyButton->setEnabled(true);
0108             return;
0109         }
0110         if (ui.checkBox_add_mcn_and_isrc->isChecked() != add_mcn_and_isrc) {
0111             applyButton->setEnabled(true);
0112             return;
0113         }
0114         applyButton->setEnabled(false);
0115     }
0116 }
0117 
0118 bool ProfileDataCueSheetDialog::save()
0119 {
0120     QString scheme = ui.qlineedit_scheme->text();
0121     bool add_mcn_and_isrc = ui.checkBox_add_mcn_and_isrc->isChecked();
0122 
0123     error.clear();
0124     bool success = true;
0125 
0126     if (success)
0127         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_CUE_NAME_INDEX), scheme);
0128     if (success)
0129         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_CUE_ADD_MCN_AND_ISRC_INDEX), add_mcn_and_isrc);
0130 
0131     if (!success)
0132         error = profile_model->lastError();
0133 
0134     if (success) {
0135         profile_model->commit();
0136         if (applyButton)
0137             applyButton->setEnabled(false);
0138         return true;
0139     }
0140 
0141     return false;
0142 }