1 /**
2 * \file textimportdialog.cpp
3 * Dialog to import from a text (file or clipboard).
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 19 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 "textimportdialog.h"
28 #include <QHBoxLayout>
29 #include <QPushButton>
30 #include <QFile>
31 #include <QFileInfo>
32 #include <QDir>
33 #include <QClipboard>
34 #include <QTextStream>
35 #include <QApplication>
36 #include "config.h"
37 #include "iplatformtools.h"
38 #include "textimporter.h"
39 #include "importparser.h"
40 #include "importconfig.h"
41 #include "contexthelp.h"
42 #include "formatlistedit.h"
43
44 /**
45 * Constructor.
46 *
47 * @param platformTools platform tools
48 * @param parent parent widget
49 * @param trackDataModel track data to be filled with imported values
50 */
TextImportDialog(IPlatformTools * platformTools,QWidget * parent,TrackDataModel * trackDataModel)51 TextImportDialog::TextImportDialog(IPlatformTools* platformTools,
52 QWidget* parent,
53 TrackDataModel* trackDataModel)
54 : QDialog(parent), m_platformTools(platformTools),
55 m_textImporter(new TextImporter(trackDataModel))
56 {
57 setObjectName(QLatin1String("TextImportDialog"));
58 setWindowTitle(tr("Import from File/Clipboard"));
59 setSizeGripEnabled(true);
60
61 auto vboxLayout = new QVBoxLayout(this);
62
63 QString formatToolTip = ImportParser::getFormatToolTip();
64 m_formatListEdit = new FormatListEdit(
65 {tr("Format:"), tr("Header:"), tr("Tracks:")},
66 {QString(), formatToolTip, formatToolTip},
67 this);
68 vboxLayout->addWidget(m_formatListEdit);
69
70 auto buttonLayout = new QHBoxLayout;
71 QPushButton* helpButton = new QPushButton(tr("&Help"), this);
72 helpButton->setAutoDefault(false);
73 buttonLayout->addWidget(helpButton);
74 connect(helpButton, &QAbstractButton::clicked, this, &TextImportDialog::showHelp);
75 QPushButton* saveButton = new QPushButton(tr("&Save Settings"), this);
76 saveButton->setAutoDefault(false);
77 buttonLayout->addWidget(saveButton);
78 connect(saveButton, &QAbstractButton::clicked, this, &TextImportDialog::saveConfig);
79 buttonLayout->addStretch();
80 QPushButton* fileButton = new QPushButton(tr("From F&ile..."), this);
81 fileButton->setAutoDefault(false);
82 buttonLayout->addWidget(fileButton);
83 connect(fileButton, &QAbstractButton::clicked, this, &TextImportDialog::fromFile);
84 QPushButton* clipButton = new QPushButton(tr("From Clip&board"), this);
85 clipButton->setAutoDefault(false);
86 buttonLayout->addWidget(clipButton);
87 connect(clipButton, &QAbstractButton::clicked, this, &TextImportDialog::fromClipboard);
88 QPushButton* closeButton = new QPushButton(tr("&Close"), this);
89 closeButton->setAutoDefault(false);
90 buttonLayout->addWidget(closeButton);
91 connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept);
92 vboxLayout->addLayout(buttonLayout);
93 }
94
95 /**
96 * Destructor.
97 */
~TextImportDialog()98 TextImportDialog::~TextImportDialog()
99 {
100 // Must not be inline because of forwared declared QScopedPointer.
101 }
102
103 /**
104 * Clear dialog data.
105 */
clear()106 void TextImportDialog::clear()
107 {
108 setFormatFromConfig();
109 }
110
111 /**
112 * Set the format combo box and line edits from the configuration.
113 */
setFormatFromConfig()114 void TextImportDialog::setFormatFromConfig()
115 {
116 const ImportConfig& importCfg = ImportConfig::instance();
117 m_formatListEdit->setFormats(
118 {importCfg.importFormatNames(), importCfg.importFormatHeaders(),
119 importCfg.importFormatTracks()},
120 importCfg.importFormatIndex());
121 }
122
123 /**
124 * Import from a file.
125 *
126 * @param fn file name
127 *
128 * @return true if ok.
129 */
importFromFile(const QString & fn)130 bool TextImportDialog::importFromFile(const QString& fn)
131 {
132 if (!fn.isEmpty()) {
133 QFile file(fn);
134 if (file.open(QIODevice::ReadOnly)) {
135 ImportConfig::instance().setImportDir(QFileInfo(file).dir().path());
136 QTextStream stream(&file);
137 QString text = stream.readAll();
138 if (!text.isNull() &&
139 m_textImporter->updateTrackData(
140 text,
141 m_formatListEdit->getCurrentFormat(1),
142 m_formatListEdit->getCurrentFormat(2))) {
143 emit trackDataUpdated();
144 }
145 file.close();
146 return true;
147 }
148 }
149 return false;
150 }
151
152 /**
153 * Let user select file, assign file contents to text and preview in
154 * table.
155 */
fromFile()156 void TextImportDialog::fromFile()
157 {
158 importFromFile(m_platformTools->getOpenFileName(this, QString(),
159 ImportConfig::instance().importDir(), QString(), nullptr)
160 );
161 }
162
163 /**
164 * Assign clipboard contents to text and preview in table.
165 */
fromClipboard()166 void TextImportDialog::fromClipboard()
167 {
168 QClipboard* cb = QApplication::clipboard();
169 QString text = cb->text(QClipboard::Clipboard);
170 if (text.isNull())
171 text = cb->text(QClipboard::Selection);
172 if (!text.isNull() &&
173 m_textImporter->updateTrackData(
174 text,
175 m_formatListEdit->getCurrentFormat(1),
176 m_formatListEdit->getCurrentFormat(2)))
177 emit trackDataUpdated();
178 }
179
180 /**
181 * Save the local settings to the configuration.
182 */
saveConfig()183 void TextImportDialog::saveConfig()
184 {
185 ImportConfig& importCfg = ImportConfig::instance();
186 int idx;
187 QList<QStringList> formats = m_formatListEdit->getFormats(&idx);
188 importCfg.setImportFormatIndex(idx);
189 importCfg.setImportFormatNames(formats.at(0));
190 importCfg.setImportFormatHeaders(formats.at(1));
191 importCfg.setImportFormatTracks(formats.at(2));
192
193 setFormatFromConfig();
194 }
195
196 /**
197 * Show help.
198 */
showHelp()199 void TextImportDialog::showHelp()
200 {
201 ContextHelp::displayHelp(QLatin1String("import-text"));
202 }
203