libkdepim

designerfields.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00005     Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <qcheckbox.h>
00024 #include <qcombobox.h>
00025 #include <qdatetimeedit.h>
00026 #include <qlayout.h>
00027 #include <qobjectlist.h>
00028 #include <qspinbox.h>
00029 #include <qregexp.h>
00030 #include <qtextedit.h>
00031 #include <qwidgetfactory.h>
00032 
00033 #include <kdatepicker.h>
00034 #include <kdatetimewidget.h>
00035 #include <kdialog.h>
00036 #include <klineedit.h>
00037 #include <kstandarddirs.h>
00038 #include <kdebug.h>
00039 
00040 #include "designerfields.h"
00041 
00042 using namespace KPIM;
00043 
00044 DesignerFields::DesignerFields( const QString &uiFile, QWidget *parent,
00045   const char *name )
00046   : QWidget( parent, name )
00047 {
00048   initGUI( uiFile );
00049 }
00050 
00051 void DesignerFields::initGUI( const QString &uiFile )
00052 {
00053   QVBoxLayout *layout = new QVBoxLayout( this );
00054 
00055   QWidget *wdg = QWidgetFactory::create( uiFile, 0, this );
00056   if ( !wdg ) {
00057     kdError() << "No ui file found" << endl;
00058     return;
00059   }
00060 
00061   mTitle = wdg->caption();
00062   mIdentifier = wdg->name();
00063 
00064   layout->addWidget( wdg );
00065 
00066   QObjectList *list = wdg->queryList( "QWidget" );
00067   QObjectListIt it( *list );
00068 
00069   QStringList allowedTypes;
00070   allowedTypes << "QLineEdit"
00071                << "QTextEdit"
00072                << "QSpinBox"
00073                << "QCheckBox"
00074                << "QComboBox"
00075                << "QDateTimeEdit"
00076                << "KLineEdit"
00077                << "KDateTimeWidget"
00078                << "KDatePicker";
00079 
00080   while ( it.current() ) {
00081     if ( allowedTypes.contains( it.current()->className() ) ) {
00082       QString name = it.current()->name();
00083       if ( name.startsWith( "X_" ) ) {
00084         name = name.mid( 2 );
00085 
00086         QWidget *widget = static_cast<QWidget*>( it.current() );
00087         if ( !name.isEmpty() )
00088           mWidgets.insert( name, widget );
00089 
00090         if ( it.current()->inherits( "QLineEdit" ) )
00091           connect( it.current(), SIGNAL( textChanged( const QString& ) ),
00092                    SIGNAL( modified() ) );
00093         else if ( it.current()->inherits( "QSpinBox" ) )
00094           connect( it.current(), SIGNAL( valueChanged( int ) ),
00095                    SIGNAL( modified() ) );
00096         else if ( it.current()->inherits( "QCheckBox" ) )
00097           connect( it.current(), SIGNAL( toggled( bool ) ),
00098                    SIGNAL( modified() ) );
00099         else if ( it.current()->inherits( "QComboBox" ) )
00100           connect( it.current(), SIGNAL( activated( const QString& ) ),
00101                    SIGNAL( modified() ) );
00102         else if ( it.current()->inherits( "QDateTimeEdit" ) )
00103           connect( it.current(), SIGNAL( valueChanged( const QDateTime& ) ),
00104                    SIGNAL( modified() ) );
00105         else if ( it.current()->inherits( "KDateTimeWidget" ) )
00106           connect( it.current(), SIGNAL( valueChanged( const QDateTime& ) ),
00107                    SIGNAL( modified() ) );
00108         else if ( it.current()->inherits( "KDatePicker" ) )
00109           connect( it.current(), SIGNAL( dateChanged( QDate ) ),
00110                    SIGNAL( modified() ) );
00111         else if ( it.current()->inherits( "QTextEdit" ) )
00112           connect( it.current(), SIGNAL( textChanged() ),
00113                    SIGNAL( modified() ) );
00114 
00115         if ( !widget->isEnabled() )
00116           mDisabledWidgets.append( widget );
00117       }
00118     }
00119 
00120     ++it;
00121   }
00122 
00123   delete list;
00124 }
00125 
00126 QString DesignerFields::identifier() const
00127 {
00128   return mIdentifier;
00129 }
00130 
00131 QString DesignerFields::title() const
00132 {
00133   return mTitle;
00134 }
00135 
00136 void DesignerFields::load( DesignerFields::Storage *storage )
00137 {
00138   QStringList keys = storage->keys();
00139     
00140   // clear all custom page widgets 
00141   // we can't do this in the following loop, as it works on the 
00142   // custom fields of the vcard, which may not be set.
00143   QMap<QString, QWidget *>::ConstIterator widIt;
00144   for ( widIt = mWidgets.begin(); widIt != mWidgets.end(); ++widIt ) {
00145     QString value;
00146     if ( widIt.data()->inherits( "QLineEdit" ) ) {
00147       QLineEdit *wdg = static_cast<QLineEdit*>( widIt.data() );
00148       wdg->setText( QString::null );
00149     } else if ( widIt.data()->inherits( "QSpinBox" ) ) {
00150       QSpinBox *wdg = static_cast<QSpinBox*>( widIt.data() );
00151       wdg->setValue( wdg->minValue() );
00152     } else if ( widIt.data()->inherits( "QCheckBox" ) ) {
00153       QCheckBox *wdg = static_cast<QCheckBox*>( widIt.data() );
00154       wdg->setChecked( false );
00155     } else if ( widIt.data()->inherits( "QDateTimeEdit" ) ) {
00156       QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( widIt.data() );
00157       wdg->setDateTime( QDateTime::currentDateTime() );
00158     } else if ( widIt.data()->inherits( "KDateTimeWidget" ) ) {
00159       KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.data() );
00160       wdg->setDateTime( QDateTime::currentDateTime() );
00161     } else if ( widIt.data()->inherits( "KDatePicker" ) ) {
00162       KDatePicker *wdg = static_cast<KDatePicker*>( widIt.data() );
00163       wdg->setDate( QDate::currentDate() );
00164     } else if ( widIt.data()->inherits( "QComboBox" ) ) {
00165       QComboBox *wdg = static_cast<QComboBox*>( widIt.data() );
00166       wdg->setCurrentItem( 0 );
00167     } else if ( widIt.data()->inherits( "QTextEdit" ) ) {
00168       QTextEdit *wdg = static_cast<QTextEdit*>( widIt.data() );
00169       wdg->setText( QString::null );
00170     }
00171   }
00172 
00173   QStringList::ConstIterator it2;
00174   for ( it2 = keys.begin(); it2 != keys.end(); ++it2 ) {
00175     QString value = storage->read( *it2 );
00176 
00177     QMap<QString, QWidget *>::ConstIterator it = mWidgets.find( *it2 );
00178     if ( it != mWidgets.end() ) {
00179       if ( it.data()->inherits( "QLineEdit" ) ) {
00180         QLineEdit *wdg = static_cast<QLineEdit*>( it.data() );
00181         wdg->setText( value );
00182       } else if ( it.data()->inherits( "QSpinBox" ) ) {
00183         QSpinBox *wdg = static_cast<QSpinBox*>( it.data() );
00184         wdg->setValue( value.toInt() );
00185       } else if ( it.data()->inherits( "QCheckBox" ) ) {
00186         QCheckBox *wdg = static_cast<QCheckBox*>( it.data() );
00187         wdg->setChecked( value == "true" || value == "1" );
00188       } else if ( it.data()->inherits( "QDateTimeEdit" ) ) {
00189         QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( it.data() );
00190         wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
00191       } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
00192         KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
00193         wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
00194       } else if ( it.data()->inherits( "KDatePicker" ) ) {
00195         KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
00196         wdg->setDate( QDate::fromString( value, Qt::ISODate ) );
00197       } else if ( it.data()->inherits( "QComboBox" ) ) {
00198         QComboBox *wdg = static_cast<QComboBox*>( it.data() );
00199         wdg->setCurrentText( value );
00200       } else if ( it.data()->inherits( "QTextEdit" ) ) {
00201         QTextEdit *wdg = static_cast<QTextEdit*>( it.data() );
00202         wdg->setText( value );
00203       }
00204     }
00205   }
00206 }
00207 
00208 void DesignerFields::save( DesignerFields::Storage *storage )
00209 {
00210   QMap<QString, QWidget*>::Iterator it;
00211   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00212     QString value;
00213     if ( it.data()->inherits( "QLineEdit" ) ) {
00214       QLineEdit *wdg = static_cast<QLineEdit*>( it.data() );
00215       value = wdg->text();
00216     } else if ( it.data()->inherits( "QSpinBox" ) ) {
00217       QSpinBox *wdg = static_cast<QSpinBox*>( it.data() );
00218       value = QString::number( wdg->value() );
00219     } else if ( it.data()->inherits( "QCheckBox" ) ) {
00220       QCheckBox *wdg = static_cast<QCheckBox*>( it.data() );
00221       value = ( wdg->isChecked() ? "true" : "false" );
00222     } else if ( it.data()->inherits( "QDateTimeEdit" ) ) {
00223       QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( it.data() );
00224       value = wdg->dateTime().toString( Qt::ISODate );
00225     } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
00226       KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
00227       value = wdg->dateTime().toString( Qt::ISODate );
00228     } else if ( it.data()->inherits( "KDatePicker" ) ) {
00229       KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
00230       value = wdg->date().toString( Qt::ISODate );
00231     } else if ( it.data()->inherits( "QComboBox" ) ) {
00232       QComboBox *wdg = static_cast<QComboBox*>( it.data() );
00233       value = wdg->currentText();
00234     } else if ( it.data()->inherits( "QTextEdit" ) ) {
00235       QTextEdit *wdg = static_cast<QTextEdit*>( it.data() );
00236       value = wdg->text();
00237    }
00238 
00239    storage->write( it.key(), value );
00240   }
00241 }
00242 
00243 void DesignerFields::setReadOnly( bool readOnly )
00244 {
00245   QMap<QString, QWidget*>::Iterator it;
00246   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
00247     if ( mDisabledWidgets.find( it.data() ) == mDisabledWidgets.end() )
00248       it.data()->setEnabled( !readOnly );
00249 }
00250 
00251 #include "designerfields.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys