File indexing completed on 2025-02-23 04:34:17
0001 /** 0002 * \file exportdialog.cpp 0003 * Export dialog. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 10 May 2006 0008 * 0009 * Copyright (C) 2006-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 "exportdialog.h" 0028 #include <QGuiApplication> 0029 #include <QClipboard> 0030 #include <QLayout> 0031 #include <QPushButton> 0032 #include <QLabel> 0033 #include <QCheckBox> 0034 #include <QSpinBox> 0035 #include <QString> 0036 #include <QTextEdit> 0037 #include <QTableView> 0038 #include <QComboBox> 0039 #include <QDir> 0040 #include <QUrl> 0041 #include <QMessageBox> 0042 #include <QHBoxLayout> 0043 #include <QVBoxLayout> 0044 #include "taggedfile.h" 0045 #include "genres.h" 0046 #include "exportconfig.h" 0047 #include "importconfig.h" 0048 #include "contexthelp.h" 0049 #include "textexporter.h" 0050 #include "texttablemodel.h" 0051 #include "formatlistedit.h" 0052 #include "iplatformtools.h" 0053 0054 /** 0055 * Constructor. 0056 * 0057 * @param platformTools platform tools 0058 * @param parent parent widget 0059 * @param textExporter text exporter to use 0060 */ 0061 ExportDialog::ExportDialog(IPlatformTools* platformTools, 0062 QWidget* parent, TextExporter* textExporter) 0063 : QDialog(parent), m_platformTools(platformTools), 0064 m_textExporter(textExporter), m_textTableModel(new TextTableModel(this)) 0065 { 0066 setObjectName(QLatin1String("ExportDialog")); 0067 setModal(true); 0068 setWindowTitle(tr("Export")); 0069 setSizeGripEnabled(true); 0070 0071 auto vlayout = new QVBoxLayout(this); 0072 m_edit = new QTextEdit(this); 0073 m_edit->setAcceptRichText(false); 0074 vlayout->addWidget(m_edit); 0075 0076 m_table = new QTableView(this); 0077 m_table->setModel(m_textTableModel); 0078 m_table->hide(); 0079 vlayout->addWidget(m_table); 0080 0081 QString formatToolTip = ImportTrackData::getFormatToolTip(); 0082 m_formatListEdit = new FormatListEdit( 0083 {tr("&Format:"), tr("H&eader:"), tr("&Tracks:"), tr("F&ooter:")}, 0084 {QString(), formatToolTip, formatToolTip, formatToolTip}, 0085 this); 0086 connect(m_formatListEdit, &FormatListEdit::formatChanged, 0087 this, &ExportDialog::showPreview); 0088 vlayout->addWidget(m_formatListEdit); 0089 0090 auto butlayout = new QHBoxLayout; 0091 m_fileButton = new QPushButton(tr("To F&ile..."), this); 0092 m_fileButton->setAutoDefault(false); 0093 butlayout->addWidget(m_fileButton); 0094 connect(m_fileButton, &QAbstractButton::clicked, this, &ExportDialog::slotToFile); 0095 0096 m_clipButton = new QPushButton(tr("To Clip&board"), this); 0097 m_clipButton->setAutoDefault(false); 0098 butlayout->addWidget(m_clipButton); 0099 connect(m_clipButton, &QAbstractButton::clicked, this, &ExportDialog::slotToClipboard); 0100 0101 auto butspacer = new QSpacerItem(16, 0, QSizePolicy::Expanding, 0102 QSizePolicy::Minimum); 0103 butlayout->addItem(butspacer); 0104 0105 auto srcLabel = new QLabel(tr("&Source:"), this); 0106 butlayout->addWidget(srcLabel); 0107 m_srcComboBox = new QComboBox(this); 0108 m_srcComboBox->setEditable(false); 0109 FOR_ALL_TAGS(tagNr) { 0110 m_srcComboBox->addItem(tr("Tag %1").arg(Frame::tagNumberToString(tagNr)), 0111 Frame::tagVersionFromNumber(tagNr)); 0112 } 0113 srcLabel->setBuddy(m_srcComboBox); 0114 butlayout->addWidget(m_srcComboBox); 0115 connect(m_srcComboBox, static_cast<void (QComboBox::*)(int)>( 0116 &QComboBox::activated), 0117 this, &ExportDialog::onSrcComboBoxActivated); 0118 0119 vlayout->addLayout(butlayout); 0120 0121 auto hlayout = new QHBoxLayout; 0122 auto helpButton = new QPushButton(tr("&Help"), this); 0123 helpButton->setAutoDefault(false); 0124 hlayout->addWidget(helpButton); 0125 connect(helpButton, &QAbstractButton::clicked, this, &ExportDialog::showHelp); 0126 0127 auto saveButton = new QPushButton(tr("&Save Settings"), this); 0128 saveButton->setAutoDefault(false); 0129 hlayout->addWidget(saveButton); 0130 connect(saveButton, &QAbstractButton::clicked, this, &ExportDialog::saveConfig); 0131 0132 auto hspacer = new QSpacerItem(16, 0, QSizePolicy::Expanding, 0133 QSizePolicy::Minimum); 0134 hlayout->addItem(hspacer); 0135 0136 auto closeButton = new QPushButton(tr("&Close"), this); 0137 closeButton->setAutoDefault(false); 0138 hlayout->addWidget(closeButton); 0139 connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept); 0140 0141 vlayout->addLayout(hlayout); 0142 } 0143 0144 /** 0145 * Export to a file. 0146 */ 0147 void ExportDialog::slotToFile() 0148 { 0149 if (QString fileName = m_platformTools->getSaveFileName( 0150 this, QString(), ImportConfig::instance().importDir(), 0151 QString(), nullptr); 0152 !fileName.isEmpty()) { 0153 if (!m_textExporter->exportToFile(fileName)) { 0154 QMessageBox::warning( 0155 nullptr, tr("File Error"), 0156 tr("Error while writing file:\n") + fileName, 0157 QMessageBox::Ok, QMessageBox::NoButton); 0158 } 0159 } 0160 } 0161 0162 /** 0163 * Export to clipboard. 0164 */ 0165 void ExportDialog::slotToClipboard() 0166 { 0167 QGuiApplication::clipboard()->setText(m_textExporter->getText(), 0168 QClipboard::Clipboard); 0169 } 0170 0171 /** 0172 * Show exported text as preview in editor. 0173 */ 0174 void ExportDialog::showPreview() 0175 { 0176 m_textExporter->updateText(m_formatListEdit->getCurrentFormat(1), 0177 m_formatListEdit->getCurrentFormat(2), 0178 m_formatListEdit->getCurrentFormat(3)); 0179 if (QString text(m_textExporter->getText()); 0180 m_textTableModel->setText( 0181 text, !m_formatListEdit->getCurrentFormat(1).isEmpty())) { 0182 m_table->resizeColumnsToContents(); 0183 m_table->show(); 0184 m_edit->hide(); 0185 } else { 0186 m_edit->setPlainText(text); 0187 m_table->hide(); 0188 m_edit->show(); 0189 } 0190 } 0191 0192 /** 0193 * Set the format combo box and line edits from the configuration. 0194 */ 0195 void ExportDialog::setFormatFromConfig() 0196 { 0197 const ExportConfig& exportCfg = ExportConfig::instance(); 0198 m_formatListEdit->setFormats( 0199 {exportCfg.exportFormatNames(), exportCfg.exportFormatHeaders(), 0200 exportCfg.exportFormatTracks(), exportCfg.exportFormatTrailers()}, 0201 exportCfg.exportFormatIndex()); 0202 } 0203 0204 /** 0205 * Read the local settings from the configuration. 0206 */ 0207 void ExportDialog::readConfig() 0208 { 0209 m_srcComboBox->setCurrentIndex( 0210 m_srcComboBox->findData(ExportConfig::instance().exportSource())); 0211 0212 setFormatFromConfig(); 0213 0214 if (!ExportConfig::instance().exportWindowGeometry().isEmpty()) { 0215 restoreGeometry(ExportConfig::instance().exportWindowGeometry()); 0216 } 0217 } 0218 0219 /** 0220 * Save the local settings to the configuration. 0221 */ 0222 void ExportDialog::saveConfig() 0223 { 0224 ExportConfig& exportCfg = ExportConfig::instance(); 0225 exportCfg.setExportSource(Frame::tagVersionCast( 0226 m_srcComboBox->itemData(m_srcComboBox->currentIndex()).toInt())); 0227 int idx; 0228 QList<QStringList> formats = m_formatListEdit->getFormats(&idx); 0229 exportCfg.setExportFormatIndex(idx); 0230 exportCfg.setExportFormatNames(formats.at(0)); 0231 exportCfg.setExportFormatHeaders(formats.at(1)); 0232 exportCfg.setExportFormatTracks(formats.at(2)); 0233 exportCfg.setExportFormatTrailers(formats.at(3)); 0234 exportCfg.setExportWindowGeometry(saveGeometry()); 0235 0236 setFormatFromConfig(); 0237 } 0238 0239 /** 0240 * Show help. 0241 */ 0242 void ExportDialog::showHelp() 0243 { 0244 ContextHelp::displayHelp(QLatin1String("export")); 0245 } 0246 0247 /** 0248 * Called when the source combo box selection is changed. 0249 * @param index combo box index 0250 */ 0251 void ExportDialog::onSrcComboBoxActivated(int index) 0252 { 0253 m_textExporter->readTagsInTrackData( 0254 Frame::tagVersionCast(m_srcComboBox->itemData(index).toInt())); 0255 showPreview(); 0256 }