1 /* This file is part of the KDE project
2 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "CSVDataCommand.h"
21
22 #include <klocale.h>
23
24 #include "CalculationSettings.h"
25 #include "Map.h"
26 #include "Sheet.h"
27 #include "Value.h"
28 #include "ValueConverter.h"
29
30 using namespace Calligra::Sheets;
31
CSVDataCommand()32 CSVDataCommand::CSVDataCommand()
33 : AbstractDataManipulator()
34 {
35 }
36
~CSVDataCommand()37 CSVDataCommand::~CSVDataCommand()
38 {
39 }
40
setValue(const Value & value)41 void CSVDataCommand::setValue(const Value& value)
42 {
43 m_value = value;
44 }
45
setColumnDataTypes(const QList<KoCsvImportDialog::DataType> & dataTypes)46 void CSVDataCommand::setColumnDataTypes(const QList<KoCsvImportDialog::DataType>& dataTypes)
47 {
48 m_dataTypes = dataTypes;
49 }
50
setDecimalSymbol(const QString & symbol)51 void CSVDataCommand::setDecimalSymbol(const QString& symbol)
52 {
53 m_decimalSymbol = symbol;
54 }
55
setThousandsSeparator(const QString & separator)56 void CSVDataCommand::setThousandsSeparator(const QString& separator)
57 {
58 m_thousandsSeparator = separator;
59 }
60
newValue(Element * element,int col,int row,bool * parse,Format::Type * fmtType)61 Value CSVDataCommand::newValue(Element* element, int col, int row, bool* parse, Format::Type* fmtType)
62 {
63 Q_UNUSED(fmtType)
64 const int colidx = col - element->rect().left();
65 const int rowidx = row - element->rect().top();
66
67 Value value;
68 switch (m_dataTypes.value(colidx)) {
69 case KoCsvImportDialog::Generic:
70 value = m_value.element(colidx, rowidx);
71 *parse = true;
72 break;
73 case KoCsvImportDialog::Text:
74 value = m_value.element(colidx, rowidx);
75 break;
76 case KoCsvImportDialog::Date:
77 value = m_sheet->map()->converter()->asDate(m_value.element(colidx, rowidx));
78 break;
79 case KoCsvImportDialog::Currency:
80 value = m_sheet->map()->converter()->asFloat(m_value.element(colidx, rowidx));
81 value.setFormat(Value::fmt_Money);
82 break;
83 case KoCsvImportDialog::None:
84 break;
85 }
86 return value;
87 }
88
wantChange(Element * element,int col,int row)89 bool CSVDataCommand::wantChange(Element* element, int col, int row)
90 {
91 Q_UNUSED(row)
92 return (m_dataTypes.value(col - element->rect().left()) != KoCsvImportDialog::None);
93 }
94
preProcessing()95 bool CSVDataCommand::preProcessing()
96 {
97 if (!AbstractDataManipulator::preProcessing())
98 return false;
99 // Initialize the decimal symbol and thousands separator to use for parsing.
100 m_documentDecimalSymbol = m_sheet->map()->calculationSettings()->locale()->decimalSymbol();
101 m_documentThousandsSeparator = m_sheet->map()->calculationSettings()->locale()->thousandsSeparator();
102 m_sheet->map()->calculationSettings()->locale()->setDecimalSymbol(m_decimalSymbol);
103 m_sheet->map()->calculationSettings()->locale()->setThousandsSeparator(m_thousandsSeparator);
104 return true;
105 }
106
postProcessing()107 bool CSVDataCommand::postProcessing()
108 {
109 if (!AbstractDataManipulator::postProcessing())
110 return false;
111 // Restore the document's decimal symbol and thousands separator.
112 m_sheet->map()->calculationSettings()->locale()->setDecimalSymbol(m_documentDecimalSymbol);
113 m_sheet->map()->calculationSettings()->locale()->setThousandsSeparator(m_documentThousandsSeparator);
114 m_documentDecimalSymbol.clear();
115 m_documentThousandsSeparator.clear();
116 return true;
117 }
118