kexi

kexicsvimportoptionsdlg.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kexicsvimportoptionsdlg.h"
00021 #include <widget/kexicharencodingcombobox.h>
00022 
00023 #include <qlabel.h>
00024 #include <qlayout.h>
00025 #include <qtextcodec.h>
00026 #include <qcheckbox.h>
00027 
00028 #include <kapplication.h>
00029 #include <kconfig.h>
00030 #include <kcombobox.h>
00031 #include <klocale.h>
00032 #include <kglobal.h>
00033 #include <kcharsets.h>
00034 
00035 KexiCSVImportOptions::KexiCSVImportOptions()
00036 {
00037     kapp->config()->setGroup("ImportExport");
00038     encoding = kapp->config()->readEntry("DefaultEncodingForImportingCSVFiles");
00039     if (encoding.isEmpty()) {
00040         encoding = QString::fromLatin1(KGlobal::locale()->encoding());
00041         defaultEncodingExplicitySet = false;
00042     }
00043     else
00044         defaultEncodingExplicitySet = true;
00045 
00046     stripWhiteSpaceInTextValuesChecked 
00047         = kapp->config()->readBoolEntry("StripBlanksOffOfTextValuesWhenImportingCSVFiles", true);
00048 }
00049 
00050 KexiCSVImportOptions::~KexiCSVImportOptions()
00051 {
00052 }
00053 
00054 bool KexiCSVImportOptions::operator== ( const KexiCSVImportOptions & opt ) const
00055 {
00056     return defaultEncodingExplicitySet==opt.defaultEncodingExplicitySet
00057         && stripWhiteSpaceInTextValuesChecked==opt.stripWhiteSpaceInTextValuesChecked
00058         && encoding==opt.encoding;
00059 }
00060 
00061 bool KexiCSVImportOptions::operator!= ( const KexiCSVImportOptions & opt ) const
00062 {
00063     return !( *this==opt );
00064 }
00065 
00066 //----------------------------------
00067 
00068 KexiCSVImportOptionsDialog::KexiCSVImportOptionsDialog( 
00069     const KexiCSVImportOptions& options, QWidget* parent )
00070  : KDialogBase( 
00071     KDialogBase::Plain, 
00072     i18n( "CSV Import Options" ),
00073     Ok|Cancel, 
00074     Ok,
00075     parent, 
00076     "KexiCSVImportOptionsDialog", 
00077     true, 
00078     false
00079  )
00080 {
00081     QGridLayout *lyr = new QGridLayout( plainPage(), 5, 3, 
00082         KDialogBase::marginHint(), KDialogBase::spacingHint());
00083 
00084     m_encodingComboBox = new KexiCharacterEncodingComboBox(plainPage(), options.encoding);
00085     lyr->addWidget( m_encodingComboBox, 0, 1 );
00086 
00087     QLabel* lbl = new QLabel( m_encodingComboBox, i18n("Text encoding:"), plainPage());
00088     lyr->addWidget( lbl, 0, 0 );
00089 
00090     lyr->addItem( new QSpacerItem( 20, KDialogBase::spacingHint(), QSizePolicy::Fixed, QSizePolicy::Fixed ), 2, 1 );
00091     lyr->addItem( new QSpacerItem( 121, KDialogBase::spacingHint(), QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
00092 
00093     m_chkAlwaysUseThisEncoding = new QCheckBox(
00094         i18n("Always use this encoding when importing CSV data files"), plainPage());
00095     lyr->addWidget( m_chkAlwaysUseThisEncoding, 1, 1 );
00096 
00097     m_chkStripWhiteSpaceInTextValues = new QCheckBox(
00098         i18n("Strip leading and trailing blanks off of text values"), plainPage());
00099     lyr->addWidget( m_chkStripWhiteSpaceInTextValues, 3, 1 );
00100     lyr->addItem( new QSpacerItem( 20, KDialogBase::spacingHint(), QSizePolicy::Minimum, QSizePolicy::Expanding ), 4, 1 );
00101 
00102     //update widgets
00103     if (options.defaultEncodingExplicitySet) {
00104         m_encodingComboBox->setSelectedEncoding(options.encoding);
00105         m_chkAlwaysUseThisEncoding->setChecked(true);
00106     }
00107     m_chkStripWhiteSpaceInTextValues->setChecked(options.stripWhiteSpaceInTextValuesChecked);
00108 
00109     adjustSize();
00110     m_encodingComboBox->setFocus();
00111 }
00112 
00113 KexiCSVImportOptionsDialog::~KexiCSVImportOptionsDialog()
00114 {
00115 }
00116 
00117 KexiCSVImportOptions KexiCSVImportOptionsDialog::options() const
00118 {
00119     KexiCSVImportOptions opt;
00120     opt.encoding = m_encodingComboBox->selectedEncoding();
00121     opt.stripWhiteSpaceInTextValuesChecked = m_chkStripWhiteSpaceInTextValues->isChecked();
00122     return opt;
00123 }
00124 
00125 void KexiCSVImportOptionsDialog::accept()
00126 {
00127     kapp->config()->setGroup("ImportExport");
00128     if (m_chkAlwaysUseThisEncoding->isChecked())
00129         kapp->config()->writeEntry("DefaultEncodingForImportingCSVFiles", 
00130             m_encodingComboBox->selectedEncoding());
00131     else
00132         kapp->config()->deleteEntry("DefaultEncodingForImportingCSVFiles");
00133 
00134     kapp->config()->writeEntry("StripBlanksOffOfTextValuesWhenImportingCSVFiles", 
00135         m_chkStripWhiteSpaceInTextValues->isChecked());
00136 
00137     KDialogBase::accept();
00138 }
00139 
00140 #include "kexicsvimportoptionsdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys