1 /**
2 * \file tagimportdialog.cpp
3 * Dialog to import from other tags.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 20 Jun 2011
8 *
9 * Copyright (C) 2011-2018 Urs Fleisch
10 *
11 * This file is part of Kid3.
12 *
13 * Kid3 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Kid3 is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include "tagimportdialog.h"
28 #include <QHBoxLayout>
29 #include <QPushButton>
30 #include <QFormLayout>
31 #include <QComboBox>
32 #include <QCoreApplication>
33 #include "textimporter.h"
34 #include "importparser.h"
35 #include "trackdatamodel.h"
36 #include "importconfig.h"
37 #include "contexthelp.h"
38 #include "formatlistedit.h"
39
40 /**
41 * Constructor.
42 *
43 * @param parent parent widget
44 * @param trackDataModel track data to be filled with imported values,
45 * nullptr if dialog is used independent from import dialog
46 */
TagImportDialog(QWidget * parent,TrackDataModel * trackDataModel)47 TagImportDialog::TagImportDialog(QWidget* parent,
48 TrackDataModel* trackDataModel)
49 : QDialog(parent), m_trackDataModel(trackDataModel)
50 {
51 setObjectName(QLatin1String("TagImportDialog"));
52 setWindowTitle(tr("Import from Tags"));
53 setSizeGripEnabled(true);
54
55 auto vboxLayout = new QVBoxLayout(this);
56
57 m_formatListEdit = new FormatListEdit(
58 {tr("Format:"), tr("Source:"), tr("Extraction:")},
59 {QString(), TrackDataFormatReplacer::getToolTip(),
60 getExtractionToolTip()},
61 this);
62 vboxLayout->addWidget(m_formatListEdit);
63
64 if (!trackDataModel) {
65 auto destLayout = new QFormLayout;
66 destLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
67 m_destComboBox = new QComboBox;
68 const auto tagVersions = Frame::availableTagVersions();
69 for (auto it = tagVersions.constBegin(); it != tagVersions.constEnd(); ++it) {
70 m_destComboBox->addItem(it->second, it->first);
71 }
72 destLayout->addRow(tr("D&estination:"), m_destComboBox);
73 vboxLayout->addLayout(destLayout);
74 } else {
75 m_destComboBox = nullptr;
76 }
77
78 auto buttonLayout = new QHBoxLayout;
79 QPushButton* helpButton = new QPushButton(tr("&Help"), this);
80 helpButton->setAutoDefault(false);
81 buttonLayout->addWidget(helpButton);
82 connect(helpButton, &QAbstractButton::clicked, this, &TagImportDialog::showHelp);
83 QPushButton* saveButton = new QPushButton(tr("&Save Settings"), this);
84 saveButton->setAutoDefault(false);
85 buttonLayout->addWidget(saveButton);
86 connect(saveButton, &QAbstractButton::clicked, this, &TagImportDialog::saveConfig);
87 buttonLayout->addStretch();
88 QPushButton* applyButton = new QPushButton(tr("&Apply"), this);
89 applyButton->setAutoDefault(false);
90 buttonLayout->addWidget(applyButton);
91 connect(applyButton, &QAbstractButton::clicked, this, &TagImportDialog::apply);
92 QPushButton* closeButton = new QPushButton(tr("&Close"), this);
93 closeButton->setAutoDefault(false);
94 buttonLayout->addWidget(closeButton);
95 connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept);
96 vboxLayout->addLayout(buttonLayout);
97 }
98
99 /**
100 * Clear dialog data.
101 */
clear()102 void TagImportDialog::clear()
103 {
104 setFormatFromConfig();
105
106 if (m_destComboBox) {
107 const ImportConfig& importCfg = ImportConfig::instance();
108 Frame::TagVersion importDest = importCfg.importDest();
109 int index = m_destComboBox->findData(importDest);
110 m_destComboBox->setCurrentIndex(index);
111 }
112 }
113
114 /**
115 * Get import destination.
116 * Is only available if dialog is not opened from import dialog.
117 * @return TagV1, TagV2 or TagV2V1 for ID3v1, ID3v2 or both.
118 */
getDestination() const119 Frame::TagVersion TagImportDialog::getDestination() const
120 {
121 return m_destComboBox
122 ? Frame::tagVersionCast(
123 m_destComboBox->itemData(m_destComboBox->currentIndex()).toInt())
124 : ImportConfig::instance().importDest();
125 }
126
127 /**
128 * Get selected source format.
129 * @return source format.
130 */
getSourceFormat() const131 QString TagImportDialog::getSourceFormat() const
132 {
133 return m_formatListEdit->getCurrentFormat(1);
134 }
135
136 /**
137 * Get selected extraction format.
138 * @return extraction format.
139 */
getExtractionFormat() const140 QString TagImportDialog::getExtractionFormat() const
141 {
142 return m_formatListEdit->getCurrentFormat(2);
143 }
144
145 /**
146 * Apply import to track data.
147 */
apply()148 void TagImportDialog::apply()
149 {
150 if (m_trackDataModel) {
151 ImportTrackDataVector trackDataVector(m_trackDataModel->getTrackData());
152 TextImporter::importFromTags(m_formatListEdit->getCurrentFormat(1),
153 m_formatListEdit->getCurrentFormat(2),
154 trackDataVector);
155 m_trackDataModel->setTrackData(trackDataVector);
156 }
157 emit trackDataUpdated();
158 }
159
160 /**
161 * Set the format combo box and line edits from the configuration.
162 */
setFormatFromConfig()163 void TagImportDialog::setFormatFromConfig()
164 {
165 const ImportConfig& importCfg = ImportConfig::instance();
166 m_formatListEdit->setFormats(
167 {importCfg.importTagsNames(), importCfg.importTagsSources(),
168 importCfg.importTagsExtractions()},
169 importCfg.importTagsIndex());
170 }
171
172 /**
173 * Save the local settings to the configuration.
174 */
saveConfig()175 void TagImportDialog::saveConfig()
176 {
177 ImportConfig& importCfg = ImportConfig::instance();
178 int idx;
179 QList<QStringList> formats = m_formatListEdit->getFormats(&idx);
180 importCfg.setImportTagsIndex(idx);
181 importCfg.setImportTagsNames(formats.at(0));
182 importCfg.setImportTagsSources(formats.at(1));
183 importCfg.setImportTagsExtractions(formats.at(2));
184
185 if (m_destComboBox) {
186 importCfg.setImportDest(Frame::tagVersionCast(
187 m_destComboBox->itemData(m_destComboBox->currentIndex()).toInt()));
188 }
189
190 setFormatFromConfig();
191 }
192
193 /**
194 * Show help.
195 */
showHelp()196 void TagImportDialog::showHelp()
197 {
198 ContextHelp::displayHelp(QLatin1String("import-tags"));
199 }
200
201 /**
202 * Get help text for format codes supported in extraction field.
203 * @return help text.
204 */
getExtractionToolTip()205 QString TagImportDialog::getExtractionToolTip()
206 {
207 QString str;
208 str += QLatin1String("<table>\n");
209 str += ImportParser::getFormatToolTip(true);
210
211 str += QLatin1String("<tr><td>%f</td><td>%{file}</td><td>");
212 str += QCoreApplication::translate("@default", "Filename");
213 str += QLatin1String("</td></tr>\n");
214
215 str += QLatin1String("</table>\n");
216 return str;
217 }
218