File indexing completed on 2024-05-12 16:35:11

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "CSVDataCommand.h"
0021 
0022 #include <klocale.h>
0023 
0024 #include "CalculationSettings.h"
0025 #include "Map.h"
0026 #include "Sheet.h"
0027 #include "Value.h"
0028 #include "ValueConverter.h"
0029 
0030 using namespace Calligra::Sheets;
0031 
0032 CSVDataCommand::CSVDataCommand()
0033         : AbstractDataManipulator()
0034 {
0035 }
0036 
0037 CSVDataCommand::~CSVDataCommand()
0038 {
0039 }
0040 
0041 void CSVDataCommand::setValue(const Value& value)
0042 {
0043     m_value = value;
0044 }
0045 
0046 void CSVDataCommand::setColumnDataTypes(const QList<KoCsvImportDialog::DataType>& dataTypes)
0047 {
0048     m_dataTypes = dataTypes;
0049 }
0050 
0051 void CSVDataCommand::setDecimalSymbol(const QString& symbol)
0052 {
0053     m_decimalSymbol = symbol;
0054 }
0055 
0056 void CSVDataCommand::setThousandsSeparator(const QString& separator)
0057 {
0058     m_thousandsSeparator = separator;
0059 }
0060 
0061 Value CSVDataCommand::newValue(Element* element, int col, int row, bool* parse, Format::Type* fmtType)
0062 {
0063     Q_UNUSED(fmtType)
0064     const int colidx = col - element->rect().left();
0065     const int rowidx = row - element->rect().top();
0066 
0067     Value value;
0068     switch (m_dataTypes.value(colidx)) {
0069     case KoCsvImportDialog::Generic:
0070         value = m_value.element(colidx, rowidx);
0071         *parse = true;
0072         break;
0073     case KoCsvImportDialog::Text:
0074         value = m_value.element(colidx, rowidx);
0075         break;
0076     case KoCsvImportDialog::Date:
0077         value = m_sheet->map()->converter()->asDate(m_value.element(colidx, rowidx));
0078         break;
0079     case KoCsvImportDialog::Currency:
0080         value = m_sheet->map()->converter()->asFloat(m_value.element(colidx, rowidx));
0081         value.setFormat(Value::fmt_Money);
0082         break;
0083     case KoCsvImportDialog::None:
0084         break;
0085     }
0086     return value;
0087 }
0088 
0089 bool CSVDataCommand::wantChange(Element* element, int col, int row)
0090 {
0091     Q_UNUSED(row)
0092     return (m_dataTypes.value(col - element->rect().left()) != KoCsvImportDialog::None);
0093 }
0094 
0095 bool CSVDataCommand::preProcessing()
0096 {
0097     if (!AbstractDataManipulator::preProcessing())
0098         return false;
0099     // Initialize the decimal symbol and thousands separator to use for parsing.
0100     m_documentDecimalSymbol = m_sheet->map()->calculationSettings()->locale()->decimalSymbol();
0101     m_documentThousandsSeparator = m_sheet->map()->calculationSettings()->locale()->thousandsSeparator();
0102     m_sheet->map()->calculationSettings()->locale()->setDecimalSymbol(m_decimalSymbol);
0103     m_sheet->map()->calculationSettings()->locale()->setThousandsSeparator(m_thousandsSeparator);
0104     return true;
0105 }
0106 
0107 bool CSVDataCommand::postProcessing()
0108 {
0109     if (!AbstractDataManipulator::postProcessing())
0110         return false;
0111     // Restore the document's decimal symbol and thousands separator.
0112     m_sheet->map()->calculationSettings()->locale()->setDecimalSymbol(m_documentDecimalSymbol);
0113     m_sheet->map()->calculationSettings()->locale()->setThousandsSeparator(m_documentThousandsSeparator);
0114     m_documentDecimalSymbol.clear();
0115     m_documentThousandsSeparator.clear();
0116     return true;
0117 }