kspread Library API Documentation

kspread_dlg_csv.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
00003              (C) 2002-2003 Ariya Hidayat <ariya@kde.org>
00004              (C) 2002      Laurent Montel <montel@kde.org>
00005              (C) 1999 David Faure <faure@kde.org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include <qbuttongroup.h>
00024 #include <qcheckbox.h>
00025 #include <qclipboard.h>
00026 #include <qcombobox.h>
00027 #include <qlabel.h>
00028 #include <qlineedit.h>
00029 #include <qmime.h>
00030 #include <qpushbutton.h>
00031 #include <qradiobutton.h>
00032 #include <qtable.h>
00033 #include <qlayout.h>
00034 
00035 #include <kapplication.h>
00036 #include <kdebug.h>
00037 #include <kdialogbase.h>
00038 #include <kfiledialog.h>
00039 #include <klocale.h>
00040 #include <kmessagebox.h>
00041 
00042 #include "kspread_dlg_csv.h"
00043 #include <kspread_cell.h>
00044 #include <kspread_doc.h>
00045 #include <kspread_sheet.h>
00046 #include <kspread_undo.h>
00047 #include <kspread_view.h>
00048 
00049 KSpreadCSVDialog::KSpreadCSVDialog( KSpreadView * parent, const char * name, QRect const & rect, Mode mode)
00050   : KDialogBase( parent, name, true, QString::null, Ok|Cancel ),
00051     m_pView( parent ),
00052     m_cancelled( false ),
00053     m_adjustRows( 0 ),
00054     m_startline( 0 ),
00055     m_textquote( '"' ),
00056     m_delimiter( "," ),
00057     m_targetRect( rect ),
00058     m_mode( mode )
00059 {
00060   if ( !name )
00061     setName( "KSpreadCSVDialog" );
00062 
00063   setSizeGripEnabled( TRUE );
00064 
00065   QWidget* page = new QWidget( this );
00066   setMainWidget( page );
00067   //  MyDialogLayout = new QGridLayout( page, 4, 4, marginHint(), spacingHint(), "MyDialogLayout");
00068   MyDialogLayout = new QGridLayout( page, 1, 1, 11, 6, "MyDialogLayout");
00069 
00070   // Limit the range
00071   int column = m_targetRect.left();
00072   KSpreadCell* lastCell = m_pView->activeSheet()->getLastCellColumn( column );
00073   if( lastCell )
00074     if( m_targetRect.bottom() > lastCell->row() )
00075       m_targetRect.setBottom( lastCell->row() );
00076 
00077   m_sheet = new QTable( page, "m_table" );
00078   //m_sheet->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)7, 0, 0, m_sheet->sizePolicy().hasHeightForWidth() ) );
00079   m_sheet->setNumRows( 0 );
00080   m_sheet->setNumCols( 0 );
00081 
00082   MyDialogLayout->addMultiCellWidget( m_sheet, 3, 3, 0, 3 );
00083 
00084   // Delimiter: comma, semicolon, tab, space, other
00085   m_delimiterBox = new QButtonGroup( page, "m_delimiterBox" );
00086   m_delimiterBox->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, m_delimiterBox->sizePolicy().hasHeightForWidth() ) );
00087   m_delimiterBox->setTitle( i18n( "Delimiter" ) );
00088   m_delimiterBox->setColumnLayout(0, Qt::Vertical );
00089   m_delimiterBox->layout()->setSpacing( KDialog::spacingHint() );
00090   m_delimiterBox->layout()->setMargin( KDialog::marginHint() );
00091   m_delimiterBoxLayout = new QGridLayout( m_delimiterBox->layout() );
00092   m_delimiterBoxLayout->setAlignment( Qt::AlignTop );
00093   MyDialogLayout->addMultiCellWidget( m_delimiterBox, 0, 2, 0, 0 );
00094 
00095   m_ignoreDuplicates = new QCheckBox( page, "m_ignoreDuplicates" );
00096   m_ignoreDuplicates->setText( i18n( "Ignore duplicate delimiters" ) );
00097 
00098   MyDialogLayout->addMultiCellWidget( m_ignoreDuplicates, 2, 2, 2, 3 );
00099 
00100   m_radioComma = new QRadioButton( m_delimiterBox, "m_radioComma" );
00101   m_radioComma->setText( i18n( "Comma" ) );
00102   m_radioComma->setChecked( TRUE );
00103   m_delimiterBoxLayout->addWidget( m_radioComma, 0, 0 );
00104 
00105   m_radioSemicolon = new QRadioButton( m_delimiterBox, "m_radioSemicolon" );
00106   m_radioSemicolon->setText( i18n( "Semicolon" ) );
00107   m_delimiterBoxLayout->addWidget( m_radioSemicolon, 0, 1 );
00108 
00109   m_radioTab = new QRadioButton( m_delimiterBox, "m_radioTab" );
00110   m_radioTab->setText( i18n( "Tabulator" ) );
00111   m_delimiterBoxLayout->addWidget( m_radioTab, 1, 0 );
00112 
00113   m_radioSpace = new QRadioButton( m_delimiterBox, "m_radioSpace" );
00114   m_radioSpace->setText( i18n( "Space" ) );
00115   m_delimiterBoxLayout->addWidget( m_radioSpace, 1, 1 );
00116 
00117   m_radioOther = new QRadioButton( m_delimiterBox, "m_radioOther" );
00118   m_radioOther->setText( i18n( "Other" ) );
00119   m_delimiterBoxLayout->addWidget( m_radioOther, 0, 2 );
00120 
00121   m_delimiterEdit = new QLineEdit( m_delimiterBox, "m_delimiterEdit" );
00122   m_delimiterEdit->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, m_delimiterEdit->sizePolicy().hasHeightForWidth() ) );
00123   m_delimiterEdit->setMaximumSize( QSize( 30, 32767 ) );
00124   m_delimiterBoxLayout->addWidget( m_delimiterEdit, 1, 2 );
00125 
00126 
00127   // Format: number, text, currency,
00128   m_formatBox = new QButtonGroup( page, "m_formatBox" );
00129   m_formatBox->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, m_formatBox->sizePolicy().hasHeightForWidth() ) );
00130   m_formatBox->setTitle( i18n( "Format" ) );
00131   m_formatBox->setColumnLayout(0, Qt::Vertical );
00132   m_formatBox->layout()->setSpacing( KDialog::spacingHint() );
00133   m_formatBox->layout()->setMargin( KDialog::marginHint() );
00134   m_formatBoxLayout = new QGridLayout( m_formatBox->layout() );
00135   m_formatBoxLayout->setAlignment( Qt::AlignTop );
00136   MyDialogLayout->addMultiCellWidget( m_formatBox, 0, 2, 1, 1 );
00137 
00138   m_radioNumber = new QRadioButton( m_formatBox, "m_radioNumber" );
00139   m_radioNumber->setText( i18n( "Number" ) );
00140   m_formatBoxLayout->addMultiCellWidget( m_radioNumber, 1, 1, 0, 1 );
00141 
00142   m_radioText = new QRadioButton( m_formatBox, "m_radioText" );
00143   m_radioText->setText( i18n( "Text" ) );
00144   m_radioText->setChecked( TRUE );
00145   m_formatBoxLayout->addWidget( m_radioText, 0, 0 );
00146 
00147   m_radioCurrency = new QRadioButton( m_formatBox, "m_radioCurrency" );
00148   m_radioCurrency->setText( i18n( "Currency" ) );
00149   m_formatBoxLayout->addMultiCellWidget( m_radioCurrency, 0, 0, 1, 2 );
00150 
00151   m_radioDate = new QRadioButton( m_formatBox, "m_radioDate" );
00152   m_radioDate->setText( i18n( "Date" ) );
00153   m_formatBoxLayout->addWidget( m_radioDate, 1, 2 );
00154 
00155   m_comboLine = new QComboBox( FALSE, page, "m_comboLine" );
00156   m_comboLine->insertItem( i18n( "1" ) );
00157   m_comboLine->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, m_comboLine->sizePolicy().hasHeightForWidth() ) );
00158 
00159   MyDialogLayout->addWidget( m_comboLine, 1, 3 );
00160 
00161   m_comboQuote = new QComboBox( FALSE, page, "m_comboQuote" );
00162   m_comboQuote->insertItem( i18n( "\"" ) );
00163   m_comboQuote->insertItem( i18n( "'" ) );
00164   m_comboQuote->insertItem( i18n( "None" ) );
00165   m_comboQuote->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, m_comboQuote->sizePolicy().hasHeightForWidth() ) );
00166 
00167   MyDialogLayout->addWidget( m_comboQuote, 1, 2 );
00168   QSpacerItem* spacer_2 = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Preferred );
00169   MyDialogLayout->addItem( spacer_2, 2, 3 );
00170 
00171   TextLabel3 = new QLabel( page, "TextLabel3" );
00172   TextLabel3->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, TextLabel3->sizePolicy().hasHeightForWidth() ) );
00173   TextLabel3->setText( i18n( "Start at line:" ) );
00174 
00175   MyDialogLayout->addWidget( TextLabel3, 0, 3 );
00176 
00177   TextLabel2 = new QLabel( page, "TextLabel2" );
00178   TextLabel2->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, TextLabel2->sizePolicy().hasHeightForWidth() ) );
00179   TextLabel2->setText( i18n( "Textquote:" ) );
00180 
00181   MyDialogLayout->addWidget( TextLabel2, 0, 2 );
00182 
00183   if ( m_mode == Clipboard )
00184   {
00185     setCaption( i18n( "Inserting From Clipboard" ) );
00186     QMimeSource * mime = QApplication::clipboard()->data();
00187     if ( !mime )
00188     {
00189       KMessageBox::information( this, i18n("There is no data in the clipboard.") );
00190       m_cancelled = true;
00191       return;
00192     }
00193 
00194     if ( !mime->provides( "text/plain" ) )
00195     {
00196       KMessageBox::information( this, i18n("There is no usable data in the clipboard.") );
00197       m_cancelled = true;
00198       return;
00199     }
00200     m_fileArray = QByteArray(mime->encodedData( "text/plain" ) );
00201   }
00202   else if ( mode == File )
00203   {
00204     setCaption( i18n( "Inserting Text File" ) );
00205     QString file = KFileDialog::getOpenFileName(":",
00206                                                 "text/plain",
00207                                                 this);
00208     //cancel action !
00209     if ( file.isEmpty() )
00210     {
00211         actionButton( Ok )->setEnabled( false );
00212         m_cancelled = true;
00213         return;
00214     }
00215     QFile in(file);
00216     if (!in.open(IO_ReadOnly))
00217     {
00218       KMessageBox::sorry( this, i18n("Cannot open input file.") );
00219       in.close();
00220       actionButton( Ok )->setEnabled( false );
00221       m_cancelled = true;
00222       return;
00223     }
00224     m_fileArray = QByteArray(in.size());
00225     in.readBlock(m_fileArray.data(), in.size());
00226     in.close();
00227   }
00228   else
00229   {
00230     setCaption( i18n( "Text to Columns" ) );
00231     m_data = "";
00232     KSpreadCell  * cell;
00233     KSpreadSheet * sheet = m_pView->activeSheet();
00234     int col = m_targetRect.left();
00235     for (int i = m_targetRect.top(); i <= m_targetRect.bottom(); ++i)
00236     {
00237       cell = sheet->cellAt( col, i );
00238       if ( !cell->isEmpty() && !cell->isDefault() )
00239       {
00240         m_data += cell->strOutText();
00241       }
00242       m_data += "\n";
00243     }
00244   }
00245 
00246   fillSheet();
00247   fillComboBox();
00248 
00249   resize(sizeHint());
00250 
00251   m_sheet->setSelectionMode(QTable::NoSelection);
00252 
00253   connect(m_formatBox, SIGNAL(clicked(int)),
00254           this, SLOT(formatClicked(int)));
00255   connect(m_delimiterBox, SIGNAL(clicked(int)),
00256           this, SLOT(delimiterClicked(int)));
00257   connect(m_delimiterEdit, SIGNAL(returnPressed()),
00258           this, SLOT(returnPressed()));
00259   connect(m_delimiterEdit, SIGNAL(textChanged ( const QString & )),
00260           this, SLOT(textChanged ( const QString & ) ));
00261   connect(m_comboLine, SIGNAL(activated(const QString&)),
00262           this, SLOT(lineSelected(const QString&)));
00263   connect(m_comboQuote, SIGNAL(activated(const QString&)),
00264           this, SLOT(textquoteSelected(const QString&)));
00265   connect(m_sheet, SIGNAL(currentChanged(int, int)),
00266           this, SLOT(currentCellChanged(int, int)));
00267   connect(m_ignoreDuplicates, SIGNAL(stateChanged(int)),
00268           this, SLOT(ignoreDuplicatesChanged(int)));
00269 }
00270 
00271 KSpreadCSVDialog::~KSpreadCSVDialog()
00272 {
00273   // no need to delete child widgets, Qt does it all for us
00274 }
00275 
00276 bool KSpreadCSVDialog::cancelled()
00277 {
00278   return m_cancelled;
00279 }
00280 
00281 void KSpreadCSVDialog::fillSheet()
00282 {
00283   int row, column;
00284   bool lastCharDelimiter = false;
00285   bool ignoreDups = m_ignoreDuplicates->isChecked();
00286   enum { S_START, S_QUOTED_FIELD, S_MAYBE_END_OF_QUOTED_FIELD, S_END_OF_QUOTED_FIELD,
00287          S_MAYBE_NORMAL_FIELD, S_NORMAL_FIELD } state = S_START;
00288 
00289   QChar x;
00290   QString field = "";
00291 
00292   for (row = 0; row < m_sheet->numRows(); ++row)
00293     for (column = 0; column < m_sheet->numCols(); ++column)
00294       m_sheet->clearCell(row, column);
00295 
00296   row = column = 1;
00297   if (m_mode != Column)
00298   {
00299     m_mode = Column;
00300     m_data = QString(m_fileArray);
00301     m_fileArray.resize(0);
00302   }
00303 
00304   QTextStream inputStream(m_data, IO_ReadOnly);
00305   inputStream.setEncoding(QTextStream::Locale);
00306 
00307   while (!inputStream.atEnd())
00308   {
00309     inputStream >> x; // read one char
00310 
00311     if (x == '\r') inputStream >> x; // eat '\r', to handle DOS/LOSEDOWS files correctly
00312 
00313     switch (state)
00314     {
00315      case S_START :
00316       if (x == m_textquote)
00317       {
00318         state = S_QUOTED_FIELD;
00319       }
00320       else if (x == m_delimiter)
00321       {
00322         if ((ignoreDups == false) || (lastCharDelimiter == false))
00323           ++column;
00324         lastCharDelimiter = true;
00325       }
00326       else if (x == '\n')
00327       {
00328         ++row;
00329         column = 1;
00330       }
00331       else
00332       {
00333         field += x;
00334         state = S_MAYBE_NORMAL_FIELD;
00335       }
00336       break;
00337      case S_QUOTED_FIELD :
00338       if (x == m_textquote)
00339       {
00340         state = S_MAYBE_END_OF_QUOTED_FIELD;
00341       }
00342       else if (x == '\n')
00343       {
00344         setText(row - m_startline, column, field);
00345         field = "";
00346         if (x == '\n')
00347         {
00348           ++row;
00349           column = 1;
00350         }
00351         else
00352         {
00353           if ((ignoreDups == false) || (lastCharDelimiter == false))
00354             ++column;
00355           lastCharDelimiter = true;
00356         }
00357         state = S_START;
00358       }
00359       else
00360       {
00361         field += x;
00362       }
00363       break;
00364      case S_MAYBE_END_OF_QUOTED_FIELD :
00365       if (x == m_textquote)
00366       {
00367         field += x;
00368         state = S_QUOTED_FIELD;
00369       }
00370       else if (x == m_delimiter || x == '\n')
00371       {
00372         setText(row - m_startline, column, field);
00373         field = "";
00374         if (x == '\n')
00375         {
00376           ++row;
00377           column = 1;
00378         }
00379         else
00380         {
00381           if ((ignoreDups == false) || (lastCharDelimiter == false))
00382             ++column;
00383           lastCharDelimiter = true;
00384         }
00385         state = S_START;
00386       }
00387       else
00388       {
00389         state = S_END_OF_QUOTED_FIELD;
00390       }
00391       break;
00392      case S_END_OF_QUOTED_FIELD :
00393       if (x == m_delimiter || x == '\n')
00394       {
00395         setText(row - m_startline, column, field);
00396         field = "";
00397         if (x == '\n')
00398         {
00399           ++row;
00400           column = 1;
00401         }
00402         else
00403         {
00404           if ((ignoreDups == false) || (lastCharDelimiter == false))
00405             ++column;
00406           lastCharDelimiter = true;
00407         }
00408         state = S_START;
00409       }
00410       else
00411       {
00412         state = S_END_OF_QUOTED_FIELD;
00413       }
00414       break;
00415      case S_MAYBE_NORMAL_FIELD :
00416       if (x == m_textquote)
00417       {
00418         field = "";
00419         state = S_QUOTED_FIELD;
00420         break;
00421       }
00422      case S_NORMAL_FIELD :
00423       if (x == m_delimiter || x == '\n')
00424       {
00425         setText(row - m_startline, column, field);
00426         field = "";
00427         if (x == '\n')
00428         {
00429           ++row;
00430           column = 1;
00431         }
00432         else
00433         {
00434           if ((ignoreDups == false) || (lastCharDelimiter == false))
00435             ++column;
00436           lastCharDelimiter = true;
00437         }
00438         state = S_START;
00439       }
00440       else
00441       {
00442         field += x;
00443       }
00444     }
00445     if (x != m_delimiter)
00446       lastCharDelimiter = false;
00447   }
00448 
00449   // file with only one line without '\n'
00450   if (field.length() > 0)
00451   {
00452     setText(row - m_startline, column, field);
00453     ++row;
00454     field = "";
00455   }
00456 
00457   adjustRows( row - m_startline );
00458 
00459   for (column = 0; column < m_sheet->numCols(); ++column)
00460   {
00461     QString header = m_sheet->horizontalHeader()->label(column);
00462     if (header != i18n("Text") && header != i18n("Number") &&
00463         header != i18n("Date") && header != i18n("Currency"))
00464       m_sheet->horizontalHeader()->setLabel(column, i18n("Text"));
00465 
00466     m_sheet->adjustColumn(column);
00467   }
00468 }
00469 
00470 void KSpreadCSVDialog::fillComboBox()
00471 {
00472   m_comboLine->clear();
00473   for (int row = 0; row < m_sheet->numRows(); ++row)
00474     m_comboLine->insertItem(QString::number(row + 1), row);
00475 }
00476 
00477 void KSpreadCSVDialog::setText(int row, int col, const QString& text)
00478 {
00479   if (row < 1) // skipped by the user
00480     return;
00481 
00482   if (m_sheet->numRows() < row) {
00483     m_sheet->setNumRows(row+5000); /* We add 5000 at a time to limit recalculations */
00484     m_adjustRows=1;
00485   }
00486 
00487   if (m_sheet->numCols() < col)
00488     m_sheet->setNumCols(col);
00489 
00490   m_sheet->setText(row - 1, col - 1, text);
00491 }
00492 
00493 /*
00494  * Called after the first fillSheet() when number of rows are unknown.
00495  */
00496 void KSpreadCSVDialog::adjustRows(int iRows)
00497 {
00498   if (m_adjustRows)
00499   {
00500     m_sheet->setNumRows( iRows );
00501     m_adjustRows=0;
00502   }
00503 }
00504 
00505 void KSpreadCSVDialog::returnPressed()
00506 {
00507   if (m_delimiterBox->id(m_delimiterBox->selected()) != 4)
00508     return;
00509 
00510   m_delimiter = m_delimiterEdit->text();
00511   fillSheet();
00512 }
00513 
00514 void KSpreadCSVDialog::textChanged ( const QString & )
00515 {
00516   m_radioOther->setChecked ( true );
00517   delimiterClicked(4); // other
00518 }
00519 
00520 void KSpreadCSVDialog::formatClicked(int id)
00521 {
00522   QString header;
00523 
00524   switch (id)
00525   {
00526    case 1: // text
00527     header = i18n("Text");
00528     break;
00529    case 0: // number
00530     header = i18n("Number");
00531     break;
00532    case 3: // date
00533     header = i18n("Date");
00534     break;
00535    case 2: // currency
00536     header = i18n("Currency");
00537     break;
00538   }
00539 
00540   m_sheet->horizontalHeader()->setLabel(m_sheet->currentColumn(), header);
00541 }
00542 
00543 void KSpreadCSVDialog::delimiterClicked(int id)
00544 {
00545   switch (id)
00546   {
00547    case 0: // comma
00548     m_delimiter = ",";
00549     break;
00550    case 4: // other
00551     m_delimiter = m_delimiterEdit->text();
00552     break;
00553    case 2: // tab
00554     m_delimiter = "\t";
00555     break;
00556    case 3: // space
00557     m_delimiter = " ";
00558     break;
00559    case 1: // semicolon
00560     m_delimiter = ";";
00561     break;
00562   }
00563 
00564   fillSheet();
00565 }
00566 
00567 void KSpreadCSVDialog::textquoteSelected(const QString& mark)
00568 {
00569   if (mark == i18n("none"))
00570     m_textquote = 0;
00571   else
00572     m_textquote = mark[0];
00573 
00574   fillSheet();
00575 }
00576 
00577 void KSpreadCSVDialog::lineSelected(const QString& line)
00578 {
00579   m_startline = line.toInt() - 1;
00580   fillSheet();
00581 }
00582 
00583 void KSpreadCSVDialog::currentCellChanged(int, int col)
00584 {
00585   int id;
00586   QString header = m_sheet->horizontalHeader()->label(col);
00587 
00588   if (header == i18n("Text"))
00589     id = 1;
00590   else if (header == i18n("Number"))
00591     id = 0;
00592   else if (header == i18n("Date"))
00593     id = 3;
00594   else
00595     id = 2;
00596 
00597   m_formatBox->setButton(id);
00598 }
00599 
00600 void KSpreadCSVDialog::accept()
00601 {
00602   KSpreadSheet * sheet  = m_pView->activeSheet();
00603   QString csv_delimiter = QString::null;
00604   KSpreadCell  * cell;
00605 
00606   int numRows = m_sheet->numRows();
00607   int numCols = m_sheet->numCols();
00608 
00609   if (numRows == 0)
00610     ++numRows;
00611 
00612   if ( (numCols > m_targetRect.width()) && (m_targetRect.width() > 1) )
00613   {
00614     numCols = m_targetRect.width();
00615   }
00616   else
00617     m_targetRect.setRight( m_targetRect.left() + numCols );
00618 
00619   if ( (numRows > m_targetRect.height()) && (m_targetRect.height() > 1) )
00620     numRows = m_targetRect.height();
00621   else
00622     m_targetRect.setBottom( m_targetRect.top() + numRows );
00623 
00624   if ( numRows == 1 && numCols == 1)
00625   {
00626     KSpreadDoc * doc = m_pView->doc();
00627     cell = sheet->nonDefaultCell( m_targetRect.left(), m_targetRect.top() );
00628     if ( !doc->undoLocked() )
00629     {
00630       KSpreadUndoSetText * undo = new KSpreadUndoSetText( doc, sheet , cell->text(), m_targetRect.left(),
00631                                                           m_targetRect.top(), cell->formatType() );
00632       doc->addCommand( undo );
00633     }
00634   }
00635   else
00636   {
00637       KSpreadUndoChangeAreaTextCell * undo = new KSpreadUndoChangeAreaTextCell( m_pView->doc(), sheet , m_targetRect );
00638       m_pView->doc()->addCommand( undo );
00639   }
00640 
00641   m_pView->doc()->emitBeginOperation();
00642 
00643   int i;
00644   int left = m_targetRect.left();
00645   int top  = m_targetRect.top();
00646 
00647   QMemArray<double> widths( numCols );
00648   for ( i = 0; i < numCols; ++i )
00649   {
00650     ColumnFormat * c  = sheet->nonDefaultColumnFormat( left + i );
00651     widths[i] = c->dblWidth();
00652   }
00653 
00654   for (int row = 0; row < numRows; ++row)
00655   {
00656     for (int col = 0; col < numCols; ++col)
00657     {
00658       cell = sheet->nonDefaultCell( left + col, top + row );
00659       cell->setCellText( getText( row, col ) );
00660 
00661       QFontMetrics fm = sheet->painter().fontMetrics();
00662       double w = fm.width( cell->strOutText() );
00663       if ( w == 0.0 )
00664       {
00665         QFontMetrics fm( cell->textFont( left + col, top + row ) );
00666         w = fm.width('x') * (double) getText( row, col ).length();
00667       }
00668 
00669       if ( w > widths[col] )
00670         widths[col] = w;
00671 
00672       cell->setFormatType (Generic_format);
00673       /*
00674       Disabling this code for now, everything will use Generic formatting,
00675       hoping for the best (Tomas)
00676       //### FIXME: long term solution is to allow to select Generic format ("autodetect") in the dialog and make it the default
00677       
00678       switch (getHeader(col))
00679       {
00680        case TEXT:
00681         break;
00682        case NUMBER:
00683         cell->setFormatType(Number_format);
00684         cell->setPrecision(2);
00685         break;
00686        case DATE:
00687         cell->setFormatType(ShortDate_format);
00688         break;
00689        case CURRENCY:
00690         cell->setFormatType(Money_format);
00691         break;
00692       }
00693       */
00694     }
00695   }
00696 
00697   for ( i = 0; i < numCols; ++i )
00698   {
00699     ColumnFormat * c  = sheet->nonDefaultColumnFormat( left + i );
00700     c->setDblWidth( widths[i] );
00701     sheet->emit_updateColumn( c, left + i );
00702   }
00703 
00704   m_pView->slotUpdateView( sheet );
00705   QDialog::accept();
00706 }
00707 
00708 int KSpreadCSVDialog::getHeader(int col)
00709 {
00710   QString header = m_sheet->horizontalHeader()->label(col);
00711 
00712   if (header == i18n("Text"))
00713     return TEXT;
00714   else if (header == i18n("Number"))
00715     return NUMBER;
00716   else if (header == i18n("Currency"))
00717     return CURRENCY;
00718   else
00719     return DATE;
00720 }
00721 
00722 QString KSpreadCSVDialog::getText(int row, int col)
00723 {
00724   return m_sheet->text(row, col);
00725 }
00726 
00727 void KSpreadCSVDialog::ignoreDuplicatesChanged(int)
00728 {
00729   fillSheet();
00730 }
00731 
00732 #include "kspread_dlg_csv.moc"
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:42:54 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003