23#include <tqbuttongroup.h>
24#include <tqcheckbox.h>
25#include <tqclipboard.h>
26#include <tqcombobox.h>
28#include <tqlineedit.h>
30#include <tqpushbutton.h>
31#include <tqradiobutton.h>
35#include <tdeapplication.h>
37#include <kdialogbase.h>
38#include <tdefiledialog.h>
40#include <tdemessagebox.h>
42#include <kspread_cell.h>
43#include <kspread_doc.h>
44#include <kspread_sheet.h>
45#include <kspread_undo.h>
46#include <kspread_view.h>
48#include "kspread_dlg_csv.h"
52CSVDialog::CSVDialog(
View * parent,
const char * name, TQRect
const & rect, Mode mode)
53 : KDialogBase( parent, name, true, TQString(), Ok|Cancel ),
66 setSizeGripEnabled(
true );
68 TQWidget* page =
new TQWidget(
this );
69 setMainWidget( page );
71 MyDialogLayout =
new TQGridLayout( page, 1, 1, 11, 6,
"MyDialogLayout");
74 int column = m_targetRect.left();
75 Cell* lastCell = m_pView->activeSheet()->getLastCellColumn( column );
77 if( m_targetRect.bottom() > lastCell->
row() )
78 m_targetRect.setBottom( lastCell->
row() );
80 m_sheet =
new TQTable( page,
"m_table" );
82 m_sheet->setNumRows( 0 );
83 m_sheet->setNumCols( 0 );
85 MyDialogLayout->addMultiCellWidget( m_sheet, 3, 3, 0, 3 );
88 m_delimiterBox =
new TQButtonGroup( page,
"m_delimiterBox" );
89 m_delimiterBox->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)1, 0, 0, m_delimiterBox->sizePolicy().hasHeightForWidth() ) );
90 m_delimiterBox->setTitle( i18n(
"Delimiter" ) );
91 m_delimiterBox->setColumnLayout(0, TQt::Vertical );
92 m_delimiterBox->layout()->setSpacing( KDialog::spacingHint() );
93 m_delimiterBox->layout()->setMargin( KDialog::marginHint() );
94 m_delimiterBoxLayout =
new TQGridLayout( m_delimiterBox->layout() );
95 m_delimiterBoxLayout->setAlignment( TQt::AlignTop );
96 MyDialogLayout->addMultiCellWidget( m_delimiterBox, 0, 2, 0, 0 );
98 m_ignoreDuplicates =
new TQCheckBox( page,
"m_ignoreDuplicates" );
99 m_ignoreDuplicates->setText( i18n(
"Ignore duplicate delimiters" ) );
101 MyDialogLayout->addMultiCellWidget( m_ignoreDuplicates, 2, 2, 2, 3 );
103 m_radioComma =
new TQRadioButton( m_delimiterBox,
"m_radioComma" );
104 m_radioComma->setText( i18n(
"Comma" ) );
105 m_radioComma->setChecked(
true );
106 m_delimiterBoxLayout->addWidget( m_radioComma, 0, 0 );
108 m_radioSemicolon =
new TQRadioButton( m_delimiterBox,
"m_radioSemicolon" );
109 m_radioSemicolon->setText( i18n(
"Semicolon" ) );
110 m_delimiterBoxLayout->addWidget( m_radioSemicolon, 0, 1 );
112 m_radioTab =
new TQRadioButton( m_delimiterBox,
"m_radioTab" );
113 m_radioTab->setText( i18n(
"Tabulator" ) );
114 m_delimiterBoxLayout->addWidget( m_radioTab, 1, 0 );
116 m_radioSpace =
new TQRadioButton( m_delimiterBox,
"m_radioSpace" );
117 m_radioSpace->setText( i18n(
"Space" ) );
118 m_delimiterBoxLayout->addWidget( m_radioSpace, 1, 1 );
120 m_radioOther =
new TQRadioButton( m_delimiterBox,
"m_radioOther" );
121 m_radioOther->setText( i18n(
"Other" ) );
122 m_delimiterBoxLayout->addWidget( m_radioOther, 0, 2 );
124 m_delimiterEdit =
new TQLineEdit( m_delimiterBox,
"m_delimiterEdit" );
125 m_delimiterEdit->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)0, (TQSizePolicy::SizeType)0, 0, 0, m_delimiterEdit->sizePolicy().hasHeightForWidth() ) );
126 m_delimiterEdit->setMaximumSize( TQSize( 30, 32767 ) );
127 m_delimiterBoxLayout->addWidget( m_delimiterEdit, 1, 2 );
131 m_formatBox =
new TQButtonGroup( page,
"m_formatBox" );
132 m_formatBox->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)1, 0, 0, m_formatBox->sizePolicy().hasHeightForWidth() ) );
133 m_formatBox->setTitle( i18n(
"Format" ) );
134 m_formatBox->setColumnLayout(0, TQt::Vertical );
135 m_formatBox->layout()->setSpacing( KDialog::spacingHint() );
136 m_formatBox->layout()->setMargin( KDialog::marginHint() );
137 m_formatBoxLayout =
new TQGridLayout( m_formatBox->layout() );
138 m_formatBoxLayout->setAlignment( TQt::AlignTop );
139 MyDialogLayout->addMultiCellWidget( m_formatBox, 0, 2, 1, 1 );
141 m_radioNumber =
new TQRadioButton( m_formatBox,
"m_radioNumber" );
142 m_radioNumber->setText( i18n(
"Number" ) );
143 m_formatBoxLayout->addMultiCellWidget( m_radioNumber, 1, 1, 0, 1 );
145 m_radioText =
new TQRadioButton( m_formatBox,
"m_radioText" );
146 m_radioText->setText( i18n(
"Text" ) );
147 m_radioText->setChecked(
true );
148 m_formatBoxLayout->addWidget( m_radioText, 0, 0 );
150 m_radioCurrency =
new TQRadioButton( m_formatBox,
"m_radioCurrency" );
151 m_radioCurrency->setText( i18n(
"Currency" ) );
152 m_formatBoxLayout->addMultiCellWidget( m_radioCurrency, 0, 0, 1, 2 );
154 m_radioDate =
new TQRadioButton( m_formatBox,
"m_radioDate" );
155 m_radioDate->setText( i18n(
"Date" ) );
156 m_formatBoxLayout->addWidget( m_radioDate, 1, 2 );
158 m_comboLine =
new TQComboBox(
false, page,
"m_comboLine" );
159 m_comboLine->insertItem( i18n(
"1" ) );
160 m_comboLine->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0, m_comboLine->sizePolicy().hasHeightForWidth() ) );
162 MyDialogLayout->addWidget( m_comboLine, 1, 3 );
164 m_comboQuote =
new TQComboBox(
false, page,
"m_comboQuote" );
165 m_comboQuote->insertItem( i18n(
"\"" ) );
166 m_comboQuote->insertItem( i18n(
"'" ) );
167 m_comboQuote->insertItem( i18n(
"None" ) );
168 m_comboQuote->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0, m_comboQuote->sizePolicy().hasHeightForWidth() ) );
170 MyDialogLayout->addWidget( m_comboQuote, 1, 2 );
171 TQSpacerItem* spacer_2 =
new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Preferred );
172 MyDialogLayout->addItem( spacer_2, 2, 3 );
174 TextLabel3 =
new TQLabel( page,
"TextLabel3" );
175 TextLabel3->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0, TextLabel3->sizePolicy().hasHeightForWidth() ) );
176 TextLabel3->setText( i18n(
"Start at line:" ) );
178 MyDialogLayout->addWidget( TextLabel3, 0, 3 );
180 TextLabel2 =
new TQLabel( page,
"TextLabel2" );
181 TextLabel2->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType)1, (TQSizePolicy::SizeType)0, 0, 0, TextLabel2->sizePolicy().hasHeightForWidth() ) );
182 TextLabel2->setText( i18n(
"Textquote:" ) );
184 MyDialogLayout->addWidget( TextLabel2, 0, 2 );
186 if ( m_mode == Clipboard )
188 setCaption( i18n(
"Inserting From Clipboard" ) );
189 TQMimeSource * mime = TQApplication::clipboard()->data();
192 KMessageBox::information(
this, i18n(
"There is no data in the clipboard.") );
197 if ( !mime->provides(
"text/plain" ) )
199 KMessageBox::information(
this, i18n(
"There is no usable data in the clipboard.") );
203 m_fileArray = TQByteArray(mime->encodedData(
"text/plain" ) );
205 else if ( mode == File )
207 setCaption( i18n(
"Inserting Text File" ) );
208 TQString file = KFileDialog::getOpenFileName(
":",
212 if ( file.isEmpty() )
214 actionButton( Ok )->setEnabled(
false );
219 if (!in.open(IO_ReadOnly))
221 KMessageBox::sorry(
this, i18n(
"Cannot open input file.") );
223 actionButton( Ok )->setEnabled(
false );
227 m_fileArray = TQByteArray(in.size());
228 in.readBlock(m_fileArray.data(), in.size());
233 setCaption( i18n(
"Text to Columns" ) );
236 Sheet * sheet = m_pView->activeSheet();
237 int col = m_targetRect.left();
238 for (
int i = m_targetRect.top(); i <= m_targetRect.bottom(); ++i)
240 cell = sheet->cellAt( col, i );
243 m_data += cell->strOutText();
254 m_sheet->setSelectionMode(TQTable::NoSelection);
256 connect(m_formatBox, TQ_SIGNAL(clicked(
int)),
257 this, TQ_SLOT(formatClicked(
int)));
258 connect(m_delimiterBox, TQ_SIGNAL(clicked(
int)),
259 this, TQ_SLOT(delimiterClicked(
int)));
260 connect(m_delimiterEdit, TQ_SIGNAL(returnPressed()),
261 this, TQ_SLOT(returnPressed()));
262 connect(m_delimiterEdit, TQ_SIGNAL(textChanged (
const TQString & )),
263 this, TQ_SLOT(textChanged (
const TQString & ) ));
264 connect(m_comboLine, TQ_SIGNAL(activated(
const TQString&)),
265 this, TQ_SLOT(lineSelected(
const TQString&)));
266 connect(m_comboQuote, TQ_SIGNAL(activated(
const TQString&)),
267 this, TQ_SLOT(textquoteSelected(
const TQString&)));
268 connect(m_sheet, TQ_SIGNAL(currentChanged(
int,
int)),
269 this, TQ_SLOT(currentCellChanged(
int,
int)));
270 connect(m_ignoreDuplicates, TQ_SIGNAL(stateChanged(
int)),
271 this, TQ_SLOT(ignoreDuplicatesChanged(
int)));
274CSVDialog::~CSVDialog()
279bool CSVDialog::cancelled()
284void CSVDialog::fillSheet()
287 bool lastCharDelimiter =
false;
288 bool ignoreDups = m_ignoreDuplicates->isChecked();
289 enum { S_START, S_QUOTED_FIELD, S_MAYBE_END_OF_QUOTED_FIELD, S_END_OF_QUOTED_FIELD,
290 S_MAYBE_NORMAL_FIELD, S_NORMAL_FIELD } state = S_START;
295 for (row = 0; row < m_sheet->numRows(); ++row)
296 for (column = 0; column < m_sheet->numCols(); ++column)
297 m_sheet->clearCell(row, column);
300 if (m_mode != Column)
303 m_data = TQString(m_fileArray);
304 m_fileArray.resize(0);
307 TQTextStream inputStream(m_data, IO_ReadOnly);
308 inputStream.setEncoding(TQTextStream::Locale);
310 while (!inputStream.atEnd())
314 if (x ==
'\r') inputStream >> x;
319 if (x == m_textquote)
321 state = S_QUOTED_FIELD;
323 else if (x == m_delimiter)
325 if ((ignoreDups ==
false) || (lastCharDelimiter ==
false))
327 lastCharDelimiter =
true;
337 state = S_MAYBE_NORMAL_FIELD;
340 case S_QUOTED_FIELD :
341 if (x == m_textquote)
343 state = S_MAYBE_END_OF_QUOTED_FIELD;
347 setText(row - m_startline, column, field);
356 if ((ignoreDups ==
false) || (lastCharDelimiter ==
false))
358 lastCharDelimiter =
true;
367 case S_MAYBE_END_OF_QUOTED_FIELD :
368 if (x == m_textquote)
371 state = S_QUOTED_FIELD;
373 else if (x == m_delimiter || x ==
'\n')
375 setText(row - m_startline, column, field);
384 if ((ignoreDups ==
false) || (lastCharDelimiter ==
false))
386 lastCharDelimiter =
true;
392 state = S_END_OF_QUOTED_FIELD;
395 case S_END_OF_QUOTED_FIELD :
396 if (x == m_delimiter || x ==
'\n')
398 setText(row - m_startline, column, field);
407 if ((ignoreDups ==
false) || (lastCharDelimiter ==
false))
409 lastCharDelimiter =
true;
415 state = S_END_OF_QUOTED_FIELD;
418 case S_MAYBE_NORMAL_FIELD :
419 if (x == m_textquote)
422 state = S_QUOTED_FIELD;
425 case S_NORMAL_FIELD :
426 if (x == m_delimiter || x ==
'\n')
428 setText(row - m_startline, column, field);
437 if ((ignoreDups ==
false) || (lastCharDelimiter ==
false))
439 lastCharDelimiter =
true;
448 if (x != m_delimiter)
449 lastCharDelimiter =
false;
453 if (field.length() > 0)
455 setText(row - m_startline, column, field);
460 adjustRows( row - m_startline );
462 for (column = 0; column < m_sheet->numCols(); ++column)
464 TQString header = m_sheet->horizontalHeader()->label(column);
465 if (header != i18n(
"Text") && header != i18n(
"Number") &&
466 header != i18n(
"Date") && header != i18n(
"Currency"))
467 m_sheet->horizontalHeader()->setLabel(column, i18n(
"Text"));
469 m_sheet->adjustColumn(column);
473void CSVDialog::fillComboBox()
475 m_comboLine->clear();
476 for (
int row = 0; row < m_sheet->numRows(); ++row)
477 m_comboLine->insertItem(TQString::number(row + 1), row);
480void CSVDialog::setText(
int row,
int col,
const TQString& text)
485 if (m_sheet->numRows() < row) {
486 m_sheet->setNumRows(row+5000);
490 if (m_sheet->numCols() < col)
491 m_sheet->setNumCols(col);
493 m_sheet->setText(row - 1, col - 1, text);
499void CSVDialog::adjustRows(
int iRows)
503 m_sheet->setNumRows( iRows );
508void CSVDialog::returnPressed()
510 if (m_delimiterBox->id(m_delimiterBox->selected()) != 4)
513 m_delimiter = m_delimiterEdit->text();
517void CSVDialog::textChanged (
const TQString & )
519 m_radioOther->setChecked (
true );
523void CSVDialog::formatClicked(
int id)
530 header = i18n(
"Text");
533 header = i18n(
"Number");
536 header = i18n(
"Date");
539 header = i18n(
"Currency");
543 m_sheet->horizontalHeader()->setLabel(m_sheet->currentColumn(), header);
546void CSVDialog::delimiterClicked(
int id)
554 m_delimiter = m_delimiterEdit->text();
570void CSVDialog::textquoteSelected(
const TQString& mark)
572 if (mark == i18n(
"none"))
575 m_textquote = mark[0];
580void CSVDialog::lineSelected(
const TQString& line)
582 m_startline = line.toInt() - 1;
586void CSVDialog::currentCellChanged(
int,
int col)
589 TQString header = m_sheet->horizontalHeader()->label(col);
591 if (header == i18n(
"Text"))
593 else if (header == i18n(
"Number"))
595 else if (header == i18n(
"Date"))
600 m_formatBox->setButton(
id);
603void CSVDialog::accept()
605 Sheet * sheet = m_pView->activeSheet();
606 TQString csv_delimiter = TQString();
609 int numRows = m_sheet->numRows();
610 int numCols = m_sheet->numCols();
615 if ( (numCols > m_targetRect.width()) && (m_targetRect.width() > 1) )
617 numCols = m_targetRect.width();
620 m_targetRect.setRight( m_targetRect.left() + numCols );
622 if ( (numRows > m_targetRect.height()) && (m_targetRect.height() > 1) )
623 numRows = m_targetRect.height();
625 m_targetRect.setBottom( m_targetRect.top() + numRows );
627 if ( numRows == 1 && numCols == 1)
629 Doc * doc = m_pView->doc();
630 cell = sheet->nonDefaultCell( m_targetRect.left(), m_targetRect.top() );
633 UndoSetText * undo =
new UndoSetText( doc, sheet , cell->
text(), m_targetRect.left(),
640 UndoChangeAreaTextCell * undo =
new UndoChangeAreaTextCell( m_pView->doc(), sheet , m_targetRect );
647 int left = m_targetRect.left();
648 int top = m_targetRect.top();
650 TQMemArray<double> widths( numCols );
651 for ( i = 0; i < numCols; ++i )
653 ColumnFormat * c = sheet->nonDefaultColumnFormat( left + i );
654 widths[i] = c->dblWidth();
657 for (
int row = 0; row < numRows; ++row)
659 for (
int col = 0; col < numCols; ++col)
661 cell = sheet->nonDefaultCell( left + col, top + row );
664 TQFontMetrics fm = sheet->painter().fontMetrics();
665 double w = fm.width( cell->strOutText() );
668 TQFontMetrics fm( cell->
format()->textFont( left + col, top + row ) );
669 w = fm.width(
'x') * (double) getText( row, col ).length();
672 if ( w > widths[col] )
675 cell->
format()->setFormatType(Generic_format);
700 for ( i = 0; i < numCols; ++i )
702 ColumnFormat * c = sheet->nonDefaultColumnFormat( left + i );
703 c->setDblWidth( widths[i] );
704 sheet->emit_updateColumn( c, left + i );
707 m_pView->slotUpdateView( sheet );
711int CSVDialog::getHeader(
int col)
713 TQString header = m_sheet->horizontalHeader()->label(col);
715 if (header == i18n(
"Text"))
717 else if (header == i18n(
"Number"))
719 else if (header == i18n(
"Currency"))
725TQString CSVDialog::getText(
int row,
int col)
727 return m_sheet->text(row, col);
730void CSVDialog::ignoreDuplicatesChanged(
int)
735#include "kspread_dlg_csv.moc"
For every cell in the spread sheet there is a Cell object.
FormatType formatType() const
Return the format of this cell.
TQString text() const
Return the text the user entered.
int row() const
Returns the cell's row.
bool isEmpty() const
Returns true if this cell has no content, i.e no text and no formula.
void setCellText(const TQString &_text, bool asString=false)
The high-level method for setting text, when the user inputs it.
Format * format() const
Returns the format object of this cell.
bool isDefault() const
Returns true if this is a default cell (with row and column equal to zero).
This class holds the data that makes up a spreadsheet.
bool undoLocked() const
Returns true if undo buffer is locked.
void emitBeginOperation(bool waitCursor)
Functions to begin a kspread 'operation'.
void addCommand(KCommand *command)
Adds a command to the command history.
The View class displays a KSpread document.