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 <kconfig.h>
00043 #include <klocale.h>
00044 #include <kdebug.h>
00045 #include <kcombobox.h>
00046 #include <kmessagebox.h>
00047 #include <kcharsets.h>
00048 
00049 using namespace KSpread;
00050 
00051 CSVExportDialog::CSVExportDialog( QWidget * parent )
00052   : KDialogBase( parent, 0, true, QString::null, Ok | Cancel, No, true ),
00053     m_dialog( new ExportDialogUI( this ) ),
00054     m_delimiter( "," ),
00055     m_textquote('"')
00056 {
00057   kapp->restoreOverrideCursor();
00058 
00059   QStringList encodings;
00060   encodings << i18n( "Descriptive encoding name", "Recommended ( %1 )" ).arg( "UTF-8" );
00061   encodings << i18n( "Descriptive encoding name", "Locale ( %1 )" ).arg( QTextCodec::codecForLocale()->name() );
00062   encodings += KGlobal::charsets()->descriptiveEncodingNames();
00063   // Add a few non-standard encodings, which might be useful for text files
00064   const QString description(i18n("Descriptive encoding name","Other ( %1 )"));
00065   encodings << description.arg("Apple Roman"); // Apple
00066   encodings << description.arg("IBM 850") << description.arg("IBM 866"); // MS DOS
00067   encodings << description.arg("CP 1258"); // Windows
00068 
00069   m_dialog->comboBoxEncoding->insertStringList(encodings);
00070 
00071   setButtonBoxOrientation ( Vertical );
00072 
00073   setMainWidget(m_dialog);
00074 
00075   // Invalid 'Other' delimiters
00076   // - Quotes
00077   // - CR,LF,Vetical-tab,Formfeed,ASCII bel
00078   QRegExp rx( "^[^\"'\r\n\v\f\a]{0,1}$" );
00079   m_delimiterValidator = new QRegExpValidator( rx, m_dialog->m_delimiterBox );
00080   m_dialog->m_delimiterEdit->setValidator( m_delimiterValidator );
00081 
00082   connect( m_dialog->m_delimiterBox, SIGNAL( clicked(int) ),
00083            this, SLOT( delimiterClicked( int ) ) );
00084   connect( m_dialog->m_delimiterEdit, SIGNAL( returnPressed() ),
00085            this, SLOT( returnPressed() ) );
00086   connect( m_dialog->m_delimiterEdit, SIGNAL( textChanged ( const QString & ) ),
00087            this, SLOT(textChanged ( const QString & ) ) );
00088   connect( m_dialog->m_comboQuote, SIGNAL( activated( const QString & ) ),
00089            this, SLOT( textquoteSelected( const QString & ) ) );
00090   connect( m_dialog->m_selectionOnly, SIGNAL( toggled( bool ) ),
00091            this, SLOT( selectionOnlyChanged( bool ) ) );
00092 
00093   loadSettings();
00094 }
00095 
00096 CSVExportDialog::~CSVExportDialog()
00097 {
00098   saveSettings();
00099   kapp->setOverrideCursor(Qt::waitCursor);
00100   delete m_delimiterValidator;
00101 }
00102 
00103 void CSVExportDialog::loadSettings()
00104 {
00105     KConfig *config = kapp->config();
00106     config->setGroup("CSVDialog Settings");
00107     m_textquote = config->readEntry("textquote", "\"")[0];
00108     m_delimiter = config->readEntry("delimiter", ",");
00109     const QString codecText = config->readEntry("codec", "");
00110     bool selectionOnly = config->readBoolEntry("selectionOnly", false);
00111     const QString sheetDelim = config->readEntry("sheetDelimiter", m_dialog->m_sheetDelimiter->text());
00112     bool delimAbove = config->readBoolEntry("sheetDelimiterAbove", false);
00113     const QString eol = config->readEntry("eol", "\r\n");
00114 
00115     // update widgets
00116     if (!codecText.isEmpty()) {
00117       m_dialog->comboBoxEncoding->setCurrentText(codecText);
00118     }
00119     if (m_delimiter == ",") m_dialog->m_radioComma->setChecked(true);
00120     else if (m_delimiter == "\t") m_dialog->m_radioTab->setChecked(true);
00121     else if (m_delimiter == " ") m_dialog->m_radioSpace->setChecked(true);
00122     else if (m_delimiter == ";") m_dialog->m_radioSemicolon->setChecked(true);
00123     else {
00124         m_dialog->m_radioOther->setChecked(true);
00125         m_dialog->m_delimiterEdit->setText(m_delimiter);
00126     }
00127     m_dialog->m_comboQuote->setCurrentItem(m_textquote == '\'' ? 1
00128         : m_textquote == '"' ? 0 : 2);
00129     m_dialog->m_selectionOnly->setChecked(selectionOnly);
00130     m_dialog->m_sheetDelimiter->setText(sheetDelim);
00131     m_dialog->m_delimiterAboveAll->setChecked(delimAbove);
00132     if (eol == "\r\n") m_dialog->radioEndOfLineCRLF->setChecked(true);
00133     else if (eol == "\r") m_dialog->radioEndOfLineCR->setChecked(true);
00134     else m_dialog->radioEndOfLineLF->setChecked(true);
00135 }
00136 
00137 void CSVExportDialog::saveSettings()
00138 {
00139     KConfig *config = kapp->config();
00140     config->setGroup("CSVDialog Settings");
00141     QString q = m_textquote;
00142     config->writeEntry("textquote", q);
00143     config->writeEntry("delimiter", m_delimiter);
00144     config->writeEntry("codec", m_dialog->comboBoxEncoding->currentText());
00145     config->writeEntry("selectionOnly", exportSelectionOnly());
00146     config->writeEntry("sheetDelimiter", getSheetDelimiter());
00147     config->writeEntry("sheetDelimiterAbove", printAlwaysSheetDelimiter());
00148     config->writeEntry("eol", getEndOfLine());
00149     config->sync();
00150 }
00151 
00152 void CSVExportDialog::fillSheet( Map * map )
00153 {
00154   m_dialog->m_sheetList->clear();
00155   QCheckListItem * item;
00156 
00157   QPtrListIterator<Sheet> it( map->sheetList() );
00158   for( ; it.current(); ++it )
00159   {
00160     item = new QCheckListItem( m_dialog->m_sheetList,
00161                                it.current()->sheetName(),
00162                                QCheckListItem::CheckBox );
00163     item->setOn(true);
00164     m_dialog->m_sheetList->insertItem( item );
00165   }
00166 
00167   m_dialog->m_sheetList->setSorting(0, true);
00168   m_dialog->m_sheetList->sort();
00169   m_dialog->m_sheetList->setSorting( -1 );
00170 }
00171 
00172 QChar CSVExportDialog::getDelimiter() const
00173 {
00174   return m_delimiter[0];
00175 }
00176 
00177 QChar CSVExportDialog::getTextQuote() const
00178 {
00179   return m_textquote;
00180 }
00181 
00182 bool CSVExportDialog::printAlwaysSheetDelimiter() const
00183 {
00184   return m_dialog->m_delimiterAboveAll->isChecked();
00185 }
00186 
00187 QString CSVExportDialog::getSheetDelimiter() const
00188 {
00189   return m_dialog->m_sheetDelimiter->text();
00190 }
00191 
00192 bool CSVExportDialog::exportSheet(QString const & sheetName) const
00193 {
00194   for (QListViewItem * item = m_dialog->m_sheetList->firstChild(); item; item = item->nextSibling())
00195   {
00196     if (((QCheckListItem * ) item)->isOn())
00197     {
00198       if ( ((QCheckListItem * ) item)->text() == sheetName )
00199         return true;
00200     }
00201   }
00202   return false;
00203 }
00204 
00205 void CSVExportDialog::slotOk()
00206 {
00207   accept();
00208 }
00209 
00210 void CSVExportDialog::slotCancel()
00211 {
00212   reject();
00213 }
00214 
00215 void CSVExportDialog::returnPressed()
00216 {
00217   if ( m_dialog->m_delimiterBox->id( m_dialog->m_delimiterBox->selected() ) != 4 )
00218     return;
00219 
00220   m_delimiter = m_dialog->m_delimiterEdit->text();
00221 }
00222 
00223 void CSVExportDialog::textChanged ( const QString & )
00224 {
00225 
00226   if ( m_dialog->m_delimiterEdit->text().isEmpty() )
00227   {
00228     enableButtonOK( ! m_dialog->m_radioOther->isChecked() );
00229     return;
00230   }
00231 
00232   m_dialog->m_radioOther->setChecked ( true );
00233   delimiterClicked(4);
00234 }
00235 
00236 void CSVExportDialog::delimiterClicked( int id )
00237 {
00238   enableButtonOK( true );
00239 
00240   //Erase "Other Delimiter" text box if the user has selected one of 
00241   //the standard options instead (comma, semicolon, tab or space)
00242   if (id != 4)
00243     m_dialog->m_delimiterEdit->setText("");
00244   
00245   switch (id)
00246   {
00247     case 0: // comma
00248       m_delimiter = ",";
00249       break;
00250     case 1: // semicolon
00251       m_delimiter = ";";
00252       break;
00253     case 2: // tab
00254       m_delimiter = "\t";
00255       break;
00256     case 3: // space
00257       m_delimiter = " ";
00258       break;
00259     case 4: // other
00260       enableButtonOK( ! m_dialog->m_delimiterEdit->text().isEmpty() );
00261       m_delimiter = m_dialog->m_delimiterEdit->text();
00262       break;
00263   }
00264 }
00265 
00266 void CSVExportDialog::textquoteSelected( const QString & mark )
00267 {
00268   m_textquote = mark[0];
00269 }
00270 
00271 void CSVExportDialog::selectionOnlyChanged( bool on )
00272 {
00273   m_dialog->m_sheetList->setEnabled( !on );
00274   m_dialog->m_delimiterLineBox->setEnabled( !on );
00275 
00276   if ( on )
00277     m_dialog->m_tabWidget->setCurrentPage( 1 );
00278 }
00279 
00280 bool CSVExportDialog::exportSelectionOnly() const
00281 {
00282   return m_dialog->m_selectionOnly->isChecked();
00283 }
00284 
00285 QTextCodec* CSVExportDialog::getCodec(void) const
00286 {
00287     const QString strCodec( KGlobal::charsets()->encodingForName( m_dialog->comboBoxEncoding->currentText() ) );
00288     kdDebug(30502) << "Encoding: " << strCodec << endl;
00289 
00290     bool ok = false;
00291     QTextCodec* codec = QTextCodec::codecForName( strCodec.utf8() );
00292 
00293     // If QTextCodec has not found a valid encoding, so try with KCharsets.
00294     if ( codec )
00295     {
00296         ok = true;
00297     }
00298     else
00299     {
00300         codec = KGlobal::charsets()->codecForName( strCodec, ok );
00301     }
00302 
00303     // Still nothing?
00304     if ( !codec || !ok )
00305     {
00306         // Default: UTF-8
00307         kdWarning(30502) << "Cannot find encoding:" << strCodec << endl;
00308         // ### TODO: what parent to use?
00309         KMessageBox::error( 0, i18n("Cannot find encoding: %1").arg( strCodec ) );
00310         return 0;
00311     }
00312 
00313     return codec;
00314 }
00315 
00316 QString CSVExportDialog::getEndOfLine(void) const
00317 {
00318     QString strReturn;
00319     if (m_dialog->radioEndOfLineLF==m_dialog->buttonGroupEndOfLine->selected())
00320         strReturn="\n";
00321     else if (m_dialog->radioEndOfLineCRLF==m_dialog->buttonGroupEndOfLine->selected())
00322         strReturn="\r\n";
00323     else if (m_dialog->radioEndOfLineCR==m_dialog->buttonGroupEndOfLine->selected())
00324         strReturn="\r";
00325     else
00326         strReturn="\n";
00327 
00328     return strReturn;
00329 }
00330 
00331 #include "csvexportdialog.moc"
00332 
KDE Home | KDE Accessibility Home | Description of Access Keys