File indexing completed on 2024-04-21 16:32:29

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