File indexing completed on 2024-04-28 16:06:15

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 "profiledatalogfiledialog.h"
0009 
0010 #include <QDialogButtonBox>
0011 #include <QVBoxLayout>
0012 
0013 ProfileDataLogFileDialog::ProfileDataLogFileDialog(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     setWindowTitle(i18n("Log Files Settings"));
0025 
0026     // profile data logfile data
0027     QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_LOG_NAME_INDEX)).toString();
0028     bool write_timestamps = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_LOG_WRITE_TIMESTAMPS_INDEX)).toBool();
0029 
0030     auto *mainLayout = new QVBoxLayout;
0031     setLayout(mainLayout);
0032 
0033     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
0034     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0035     okButton->setDefault(true);
0036     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0037     applyButton = buttonBox->button(QDialogButtonBox::Apply);
0038     connect(buttonBox, &QDialogButtonBox::accepted, this, &ProfileDataLogFileDialog::slotAccepted);
0039     connect(buttonBox, &QDialogButtonBox::rejected, this, &ProfileDataLogFileDialog::reject);
0040     connect(applyButton, &QPushButton::clicked, this, &ProfileDataLogFileDialog::slotApplied);
0041 
0042     QWidget *widget = new QWidget(this);
0043     mainLayout->addWidget(widget);
0044     mainLayout->addWidget(buttonBox);
0045     ui.setupUi(widget);
0046 
0047     connect(ui.kpushbutton_scheme, SIGNAL(clicked()), this, SLOT(scheme_wizard()));
0048     ui.kpushbutton_scheme->setIcon(QIcon::fromTheme("tools-wizard"));
0049 
0050     ui.qlineedit_scheme->setText(scheme);
0051     connect(ui.qlineedit_scheme, SIGNAL(textEdited(const QString &)), this, SLOT(trigger_changed()));
0052 
0053     ui.checkBox_timestamps->setChecked(write_timestamps);
0054     connect(ui.checkBox_timestamps, SIGNAL(toggled(bool)), this, SLOT(trigger_changed()));
0055 
0056     if (applyButton)
0057         applyButton->setEnabled(false);
0058 }
0059 
0060 ProfileDataLogFileDialog::~ProfileDataLogFileDialog()
0061 {
0062 }
0063 
0064 void ProfileDataLogFileDialog::slotAccepted()
0065 {
0066     if (save())
0067         accept();
0068     else
0069         ErrorDialog::show(this, error.message(), error.details());
0070 }
0071 
0072 void ProfileDataLogFileDialog::slotApplied()
0073 {
0074     if (!save())
0075         ErrorDialog::show(this, error.message(), error.details());
0076 }
0077 
0078 void ProfileDataLogFileDialog::scheme_wizard()
0079 {
0080     FilenameSchemeWizardDialog *dialog = new FilenameSchemeWizardDialog(ui.qlineedit_scheme->text(), "log", this);
0081 
0082     if (dialog->exec() != QDialog::Accepted) {
0083         delete dialog;
0084         return;
0085     }
0086 
0087     ui.qlineedit_scheme->setText(dialog->scheme);
0088 
0089     delete dialog;
0090 
0091     trigger_changed();
0092 }
0093 
0094 void ProfileDataLogFileDialog::trigger_changed()
0095 {
0096     if (applyButton) {
0097         QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_LOG_NAME_INDEX)).toString();
0098         bool write_timestamps = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_LOG_WRITE_TIMESTAMPS_INDEX)).toBool();
0099 
0100         if (ui.qlineedit_scheme->text() != scheme) {
0101             applyButton->setEnabled(true);
0102             return;
0103         }
0104         if (ui.checkBox_timestamps->isChecked() != write_timestamps) {
0105             applyButton->setEnabled(true);
0106             return;
0107         }
0108         applyButton->setEnabled(false);
0109     }
0110 }
0111 
0112 bool ProfileDataLogFileDialog::save()
0113 {
0114     QString scheme = ui.qlineedit_scheme->text();
0115     bool write_timestamps = ui.checkBox_timestamps->isChecked();
0116 
0117     error.clear();
0118     bool success = true;
0119 
0120     if (success)
0121         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_LOG_NAME_INDEX), scheme);
0122     if (success)
0123         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_LOG_WRITE_TIMESTAMPS_INDEX), write_timestamps);
0124 
0125     if (!success)
0126         error = profile_model->lastError();
0127 
0128     if (success) {
0129         profile_model->commit();
0130         if (applyButton)
0131             applyButton->setEnabled(false);
0132         return true;
0133     }
0134 
0135     return false;
0136 }