kexi

kexicsvwidgets.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library 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 library 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 library; see the file COPYING.LIB.  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 "kexicsvwidgets.h"
00021 
00022 #include <qdir.h>
00023 #include <qlabel.h>
00024 #include <qlayout.h>
00025 
00026 #include <klocale.h>
00027 #include <klineedit.h>
00028 #include <kdialogbase.h>
00029 #include <kactivelabel.h>
00030 #include <kiconloader.h>
00031 #include <kmimetype.h>
00032 
00033 #define KEXICSV_OTHER_DELIMITER_INDEX 4
00034 
00035 KexiCSVDelimiterWidget::KexiCSVDelimiterWidget( bool lineEditOnBottom, QWidget * parent )
00036  : QWidget(parent, "KexiCSVDelimiterWidget")
00037  , m_availableDelimiters(KEXICSV_OTHER_DELIMITER_INDEX)
00038 
00039 {
00040     QBoxLayout *lyr = 
00041         lineEditOnBottom ? 
00042         (QBoxLayout *)new QVBoxLayout( this, 0, KDialogBase::spacingHint() )
00043         : (QBoxLayout *)new QHBoxLayout( this, 0, KDialogBase::spacingHint() );
00044 
00045     m_availableDelimiters[0]=KEXICSV_DEFAULT_FILE_DELIMITER;
00046     m_availableDelimiters[1]=";";
00047     m_availableDelimiters[2]="\t";
00048     m_availableDelimiters[3]=" ";
00049 
00050     m_combo = new KComboBox(this, "KexiCSVDelimiterComboBox");
00051     m_combo->insertItem( i18n("Comma \",\"") ); //<-- KEXICSV_DEFAULT_FILE_DELIMITER
00052     m_combo->insertItem( i18n( "Semicolon \";\"" ) );
00053     m_combo->insertItem( i18n( "Tabulator" ) );
00054     m_combo->insertItem( i18n( "Space \" \"" ) );
00055     m_combo->insertItem( i18n( "Other" ) );
00056     lyr->addWidget(m_combo);
00057     setFocusProxy(m_combo);
00058 
00059     m_delimiterEdit = new KLineEdit( this, "m_delimiterEdit" );
00060 //  m_delimiterEdit->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, m_delimiterEdit->sizePolicy().hasHeightForWidth() ) );
00061     m_delimiterEdit->setMaximumSize( QSize( 30, 32767 ) );
00062     m_delimiterEdit->setMaxLength(1);
00063     lyr->addWidget( m_delimiterEdit );
00064     if (!lineEditOnBottom)
00065         lyr->addStretch(2);
00066 
00067     slotDelimiterChangedInternal(KEXICSV_DEFAULT_FILE_DELIMITER_INDEX); //this will init m_delimiter
00068     connect(m_combo, SIGNAL(activated(int)),
00069       this, SLOT(slotDelimiterChanged(int)));
00070     connect(m_delimiterEdit, SIGNAL(returnPressed()),
00071       this, SLOT(slotDelimiterLineEditReturnPressed()));
00072     connect(m_delimiterEdit, SIGNAL(textChanged( const QString & )),
00073       this, SLOT(slotDelimiterLineEditTextChanged( const QString & ) ));
00074 }
00075 
00076 void KexiCSVDelimiterWidget::slotDelimiterChanged(int index)
00077 {
00078     slotDelimiterChangedInternal(index);
00079     if (index==KEXICSV_OTHER_DELIMITER_INDEX)
00080         m_delimiterEdit->setFocus();
00081 }
00082 
00083 void KexiCSVDelimiterWidget::slotDelimiterChangedInternal(int index)
00084 {
00085     if (index > KEXICSV_OTHER_DELIMITER_INDEX)
00086         return;
00087     else if (index == KEXICSV_OTHER_DELIMITER_INDEX)
00088         m_delimiter = m_delimiterEdit->text();
00089     else
00090         m_delimiter = m_availableDelimiters[index];
00091     m_delimiterEdit->setEnabled(index == KEXICSV_OTHER_DELIMITER_INDEX);
00092     emit delimiterChanged(m_delimiter);
00093 }
00094 
00095 void KexiCSVDelimiterWidget::slotDelimiterLineEditReturnPressed()
00096 {
00097     if (m_combo->currentItem() != KEXICSV_OTHER_DELIMITER_INDEX)
00098         return;
00099     slotDelimiterChangedInternal(KEXICSV_OTHER_DELIMITER_INDEX);
00100 }
00101 
00102 void KexiCSVDelimiterWidget::slotDelimiterLineEditTextChanged( const QString & )
00103 {
00104     slotDelimiterChangedInternal(KEXICSV_OTHER_DELIMITER_INDEX);
00105 }
00106 
00107 void KexiCSVDelimiterWidget::setDelimiter(const QString& delimiter)
00108 {
00109     QValueVector<QString>::ConstIterator it = m_availableDelimiters.constBegin();
00110     int index = 0;
00111     for (; it != m_availableDelimiters.constEnd(); ++it, index++) {
00112         if (*it == delimiter) {
00113             m_combo->setCurrentItem(index);
00114             slotDelimiterChangedInternal(index);
00115             return;
00116         }
00117     }
00118     //else: set other (custom) delimiter
00119     m_delimiterEdit->setText(delimiter);
00120     m_combo->setCurrentItem(KEXICSV_OTHER_DELIMITER_INDEX);
00121     slotDelimiterChangedInternal(KEXICSV_OTHER_DELIMITER_INDEX);
00122 }
00123 
00124 //----------------------------------------------------
00125 
00126 KexiCSVTextQuoteComboBox::KexiCSVTextQuoteComboBox( QWidget * parent )
00127  : KComboBox(parent, "KexiCSVTextQuoteComboBox")
00128 {
00129     insertItem( "\"" );
00130     insertItem( "'" );
00131     insertItem( i18n( "None" ) );
00132 }
00133 
00134 QString KexiCSVTextQuoteComboBox::textQuote() const
00135 {
00136     if (currentItem()==2)
00137         return QString::null;
00138     return currentText();
00139 }
00140 
00141 void KexiCSVTextQuoteComboBox::setTextQuote(const QString& textQuote)
00142 {
00143     if (textQuote=="\"" || textQuote=="'")
00144         setCurrentText(textQuote);
00145     else if (textQuote.isEmpty())
00146         setCurrentText(i18n( "None" ));
00147 }
00148 
00149 //----------------------------------------------------
00150 
00151 KexiCSVInfoLabel::KexiCSVInfoLabel( const QString& labelText, QWidget* parent )
00152  : QWidget(parent, "KexiCSVInfoLabel")
00153 {
00154     QVBoxLayout *vbox = new QVBoxLayout( this, 0, KDialogBase::spacingHint() );
00155     QHBoxLayout *hbox = new QHBoxLayout( this );
00156     vbox->addLayout(hbox);
00157     m_leftLabel = new QLabel(labelText, this);
00158     m_leftLabel->setMinimumWidth(130);
00159     m_leftLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
00160     m_leftLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft | Qt::WordBreak);
00161     hbox->addWidget(m_leftLabel);
00162     m_iconLbl = new QLabel(this);
00163     m_iconLbl->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
00164     m_iconLbl->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
00165     m_fnameLbl = new KActiveLabel(this);
00166     m_fnameLbl->setFocusPolicy(NoFocus);
00167     m_fnameLbl->setTextFormat(Qt::PlainText);
00168     m_fnameLbl->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
00169     m_fnameLbl->setLineWidth(1);
00170     m_fnameLbl->setFrameStyle(QFrame::Box);
00171     m_fnameLbl->setAlignment(Qt::AlignVCenter | Qt::AlignLeft | Qt::WordBreak);
00172     hbox->addSpacing(5);
00173     hbox->addWidget(m_iconLbl);
00174     hbox->addWidget(m_fnameLbl, 0, Qt::AlignVCenter | Qt::AlignLeft | Qt::WordBreak);
00175 
00176     m_separator = new QFrame(this);
00177     m_separator->setFrameShape(QFrame::HLine);
00178     m_separator->setFrameShadow(QFrame::Sunken);
00179     vbox->addWidget(m_separator);
00180 }
00181 
00182 void KexiCSVInfoLabel::setFileName( const QString& fileName )
00183 {
00184     m_fnameLbl->setText( QDir::convertSeparators(fileName) );
00185     if (!fileName.isEmpty()) {
00186         m_iconLbl->setPixmap( 
00187             KMimeType::pixmapForURL(KURL::fromPathOrURL(fileName), 0, KIcon::Desktop) );
00188     }
00189 }
00190 
00191 void KexiCSVInfoLabel::setLabelText( const QString& text )
00192 {
00193     m_fnameLbl->setText( text );
00194 //  int lines = m_fnameLbl->lines();
00195 //  m_fnameLbl->setFixedHeight( 
00196 //      QFontMetrics(m_fnameLbl->currentFont()).height() * lines );
00197 }
00198 
00199 void KexiCSVInfoLabel::setIcon(const QString& iconName)
00200 {
00201     m_iconLbl->setPixmap( DesktopIcon(iconName) );
00202 }
00203 
00204 //----------------------------------------------------
00205 
00206 QStringList csvMimeTypes()
00207 {
00208     QStringList mimetypes;
00209     mimetypes << "text/x-csv" << "text/plain" << "all/allfiles";
00210     return mimetypes;
00211 }
00212 
00213 #include "kexicsvwidgets.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys