~ubuntu-branches/ubuntu/edgy/koffice/edgy-updates

« back to all changes in this revision

Viewing changes to kspread/kspread_dlg_csv.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040509113300-xi5t1z4yxe7n03x7
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2002 Norbert Andres (nandres@web.de)
 
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., 59 Temple Place - Suite 330,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
#include <qbuttongroup.h>
 
21
#include <qcheckbox.h>
 
22
#include <qclipboard.h>
 
23
#include <qcombobox.h>
 
24
#include <qlabel.h>
 
25
#include <qlineedit.h>
 
26
#include <qmime.h>
 
27
#include <qpushbutton.h>
 
28
#include <qradiobutton.h>
 
29
#include <qtable.h>
 
30
#include <qlayout.h>
 
31
 
 
32
#include <kapplication.h>
 
33
#include <kdebug.h>
 
34
#include <kdialogbase.h>
 
35
#include <kfiledialog.h>
 
36
#include <klocale.h>
 
37
#include <kmessagebox.h>
 
38
 
 
39
#include <kspread_dlg_csv.h>
 
40
#include <kspread_cell.h>
 
41
#include <kspread_doc.h>
 
42
#include <kspread_sheet.h>
 
43
#include <kspread_undo.h>
 
44
#include <kspread_view.h>
 
45
 
 
46
KSpreadCSVDialog::KSpreadCSVDialog( KSpreadView * parent, const char * name, QRect const & rect, Mode mode)
 
47
  : KDialogBase( parent, name, true, QString::null, Ok|Cancel ),
 
48
    m_pView( parent ),
 
49
    m_cancelled( false ),
 
50
    m_adjustRows( 0 ),
 
51
    m_startline( 0 ),
 
52
    m_textquote( '"' ),
 
53
    m_delimiter( "," ),
 
54
    m_targetRect( rect ),
 
55
    m_mode( mode )
 
56
{
 
57
  if ( !name )
 
58
    setName( "KSpreadCSVDialog" );
 
59
 
 
60
  setSizeGripEnabled( TRUE );
 
61
 
 
62
  QWidget* page = new QWidget( this );
 
63
  setMainWidget( page );
 
64
  //  MyDialogLayout = new QGridLayout( page, 4, 4, marginHint(), spacingHint(), "MyDialogLayout");
 
65
  MyDialogLayout = new QGridLayout( page, 1, 1, 11, 6, "MyDialogLayout");
 
66
 
 
67
  // Limit the range
 
68
  int column = m_targetRect.left();
 
69
  KSpreadCell* lastCell = m_pView->activeTable()->getLastCellColumn( column );
 
70
  if( lastCell )
 
71
    if( m_targetRect.bottom() > lastCell->row() )
 
72
      m_targetRect.setBottom( lastCell->row() );
 
73
 
 
74
  m_table = new QTable( page, "m_table" );
 
75
  //m_table->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)7, 0, 0, m_table->sizePolicy().hasHeightForWidth() ) );
 
76
  m_table->setNumRows( 0 );
 
77
  m_table->setNumCols( 0 );
 
78
 
 
79
  MyDialogLayout->addMultiCellWidget( m_table, 3, 3, 0, 3 );
 
80
 
 
81
  // Delimiter: comma, semicolon, tab, space, other
 
82
  m_delimiterBox = new QButtonGroup( page, "m_delimiterBox" );
 
83
  m_delimiterBox->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, m_delimiterBox->sizePolicy().hasHeightForWidth() ) );
 
84
  m_delimiterBox->setTitle( i18n( "Delimiter" ) );
 
85
  m_delimiterBox->setColumnLayout(0, Qt::Vertical );
 
86
  m_delimiterBox->layout()->setSpacing( KDialog::spacingHint() );
 
87
  m_delimiterBox->layout()->setMargin( KDialog::marginHint() );
 
88
  m_delimiterBoxLayout = new QGridLayout( m_delimiterBox->layout() );
 
89
  m_delimiterBoxLayout->setAlignment( Qt::AlignTop );
 
90
  MyDialogLayout->addMultiCellWidget( m_delimiterBox, 0, 2, 0, 0 );
 
91
 
 
92
  m_ignoreDuplicates = new QCheckBox( page, "m_ignoreDuplicates" );
 
93
  m_ignoreDuplicates->setText( i18n( "Ignore duplicate delimiters" ) );
 
94
 
 
95
  MyDialogLayout->addMultiCellWidget( m_ignoreDuplicates, 2, 2, 2, 3 );
 
96
 
 
97
  m_radioComma = new QRadioButton( m_delimiterBox, "m_radioComma" );
 
98
  m_radioComma->setText( i18n( "Comma" ) );
 
99
  m_radioComma->setChecked( TRUE );
 
100
  m_delimiterBoxLayout->addWidget( m_radioComma, 0, 0 );
 
101
 
 
102
  m_radioSemicolon = new QRadioButton( m_delimiterBox, "m_radioSemicolon" );
 
103
  m_radioSemicolon->setText( i18n( "Semicolon" ) );
 
104
  m_delimiterBoxLayout->addWidget( m_radioSemicolon, 0, 1 );
 
105
 
 
106
  m_radioTab = new QRadioButton( m_delimiterBox, "m_radioTab" );
 
107
  m_radioTab->setText( i18n( "Tabulator" ) );
 
108
  m_delimiterBoxLayout->addWidget( m_radioTab, 1, 0 );
 
109
 
 
110
  m_radioSpace = new QRadioButton( m_delimiterBox, "m_radioSpace" );
 
111
  m_radioSpace->setText( i18n( "Space" ) );
 
112
  m_delimiterBoxLayout->addWidget( m_radioSpace, 1, 1 );
 
113
 
 
114
  m_radioOther = new QRadioButton( m_delimiterBox, "m_radioOther" );
 
115
  m_radioOther->setText( i18n( "Other" ) );
 
116
  m_delimiterBoxLayout->addWidget( m_radioOther, 0, 2 );
 
117
 
 
118
  m_delimiterEdit = new QLineEdit( m_delimiterBox, "m_delimiterEdit" );
 
119
  m_delimiterEdit->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, m_delimiterEdit->sizePolicy().hasHeightForWidth() ) );
 
120
  m_delimiterEdit->setMaximumSize( QSize( 30, 32767 ) );
 
121
  m_delimiterBoxLayout->addWidget( m_delimiterEdit, 1, 2 );
 
122
 
 
123
 
 
124
  // Format: number, text, currency,
 
125
  m_formatBox = new QButtonGroup( page, "m_formatBox" );
 
126
  m_formatBox->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, m_formatBox->sizePolicy().hasHeightForWidth() ) );
 
127
  m_formatBox->setTitle( i18n( "Format" ) );
 
128
  m_formatBox->setColumnLayout(0, Qt::Vertical );
 
129
  m_formatBox->layout()->setSpacing( KDialog::spacingHint() );
 
130
  m_formatBox->layout()->setMargin( KDialog::marginHint() );
 
131
  m_formatBoxLayout = new QGridLayout( m_formatBox->layout() );
 
132
  m_formatBoxLayout->setAlignment( Qt::AlignTop );
 
133
  MyDialogLayout->addMultiCellWidget( m_formatBox, 0, 2, 1, 1 );
 
134
 
 
135
  m_radioNumber = new QRadioButton( m_formatBox, "m_radioNumber" );
 
136
  m_radioNumber->setText( i18n( "Number" ) );
 
137
  m_formatBoxLayout->addMultiCellWidget( m_radioNumber, 1, 1, 0, 1 );
 
138
 
 
139
  m_radioText = new QRadioButton( m_formatBox, "m_radioText" );
 
140
  m_radioText->setText( i18n( "Text" ) );
 
141
  m_radioText->setChecked( TRUE );
 
142
  m_formatBoxLayout->addWidget( m_radioText, 0, 0 );
 
143
 
 
144
  m_radioCurrency = new QRadioButton( m_formatBox, "m_radioCurrency" );
 
145
  m_radioCurrency->setText( i18n( "Currency" ) );
 
146
  m_formatBoxLayout->addMultiCellWidget( m_radioCurrency, 0, 0, 1, 2 );
 
147
 
 
148
  m_radioDate = new QRadioButton( m_formatBox, "m_radioDate" );
 
149
  m_radioDate->setText( i18n( "Date" ) );
 
150
  m_formatBoxLayout->addWidget( m_radioDate, 1, 2 );
 
151
 
 
152
  m_comboLine = new QComboBox( FALSE, page, "m_comboLine" );
 
153
  m_comboLine->insertItem( i18n( "1" ) );
 
154
  m_comboLine->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, m_comboLine->sizePolicy().hasHeightForWidth() ) );
 
155
 
 
156
  MyDialogLayout->addWidget( m_comboLine, 1, 3 );
 
157
 
 
158
  m_comboQuote = new QComboBox( FALSE, page, "m_comboQuote" );
 
159
  m_comboQuote->insertItem( i18n( "\"" ) );
 
160
  m_comboQuote->insertItem( i18n( "'" ) );
 
161
  m_comboQuote->insertItem( i18n( "None" ) );
 
162
  m_comboQuote->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, m_comboQuote->sizePolicy().hasHeightForWidth() ) );
 
163
 
 
164
  MyDialogLayout->addWidget( m_comboQuote, 1, 2 );
 
165
  QSpacerItem* spacer_2 = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Preferred );
 
166
  MyDialogLayout->addItem( spacer_2, 2, 3 );
 
167
 
 
168
  TextLabel3 = new QLabel( page, "TextLabel3" );
 
169
  TextLabel3->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, TextLabel3->sizePolicy().hasHeightForWidth() ) );
 
170
  TextLabel3->setText( i18n( "Start at line:" ) );
 
171
 
 
172
  MyDialogLayout->addWidget( TextLabel3, 0, 3 );
 
173
 
 
174
  TextLabel2 = new QLabel( page, "TextLabel2" );
 
175
  TextLabel2->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, 0, 0, TextLabel2->sizePolicy().hasHeightForWidth() ) );
 
176
  TextLabel2->setText( i18n( "Textquote:" ) );
 
177
 
 
178
  MyDialogLayout->addWidget( TextLabel2, 0, 2 );
 
179
 
 
180
  if ( m_mode == Clipboard )
 
181
  {
 
182
    setCaption( i18n( "Inserting From Clipboard" ) );
 
183
    QMimeSource * mime = QApplication::clipboard()->data();
 
184
    if ( !mime )
 
185
    {
 
186
      KMessageBox::information( this, i18n("There is no data in the clipboard.") );
 
187
      m_cancelled = true;
 
188
      return;
 
189
    }
 
190
 
 
191
    if ( !mime->provides( "text/plain" ) )
 
192
    {
 
193
      KMessageBox::information( this, i18n("There is no usable data in the clipboard.") );
 
194
      m_cancelled = true;
 
195
      return;
 
196
    }
 
197
    m_fileArray = QByteArray(mime->encodedData( "text/plain" ) );
 
198
  }
 
199
  else if ( mode == File )
 
200
  {
 
201
    setCaption( i18n( "Inserting Text File" ) );
 
202
    QString file = KFileDialog::getOpenFileName(":",
 
203
                                                "text/plain",
 
204
                                                this);
 
205
    //cancel action !
 
206
    if ( file.isEmpty() )
 
207
    {
 
208
        actionButton( Ok )->setEnabled( false );
 
209
        m_cancelled = true;
 
210
        return;
 
211
    }
 
212
    QFile in(file);
 
213
    if (!in.open(IO_ReadOnly))
 
214
    {
 
215
      KMessageBox::sorry( this, i18n("Cannot open input file!") );
 
216
      in.close();
 
217
      actionButton( Ok )->setEnabled( false );
 
218
      m_cancelled = true;
 
219
      return;
 
220
    }
 
221
    m_fileArray = QByteArray(in.size());
 
222
    in.readBlock(m_fileArray.data(), in.size());
 
223
    in.close();
 
224
  }
 
225
  else
 
226
  {
 
227
    setCaption( i18n( "Text to Columns" ) );
 
228
    m_data = "";
 
229
    KSpreadCell  * cell;
 
230
    KSpreadSheet * table = m_pView->activeTable();
 
231
    int col = m_targetRect.left();
 
232
    for (int i = m_targetRect.top(); i <= m_targetRect.bottom(); ++i)
 
233
    {
 
234
      cell = table->cellAt( col, i );
 
235
      if ( !cell->isEmpty() && !cell->isDefault() )
 
236
      {
 
237
        m_data += cell->strOutText();
 
238
      }
 
239
      m_data += "\n";
 
240
    }
 
241
  }
 
242
 
 
243
  fillTable();
 
244
  fillComboBox();
 
245
 
 
246
  resize(sizeHint());
 
247
 
 
248
  m_table->setSelectionMode(QTable::NoSelection);
 
249
 
 
250
  connect(m_formatBox, SIGNAL(clicked(int)),
 
251
          this, SLOT(formatClicked(int)));
 
252
  connect(m_delimiterBox, SIGNAL(clicked(int)),
 
253
          this, SLOT(delimiterClicked(int)));
 
254
  connect(m_delimiterEdit, SIGNAL(returnPressed()),
 
255
          this, SLOT(returnPressed()));
 
256
  connect(m_delimiterEdit, SIGNAL(textChanged ( const QString & )),
 
257
          this, SLOT(textChanged ( const QString & ) ));
 
258
  connect(m_comboLine, SIGNAL(activated(const QString&)),
 
259
          this, SLOT(lineSelected(const QString&)));
 
260
  connect(m_comboQuote, SIGNAL(activated(const QString&)),
 
261
          this, SLOT(textquoteSelected(const QString&)));
 
262
  connect(m_table, SIGNAL(currentChanged(int, int)),
 
263
          this, SLOT(currentCellChanged(int, int)));
 
264
  connect(m_ignoreDuplicates, SIGNAL(stateChanged(int)),
 
265
          this, SLOT(ignoreDuplicatesChanged(int)));
 
266
}
 
267
 
 
268
KSpreadCSVDialog::~KSpreadCSVDialog()
 
269
{
 
270
  // no need to delete child widgets, Qt does it all for us
 
271
}
 
272
 
 
273
bool KSpreadCSVDialog::cancelled()
 
274
{
 
275
  return m_cancelled;
 
276
}
 
277
 
 
278
void KSpreadCSVDialog::fillTable()
 
279
{
 
280
  int row, column;
 
281
  bool lastCharDelimiter = false;
 
282
  bool ignoreDups = m_ignoreDuplicates->isChecked();
 
283
  enum { S_START, S_QUOTED_FIELD, S_MAYBE_END_OF_QUOTED_FIELD, S_END_OF_QUOTED_FIELD,
 
284
         S_MAYBE_NORMAL_FIELD, S_NORMAL_FIELD } state = S_START;
 
285
 
 
286
  QChar x;
 
287
  QString field = "";
 
288
 
 
289
  for (row = 0; row < m_table->numRows(); ++row)
 
290
    for (column = 0; column < m_table->numCols(); ++column)
 
291
      m_table->clearCell(row, column);
 
292
 
 
293
  row = column = 1;
 
294
  if (m_mode != Column)
 
295
  {
 
296
    m_mode = Column;
 
297
    m_data = QString(m_fileArray);
 
298
    m_fileArray.resize(0);
 
299
  }
 
300
 
 
301
  QTextStream inputStream(m_data, IO_ReadOnly);
 
302
  inputStream.setEncoding(QTextStream::Locale);
 
303
 
 
304
  while (!inputStream.atEnd())
 
305
  {
 
306
    inputStream >> x; // read one char
 
307
 
 
308
    if (x == '\r') inputStream >> x; // eat '\r', to handle DOS/LOSEDOWS files correctly
 
309
 
 
310
    switch (state)
 
311
    {
 
312
     case S_START :
 
313
      if (x == m_textquote)
 
314
      {
 
315
        state = S_QUOTED_FIELD;
 
316
      }
 
317
      else if (x == m_delimiter)
 
318
      {
 
319
        if ((ignoreDups == false) || (lastCharDelimiter == false))
 
320
          ++column;
 
321
        lastCharDelimiter = true;
 
322
      }
 
323
      else if (x == '\n')
 
324
      {
 
325
        ++row;
 
326
        column = 1;
 
327
      }
 
328
      else
 
329
      {
 
330
        field += x;
 
331
        state = S_MAYBE_NORMAL_FIELD;
 
332
      }
 
333
      break;
 
334
     case S_QUOTED_FIELD :
 
335
      if (x == m_textquote)
 
336
      {
 
337
        state = S_MAYBE_END_OF_QUOTED_FIELD;
 
338
      }
 
339
      else if (x == '\n')
 
340
      {
 
341
        setText(row - m_startline, column, field);
 
342
        field = "";
 
343
        if (x == '\n')
 
344
        {
 
345
          ++row;
 
346
          column = 1;
 
347
        }
 
348
        else
 
349
        {
 
350
          if ((ignoreDups == false) || (lastCharDelimiter == false))
 
351
            ++column;
 
352
          lastCharDelimiter = true;
 
353
        }
 
354
        state = S_START;
 
355
      }
 
356
      else
 
357
      {
 
358
        field += x;
 
359
      }
 
360
      break;
 
361
     case S_MAYBE_END_OF_QUOTED_FIELD :
 
362
      if (x == m_textquote)
 
363
      {
 
364
        field += x;
 
365
        state = S_QUOTED_FIELD;
 
366
      }
 
367
      else if (x == m_delimiter || x == '\n')
 
368
      {
 
369
        setText(row - m_startline, column, field);
 
370
        field = "";
 
371
        if (x == '\n')
 
372
        {
 
373
          ++row;
 
374
          column = 1;
 
375
        }
 
376
        else
 
377
        {
 
378
          if ((ignoreDups == false) || (lastCharDelimiter == false))
 
379
            ++column;
 
380
          lastCharDelimiter = true;
 
381
        }
 
382
        state = S_START;
 
383
      }
 
384
      else
 
385
      {
 
386
        state = S_END_OF_QUOTED_FIELD;
 
387
      }
 
388
      break;
 
389
     case S_END_OF_QUOTED_FIELD :
 
390
      if (x == m_delimiter || x == '\n')
 
391
      {
 
392
        setText(row - m_startline, column, field);
 
393
        field = "";
 
394
        if (x == '\n')
 
395
        {
 
396
          ++row;
 
397
          column = 1;
 
398
        }
 
399
        else
 
400
        {
 
401
          if ((ignoreDups == false) || (lastCharDelimiter == false))
 
402
            ++column;
 
403
          lastCharDelimiter = true;
 
404
        }
 
405
        state = S_START;
 
406
      }
 
407
      else
 
408
      {
 
409
        state = S_END_OF_QUOTED_FIELD;
 
410
      }
 
411
      break;
 
412
     case S_MAYBE_NORMAL_FIELD :
 
413
      if (x == m_textquote)
 
414
      {
 
415
        field = "";
 
416
        state = S_QUOTED_FIELD;
 
417
        break;
 
418
      }
 
419
     case S_NORMAL_FIELD :
 
420
      if (x == m_delimiter || x == '\n')
 
421
      {
 
422
        setText(row - m_startline, column, field);
 
423
        field = "";
 
424
        if (x == '\n')
 
425
        {
 
426
          ++row;
 
427
          column = 1;
 
428
        }
 
429
        else
 
430
        {
 
431
          if ((ignoreDups == false) || (lastCharDelimiter == false))
 
432
            ++column;
 
433
          lastCharDelimiter = true;
 
434
        }
 
435
        state = S_START;
 
436
      }
 
437
      else
 
438
      {
 
439
        field += x;
 
440
      }
 
441
    }
 
442
    if (x != m_delimiter)
 
443
      lastCharDelimiter = false;
 
444
  }
 
445
 
 
446
  // file with only one line without '\n'
 
447
  if (field.length() > 0)
 
448
  {
 
449
    setText(row - m_startline, column, field);
 
450
    ++row;
 
451
    field = "";
 
452
  }
 
453
 
 
454
  adjustRows( row - m_startline );
 
455
 
 
456
  for (column = 0; column < m_table->numCols(); ++column)
 
457
  {
 
458
    QString header = m_table->horizontalHeader()->label(column);
 
459
    if (header != i18n("Text") && header != i18n("Number") &&
 
460
        header != i18n("Date") && header != i18n("Currency"))
 
461
      m_table->horizontalHeader()->setLabel(column, i18n("Text"));
 
462
 
 
463
    m_table->adjustColumn(column);
 
464
  }
 
465
}
 
466
 
 
467
void KSpreadCSVDialog::fillComboBox()
 
468
{
 
469
  m_comboLine->clear();
 
470
  for (int row = 0; row < m_table->numRows(); ++row)
 
471
    m_comboLine->insertItem(QString::number(row + 1), row);
 
472
}
 
473
 
 
474
void KSpreadCSVDialog::setText(int row, int col, const QString& text)
 
475
{
 
476
  if (row < 1) // skipped by the user
 
477
    return;
 
478
 
 
479
  if (m_table->numRows() < row) {
 
480
    m_table->setNumRows(row+5000); /* We add 5000 at a time to limit recalculations */
 
481
    m_adjustRows=1;
 
482
  }
 
483
 
 
484
  if (m_table->numCols() < col)
 
485
    m_table->setNumCols(col);
 
486
 
 
487
  m_table->setText(row - 1, col - 1, text);
 
488
}
 
489
 
 
490
/*
 
491
 * Called after the first fillTable() when number of rows are unknown.
 
492
 */
 
493
void KSpreadCSVDialog::adjustRows(int iRows)
 
494
{
 
495
  if (m_adjustRows)
 
496
  {
 
497
    m_table->setNumRows( iRows );
 
498
    m_adjustRows=0;
 
499
  }
 
500
}
 
501
 
 
502
void KSpreadCSVDialog::returnPressed()
 
503
{
 
504
  if (m_delimiterBox->id(m_delimiterBox->selected()) != 4)
 
505
    return;
 
506
 
 
507
  m_delimiter = m_delimiterEdit->text();
 
508
  fillTable();
 
509
}
 
510
 
 
511
void KSpreadCSVDialog::textChanged ( const QString & )
 
512
{
 
513
  m_radioOther->setChecked ( true );
 
514
  delimiterClicked(4); // other
 
515
}
 
516
 
 
517
void KSpreadCSVDialog::formatClicked(int id)
 
518
{
 
519
  QString header;
 
520
 
 
521
  switch (id)
 
522
  {
 
523
   case 1: // text
 
524
    header = i18n("Text");
 
525
    break;
 
526
   case 0: // number
 
527
    header = i18n("Number");
 
528
    break;
 
529
   case 3: // date
 
530
    header = i18n("Date");
 
531
    break;
 
532
   case 2: // currency
 
533
    header = i18n("Currency");
 
534
    break;
 
535
  }
 
536
 
 
537
  m_table->horizontalHeader()->setLabel(m_table->currentColumn(), header);
 
538
}
 
539
 
 
540
void KSpreadCSVDialog::delimiterClicked(int id)
 
541
{
 
542
  switch (id)
 
543
  {
 
544
   case 0: // comma
 
545
    m_delimiter = ",";
 
546
    break;
 
547
   case 4: // other
 
548
    m_delimiter = m_delimiterEdit->text();
 
549
    break;
 
550
   case 2: // tab
 
551
    m_delimiter = "\t";
 
552
    break;
 
553
   case 3: // space
 
554
    m_delimiter = " ";
 
555
    break;
 
556
   case 1: // semicolon
 
557
    m_delimiter = ";";
 
558
    break;
 
559
  }
 
560
 
 
561
  fillTable();
 
562
}
 
563
 
 
564
void KSpreadCSVDialog::textquoteSelected(const QString& mark)
 
565
{
 
566
  if (mark == i18n("none"))
 
567
    m_textquote = 0;
 
568
  else
 
569
    m_textquote = mark[0];
 
570
 
 
571
  fillTable();
 
572
}
 
573
 
 
574
void KSpreadCSVDialog::lineSelected(const QString& line)
 
575
{
 
576
  m_startline = line.toInt() - 1;
 
577
  fillTable();
 
578
}
 
579
 
 
580
void KSpreadCSVDialog::currentCellChanged(int, int col)
 
581
{
 
582
  int id;
 
583
  QString header = m_table->horizontalHeader()->label(col);
 
584
 
 
585
  if (header == i18n("Text"))
 
586
    id = 1;
 
587
  else if (header == i18n("Number"))
 
588
    id = 0;
 
589
  else if (header == i18n("Date"))
 
590
    id = 3;
 
591
  else
 
592
    id = 2;
 
593
 
 
594
  m_formatBox->setButton(id);
 
595
}
 
596
 
 
597
void KSpreadCSVDialog::accept()
 
598
{
 
599
  KSpreadSheet * table  = m_pView->activeTable();
 
600
  QString csv_delimiter = QString::null;
 
601
  KSpreadCell  * cell;
 
602
 
 
603
  int numRows = m_table->numRows();
 
604
  int numCols = m_table->numCols();
 
605
 
 
606
  if (numRows == 0)
 
607
    ++numRows;
 
608
 
 
609
  if ( (numCols > m_targetRect.width()) && (m_targetRect.width() > 1) )
 
610
  {
 
611
    numCols = m_targetRect.width();
 
612
  }
 
613
  else
 
614
    m_targetRect.setRight( m_targetRect.left() + numCols );
 
615
 
 
616
  if ( (numRows > m_targetRect.height()) && (m_targetRect.height() > 1) )
 
617
    numRows = m_targetRect.height();
 
618
  else
 
619
    m_targetRect.setBottom( m_targetRect.top() + numRows );
 
620
 
 
621
  if ( numRows == 1 && numCols == 1)
 
622
  {
 
623
    KSpreadDoc * doc = m_pView->doc();
 
624
    cell = table->nonDefaultCell( m_targetRect.left(), m_targetRect.top() );
 
625
    if ( !doc->undoBuffer()->isLocked() )
 
626
    {
 
627
      KSpreadUndoSetText * undo = new KSpreadUndoSetText( doc, table , cell->text(), m_targetRect.left(),
 
628
                                                          m_targetRect.top(), cell->formatType() );
 
629
      doc->undoBuffer()->appendUndo( undo );
 
630
    }
 
631
  }
 
632
  else
 
633
  {
 
634
      KSpreadUndoChangeAreaTextCell * undo = new KSpreadUndoChangeAreaTextCell( m_pView->doc(), table , m_targetRect );
 
635
      m_pView->doc()->undoBuffer()->appendUndo( undo );
 
636
  }
 
637
 
 
638
  m_pView->doc()->emitBeginOperation();
 
639
 
 
640
  int i;
 
641
  int left = m_targetRect.left();
 
642
  int top  = m_targetRect.top();
 
643
 
 
644
  QMemArray<double> widths( numCols );
 
645
  for ( i = 0; i < numCols; ++i )
 
646
  {
 
647
    ColumnFormat * c  = table->nonDefaultColumnFormat( left + i );
 
648
    widths[i] = c->dblWidth();
 
649
  }
 
650
 
 
651
  for (int row = 0; row < numRows; ++row)
 
652
  {
 
653
    for (int col = 0; col < numCols; ++col)
 
654
    {
 
655
      cell = table->nonDefaultCell( left + col, top + row );
 
656
      cell->setCellText( getText( row, col ) );
 
657
 
 
658
      QFontMetrics fm = table->painter().fontMetrics();
 
659
      double w = fm.width( cell->strOutText() );
 
660
      if ( w == 0.0 )
 
661
      {
 
662
        QFontMetrics fm( cell->textFont( left + col, top + row ) );
 
663
        w = fm.width('x') * (double) getText( row, col ).length();
 
664
      }
 
665
 
 
666
      if ( w > widths[col] )
 
667
        widths[col] = w;
 
668
 
 
669
      switch (getHeader(col))
 
670
      {
 
671
       case TEXT:
 
672
        break;
 
673
       case NUMBER:
 
674
        cell->setFormatType(KSpreadCell::Number);
 
675
        cell->setPrecision(2);
 
676
        break;
 
677
       case DATE:
 
678
        cell->setFormatType(KSpreadCell::ShortDate);
 
679
        break;
 
680
       case CURRENCY:
 
681
        cell->setFormatType(KSpreadCell::Money);
 
682
        break;
 
683
      }
 
684
    }
 
685
  }
 
686
 
 
687
  for ( i = 0; i < numCols; ++i )
 
688
  {
 
689
    ColumnFormat * c  = table->nonDefaultColumnFormat( left + i );
 
690
    c->setDblWidth( widths[i] );
 
691
    table->emit_updateColumn( c, left + i );
 
692
  }
 
693
 
 
694
  m_pView->slotUpdateView( table );
 
695
  QDialog::accept();
 
696
}
 
697
 
 
698
int KSpreadCSVDialog::getHeader(int col)
 
699
{
 
700
  QString header = m_table->horizontalHeader()->label(col);
 
701
 
 
702
  if (header == i18n("Text"))
 
703
    return TEXT;
 
704
  else if (header == i18n("Number"))
 
705
    return NUMBER;
 
706
  else if (header == i18n("Currency"))
 
707
    return CURRENCY;
 
708
  else
 
709
    return DATE;
 
710
}
 
711
 
 
712
QString KSpreadCSVDialog::getText(int row, int col)
 
713
{
 
714
  return m_table->text(row, col);
 
715
}
 
716
 
 
717
void KSpreadCSVDialog::ignoreDuplicatesChanged(int)
 
718
{
 
719
  fillTable();
 
720
}
 
721
 
 
722
#include <kspread_dlg_csv.moc>