File indexing completed on 2022-10-04 17:30:12

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "customdialog.h"
0005 
0006 #include "krenamefile.h"
0007 
0008 #include <KConfigGroup>
0009 
0010 #include <QDialogButtonBox>
0011 
0012 CustomDialog::CustomDialog(const KRenameFile &file, QWidget *parent)
0013     : QDialog(parent)
0014 {
0015     QWidget *mainWidget = new QWidget(this);
0016     QVBoxLayout *mainLayout = new QVBoxLayout;
0017     setLayout(mainLayout);
0018     mainLayout->addWidget(mainWidget);
0019     m_widget.setupUi(mainWidget);
0020 
0021     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0022     connect(buttonBox, &QDialogButtonBox::accepted,
0023             this, &CustomDialog::accept);
0024     connect(buttonBox, &QDialogButtonBox::rejected,
0025             this, &CustomDialog::reject);
0026     mainLayout->addWidget(buttonBox);
0027 
0028     connect(m_widget.radioKRename, &QRadioButton::clicked,
0029             this, &CustomDialog::slotEnableControls);
0030     connect(m_widget.radioInput, &QRadioButton::clicked,
0031             this, &CustomDialog::slotEnableControls);
0032     connect(m_widget.radioCustom, &QRadioButton::clicked,
0033             this, &CustomDialog::slotEnableControls);
0034 
0035     // Set default vallues
0036     m_widget.radioCustom->setChecked(true);
0037     m_widget.radioKRename->setChecked(false);
0038     m_widget.radioInput->setChecked(false);
0039 
0040     QString srcFilename = file.srcFilename();
0041     if (!file.srcExtension().isEmpty()) {
0042         srcFilename += '.';
0043         srcFilename += file.srcExtension();
0044     }
0045 
0046     QString krenameFilename = file.dstFilename();
0047     if (!file.dstExtension().isEmpty()) {
0048         krenameFilename += '.';
0049         krenameFilename += file.dstExtension();
0050     }
0051 
0052     if (!file.manualChanges().isNull()) {
0053         switch (file.manualChangeMode()) {
0054         case eManualChangeMode_Custom:
0055             krenameFilename = file.manualChanges();
0056             break;
0057         case eManualChangeMode_Input:
0058             m_widget.radioInput->setChecked(true);
0059             m_widget.radioKRename->setChecked(false);
0060             m_widget.radioCustom->setChecked(false);
0061             srcFilename = file.manualChanges();
0062             break;
0063         case eManualChangeMode_None:
0064         default:
0065             break;
0066         }
0067     }
0068 
0069     m_widget.labelPreview->setPixmap(file.icon());
0070     m_widget.lineEditInput->setText(srcFilename);
0071     m_widget.lineEditCustom->setText(krenameFilename);
0072     slotEnableControls();
0073 
0074     KSharedConfigPtr config = KSharedConfig::openConfig();
0075     KConfigGroup group = config->group(QString("CustomDialogGroup"));
0076     restoreGeometry(group.readEntry<QByteArray>("Geometry", QByteArray()));
0077 }
0078 
0079 CustomDialog::~CustomDialog()
0080 {
0081     KSharedConfigPtr config = KSharedConfig::openConfig();
0082     KConfigGroup group = config->group(QString("CustomDialogGroup"));
0083     group.writeEntry("Geometry", saveGeometry());
0084 }
0085 
0086 void CustomDialog::slotEnableControls()
0087 {
0088     m_widget.lineEditCustom->setEnabled(m_widget.radioCustom->isChecked());
0089     m_widget.lineEditInput->setEnabled(m_widget.radioInput->isChecked());
0090 }
0091 
0092 bool CustomDialog::hasManualChanges() const
0093 {
0094     return !(m_widget.radioKRename->isChecked());
0095 }
0096 
0097 const QString CustomDialog::manualChanges() const
0098 {
0099     if (m_widget.radioCustom->isChecked()) {
0100         return m_widget.lineEditCustom->text();
0101     } else if (m_widget.radioInput->isChecked()) {
0102         return m_widget.lineEditInput->text();
0103     } else {
0104         return QString();
0105     }
0106 }
0107 
0108 EManualChangeMode CustomDialog::manualChangeMode() const
0109 {
0110     if (m_widget.radioCustom->isChecked()) {
0111         return eManualChangeMode_Custom;
0112     } else if (m_widget.radioInput->isChecked()) {
0113         return eManualChangeMode_Input;
0114     } else {
0115         return eManualChangeMode_None;
0116     }
0117 
0118 }