File indexing completed on 2024-06-02 04:59:25

0001 /**
0002  * \file tagimportdialog.cpp
0003  * Dialog to import from other tags.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 20 Jun 2011
0008  *
0009  * Copyright (C) 2011-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "tagimportdialog.h"
0028 #include <QHBoxLayout>
0029 #include <QPushButton>
0030 #include <QFormLayout>
0031 #include <QComboBox>
0032 #include <QCoreApplication>
0033 #include "textimporter.h"
0034 #include "importparser.h"
0035 #include "trackdatamodel.h"
0036 #include "importconfig.h"
0037 #include "contexthelp.h"
0038 #include "formatlistedit.h"
0039 
0040 /**
0041  * Constructor.
0042  *
0043  * @param parent  parent widget
0044  * @param trackDataModel track data to be filled with imported values,
0045  *        nullptr if dialog is used independent from import dialog
0046  */
0047 TagImportDialog::TagImportDialog(QWidget* parent,
0048                                  TrackDataModel* trackDataModel)
0049   : QDialog(parent), m_trackDataModel(trackDataModel)
0050 {
0051   setObjectName(QLatin1String("TagImportDialog"));
0052   setWindowTitle(tr("Import from Tags"));
0053   setSizeGripEnabled(true);
0054 
0055   auto vboxLayout = new QVBoxLayout(this);
0056 
0057   m_formatListEdit = new FormatListEdit(
0058         {tr("Format:"), tr("Source:"), tr("Extraction:")},
0059         {QString(), TrackDataFormatReplacer::getToolTip(),
0060          getExtractionToolTip()},
0061         this);
0062   vboxLayout->addWidget(m_formatListEdit);
0063 
0064   if (!trackDataModel) {
0065     auto destLayout = new QFormLayout;
0066     destLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
0067     m_destComboBox = new QComboBox;
0068     const auto tagVersions = Frame::availableTagVersions();
0069     for (auto it = tagVersions.constBegin(); it != tagVersions.constEnd(); ++it) {
0070       m_destComboBox->addItem(it->second, it->first);
0071     }
0072     destLayout->addRow(tr("D&estination:"), m_destComboBox);
0073     vboxLayout->addLayout(destLayout);
0074   } else {
0075     m_destComboBox = nullptr;
0076   }
0077 
0078   auto buttonLayout = new QHBoxLayout;
0079   auto helpButton = new QPushButton(tr("&Help"), this);
0080   helpButton->setAutoDefault(false);
0081   buttonLayout->addWidget(helpButton);
0082   connect(helpButton, &QAbstractButton::clicked, this, &TagImportDialog::showHelp);
0083   auto saveButton = new QPushButton(tr("&Save Settings"), this);
0084   saveButton->setAutoDefault(false);
0085   buttonLayout->addWidget(saveButton);
0086   connect(saveButton, &QAbstractButton::clicked, this, &TagImportDialog::saveConfig);
0087   buttonLayout->addStretch();
0088   auto applyButton = new QPushButton(tr("&Apply"), this);
0089   applyButton->setAutoDefault(false);
0090   buttonLayout->addWidget(applyButton);
0091   connect(applyButton, &QAbstractButton::clicked, this, &TagImportDialog::apply);
0092   auto closeButton = new QPushButton(tr("&Close"), this);
0093   closeButton->setAutoDefault(false);
0094   buttonLayout->addWidget(closeButton);
0095   connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept);
0096   vboxLayout->addLayout(buttonLayout);
0097 }
0098 
0099 /**
0100  * Clear dialog data.
0101  */
0102 void TagImportDialog::clear()
0103 {
0104   setFormatFromConfig();
0105 
0106   if (m_destComboBox) {
0107     const ImportConfig& importCfg = ImportConfig::instance();
0108     Frame::TagVersion importDest = importCfg.importDest();
0109     int index = m_destComboBox->findData(importDest);
0110     m_destComboBox->setCurrentIndex(index);
0111   }
0112 }
0113 
0114 /**
0115  * Get import destination.
0116  * Is only available if dialog is not opened from import dialog.
0117  * @return TagV1, TagV2 or TagV2V1 for ID3v1, ID3v2 or both.
0118  */
0119 Frame::TagVersion TagImportDialog::getDestination() const
0120 {
0121   return m_destComboBox
0122       ? Frame::tagVersionCast(
0123           m_destComboBox->itemData(m_destComboBox->currentIndex()).toInt())
0124       : ImportConfig::instance().importDest();
0125 }
0126 
0127 /**
0128  * Get selected source format.
0129  * @return source format.
0130  */
0131 QString TagImportDialog::getSourceFormat() const
0132 {
0133   return m_formatListEdit->getCurrentFormat(1);
0134 }
0135 
0136 /**
0137  * Get selected extraction format.
0138  * @return extraction format.
0139  */
0140 QString TagImportDialog::getExtractionFormat() const
0141 {
0142   return m_formatListEdit->getCurrentFormat(2);
0143 }
0144 
0145 /**
0146  * Apply import to track data.
0147  */
0148 void TagImportDialog::apply()
0149 {
0150   if (m_trackDataModel) {
0151     ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
0152     TextImporter::importFromTags(m_formatListEdit->getCurrentFormat(1),
0153                                  m_formatListEdit->getCurrentFormat(2),
0154                                  trackDataVector);
0155     m_trackDataModel->setTrackData(trackDataVector);
0156   }
0157   emit trackDataUpdated();
0158 }
0159 
0160 /**
0161  * Set the format combo box and line edits from the configuration.
0162  */
0163 void TagImportDialog::setFormatFromConfig()
0164 {
0165   const ImportConfig& importCfg = ImportConfig::instance();
0166   m_formatListEdit->setFormats(
0167         {importCfg.importTagsNames(), importCfg.importTagsSources(),
0168          importCfg.importTagsExtractions()},
0169         importCfg.importTagsIndex());
0170 }
0171 
0172 /**
0173  * Save the local settings to the configuration.
0174  */
0175 void TagImportDialog::saveConfig()
0176 {
0177   ImportConfig& importCfg = ImportConfig::instance();
0178   int idx;
0179   QList<QStringList> formats = m_formatListEdit->getFormats(&idx);
0180   importCfg.setImportTagsIndex(idx);
0181   importCfg.setImportTagsNames(formats.at(0));
0182   importCfg.setImportTagsSources(formats.at(1));
0183   importCfg.setImportTagsExtractions(formats.at(2));
0184 
0185   if (m_destComboBox) {
0186     importCfg.setImportDest(Frame::tagVersionCast(
0187       m_destComboBox->itemData(m_destComboBox->currentIndex()).toInt()));
0188   }
0189 
0190   setFormatFromConfig();
0191 }
0192 
0193 /**
0194  * Show help.
0195  */
0196 void TagImportDialog::showHelp()
0197 {
0198   ContextHelp::displayHelp(QLatin1String("import-tags"));
0199 }
0200 
0201 /**
0202  * Get help text for format codes supported in extraction field.
0203  * @return help text.
0204  */
0205 QString TagImportDialog::getExtractionToolTip()
0206 {
0207   QString str;
0208   str += QLatin1String("<table>\n");
0209   str += ImportParser::getFormatToolTip(true);
0210 
0211   str += QLatin1String("<tr><td>%f</td><td>%{file}</td><td>");
0212   str += QCoreApplication::translate("@default", "Filename");
0213   str += QLatin1String("</td></tr>\n");
0214 
0215   str += QLatin1String("</table>\n");
0216   return str;
0217 }