filters

csvexportdialog.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1999 David Faure <faure@kde.org>
00003    Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <csvexportdialog.h>
00022 #include <exportdialogui.h>
00023 
00024 #include <kspread_map.h>
00025 #include <kspread_sheet.h>
00026 
00027 #include <qbuttongroup.h>
00028 #include <qcheckbox.h>
00029 #include <qcombobox.h>
00030 #include <qcursor.h>
00031 #include <qlabel.h>
00032 #include <qlineedit.h>
00033 #include <qlistview.h>
00034 #include <qptrlist.h>
00035 #include <qradiobutton.h>
00036 #include <qtextstream.h>
00037 #include <qtabwidget.h>
00038 #include <qtextcodec.h>
00039 #include <qvalidator.h>
00040 
00041 #include <kapplication.h>
00042 #include <klocale.h>
00043 #include <kdebug.h>
00044 #include <kcombobox.h>
00045 #include <kmessagebox.h>
00046 #include <kcharsets.h>
00047 
00048 using namespace KSpread;
00049 
00050 CSVExportDialog::CSVExportDialog( QWidget * parent )
00051   : KDialogBase( parent, 0, true, QString::null, Ok | Cancel, No, true ),
00052     m_dialog( new ExportDialogUI( this ) ),
00053     m_delimiter( "," ),
00054     m_textquote('"')
00055 {
00056   kapp->restoreOverrideCursor();
00057 
00058   QStringList encodings;
00059   encodings << i18n( "Descriptive encoding name", "Recommended ( %1 )" ).arg( "UTF-8" );
00060   encodings << i18n( "Descriptive encoding name", "Locale ( %1 )" ).arg( QTextCodec::codecForLocale()->name() );
00061   encodings += KGlobal::charsets()->descriptiveEncodingNames();
00062   // Add a few non-standard encodings, which might be useful for text files
00063   const QString description(i18n("Descriptive encoding name","Other ( %1 )"));
00064   encodings << description.arg("Apple Roman"); // Apple
00065   encodings << description.arg("IBM 850") << description.arg("IBM 866"); // MS DOS
00066   encodings << description.arg("CP 1258"); // Windows
00067 
00068   m_dialog->comboBoxEncoding->insertStringList(encodings);
00069 
00070   setButtonBoxOrientation ( Vertical );
00071 
00072   setMainWidget(m_dialog);
00073 
00074   // Invalid 'Other' delimiters
00075   // - Quotes
00076   // - CR,LF,Vetical-tab,Formfeed,ASCII bel
00077   QRegExp rx( "^[^\"'\r\n\v\f\a]{0,1}$" );
00078   m_delimiterValidator = new QRegExpValidator( rx, m_dialog->m_delimiterBox );
00079   m_dialog->m_delimiterEdit->setValidator( m_delimiterValidator );
00080 
00081   connect( m_dialog->m_delimiterBox, SIGNAL( clicked(int) ),
00082            this, SLOT( delimiterClicked( int ) ) );
00083   connect( m_dialog->m_delimiterEdit, SIGNAL( returnPressed() ),
00084            this, SLOT( returnPressed() ) );
00085   connect( m_dialog->m_delimiterEdit, SIGNAL( textChanged ( const QString & ) ),
00086            this, SLOT(textChanged ( const QString & ) ) );
00087   connect( m_dialog->m_comboQuote, SIGNAL( activated( const QString & ) ),
00088            this, SLOT( textquoteSelected( const QString & ) ) );
00089   connect( m_dialog->m_selectionOnly, SIGNAL( toggled( bool ) ),
00090            this, SLOT( selectionOnlyChanged( bool ) ) );
00091 }
00092 
00093 CSVExportDialog::~CSVExportDialog()
00094 {
00095   kapp->setOverrideCursor(Qt::waitCursor);
00096   delete m_delimiterValidator;
00097 }
00098 
00099 void CSVExportDialog::fillSheet( Map * map )
00100 {
00101   m_dialog->m_sheetList->clear();
00102   QCheckListItem * item;
00103 
00104   QPtrListIterator<Sheet> it( map->sheetList() );
00105   for( ; it.current(); ++it )
00106   {
00107     item = new QCheckListItem( m_dialog->m_sheetList,
00108                                it.current()->sheetName(),
00109                                QCheckListItem::CheckBox );
00110     item->setOn(true);
00111     m_dialog->m_sheetList->insertItem( item );
00112   }
00113 
00114   m_dialog->m_sheetList->setSorting(0, true);
00115   m_dialog->m_sheetList->sort();
00116   m_dialog->m_sheetList->setSorting( -1 );
00117 }
00118 
00119 QChar CSVExportDialog::getDelimiter() const
00120 {
00121   return m_delimiter[0];
00122 }
00123 
00124 QChar CSVExportDialog::getTextQuote() const
00125 {
00126   return m_textquote;
00127 }
00128 
00129 bool CSVExportDialog::printAlwaysSheetDelimiter() const
00130 {
00131   return m_dialog->m_delimiterAboveAll->isChecked();
00132 }
00133 
00134 QString CSVExportDialog::getSheetDelimiter() const
00135 {
00136   return m_dialog->m_sheetDelimiter->text();
00137 }
00138 
00139 bool CSVExportDialog::exportSheet(QString const & sheetName) const
00140 {
00141   for (QListViewItem * item = m_dialog->m_sheetList->firstChild(); item; item = item->nextSibling())
00142   {
00143     if (((QCheckListItem * ) item)->isOn())
00144     {
00145       if ( ((QCheckListItem * ) item)->text() == sheetName )
00146         return true;
00147     }
00148   }
00149   return false;
00150 }
00151 
00152 void CSVExportDialog::slotOk()
00153 {
00154   accept();
00155 }
00156 
00157 void CSVExportDialog::slotCancel()
00158 {
00159   reject();
00160 }
00161 
00162 void CSVExportDialog::returnPressed()
00163 {
00164   if ( m_dialog->m_delimiterBox->id( m_dialog->m_delimiterBox->selected() ) != 4 )
00165     return;
00166 
00167   m_delimiter = m_dialog->m_delimiterEdit->text();
00168 }
00169 
00170 void CSVExportDialog::textChanged ( const QString & )
00171 {
00172 
00173   if ( m_dialog->m_delimiterEdit->text().isEmpty() )
00174   {
00175     enableButtonOK( ! m_dialog->m_radioOther->isChecked() );
00176     return;
00177   }
00178 
00179   m_dialog->m_radioOther->setChecked ( true );
00180   delimiterClicked(4);
00181 }
00182 
00183 void CSVExportDialog::delimiterClicked( int id )
00184 {
00185   enableButtonOK( true );
00186 
00187   //Erase "Other Delimiter" text box if the user has selected one of 
00188   //the standard options instead (comma, semicolon, tab or space)
00189   if (id != 4)
00190     m_dialog->m_delimiterEdit->setText("");
00191   
00192   switch (id)
00193   {
00194     case 0: // comma
00195       m_delimiter = ",";
00196       break;
00197     case 1: // semicolon
00198       m_delimiter = ";";
00199       break;
00200     case 2: // tab
00201       m_delimiter = "\t";
00202       break;
00203     case 3: // space
00204       m_delimiter = " ";
00205       break;
00206     case 4: // other
00207       enableButtonOK( ! m_dialog->m_delimiterEdit->text().isEmpty() );
00208       m_delimiter = m_dialog->m_delimiterEdit->text();
00209       break;
00210   }
00211 }
00212 
00213 void CSVExportDialog::textquoteSelected( const QString & mark )
00214 {
00215   m_textquote = mark[0];
00216 }
00217 
00218 void CSVExportDialog::selectionOnlyChanged( bool on )
00219 {
00220   m_dialog->m_sheetList->setEnabled( !on );
00221   m_dialog->m_delimiterLineBox->setEnabled( !on );
00222 
00223   if ( on )
00224     m_dialog->m_tabWidget->setCurrentPage( 1 );
00225 }
00226 
00227 bool CSVExportDialog::exportSelectionOnly() const
00228 {
00229   return m_dialog->m_selectionOnly->isChecked();
00230 }
00231 
00232 QTextCodec* CSVExportDialog::getCodec(void) const
00233 {
00234     const QString strCodec( KGlobal::charsets()->encodingForName( m_dialog->comboBoxEncoding->currentText() ) );
00235     kdDebug(30502) << "Encoding: " << strCodec << endl;
00236 
00237     bool ok = false;
00238     QTextCodec* codec = QTextCodec::codecForName( strCodec.utf8() );
00239 
00240     // If QTextCodec has not found a valid encoding, so try with KCharsets.
00241     if ( codec )
00242     {
00243         ok = true;
00244     }
00245     else
00246     {
00247         codec = KGlobal::charsets()->codecForName( strCodec, ok );
00248     }
00249 
00250     // Still nothing?
00251     if ( !codec || !ok )
00252     {
00253         // Default: UTF-8
00254         kdWarning(30502) << "Cannot find encoding:" << strCodec << endl;
00255         // ### TODO: what parent to use?
00256         KMessageBox::error( 0, i18n("Cannot find encoding: %1").arg( strCodec ) );
00257         return 0;
00258     }
00259 
00260     return codec;
00261 }
00262 
00263 QString CSVExportDialog::getEndOfLine(void) const
00264 {
00265     QString strReturn;
00266     if (m_dialog->radioEndOfLineLF==m_dialog->buttonGroupEndOfLine->selected())
00267         strReturn="\n";
00268     else if (m_dialog->radioEndOfLineCRLF==m_dialog->buttonGroupEndOfLine->selected())
00269         strReturn="\r\n";
00270     else if (m_dialog->radioEndOfLineCR==m_dialog->buttonGroupEndOfLine->selected())
00271         strReturn="\r";
00272     else
00273         strReturn="\n";
00274 
00275     return strReturn;
00276 }
00277 
00278 #include "csvexportdialog.moc"
00279 
KDE Home | KDE Accessibility Home | Description of Access Keys