26#include <tdeapplication.h>
27#include <tdemessagebox.h>
29#include <kgenericfactory.h>
31#include <KoFilterChain.h>
32#include <KoFilterManager.h>
33#include <kspread_doc.h>
34#include <kspread_global.h>
35#include <kspread_map.h>
36#include <kspread_sheet.h>
37#include <kspread_style.h>
38#include <kspread_style_manager.h>
39#include <kspread_cell.h>
43using namespace KSpread;
53typedef KGenericFactory<CSVFilter, KoFilter> CSVImportFactory;
54K_EXPORT_COMPONENT_FACTORY( libcsvimport, CSVImportFactory(
"kofficefilters" ) )
56CSVFilter::CSVFilter(KoFilter *, const
char*, const TQStringList&) :
60KoFilter::ConversionStatus CSVFilter::convert(
const TQCString& from,
const TQCString& to )
62 TQString file( m_chain->inputFile() );
63 KoDocument* document = m_chain->outputDocument();
66 return KoFilter::StupidError;
68 kdDebug(30501) <<
"here we go... " << document->className() << endl;
70 if ( !::tqt_cast<const KSpread::Doc *>( document ) )
72 kdWarning(30501) <<
"document isn't a KSpread::Doc but a " << document->className() << endl;
73 return KoFilter::NotImplemented;
75 if(from!=
"text/x-csv" && from!=
"text/plain" || to!=
"application/x-kspread")
77 kdWarning(30501) <<
"Invalid mimetypes " << from <<
" " << to << endl;
78 return KoFilter::NotImplemented;
81 kdDebug(30501) <<
"...still here..." << endl;
83 Doc *ksdoc =
static_cast<Doc *
>( document );
85 if(ksdoc->mimeType()!=
"application/x-kspread")
87 kdWarning(30501) <<
"Invalid document mimetype " << ksdoc->mimeType() << endl;
88 return KoFilter::NotImplemented;
92 if(!in.open(IO_ReadOnly)) {
93 KMessageBox::sorry( 0L, i18n(
"CSV filter cannot open input file - please report.") );
95 return KoFilter::FileNotFound;
98 TQString csv_delimiter;
103 TQByteArray inputFile( in.readAll() );
106 CSVDialog *dialog =
new CSVDialog(0L, inputFile, csv_delimiter );
107 if (!m_chain->manager()->getBatchMode() && !dialog->exec())
108 return KoFilter::UserCancelled;
109 inputFile.resize( 0 );
111 ElapsedTime t(
"Filling data into document" );
114 Sheet *sheet=ksdoc->map()->addNewSheet();
116 int numRows = dialog->getRows();
117 int numCols = dialog->getCols();
122 int step = 100 / numRows * numCols;
125 emit sigProgress(value);
126 TQApplication::setOverrideCursor(TQt::waitCursor);
129 double init = sheet->nonDefaultColumnFormat( 1 )->dblWidth();
130 TQMemArray<double> widths( numCols );
131 for ( i = 0; i < numCols; ++i )
134 Cell* c = sheet->nonDefaultCell( 1, 1 );
135 TQFontMetrics fm( c->format()->textFont( 1, 1 ) );
137 Style * s = ksdoc->styleManager()->defaultStyle();
139 for (
int row = 0; row < numRows; ++row )
141 for (
int col = 0; col < numCols; ++col)
144 emit sigProgress(value);
145 const TQString text( dialog->getText( row, col ) );
148 const double len = fm.width( text );
149 if ( len > widths[col] )
152 switch (dialog->getHeader(col))
154 case CSVDialog::TEXT:
160 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
161 cell->setCellText( text );
163 cell->format()->setFormatType (Generic_format);
171 case CSVDialog::NUMBER:
174 double d = ksdoc->locale()->readNumber( text, &ok );
177 d = text.toDouble( &ok );
180 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
181 cell->setCellText( text,
true );
185 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
186 cell->setNumber( d );
188 cell->format()->setPrecision( 2 );
191 case CSVDialog::COMMANUMBER:
194 TQString tmp ( text );
195 tmp.remove ( TQRegExp(
"[^0-9,Ee+-]" ) );
196 tmp.replace (
',',
'.' );
197 kdDebug(30501) <<
"Comma: " << text <<
" => " << tmp << endl;
198 const double d = tmp.toDouble( &ok );
201 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
202 cell->setCellText( text,
true );
206 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
207 cell->setNumber( d );
209 cell->format()->setPrecision( 2 );
212 case CSVDialog::POINTNUMBER:
215 TQString tmp ( text );
216 tmp.remove ( TQRegExp(
"[^0-9\\.EeD+-]" ) );
217 tmp.replace (
'D',
'E' );
218 kdDebug(30501) <<
"Point: " << text <<
" => " << tmp << endl;
219 const double d = tmp.toDouble( &ok );
222 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
223 cell->setCellText( text,
true );
227 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
228 cell->setNumber( d );
230 cell->format()->setPrecision( 2 );
233 case CSVDialog::DATE:
234 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
235 cell->setCellText( text );
236 cell->format()->setFormatType( ShortDate_format );
238 case CSVDialog::CURRENCY:
239 cell = sheet->nonDefaultCell( col + 1, row + 1,
false, s );
240 cell->setCellText( text,
false );
241 cell->format()->setFormatType( Money_format );
242 cell->format()->setPrecision( 2 );
248 emit sigProgress( 98 );
250 ElapsedTime t2(
"Resizing columns" );
251 for ( i = 0; i < numCols; ++i )
253 ColumnFormat * c = sheet->nonDefaultColumnFormat( i + 1 );
254 c->setDblWidth( widths[i] );
257 emit sigProgress( 100 );
258 TQApplication::restoreOverrideCursor();
264#include <csvimport.moc>