File indexing completed on 2024-04-21 04:48:11
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 "profiledatacoverdialog.h" 0009 0010 #include <QDialogButtonBox> 0011 #include <QVBoxLayout> 0012 0013 ProfileDataCoverDialog::ProfileDataCoverDialog(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 bool scale = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_SCALE_INDEX)).toBool(); 0025 QSize size = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_SIZE_INDEX)).toSize(); 0026 QString format = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_FORMAT_INDEX)).toString(); 0027 QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_NAME_INDEX)).toString(); 0028 0029 setWindowTitle(i18n("Cover Settings")); 0030 0031 auto *mainLayout = new QVBoxLayout; 0032 setLayout(mainLayout); 0033 0034 QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok | QDialogButtonBox::Cancel; 0035 if (!new_profile_mode) 0036 buttons |= QDialogButtonBox::Apply; 0037 0038 QDialogButtonBox *buttonBox = new QDialogButtonBox(buttons); 0039 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0040 okButton->setDefault(true); 0041 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0042 if (!new_profile_mode) 0043 applyButton = buttonBox->button(QDialogButtonBox::Apply); 0044 connect(buttonBox, &QDialogButtonBox::accepted, this, &ProfileDataCoverDialog::slotAccepted); 0045 connect(buttonBox, &QDialogButtonBox::rejected, this, &ProfileDataCoverDialog::reject); 0046 if (!new_profile_mode) 0047 connect(applyButton, &QPushButton::clicked, this, &ProfileDataCoverDialog::slotApplied); 0048 0049 QWidget *widget = new QWidget(this); 0050 mainLayout->addWidget(widget); 0051 mainLayout->addWidget(buttonBox); 0052 ui.setupUi(widget); 0053 0054 connect(ui.kpushbutton_scheme, SIGNAL(clicked()), this, SLOT(scheme_wizard())); 0055 ui.kpushbutton_scheme->setIcon(QIcon::fromTheme("tools-wizard")); 0056 0057 ui.checkBox_scale->setChecked(scale); 0058 enable_scale(ui.checkBox_scale->isChecked()); 0059 connect(ui.checkBox_scale, SIGNAL(toggled(bool)), this, SLOT(trigger_changed())); 0060 connect(ui.checkBox_scale, SIGNAL(toggled(bool)), this, SLOT(enable_scale(bool))); 0061 0062 ui.kintspinbox_x->setValue(size.width()); 0063 connect(ui.kintspinbox_x, SIGNAL(valueChanged(int)), this, SLOT(trigger_changed())); 0064 0065 ui.kintspinbox_y->setValue(size.height()); 0066 connect(ui.kintspinbox_y, SIGNAL(valueChanged(int)), this, SLOT(trigger_changed())); 0067 0068 ui.kcombobox_format->addItem(i18n("JPEG (Joint Photographic Experts Group)"), "JPEG"); 0069 ui.kcombobox_format->addItem(i18n("PNG (Portable Network Graphics)"), "PNG"); 0070 ui.kcombobox_format->addItem(i18n("BMP (Windows Bitmap)"), "BMP"); 0071 { 0072 int i = ui.kcombobox_format->findData(format); 0073 ui.kcombobox_format->setCurrentIndex(i); 0074 } 0075 connect(ui.kcombobox_format, SIGNAL(currentIndexChanged(int)), this, SLOT(trigger_changed())); 0076 0077 ui.qlineedit_scheme->setText(scheme); 0078 connect(ui.qlineedit_scheme, SIGNAL(textEdited(const QString &)), this, SLOT(trigger_changed())); 0079 0080 if (applyButton) 0081 applyButton->setEnabled(false); 0082 } 0083 0084 ProfileDataCoverDialog::~ProfileDataCoverDialog() 0085 { 0086 } 0087 0088 void ProfileDataCoverDialog::slotAccepted() 0089 { 0090 if (save()) 0091 accept(); 0092 else 0093 ErrorDialog::show(this, error.message(), error.details()); 0094 } 0095 0096 void ProfileDataCoverDialog::slotApplied() 0097 { 0098 if (!save()) 0099 ErrorDialog::show(this, error.message(), error.details()); 0100 } 0101 0102 void ProfileDataCoverDialog::scheme_wizard() 0103 { 0104 QString suffix = ui.kcombobox_format->itemData(ui.kcombobox_format->currentIndex()).toString().toLower(); 0105 0106 FilenameSchemeWizardDialog *dialog = new FilenameSchemeWizardDialog(ui.qlineedit_scheme->text(), suffix, this); 0107 0108 if (dialog->exec() != QDialog::Accepted) { 0109 delete dialog; 0110 return; 0111 } 0112 0113 ui.qlineedit_scheme->setText(dialog->scheme); 0114 0115 delete dialog; 0116 0117 trigger_changed(); 0118 } 0119 0120 void ProfileDataCoverDialog::trigger_changed() 0121 { 0122 if (applyButton) { 0123 bool scale = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_SCALE_INDEX)).toBool(); 0124 QSize size = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_SIZE_INDEX)).toSize(); 0125 QString format = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_FORMAT_INDEX)).toString(); 0126 QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_NAME_INDEX)).toString(); 0127 0128 if (ui.checkBox_scale->isChecked() != scale) { 0129 applyButton->setEnabled(true); 0130 return; 0131 } 0132 if (ui.kintspinbox_x->value() != size.width()) { 0133 applyButton->setEnabled(true); 0134 return; 0135 } 0136 if (ui.kintspinbox_y->value() != size.height()) { 0137 applyButton->setEnabled(true); 0138 return; 0139 } 0140 if (ui.kcombobox_format->itemData(ui.kcombobox_format->currentIndex()).toString() != format) { 0141 applyButton->setEnabled(true); 0142 return; 0143 } 0144 if (ui.qlineedit_scheme->text() != scheme) { 0145 applyButton->setEnabled(true); 0146 return; 0147 } 0148 applyButton->setEnabled(false); 0149 } 0150 } 0151 0152 void ProfileDataCoverDialog::enable_scale(bool enabled) 0153 { 0154 ui.label_x->setEnabled(enabled); 0155 ui.kintspinbox_x->setEnabled(enabled); 0156 ui.kintspinbox_y->setEnabled(enabled); 0157 } 0158 0159 bool ProfileDataCoverDialog::save() 0160 { 0161 bool scale = ui.checkBox_scale->isChecked(); 0162 QSize size = QSize(ui.kintspinbox_x->value(), ui.kintspinbox_y->value()); 0163 QString format = ui.kcombobox_format->itemData(ui.kcombobox_format->currentIndex()).toString(); 0164 QString scheme = ui.qlineedit_scheme->text(); 0165 0166 error.clear(); 0167 bool success = true; 0168 0169 if (success) 0170 success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_SCALE_INDEX), scale); 0171 if (success) 0172 success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_SIZE_INDEX), size); 0173 if (success) 0174 success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_FORMAT_INDEX), format); 0175 if (success) 0176 success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_SC_NAME_INDEX), scheme); 0177 0178 if (!success) 0179 error = profile_model->lastError(); 0180 0181 if (success) { 0182 profile_model->commit(); 0183 if (applyButton) 0184 applyButton->setEnabled(false); 0185 return true; 0186 } 0187 0188 return false; 0189 }