File indexing completed on 2021-12-21 13:27:53

0001 /**
0002  * Copyright (C) 2004 Michael Pyne <mpyne@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "filerenameroptions.h"
0018 
0019 #include <KLocalizedString>
0020 
0021 #include "juk_debug.h"
0022 
0023 FileRenamerTagOptions::FileRenamerTagOptions(QWidget *parent,
0024                                              const TagRenamerOptions &options)
0025   : QWidget(parent)
0026   , Ui::FileRenamerTagOptionsBase()
0027   , m_options(options)
0028 {
0029     setupUi(this);
0030 
0031     layout()->setContentsMargins(0, 0, 0, 0);
0032 
0033     if(m_options.category() != Track)
0034         m_trackGroup->hide();
0035 
0036     QString tagText = m_options.tagTypeText();
0037 
0038     setWindowTitle(i18nc("%1 will be a music tag category like Artist or Album", "%1 Options",tagText));
0039     m_tagFormatGroup->setTitle(i18n("%1 Format",tagText));
0040     m_emptyTagGroup->setTitle(i18n("When the Track's %1 is Empty",tagText));
0041     m_description->setText(i18n("When using the file renamer your files will be renamed to the values that you have in your track's %1 tag, plus any additional text that you specify below.",tagText));
0042     m_tagLabel->setText(tagText);
0043 
0044     m_prefixText->setText(options.prefix());
0045     m_suffixText->setText(options.suffix());
0046     if(options.emptyAction() == TagRenamerOptions::ForceEmptyInclude)
0047         m_includeEmptyButton->setChecked(true);
0048     else if(options.emptyAction() == TagRenamerOptions::UseReplacementValue)
0049         m_useValueButton->setChecked(true);
0050     m_emptyTagValue->setText(options.emptyText());
0051     m_trackWidth->setValue(options.trackWidth());
0052 
0053     slotBracketsChanged();
0054     slotEmptyActionChanged();
0055     slotTrackWidthChanged();
0056 }
0057 
0058 void FileRenamerTagOptions::slotBracketsChanged()
0059 {
0060     QString tag = m_options.tagTypeText();
0061 
0062     m_options.setPrefix(m_prefixText->text());
0063     m_options.setSuffix(m_suffixText->text());
0064 
0065     m_substitution->setText(m_options.prefix() + tag + m_options.suffix());
0066 }
0067 
0068 void FileRenamerTagOptions::slotTrackWidthChanged()
0069 {
0070     int width = m_trackWidth->value();
0071 
0072     m_options.setTrackWidth(width);
0073 
0074     QString singleDigitText = m_singleDigit->text();
0075     singleDigitText.remove(" ->");
0076     QString doubleDigitText = m_doubleDigit->text();
0077     doubleDigitText.remove(" ->");
0078 
0079     if(singleDigitText.length() < width) {
0080         QString p;
0081         p.fill('0', width - singleDigitText.length());
0082         singleDigitText.prepend(p);
0083     }
0084 
0085     if(doubleDigitText.length() < width) {
0086         QString p;
0087         p.fill('0', width - doubleDigitText.length());
0088         doubleDigitText.prepend(p);
0089     }
0090 
0091     m_singleDigitExample->setText(singleDigitText);
0092     m_doubleDigitExample->setText(doubleDigitText);
0093 }
0094 
0095 void FileRenamerTagOptions::slotEmptyActionChanged()
0096 {
0097     m_options.setEmptyText(m_emptyTagValue->text());
0098     m_options.setEmptyAction(TagRenamerOptions::IgnoreEmptyTag);
0099 
0100     if(m_useValueButton->isChecked())
0101         m_options.setEmptyAction(TagRenamerOptions::UseReplacementValue);
0102     else if(m_includeEmptyButton->isChecked())
0103         m_options.setEmptyAction(TagRenamerOptions::ForceEmptyInclude);
0104 }
0105 
0106 TagOptionsDialog::TagOptionsDialog(QWidget *parent,
0107                                    const TagRenamerOptions &options,
0108                                    unsigned categoryNumber)
0109   : QDialog(parent)
0110   , m_options(options)
0111   , m_categoryNumber(categoryNumber)
0112 {
0113     setModal(true);
0114     setWindowTitle(i18n("File Renamer"));
0115 
0116     loadConfig();
0117 
0118     m_widget = new FileRenamerTagOptions(this, m_options);
0119     m_widget->setMinimumSize(400, 200);
0120 
0121     connect(m_widget->dlgButtonBox, &QDialogButtonBox::accepted,
0122             this,                   &QDialog::accept);
0123     connect(m_widget->dlgButtonBox, &QDialogButtonBox::rejected,
0124             this,                   &QDialog::reject);
0125 
0126     auto boxLayout = new QVBoxLayout(this);
0127     boxLayout->addWidget(m_widget);
0128 }
0129 
0130 void TagOptionsDialog::accept()
0131 {
0132     m_options = m_widget->options();
0133 
0134     saveConfig();
0135     QDialog::accept();
0136 }
0137 
0138 void TagOptionsDialog::loadConfig()
0139 {
0140     // Our m_options may not have been loaded from KConfig, force that to
0141     // happen.
0142 
0143     CategoryID category(m_options.category(), m_categoryNumber);
0144     m_options = TagRenamerOptions(category);
0145 }
0146 
0147 void TagOptionsDialog::saveConfig()
0148 {
0149     m_options.saveConfig(m_categoryNumber);
0150 }
0151 
0152 // vim: set et sw=4 tw=0 sta: