You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
768 lines
20 KiB
768 lines
20 KiB
15 years ago
|
/*
|
||
13 years ago
|
This file is part of libtdepim.
|
||
15 years ago
|
|
||
|
Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
|
||
|
Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
// Generic CSV import. Please do not add application specific code to this
|
||
|
// class. Application specific code should go to a subclass provided by the
|
||
|
// application using this dialog.
|
||
|
|
||
15 years ago
|
#include <tqbuttongroup.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqlabel.h>
|
||
|
#include <tqlayout.h>
|
||
|
#include <tqlineedit.h>
|
||
|
#include <tqlistview.h>
|
||
|
#include <tqradiobutton.h>
|
||
|
#include <tqregexp.h>
|
||
|
#include <tqtable.h>
|
||
|
#include <tqtextstream.h>
|
||
|
#include <tqvbox.h>
|
||
15 years ago
|
|
||
|
#include <kapplication.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <kcombobox.h>
|
||
|
#include <kinputdialog.h>
|
||
|
#include <klineedit.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kprogress.h>
|
||
|
#include <ksimpleconfig.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kurlrequester.h>
|
||
|
#include <kfiledialog.h>
|
||
|
|
||
|
#include "kimportdialog.h"
|
||
|
#include "kimportdialog.moc"
|
||
|
|
||
15 years ago
|
KImportColumn::KImportColumn(KImportDialog *dlg,const TQString &header, int count)
|
||
15 years ago
|
: m_maxCount(count),
|
||
|
m_refCount(0),
|
||
|
m_header(header),
|
||
|
mDialog(dlg)
|
||
|
{
|
||
|
mFormats.append(FormatPlain);
|
||
|
mFormats.append(FormatUnquoted);
|
||
|
// mFormats.append(FormatBracketed);
|
||
|
|
||
|
mDefaultFormat = FormatUnquoted;
|
||
|
|
||
|
mDialog->addColumn(this);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQValueList<int> KImportColumn::formats()
|
||
15 years ago
|
{
|
||
|
return mFormats;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KImportColumn::formatName(int format)
|
||
15 years ago
|
{
|
||
|
switch (format) {
|
||
|
case FormatPlain:
|
||
|
return i18n("Plain");
|
||
|
case FormatUnquoted:
|
||
|
return i18n("Unquoted");
|
||
|
case FormatBracketed:
|
||
|
return i18n("Bracketed");
|
||
|
default:
|
||
|
return i18n("Undefined");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int KImportColumn::defaultFormat()
|
||
|
{
|
||
|
return mDefaultFormat;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KImportColumn::preview(const TQString &value, int format)
|
||
15 years ago
|
{
|
||
|
if (format == FormatBracketed) {
|
||
|
return "(" + value + ")";
|
||
|
} else if (format == FormatUnquoted) {
|
||
|
if (value.left(1) == "\"" && value.right(1) == "\"") {
|
||
|
return value.mid(1,value.length()-2);
|
||
|
} else {
|
||
|
return value;
|
||
|
}
|
||
|
} else {
|
||
|
return value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KImportColumn::addColId(int id)
|
||
|
{
|
||
|
mColIds.append(id);
|
||
|
}
|
||
|
|
||
|
void KImportColumn::removeColId(int id)
|
||
|
{
|
||
|
mColIds.remove(id);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQValueList<int> KImportColumn::colIdList()
|
||
15 years ago
|
{
|
||
|
return mColIds;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KImportColumn::convert()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<int>::ConstIterator it = mColIds.begin();
|
||
15 years ago
|
if (it == mColIds.end()) return "";
|
||
|
else return mDialog->cell(*it);
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
class ColumnItem : public TQListViewItem {
|
||
15 years ago
|
public:
|
||
14 years ago
|
ColumnItem(KImportColumn *col,TQListView *parent) : TQListViewItem(parent), mColumn(col)
|
||
15 years ago
|
{
|
||
|
setText(0,mColumn->header());
|
||
|
}
|
||
|
|
||
|
KImportColumn *column() { return mColumn; }
|
||
|
|
||
|
private:
|
||
|
KImportColumn *mColumn;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
This is a generic class for importing line-oriented data from text files. It
|
||
|
provides a dialog for file selection, preview, separator selection and column
|
||
|
assignment as well as generic conversion routines. For conversion to special
|
||
|
data objects, this class has to be inherited by a special class, which
|
||
|
reimplements the convertRow() function.
|
||
|
*/
|
||
14 years ago
|
KImportDialog::KImportDialog(TQWidget* parent)
|
||
|
: KDialogBase(parent,"importdialog",true,i18n("Import Text File"),Ok|Cancel),
|
||
15 years ago
|
mSeparator(","),
|
||
|
mCurrentRow(0)
|
||
|
{
|
||
|
mData.setAutoDelete( true );
|
||
|
|
||
15 years ago
|
TQVBox *topBox = new TQVBox(this);
|
||
15 years ago
|
setMainWidget(topBox);
|
||
|
topBox->setSpacing(spacingHint());
|
||
|
|
||
15 years ago
|
TQHBox *fileBox = new TQHBox(topBox);
|
||
15 years ago
|
fileBox->setSpacing(spacingHint());
|
||
15 years ago
|
new TQLabel(i18n("File to import:"),fileBox);
|
||
15 years ago
|
KURLRequester *urlRequester = new KURLRequester(fileBox);
|
||
|
urlRequester->setFilter( "*.csv" );
|
||
15 years ago
|
connect(urlRequester,TQT_SIGNAL(returnPressed(const TQString &)),
|
||
|
TQT_SLOT(setFile(const TQString &)));
|
||
|
connect(urlRequester,TQT_SIGNAL(urlSelected(const TQString &)),
|
||
|
TQT_SLOT(setFile(const TQString &)));
|
||
|
connect(urlRequester->lineEdit(),TQT_SIGNAL(textChanged ( const TQString & )),
|
||
|
TQT_SLOT(slotUrlChanged(const TQString & )));
|
||
|
mTable = new TQTable(5,5,topBox);
|
||
15 years ago
|
mTable->setMinimumHeight( 150 );
|
||
15 years ago
|
connect(mTable,TQT_SIGNAL(selectionChanged()),TQT_SLOT(tableSelected()));
|
||
15 years ago
|
|
||
15 years ago
|
TQHBox *separatorBox = new TQHBox( topBox );
|
||
15 years ago
|
separatorBox->setSpacing( spacingHint() );
|
||
|
|
||
15 years ago
|
new TQLabel( i18n( "Separator:" ), separatorBox );
|
||
15 years ago
|
|
||
|
mSeparatorCombo = new KComboBox( separatorBox );
|
||
|
mSeparatorCombo->insertItem( "," );
|
||
|
mSeparatorCombo->insertItem( i18n( "Tab" ) );
|
||
|
mSeparatorCombo->insertItem( i18n( "Space" ) );
|
||
|
mSeparatorCombo->insertItem( "=" );
|
||
|
mSeparatorCombo->insertItem( ";" );
|
||
15 years ago
|
connect(mSeparatorCombo, TQT_SIGNAL( activated(int) ),
|
||
|
this, TQT_SLOT( separatorClicked(int) ) );
|
||
15 years ago
|
mSeparatorCombo->setCurrentItem( 0 );
|
||
|
|
||
15 years ago
|
TQHBox *rowsBox = new TQHBox( topBox );
|
||
15 years ago
|
rowsBox->setSpacing( spacingHint() );
|
||
|
|
||
15 years ago
|
new TQLabel( i18n( "Import starts at row:" ), rowsBox );
|
||
|
mStartRow = new TQSpinBox( rowsBox );
|
||
15 years ago
|
mStartRow->setMinValue( 1 );
|
||
|
/*
|
||
15 years ago
|
new TQLabel( i18n( "And ends at row:" ), rowsBox );
|
||
|
mEndRow = new TQSpinBox( rowsBox );
|
||
15 years ago
|
mEndRow->setMinValue( 1 );
|
||
|
*/
|
||
15 years ago
|
TQVBox *assignBox = new TQVBox(topBox);
|
||
15 years ago
|
assignBox->setSpacing(spacingHint());
|
||
|
|
||
15 years ago
|
TQHBox *listsBox = new TQHBox(assignBox);
|
||
15 years ago
|
listsBox->setSpacing(spacingHint());
|
||
|
|
||
15 years ago
|
mHeaderList = new TQListView(listsBox);
|
||
15 years ago
|
mHeaderList->addColumn(i18n("Header"));
|
||
15 years ago
|
connect(mHeaderList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
|
||
|
this, TQT_SLOT(headerSelected(TQListViewItem*)));
|
||
|
connect(mHeaderList,TQT_SIGNAL(doubleClicked(TQListViewItem*)),
|
||
|
TQT_SLOT(assignColumn(TQListViewItem *)));
|
||
15 years ago
|
|
||
|
mFormatCombo = new KComboBox( listsBox );
|
||
|
mFormatCombo->setDuplicatesEnabled( false );
|
||
|
|
||
15 years ago
|
TQPushButton *assignButton = new TQPushButton(i18n("Assign to Selected Column"),
|
||
15 years ago
|
assignBox);
|
||
15 years ago
|
connect(assignButton,TQT_SIGNAL(clicked()),TQT_SLOT(assignColumn()));
|
||
15 years ago
|
|
||
15 years ago
|
TQPushButton *removeButton = new TQPushButton(i18n("Remove Assignment From Selected Column"),
|
||
15 years ago
|
assignBox);
|
||
15 years ago
|
connect(removeButton,TQT_SIGNAL(clicked()),TQT_SLOT(removeColumn()));
|
||
15 years ago
|
|
||
15 years ago
|
TQPushButton *assignTemplateButton = new TQPushButton(i18n("Assign with Template..."),
|
||
15 years ago
|
assignBox);
|
||
15 years ago
|
connect(assignTemplateButton,TQT_SIGNAL(clicked()),TQT_SLOT(assignTemplate()));
|
||
15 years ago
|
|
||
15 years ago
|
TQPushButton *saveTemplateButton = new TQPushButton(i18n("Save Current Template"),
|
||
15 years ago
|
assignBox);
|
||
15 years ago
|
connect(saveTemplateButton,TQT_SIGNAL(clicked()),TQT_SLOT(saveTemplate()));
|
||
15 years ago
|
|
||
|
resize(500,300);
|
||
|
|
||
15 years ago
|
connect(this,TQT_SIGNAL(okClicked()),TQT_SLOT(applyConverter()));
|
||
|
connect(this,TQT_SIGNAL(applyClicked()),TQT_SLOT(applyConverter()));
|
||
15 years ago
|
enableButtonOK(!urlRequester->lineEdit()->text().isEmpty());
|
||
|
}
|
||
|
|
||
15 years ago
|
void KImportDialog::slotUrlChanged(const TQString & text)
|
||
15 years ago
|
{
|
||
|
enableButtonOK(!text.isEmpty());
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KImportDialog::setFile(const TQString& file)
|
||
15 years ago
|
{
|
||
|
enableButtonOK(!file.isEmpty());
|
||
|
kdDebug(5300) << "KImportDialog::setFile(): " << file << endl;
|
||
|
|
||
15 years ago
|
TQFile f(file);
|
||
15 years ago
|
|
||
|
if (f.open(IO_ReadOnly)) {
|
||
|
mFile = "";
|
||
15 years ago
|
TQTextStream t(&f);
|
||
15 years ago
|
mFile = t.read();
|
||
|
// while (!t.eof()) mFile.append(t.readLine());
|
||
|
f.close();
|
||
|
|
||
|
readFile();
|
||
|
|
||
|
// mEndRow->setValue( mData.count() );
|
||
|
|
||
|
return true;
|
||
|
} else {
|
||
|
kdDebug(5300) << " Open failed" << endl;
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KImportDialog::registerColumns()
|
||
|
{
|
||
15 years ago
|
TQPtrListIterator<KImportColumn> colIt(mColumns);
|
||
15 years ago
|
for (; colIt.current(); ++colIt) {
|
||
|
new ColumnItem(*colIt,mHeaderList);
|
||
|
}
|
||
|
mHeaderList->setSelected(mHeaderList->firstChild(),true);
|
||
|
}
|
||
|
|
||
|
void KImportDialog::fillTable()
|
||
|
{
|
||
|
// kdDebug(5300) << "KImportDialog::fillTable()" << endl;
|
||
|
|
||
|
int row, column;
|
||
|
|
||
|
for (row = 0; row < mTable->numRows(); ++row)
|
||
|
for (column = 0; column < mTable->numCols(); ++column)
|
||
|
mTable->clearCell(row, column);
|
||
|
|
||
|
for ( row = 0; row < int(mData.count()); ++row ) {
|
||
15 years ago
|
TQValueVector<TQString> *rowVector = mData[ row ];
|
||
15 years ago
|
for( column = 0; column < int(rowVector->size()); ++column ) {
|
||
|
setCellText( row, column, rowVector->at( column ) );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KImportDialog::readFile( int rows )
|
||
|
{
|
||
|
kdDebug(5300) << "KImportDialog::readFile(): " << rows << endl;
|
||
|
|
||
|
mData.clear();
|
||
|
|
||
|
int row, column;
|
||
14 years ago
|
enum { S_START, S_TQUOTED_FIELD, S_MAYBE_END_OF_TQUOTED_FIELD, S_END_OF_TQUOTED_FIELD,
|
||
15 years ago
|
S_MAYBE_NORMAL_FIELD, S_NORMAL_FIELD } state = S_START;
|
||
|
|
||
15 years ago
|
TQChar m_textquote = '"';
|
||
15 years ago
|
int m_startline = 0;
|
||
|
|
||
15 years ago
|
TQChar x;
|
||
|
TQString field = "";
|
||
15 years ago
|
|
||
|
row = column = 0;
|
||
15 years ago
|
TQTextStream inputStream(mFile, IO_ReadOnly);
|
||
|
inputStream.setEncoding(TQTextStream::Locale);
|
||
15 years ago
|
|
||
|
KProgressDialog pDialog(this, 0, i18n("Loading Progress"),
|
||
|
i18n("Please wait while the file is loaded."), true);
|
||
|
pDialog.setAllowCancel(true);
|
||
|
pDialog.showCancelButton(true);
|
||
|
pDialog.setAutoClose(true);
|
||
|
|
||
|
KProgress *progress = pDialog.progressBar();
|
||
14 years ago
|
progress->setTotalSteps( mFile.contains(mSeparator, false) );
|
||
15 years ago
|
progress->setValue(0);
|
||
|
int progressValue = 0;
|
||
|
|
||
|
if (progress->totalSteps() > 0) // We have data
|
||
|
pDialog.show();
|
||
|
|
||
|
while (!inputStream.atEnd() && !pDialog.wasCancelled()) {
|
||
|
inputStream >> x; // read one char
|
||
|
|
||
|
// update the dialog if needed
|
||
|
if (x == mSeparator)
|
||
|
{
|
||
|
progress->setValue(progressValue++);
|
||
14 years ago
|
if (progressValue % 15 == 0) // try not to constantly tqrepaint
|
||
15 years ago
|
kapp->processEvents();
|
||
|
}
|
||
|
|
||
|
if (x == '\r') inputStream >> x; // eat '\r', to handle DOS/LOSEDOWS files correctly
|
||
|
|
||
|
switch (state) {
|
||
|
case S_START :
|
||
|
if (x == m_textquote) {
|
||
|
field += x;
|
||
14 years ago
|
state = S_TQUOTED_FIELD;
|
||
15 years ago
|
} else if (x == mSeparator) {
|
||
|
++column;
|
||
|
} else if (x == '\n') {
|
||
|
++row;
|
||
|
column = 0;
|
||
|
} else {
|
||
|
field += x;
|
||
|
state = S_MAYBE_NORMAL_FIELD;
|
||
|
}
|
||
|
break;
|
||
14 years ago
|
case S_TQUOTED_FIELD :
|
||
15 years ago
|
if (x == m_textquote) {
|
||
|
field += x;
|
||
14 years ago
|
state = S_MAYBE_END_OF_TQUOTED_FIELD;
|
||
15 years ago
|
} else if (x == '\n') {
|
||
|
setData(row - m_startline, column, field);
|
||
|
field = "";
|
||
|
if (x == '\n') {
|
||
|
++row;
|
||
|
column = 0;
|
||
|
} else {
|
||
|
++column;
|
||
|
}
|
||
|
state = S_START;
|
||
|
} else {
|
||
|
field += x;
|
||
|
}
|
||
|
break;
|
||
14 years ago
|
case S_MAYBE_END_OF_TQUOTED_FIELD :
|
||
15 years ago
|
if (x == m_textquote) {
|
||
|
field += x;
|
||
14 years ago
|
state = S_TQUOTED_FIELD;
|
||
15 years ago
|
} else if (x == mSeparator || x == '\n') {
|
||
|
setData(row - m_startline, column, field);
|
||
|
field = "";
|
||
|
if (x == '\n') {
|
||
|
++row;
|
||
|
column = 0;
|
||
|
} else {
|
||
|
++column;
|
||
|
}
|
||
|
state = S_START;
|
||
|
} else {
|
||
14 years ago
|
state = S_END_OF_TQUOTED_FIELD;
|
||
15 years ago
|
}
|
||
|
break;
|
||
14 years ago
|
case S_END_OF_TQUOTED_FIELD :
|
||
15 years ago
|
if (x == mSeparator || x == '\n') {
|
||
|
setData(row - m_startline, column, field);
|
||
|
field = "";
|
||
|
if (x == '\n') {
|
||
|
++row;
|
||
|
column = 0;
|
||
|
} else {
|
||
|
++column;
|
||
|
}
|
||
|
state = S_START;
|
||
|
} else {
|
||
14 years ago
|
state = S_END_OF_TQUOTED_FIELD;
|
||
15 years ago
|
}
|
||
|
break;
|
||
|
case S_MAYBE_NORMAL_FIELD :
|
||
|
if (x == m_textquote) {
|
||
|
field = "";
|
||
14 years ago
|
state = S_TQUOTED_FIELD;
|
||
15 years ago
|
}
|
||
|
case S_NORMAL_FIELD :
|
||
|
if (x == mSeparator || x == '\n') {
|
||
|
setData(row - m_startline, column, field);
|
||
|
field = "";
|
||
|
if (x == '\n') {
|
||
|
++row;
|
||
|
column = 0;
|
||
|
} else {
|
||
|
++column;
|
||
|
}
|
||
|
state = S_START;
|
||
|
} else {
|
||
|
field += x;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( rows > 0 && row > rows ) break;
|
||
|
}
|
||
|
|
||
|
fillTable();
|
||
|
}
|
||
|
|
||
15 years ago
|
void KImportDialog::setCellText(int row, int col, const TQString& text)
|
||
15 years ago
|
{
|
||
|
if (row < 0) return;
|
||
|
|
||
|
if ((mTable->numRows() - 1) < row) mTable->setNumRows(row + 1);
|
||
|
if ((mTable->numCols() - 1) < col) mTable->setNumCols(col + 1);
|
||
|
|
||
14 years ago
|
KImportColumn *c = mColumnDict.find(col);
|
||
15 years ago
|
TQString formattedText;
|
||
15 years ago
|
if (c) formattedText = c->preview(text,findFormat(col));
|
||
|
else formattedText = text;
|
||
|
mTable->setText(row, col, formattedText);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KImportDialog::formatSelected(TQListViewItem*)
|
||
15 years ago
|
{
|
||
|
// kdDebug(5300) << "KImportDialog::formatSelected()" << endl;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KImportDialog::headerSelected(TQListViewItem* item)
|
||
15 years ago
|
{
|
||
|
KImportColumn *col = ((ColumnItem *)item)->column();
|
||
|
|
||
|
if (!col) return;
|
||
|
|
||
|
mFormatCombo->clear();
|
||
|
|
||
15 years ago
|
TQValueList<int> formats = col->formats();
|
||
15 years ago
|
|
||
15 years ago
|
TQValueList<int>::ConstIterator it = formats.begin();
|
||
|
TQValueList<int>::ConstIterator end = formats.end();
|
||
15 years ago
|
while(it != end) {
|
||
|
mFormatCombo->insertItem( col->formatName(*it), *it - 1 );
|
||
|
++it;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQTableSelection selection = mTable->selection(mTable->currentSelection());
|
||
15 years ago
|
|
||
|
updateFormatSelection(selection.leftCol());
|
||
|
}
|
||
|
|
||
|
void KImportDialog::updateFormatSelection(int column)
|
||
|
{
|
||
|
int format = findFormat(column);
|
||
|
|
||
|
if ( format == KImportColumn::FormatUndefined )
|
||
|
mFormatCombo->setCurrentItem( 0 );
|
||
|
else
|
||
|
mFormatCombo->setCurrentItem( format - 1 );
|
||
|
}
|
||
|
|
||
|
void KImportDialog::tableSelected()
|
||
|
{
|
||
15 years ago
|
TQTableSelection selection = mTable->selection(mTable->currentSelection());
|
||
15 years ago
|
|
||
15 years ago
|
TQListViewItem *item = mHeaderList->firstChild();
|
||
14 years ago
|
KImportColumn *col = mColumnDict.find(selection.leftCol());
|
||
15 years ago
|
if (col) {
|
||
|
while(item) {
|
||
|
if (item->text(0) == col->header()) {
|
||
|
break;
|
||
|
}
|
||
|
item = item->nextSibling();
|
||
|
}
|
||
|
}
|
||
|
if (item) {
|
||
|
mHeaderList->setSelected(item,true);
|
||
|
}
|
||
|
|
||
|
updateFormatSelection(selection.leftCol());
|
||
|
}
|
||
|
|
||
|
void KImportDialog::separatorClicked(int id)
|
||
|
{
|
||
|
switch(id) {
|
||
|
case 0:
|
||
|
mSeparator = ',';
|
||
|
break;
|
||
|
case 1:
|
||
|
mSeparator = '\t';
|
||
|
break;
|
||
|
case 2:
|
||
|
mSeparator = ' ';
|
||
|
break;
|
||
|
case 3:
|
||
|
mSeparator = '=';
|
||
|
break;
|
||
|
case 4:
|
||
|
mSeparator = ';';
|
||
|
break;
|
||
|
default:
|
||
|
mSeparator = ',';
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
readFile();
|
||
|
}
|
||
|
|
||
15 years ago
|
void KImportDialog::assignColumn(TQListViewItem *item)
|
||
15 years ago
|
{
|
||
|
if (!item) return;
|
||
|
|
||
|
// kdDebug(5300) << "KImportDialog::assignColumn(): current Col: " << mTable->currentColumn()
|
||
|
// << endl;
|
||
|
|
||
|
ColumnItem *colItem = (ColumnItem *)item;
|
||
|
|
||
15 years ago
|
TQTableSelection selection = mTable->selection(mTable->currentSelection());
|
||
15 years ago
|
|
||
|
// kdDebug(5300) << " l: " << selection.leftCol() << " r: " << selection.rightCol() << endl;
|
||
|
|
||
|
for(int i=selection.leftCol();i<=selection.rightCol();++i) {
|
||
|
if (i >= 0) {
|
||
|
mTable->horizontalHeader()->setLabel(i,colItem->text(0));
|
||
14 years ago
|
mColumnDict.replace(i,colItem->column());
|
||
15 years ago
|
int format = mFormatCombo->currentItem() + 1;
|
||
14 years ago
|
mFormats.replace(i,format);
|
||
15 years ago
|
colItem->column()->addColId(i);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
readFile();
|
||
|
}
|
||
|
|
||
|
void KImportDialog::assignColumn()
|
||
|
{
|
||
|
assignColumn(mHeaderList->currentItem());
|
||
|
}
|
||
|
|
||
|
void KImportDialog::assignTemplate()
|
||
|
{
|
||
15 years ago
|
TQMap<uint,int> columnMap;
|
||
|
TQMap<TQString, TQString> fileMap;
|
||
|
TQStringList templates;
|
||
15 years ago
|
|
||
|
// load all template files
|
||
15 years ago
|
TQStringList list = KGlobal::dirs()->findAllResources( "data" , TQString( kapp->name() ) +
|
||
15 years ago
|
"/csv-templates/*.desktop", true, true );
|
||
|
|
||
15 years ago
|
for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it )
|
||
15 years ago
|
{
|
||
|
KSimpleConfig config( *it, true );
|
||
|
|
||
|
if ( !config.hasGroup( "csv column map" ) )
|
||
|
continue;
|
||
|
|
||
|
config.setGroup( "Misc" );
|
||
|
templates.append( config.readEntry( "Name" ) );
|
||
|
fileMap.insert( config.readEntry( "Name" ), *it );
|
||
|
}
|
||
|
|
||
|
// let the user chose, what to take
|
||
|
bool ok = false;
|
||
15 years ago
|
TQString tmp;
|
||
15 years ago
|
tmp = KInputDialog::getItem( i18n( "Template Selection" ),
|
||
|
i18n( "Please select a template, that matches the CSV file:" ),
|
||
|
templates, 0, false, &ok, this );
|
||
|
|
||
|
if ( !ok )
|
||
|
return;
|
||
|
|
||
|
KSimpleConfig config( fileMap[ tmp ], true );
|
||
|
config.setGroup( "General" );
|
||
|
uint numColumns = config.readUnsignedNumEntry( "Columns" );
|
||
|
int format = config.readNumEntry( "Format" );
|
||
|
|
||
|
// create the column map
|
||
|
config.setGroup( "csv column map" );
|
||
|
for ( uint i = 0; i < numColumns; ++i ) {
|
||
15 years ago
|
int col = config.readNumEntry( TQString::number( i ) );
|
||
15 years ago
|
columnMap.insert( i, col );
|
||
|
}
|
||
|
|
||
|
// apply the column map
|
||
|
for ( uint i = 0; i < columnMap.count(); ++i ) {
|
||
|
int tableColumn = columnMap[i];
|
||
|
if ( tableColumn == -1 )
|
||
|
continue;
|
||
|
KImportColumn *col = mColumns.at(i);
|
||
|
mTable->horizontalHeader()->setLabel( tableColumn, col->header() );
|
||
14 years ago
|
mColumnDict.replace( tableColumn, col );
|
||
|
mFormats.replace( tableColumn, format );
|
||
15 years ago
|
col->addColId( tableColumn );
|
||
|
}
|
||
|
|
||
|
readFile();
|
||
|
}
|
||
|
|
||
|
void KImportDialog::removeColumn()
|
||
|
{
|
||
15 years ago
|
TQTableSelection selection = mTable->selection(mTable->currentSelection());
|
||
15 years ago
|
|
||
|
// kdDebug(5300) << " l: " << selection.leftCol() << " r: " << selection.rightCol() << endl;
|
||
|
|
||
|
for(int i=selection.leftCol();i<=selection.rightCol();++i) {
|
||
|
if (i >= 0) {
|
||
15 years ago
|
mTable->horizontalHeader()->setLabel(i,TQString::number(i+1));
|
||
14 years ago
|
KImportColumn *col = mColumnDict.find(i);
|
||
15 years ago
|
if (col) {
|
||
|
mColumnDict.remove(i);
|
||
|
mFormats.remove(i);
|
||
|
col->removeColId(i);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
readFile();
|
||
|
}
|
||
|
|
||
|
void KImportDialog::applyConverter()
|
||
|
{
|
||
|
kdDebug(5300) << "KImportDialog::applyConverter" << endl;
|
||
|
|
||
|
KProgressDialog pDialog(this, 0, i18n("Importing Progress"),
|
||
|
i18n("Please wait while the data is imported."), true);
|
||
|
pDialog.setAllowCancel(true);
|
||
|
pDialog.showCancelButton(true);
|
||
|
pDialog.setAutoClose(true);
|
||
|
|
||
|
KProgress *progress = pDialog.progressBar();
|
||
|
progress->setTotalSteps( mTable->numRows()-1 );
|
||
|
progress->setValue(0);
|
||
|
|
||
|
readFile( 0 );
|
||
|
|
||
|
pDialog.show();
|
||
|
for( uint i = mStartRow->value() - 1; i < mData.count() && !pDialog.wasCancelled(); ++i ) {
|
||
|
mCurrentRow = i;
|
||
|
progress->setValue(i);
|
||
|
if (i % 5 == 0) // try to avoid constantly processing events
|
||
|
kapp->processEvents();
|
||
|
|
||
|
convertRow();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int KImportDialog::findFormat(int column)
|
||
|
{
|
||
14 years ago
|
TQMap<int,int>::ConstIterator formatIt = mFormats.find(column);
|
||
15 years ago
|
int format;
|
||
|
if (formatIt == mFormats.end()) format = KImportColumn::FormatUndefined;
|
||
|
else format = *formatIt;
|
||
|
|
||
|
// kdDebug(5300) << "KImportDialog::findformat(): " << column << ": " << format << endl;
|
||
|
|
||
|
return format;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KImportDialog::cell(uint col)
|
||
15 years ago
|
{
|
||
|
if ( col >= mData[ mCurrentRow ]->size() ) return "";
|
||
|
else return data( mCurrentRow, col );
|
||
|
}
|
||
|
|
||
|
void KImportDialog::addColumn(KImportColumn *col)
|
||
|
{
|
||
|
mColumns.append(col);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KImportDialog::setData( uint row, uint col, const TQString &value )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString val = value;
|
||
14 years ago
|
val.replace( "\\n", "\n" );
|
||
15 years ago
|
|
||
|
if ( row >= mData.count() ) {
|
||
|
mData.resize( row + 1 );
|
||
|
}
|
||
|
|
||
15 years ago
|
TQValueVector<TQString> *rowVector = mData[ row ];
|
||
15 years ago
|
if ( !rowVector ) {
|
||
15 years ago
|
rowVector = new TQValueVector<TQString>;
|
||
15 years ago
|
mData.insert( row, rowVector );
|
||
|
}
|
||
|
if ( col >= rowVector->size() ) {
|
||
|
rowVector->resize( col + 1 );
|
||
|
}
|
||
|
|
||
14 years ago
|
KImportColumn *c = mColumnDict.find( col );
|
||
15 years ago
|
if ( c )
|
||
|
rowVector->at( col ) = c->preview( val, findFormat(col) );
|
||
|
else
|
||
|
rowVector->at( col ) = val;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KImportDialog::data( uint row, uint col )
|
||
15 years ago
|
{
|
||
|
return mData[ row ]->at( col );
|
||
|
}
|
||
|
|
||
|
void KImportDialog::saveTemplate()
|
||
|
{
|
||
15 years ago
|
TQString fileName = KFileDialog::getSaveFileName(
|
||
|
locateLocal( "data", TQString( kapp->name() ) + "/csv-templates/" ),
|
||
15 years ago
|
"*.desktop", this );
|
||
|
|
||
|
if ( fileName.isEmpty() )
|
||
|
return;
|
||
|
|
||
14 years ago
|
if ( !fileName.contains( ".desktop" ) )
|
||
15 years ago
|
fileName += ".desktop";
|
||
|
|
||
15 years ago
|
TQString name = KInputDialog::getText( i18n( "Template Name" ), i18n( "Please enter a name for the template:" ) );
|
||
15 years ago
|
|
||
|
if ( name.isEmpty() )
|
||
|
return;
|
||
|
|
||
|
KConfig config( fileName );
|
||
|
config.setGroup( "General" );
|
||
|
config.writeEntry( "Columns", mColumns.count() );
|
||
|
config.writeEntry( "Format", mFormatCombo->currentItem() + 1 );
|
||
|
|
||
|
config.setGroup( "Misc" );
|
||
|
config.writeEntry( "Name", name );
|
||
|
|
||
|
config.setGroup( "csv column map" );
|
||
|
|
||
|
KImportColumn *column;
|
||
|
uint counter = 0;
|
||
|
for ( column = mColumns.first(); column; column = mColumns.next() ) {
|
||
15 years ago
|
TQValueList<int> list = column->colIdList();
|
||
15 years ago
|
if ( list.count() > 0 )
|
||
15 years ago
|
config.writeEntry( TQString::number( counter ), list[ 0 ] );
|
||
15 years ago
|
else
|
||
15 years ago
|
config.writeEntry( TQString::number( counter ), -1 );
|
||
15 years ago
|
counter++;
|
||
|
}
|
||
|
|
||
|
config.sync();
|
||
|
}
|