diff --git a/filters/sheets/csv/csvexport.cc b/filters/sheets/csv/csvexport.cc index 40c89ee1dac..254dd8a3e59 100644 --- a/filters/sheets/csv/csvexport.cc +++ b/filters/sheets/csv/csvexport.cc @@ -1,345 +1,347 @@ /* This file is part of the KDE project Copyright (C) 2000 David Faure Copyright (C) 2004 Nicolas GOUTTE This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include #include #include #include #include +#include -#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace Calligra::Sheets; K_PLUGIN_FACTORY_WITH_JSON(CSVExportFactory, "calligra_filter_sheets2csv.json", registerPlugin();) +Q_LOGGING_CATEGORY(lcCsvExport, "calligra.filter.csv.export") + class Cell { public: int row, col; QString text; bool operator < (const Cell & c) const { return row < c.row || (row == c.row && col < c.col); } bool operator == (const Cell & c) const { return row == c.row && col == c.col; } }; CSVExport::CSVExport(QObject* parent, const QVariantList &) : KoFilter(parent), m_eol("\n") { } QString CSVExport::exportCSVCell(const Calligra::Sheets::Doc* doc, Sheet const * const sheet, int col, int row, QChar const & textQuote, QChar csvDelimiter) { // This function, given a cell, returns a string corresponding to its export in CSV format // It proceeds by: // - getting the value of the cell, if any // - protecting quote characters within cells, if any // - enclosing the cell in quotes if the cell is non empty Q_UNUSED(doc); const Calligra::Sheets::Cell cell(sheet, col, row); QString text; if (!cell.isDefault() && !cell.isEmpty()) { if (cell.isFormula()) text = cell.displayText(); else if (!cell.link().isEmpty()) text = cell.userInput(); // untested else if (cell.isTime()) text = cell.value().asTime().toString("hh:mm:ss"); else if (cell.isDate()) text = cell.value().asDate(sheet->map()->calculationSettings()).toString("yyyy-MM-dd"); else text = cell.displayText(); } // quote only when needed (try to mimic excel) bool quote = false; if (!text.isEmpty()) { if (text.indexOf(textQuote) != -1) { QString doubleTextQuote(textQuote); doubleTextQuote.append(textQuote); text.replace(textQuote, doubleTextQuote); quote = true; } else if (text[0].isSpace() || text[text.length()-1].isSpace()) quote = true; else if (text.indexOf(csvDelimiter) != -1) quote = true; } if (quote) { text.prepend(textQuote); text.append(textQuote); } return text; } // The reason why we use the KoDocument* approach and not the QDomDocument // approach is because we don't want to export formulas but values ! KoFilter::ConversionStatus CSVExport::convert(const QByteArray & from, const QByteArray & to) { - kDebug(30501) << "CSVExport::convert"; + qDebug(lcCsvExport) << "CSVExport::convert"; KoDocument* document = m_chain->inputDocument(); if (!document) return KoFilter::StupidError; if (!qobject_cast(document)) { - kWarning(30501) << "document isn't a Calligra::Sheets::Doc but a " << document->metaObject()->className(); + qWarning(lcCsvExport) << "document isn't a Calligra::Sheets::Doc but a " << document->metaObject()->className(); return KoFilter::NotImplemented; } if ((to != "text/csv" && to != "text/plain") || from != "application/vnd.oasis.opendocument.spreadsheet") { - kWarning(30501) << "Invalid mimetypes " << to << " " << from; + qWarning(lcCsvExport) << "Invalid mimetypes " << to << " " << from; return KoFilter::NotImplemented; } Doc *ksdoc = qobject_cast(document); if (ksdoc->mimeType() != "application/vnd.oasis.opendocument.spreadsheet") { - kWarning(30501) << "Invalid document mimetype " << ksdoc->mimeType(); + qWarning(lcCsvExport) << "Invalid document mimetype " << ksdoc->mimeType(); return KoFilter::NotImplemented; } CSVExportDialog *expDialog = 0; if (!m_chain->manager()->getBatchMode()) { expDialog = new CSVExportDialog(0); if (!expDialog) { - kError(30501) << "Dialog has not been created! Aborting!" << endl; + qCritical(lcCsvExport) << "Dialog has not been created! Aborting!" << endl; return KoFilter::StupidError; } expDialog->fillSheet(ksdoc->map()); if (!expDialog->exec()) { delete expDialog; return KoFilter::UserCancelled; } } QTextCodec* codec = 0; QChar csvDelimiter; if (expDialog) { codec = expDialog->getCodec(); if (!codec) { delete expDialog; return KoFilter::StupidError; } csvDelimiter = expDialog->getDelimiter(); m_eol = expDialog->getEndOfLine(); } else { codec = QTextCodec::codecForName("UTF-8"); csvDelimiter = ','; } // Now get hold of the sheet to export // (Hey, this could be part of the dialog too, choosing which sheet to export.... // It's great to have parametrable filters... IIRC even MSOffice doesn't have that) // Ok, for now we'll use the first sheet - my document has only one sheet anyway ;-))) bool first = true; QString str; QChar textQuote; if (expDialog) textQuote = expDialog->getTextQuote(); else textQuote = '"'; if (expDialog && expDialog->exportSelectionOnly()) { - kDebug(30501) << "Export as selection mode"; + qDebug(lcCsvExport) << "Export as selection mode"; View *view = ksdoc->documentPart()->views().isEmpty() ? 0 : static_cast(ksdoc->documentPart()->views().first()); if (!view) { // no view if embedded document delete expDialog; return KoFilter::StupidError; } Sheet const * const sheet = view->activeSheet(); QRect selection = view->selection()->lastRange(); // Compute the highest row and column indexes (within the selection) // containing non-empty cells, respectively called CSVMaxRow CSVMaxCol. // The CSV will have CSVMaxRow rows, all with CSVMaxCol columns int right = selection.right(); int bottom = selection.bottom(); int CSVMaxRow = 0; int CSVMaxCol = 0; for (int idxRow = 1, row = selection.top(); row <= bottom; ++row, ++idxRow) { for (int idxCol = 1, col = selection.left(); col <= right; ++col, ++idxCol) { if (!Calligra::Sheets::Cell(sheet, col, row).isEmpty()) { if (idxRow > CSVMaxRow) CSVMaxRow = idxRow; if (idxCol > CSVMaxCol) CSVMaxCol = idxCol; } } } for (int idxRow = 1, row = selection.top(); row <= bottom && idxRow <= CSVMaxRow; ++row, ++idxRow) { int idxCol = 1; for (int col = selection.left(); col <= right && idxCol <= CSVMaxCol; ++col, ++idxCol) { str += exportCSVCell(ksdoc, sheet, col, row, textQuote, csvDelimiter); if (idxCol < CSVMaxCol) str += csvDelimiter; } // This is to deal with the case of non-rectangular selections for (; idxCol < CSVMaxCol; ++idxCol) str += csvDelimiter; str += m_eol; } } else { - kDebug(30501) << "Export as full mode"; + qDebug(lcCsvExport) << "Export as full mode"; foreach(Sheet const * const sheet, ksdoc->map()->sheetList()) { if (expDialog && !expDialog->exportSheet(sheet->sheetName())) { continue; } // Compute the highest row and column indexes containing non-empty cells, // respectively called CSVMaxRow CSVMaxCol. // The CSV will have CSVMaxRow rows, all with CSVMaxCol columns int sheetMaxRow = sheet->cellStorage()->rows(); int sheetMaxCol = sheet->cellStorage()->columns(); int CSVMaxRow = 0; int CSVMaxCol = 0; for (int row = 1 ; row <= sheetMaxRow ; ++row) { for (int col = 1 ; col <= sheetMaxCol ; col++) { if (!Calligra::Sheets::Cell(sheet, col, row).isEmpty()) { if (row > CSVMaxRow) CSVMaxRow = row; if (col > CSVMaxCol) CSVMaxCol = col; } } } // Skip the sheet altogether if it is empty if (CSVMaxRow + CSVMaxCol == 0) continue; - kDebug(30501) << "Max row x column:" << CSVMaxRow << " x" << CSVMaxCol; + qDebug(lcCsvExport) << "Max row x column:" << CSVMaxRow << " x" << CSVMaxCol; // Print sheet separators, except for the first sheet if (!first || (expDialog && expDialog->printAlwaysSheetDelimiter())) { if (!first) str += m_eol; QString name; if (expDialog) name = expDialog->getSheetDelimiter(); else name = "****************"; const QString tname(i18n("")); int pos = name.indexOf(tname); if (pos != -1) { name.replace(pos, tname.length(), sheet->sheetName()); } str += name + m_eol + m_eol; } first = false; // this is just a bad approximation which fails for documents with less than 50 rows, but // we don't need any progress stuff there anyway :) (Werner) int value = 0; int step = CSVMaxRow > 50 ? CSVMaxRow / 50 : 1; // Print the CSV for the sheet data for (int row = 1, i = 1 ; row <= CSVMaxRow ; ++row, ++i) { if (i > step) { value += 2; emit sigProgress(value); i = 0; } QString collect; // buffer delimiters while reading empty cells for (int col = 1 ; col <= CSVMaxCol ; col++) { const QString txt = exportCSVCell(ksdoc, sheet, col, row, textQuote, csvDelimiter); // if we encounter a non-empty cell, commit the buffered delimiters if (!txt.isEmpty()) { str += collect + txt; collect.clear(); } collect += csvDelimiter; } // Here, throw away buffered delimiters. They're trailing and therefore // superfluous. str += m_eol; } } } emit sigProgress(100); QFile out(m_chain->outputFile()); if (!out.open(QIODevice::WriteOnly)) { - kError(30501) << "Unable to open output file!" << endl; + qCritical(lcCsvExport) << "Unable to open output file!" << endl; out.close(); delete expDialog; return KoFilter::StupidError; } QTextStream outStream(&out); outStream.setCodec(codec); outStream << str; out.close(); delete expDialog; return KoFilter::OK; } #include diff --git a/filters/sheets/csv/csvexport.h b/filters/sheets/csv/csvexport.h index ae053d415c7..f90b56207d6 100644 --- a/filters/sheets/csv/csvexport.h +++ b/filters/sheets/csv/csvexport.h @@ -1,56 +1,60 @@ /* This file is part of the KDE project Copyright (C) 2000 David Faure Copyright (C) 2004 Nicolas GOUTTE This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef CSVEXPORT_H #define CSVEXPORT_H #include #include #include #include +#include +#include + +Q_DECLARE_LOGGING_CATEGORY(lcCsvExport) namespace Calligra { namespace Sheets { class Sheet; class Doc; } } class CSVExport : public KoFilter { Q_OBJECT public: CSVExport(QObject* parent, const QVariantList &); virtual ~CSVExport() {} virtual KoFilter::ConversionStatus convert(const QByteArray & from, const QByteArray & to); private: QString exportCSVCell(const Calligra::Sheets::Doc* doc, Calligra::Sheets::Sheet const * const sheet, int col, int row, QChar const & textQuote, QChar csvDelimiter); private: QString m_eol; ///< End of line (LF, CR or CRLF) }; #endif // CSVEXPORT_H diff --git a/filters/sheets/csv/csvexportdialog.cpp b/filters/sheets/csv/csvexportdialog.cpp index 9b361dcaf45..383ebe59f6f 100644 --- a/filters/sheets/csv/csvexportdialog.cpp +++ b/filters/sheets/csv/csvexportdialog.cpp @@ -1,306 +1,307 @@ /* This file is part of the KDE project Copyright (C) 1999 David Faure Copyright (C) 2004 Nicolas GOUTTE This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "csvexportdialog.h" +#include "csvexport.h" #include "sheets/Map.h" #include "sheets/Sheet.h" #include #include #include -#include -#include +#include #include #include +#include using namespace Calligra::Sheets; CSVExportDialog::CSVExportDialog(QWidget * parent) : KoDialog(parent), m_dialog(new ExportDialogUI(this)), m_delimiter(","), m_textquote('"') { setButtons(KoDialog::Ok | KoDialog::Cancel); setDefaultButton(KoDialog::Ok); - QApplication::restoreOverrideCursor(); + QGuiApplication::restoreOverrideCursor(); QStringList encodings; encodings << i18nc("Descriptive encoding name", "Recommended ( %1 )" , "UTF-8"); encodings << i18nc("Descriptive encoding name", "Locale ( %1 )" , QString(QTextCodec::codecForLocale()->name())); encodings += KCharsets::charsets()->descriptiveEncodingNames(); // Add a few non-standard encodings, which might be useful for text files const QString description(i18nc("Descriptive encoding name", "Other ( %1 )")); encodings << description.arg("Apple Roman"); // Apple encodings << description.arg("IBM 850") << description.arg("IBM 866"); // MS DOS encodings << description.arg("CP 1258"); // Windows m_dialog->comboBoxEncoding->addItems(encodings); setMainWidget(m_dialog); // Invalid 'Other' delimiters // - Quotes // - CR,LF,Vertical-tab,Formfeed,ASCII bel QRegExp rx("^[^\"'\r\n\v\f\a]{0,1}$"); m_delimiterValidator = new QRegExpValidator(rx, m_dialog->m_delimiterBox); m_dialog->m_delimiterEdit->setValidator(m_delimiterValidator); connect(m_dialog->m_delimiterBox, SIGNAL(clicked(int)), this, SLOT(delimiterClicked(int))); connect(m_dialog->m_delimiterEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); connect(m_dialog->m_delimiterEdit, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString))); connect(m_dialog->m_comboQuote, SIGNAL(activated(QString)), this, SLOT(textquoteSelected(QString))); connect(m_dialog->m_selectionOnly, SIGNAL(toggled(bool)), this, SLOT(selectionOnlyChanged(bool))); connect(this, SIGNAL(okClicked()), SLOT(slotOk())); connect(this, SIGNAL(cancelClicked()), this, SLOT(slotCancel())); loadSettings(); } CSVExportDialog::~CSVExportDialog() { saveSettings(); - QApplication::setOverrideCursor(Qt::WaitCursor); + QGuiApplication::setOverrideCursor(Qt::WaitCursor); delete m_delimiterValidator; } void CSVExportDialog::loadSettings() { KConfigGroup configGroup = KSharedConfig::openConfig()->group("CSVDialog Settings"); m_textquote = configGroup.readEntry("textQuote", "\"")[0]; m_delimiter = configGroup.readEntry("delimiter", ","); const QString codecText = configGroup.readEntry("codec", ""); bool selectionOnly = configGroup.readEntry("selectionOnly", false); const QString sheetDelim = configGroup.readEntry("sheetDelimiter", m_dialog->m_sheetDelimiter->text()); bool delimAbove = configGroup.readEntry("sheetDelimiterAbove", false); const QString eol = configGroup.readEntry("eol", "\r\n"); // update widgets if (!codecText.isEmpty()) { m_dialog->comboBoxEncoding->setCurrentIndex(m_dialog->comboBoxEncoding->findText(codecText)); } if (m_delimiter == ",") m_dialog->m_radioComma->setChecked(true); else if (m_delimiter == "\t") m_dialog->m_radioTab->setChecked(true); else if (m_delimiter == " ") m_dialog->m_radioSpace->setChecked(true); else if (m_delimiter == ";") m_dialog->m_radioSemicolon->setChecked(true); else { m_dialog->m_radioOther->setChecked(true); m_dialog->m_delimiterEdit->setText(m_delimiter); } m_dialog->m_comboQuote->setCurrentIndex(m_textquote == '\'' ? 1 : m_textquote == '"' ? 0 : 2); m_dialog->m_selectionOnly->setChecked(selectionOnly); m_dialog->m_sheetDelimiter->setText(sheetDelim); m_dialog->m_delimiterAboveAll->setChecked(delimAbove); if (eol == "\r\n") m_dialog->radioEndOfLineCRLF->setChecked(true); else if (eol == "\r") m_dialog->radioEndOfLineCR->setChecked(true); else m_dialog->radioEndOfLineLF->setChecked(true); } void CSVExportDialog::saveSettings() { KConfigGroup configGroup = KSharedConfig::openConfig()->group("CSVDialog Settings"); configGroup.writeEntry("textQuote", QString(m_textquote)); configGroup.writeEntry("delimiter", m_delimiter); configGroup.writeEntry("codec", m_dialog->comboBoxEncoding->currentText()); configGroup.writeEntry("selectionOnly", exportSelectionOnly()); configGroup.writeEntry("sheetDelimiter", getSheetDelimiter()); configGroup.writeEntry("sheetDelimiterAbove", printAlwaysSheetDelimiter()); configGroup.writeEntry("eol", getEndOfLine()); configGroup.sync(); } void CSVExportDialog::fillSheet(Map * map) { m_dialog->m_sheetList->clear(); QListWidgetItem *item; foreach(Sheet* sheet, map->sheetList()) { item = new QListWidgetItem(sheet->sheetName() ,m_dialog->m_sheetList); item->setCheckState(Qt::Checked); m_dialog->m_sheetList->addItem(item); } } QChar CSVExportDialog::getDelimiter() const { return m_delimiter[0]; } QChar CSVExportDialog::getTextQuote() const { return m_textquote; } bool CSVExportDialog::printAlwaysSheetDelimiter() const { return m_dialog->m_delimiterAboveAll->isChecked(); } QString CSVExportDialog::getSheetDelimiter() const { return m_dialog->m_sheetDelimiter->text(); } bool CSVExportDialog::exportSheet(QString const & sheetName) const { for (int i = 0; i < m_dialog->m_sheetList->count(); ++i) { QListWidgetItem *const item = m_dialog->m_sheetList->item(i); if (item->checkState() == Qt::Checked) { if (item->text() == sheetName) { return true; } } } return false; } void CSVExportDialog::slotOk() { accept(); } void CSVExportDialog::slotCancel() { reject(); } void CSVExportDialog::returnPressed() { if (!m_dialog->m_radioOther->isChecked()) return; m_delimiter = m_dialog->m_delimiterEdit->text(); } void CSVExportDialog::textChanged(const QString &) { if (m_dialog->m_delimiterEdit->text().isEmpty()) { enableButtonOk(! m_dialog->m_radioOther->isChecked()); return; } m_dialog->m_radioOther->setChecked(true); delimiterClicked(4); } void CSVExportDialog::delimiterClicked(int id) { enableButtonOk(true); //Erase "Other Delimiter" text box if the user has selected one of //the standard options instead (comma, semicolon, tab or space) if (id != 4) m_dialog->m_delimiterEdit->setText(""); switch (id) { case 0: // comma m_delimiter = ","; break; case 1: // semicolon m_delimiter = ";"; break; case 2: // tab m_delimiter = "\t"; break; case 3: // space m_delimiter = " "; break; case 4: // other enableButtonOk(! m_dialog->m_delimiterEdit->text().isEmpty()); m_delimiter = m_dialog->m_delimiterEdit->text(); break; } } void CSVExportDialog::textquoteSelected(const QString & mark) { m_textquote = mark[0]; } void CSVExportDialog::selectionOnlyChanged(bool on) { m_dialog->m_sheetList->setEnabled(!on); m_dialog->m_delimiterLineBox->setEnabled(!on); if (on) m_dialog->m_tabWidget->setCurrentIndex(1); } bool CSVExportDialog::exportSelectionOnly() const { return m_dialog->m_selectionOnly->isChecked(); } QTextCodec* CSVExportDialog::getCodec(void) const { const QString strCodec(KCharsets::charsets()->encodingForName(m_dialog->comboBoxEncoding->currentText())); - kDebug(30502) << "Encoding:" << strCodec; + qDebug(lcCsvExport) << "Encoding:" << strCodec; bool ok = false; QTextCodec* codec = QTextCodec::codecForName(strCodec.toUtf8()); // If QTextCodec has not found a valid encoding, so try with KCharsets. if (codec) { ok = true; } else { codec = KCharsets::charsets()->codecForName(strCodec, ok); } // Still nothing? if (!codec || !ok) { // Default: UTF-8 - kWarning(30502) << "Cannot find encoding:" << strCodec; + qWarning(lcCsvExport) << "Cannot find encoding:" << strCodec; // ### TODO: what parent to use? KMessageBox::error(0, i18n("Cannot find encoding: %1", strCodec)); return 0; } return codec; } QString CSVExportDialog::getEndOfLine(void) const { QString strReturn; if (m_dialog->radioEndOfLineLF->isChecked()) strReturn = "\n"; else if (m_dialog->radioEndOfLineCRLF->isChecked()) strReturn = "\r\n"; else if (m_dialog->radioEndOfLineCR->isChecked()) strReturn = "\r"; else strReturn = "\n"; return strReturn; } diff --git a/filters/sheets/csv/csvimport.cc b/filters/sheets/csv/csvimport.cc index 2a12da4720e..89fb7162e00 100644 --- a/filters/sheets/csv/csvimport.cc +++ b/filters/sheets/csv/csvimport.cc @@ -1,208 +1,210 @@ /* This file is part of the KDE project Copyright (C) 1999 David Faure Copyright (C) 2004 Nicolas GOUTTE This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "csvimport.h" #include #include #include #include -#include +#include +#include #include -#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace Calligra::Sheets; // hehe >:-> /* 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();) +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(document)) { - kWarning(30501) << "document isn't a Calligra::Sheets::Doc but a " << document->metaObject()->className(); + qWarning(lcCsvImport) << "document isn't a Calligra::Sheets::Doc but a " << document->metaObject()->className(); return KoFilter::NotImplemented; } if ((from != "text/csv" && from != "text/plain") || to != "application/vnd.oasis.opendocument.spreadsheet") { - kWarning(30501) << "Invalid mimetypes " << from << " " << to; + qWarning(lcCsvImport) << "Invalid mimetypes " << from << " " << to; return KoFilter::NotImplemented; } Doc *ksdoc = static_cast(document); // type checked above // if (ksdoc->mimeType() != "application/vnd.oasis.opendocument.spreadsheet") { -// kWarning(30501) << "Invalid document mimetype " << ksdoc->mimeType(); +// qWarning(lcCsvImport) << "Invalid document mimetype " << ksdoc->mimeType(); // return KoFilter::NotImplemented; // } QFile in(file); if (!in.open(QIODevice::ReadOnly)) { KMessageBox::sorry(0L, i18n("CSV filter cannot open input file - please report.")); in.close(); return KoFilter::FileNotFound; } QString csv_delimiter; // ###### FIXME: disabled for now //if (!config.isNull()) // csv_delimiter = config[0]; QByteArray inputFile(in.readAll()); in.close(); KoCsvImportDialog* dialog = new KoCsvImportDialog(0); dialog->setData(inputFile); dialog->setDecimalSymbol(ksdoc->map()->calculationSettings()->locale()->decimalSymbol()); dialog->setThousandsSeparator(ksdoc->map()->calculationSettings()->locale()->thousandsSeparator()); 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 = 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. const QString documentDecimalSymbol = ksdoc->map()->calculationSettings()->locale()->decimalSymbol(); const QString documentThousandsSeparator = ksdoc->map()->calculationSettings()->locale()->thousandsSeparator(); ksdoc->map()->calculationSettings()->locale()->setDecimalSymbol(dialog->decimalSymbol()); ksdoc->map()->calculationSettings()->locale()->setThousandsSeparator(dialog->thousandsSeparator()); int step = 100 / numRows * numCols; int value = 0; emit sigProgress(value); - QApplication::setOverrideCursor(Qt::WaitCursor); + QGuiApplication::setOverrideCursor(Qt::WaitCursor); const double defaultWidth = ksdoc->map()->defaultColumnFormat()->width(); QVector widths(numCols); for (int i = 0; i < numCols; ++i) widths[i] = defaultWidth; Cell cell(sheet, 1, 1); QFontMetrics fm(cell.style().font()); for (int row = 0; row < numRows; ++row) { for (int col = 0; col < numCols; ++col) { value += step; emit sigProgress(value); const QString text(dialog->text(row, col)); // ### FIXME: how to calculate the width of numbers (as they might not be in the right format) const double len = fm.width(text); if (len > widths[col]) widths[col] = len; cell = Cell(sheet, col + 1, row + 1); switch (dialog->dataType(col)) { case KoCsvImportDialog::Generic: default: { cell.parseUserInput(text); break; } case KoCsvImportDialog::Text: { Value value(text); cell.setValue(value); cell.setUserInput(ksdoc->map()->converter()->asString(value).asString()); break; } case KoCsvImportDialog::Date: { Value value(text); cell.setValue(ksdoc->map()->converter()->asDate(value)); cell.setUserInput(ksdoc->map()->converter()->asString(value).asString()); break; } case KoCsvImportDialog::Currency: { Value value(text); value.setFormat(Value::fmt_Money); cell.setValue(value); cell.setUserInput(ksdoc->map()->converter()->asString(value).asString()); break; } case KoCsvImportDialog::None: { // just skip the content break; } } } } emit sigProgress(98); for (int i = 0; i < numCols; ++i) { if (widths[i] > defaultWidth) sheet->nonDefaultColumnFormat(i + 1)->setWidth(widths[i]); } // Restore the document's decimal symbol and thousands separator. ksdoc->map()->calculationSettings()->locale()->setDecimalSymbol(documentDecimalSymbol); ksdoc->map()->calculationSettings()->locale()->setThousandsSeparator(documentThousandsSeparator); emit sigProgress(100); - QApplication::restoreOverrideCursor(); + QGuiApplication::restoreOverrideCursor(); delete dialog; return KoFilter::OK; } #include