1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/* This file is part of the KDE project
SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org>
SPDX-FileCopyrightText: 2004 Nicolas GOUTTE <goutte@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "csvimport.h"
#include <QFile>
#include <QGuiApplication>
#include <KMessageBox>
#include <KPluginFactory>
#include <KoCsvImportDialog.h>
#include <KoFilterChain.h>
#include <KoFilterManager.h>
#include <sheets/engine/ElapsedTime_p.h>
#include <sheets/engine/CalculationSettings.h>
#include <sheets/engine/Localization.h>
#include <sheets/engine/Value.h>
#include <sheets/engine/ValueConverter.h>
#include <sheets/core/Cell.h>
#include <sheets/core/ColFormatStorage.h>
#include <sheets/core/DocBase.h>
#include <sheets/core/Map.h>
#include <sheets/core/Sheet.h>
using namespace Calligra::Sheets;
/*
To generate a test CSV file:
perl -e '$i=0;while($i<30000) { print rand().",".rand()."\n"; $i++ }' > file.csv
*/
K_PLUGIN_FACTORY_WITH_JSON(CSVImportFactory, "calligra_filter_csv2sheets.json", registerPlugin<CSVFilter>();)
Q_LOGGING_CATEGORY(lcCsvImport, "calligra.filter.csv.import")
CSVFilter::CSVFilter(QObject* parent, const QVariantList&) :
KoFilter(parent)
{
}
KoFilter::ConversionStatus CSVFilter::convert(const QByteArray& from, const QByteArray& to)
{
QString file(m_chain->inputFile());
KoDocument* document = m_chain->outputDocument();
if (!document)
return KoFilter::StupidError;
if (!qobject_cast<const Calligra::Sheets::DocBase *>(document)) {
qWarning(lcCsvImport) << "document isn't a Calligra::Sheets::Doc but a " << document->metaObject()->className();
return KoFilter::NotImplemented;
}
if ((from != "text/csv" && from != "text/tab-separated-values" && from != "text/plain") || to != "application/vnd.oasis.opendocument.spreadsheet") {
qWarning(lcCsvImport) << "Invalid mimetypes " << from << " " << to;
return KoFilter::NotImplemented;
}
DocBase *ksdoc = dynamic_cast<DocBase *>(document); // type checked above
// if (ksdoc->mimeType() != "application/vnd.oasis.opendocument.spreadsheet") {
// qWarning(lcCsvImport) << "Invalid document mimetype " << ksdoc->mimeType();
// return KoFilter::NotImplemented;
// }
QFile in(file);
if (!in.open(QIODevice::ReadOnly)) {
KMessageBox::error(nullptr, i18n("CSV filter cannot open input file - please report."));
in.close();
return KoFilter::FileNotFound;
}
// ###### FIXME: disabled for now
//QString csv_delimiter;
//if (!config.isNull())
// csv_delimiter = config[0];
QByteArray inputFile(in.readAll());
in.close();
Localization *locale = ksdoc->map()->calculationSettings()->locale();
ValueConverter *conv = ksdoc->map()->converter();
QString decimal = locale->decimalSymbol();
QString thousands = locale->thousandsSeparator();
KoCsvImportDialog* dialog = new KoCsvImportDialog(nullptr);
dialog->setData(inputFile);
dialog->setDecimalSymbol(decimal);
dialog->setThousandsSeparator(thousands);
if (!m_chain->manager()->getBatchMode() && !dialog->exec())
return KoFilter::UserCancelled;
inputFile.resize(0); // Release memory (input file content)
ElapsedTime t("Filling data into document");
Sheet *sheet = dynamic_cast<Sheet *>(ksdoc->map()->addNewSheet());
int numRows = dialog->rows();
int numCols = dialog->cols();
if (numRows == 0)
++numRows;
// Initialize the decimal symbol and thousands separator to use for parsing.
decimal = dialog->decimalSymbol();
thousands = dialog->thousandsSeparator();
int value = 0;
Q_EMIT sigProgress(value);
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
Cell cell(sheet, 1, 1);
int processed = 0;
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
processed++;
int progress = (int) (100.0 * processed / (numRows * numCols));
if (progress != value) {
value = progress;
Q_EMIT sigProgress(value);
}
const QString text(dialog->text(row, col));
cell = Cell(sheet, col + 1, row + 1);
// TODO - try to format numbers
switch (dialog->dataType(col)) {
case KoCsvImportDialog::Generic:
default: {
Value val;
// Is this a valid number?
QString numtext = text;
numtext = numtext.replace(' ', QString());
numtext = numtext.replace(thousands, QString());
numtext = numtext.replace(decimal, ".");
bool ok = false;
double num = numtext.toDouble(&ok);
if (ok) val = Value(num);
if (!val.isEmpty())
cell.setCellValue(val);
else
cell.parseUserInput(text);
break;
}
case KoCsvImportDialog::Text: {
Value value(text);
cell.setCellValue(value);
break;
}
case KoCsvImportDialog::Date: {
Value value(text);
cell.setCellValue(conv->asDate(value));
break;
}
case KoCsvImportDialog::Currency: {
Value value(text);
value = conv->asNumeric(value);
value.setFormat(Value::fmt_Money);
cell.setCellValue(value);
break;
}
case KoCsvImportDialog::None: {
// just skip the content
break;
}
}
}
}
Q_EMIT sigProgress(100);
QGuiApplication::restoreOverrideCursor();
delete dialog;
return KoFilter::OK;
}
#include <csvimport.moc>
|