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 <tqcheckbox.h>
00024 #include <tqcombobox.h>
00025 #include <tqdatetimeedit.h>
00026 #include <tqlayout.h>
00027 #include <tqobjectlist.h>
00028 #include <tqspinbox.h>
00029 #include <tqregexp.h>
00030 #include <tqtextedit.h>
00031 #include <tqwidgetfactory.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 TQString &uiFile, TQWidget *parent,
00045   const char *name )
00046   : TQWidget( parent, name )
00047 {
00048   initGUI( uiFile );
00049 }
00050 
00051 void DesignerFields::initGUI( const TQString &uiFile )
00052 {
00053   TQVBoxLayout *layout = new TQVBoxLayout( this );
00054 
00055   TQWidget *wdg = TQWidgetFactory::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   TQObjectList *list = wdg->queryList( TQWIDGET_OBJECT_NAME_STRING );
00067   TQObjectListIt it( *list );
00068 
00069   TQStringList allowedTypes;
00070   allowedTypes << TQLINEEDIT_OBJECT_NAME_STRING
00071                << TQTEXTEDIT_OBJECT_NAME_STRING
00072                << TQSPINBOX_OBJECT_NAME_STRING
00073                << TQCHECKBOX_OBJECT_NAME_STRING
00074                << TQCOMBOBOX_OBJECT_NAME_STRING
00075                << TQDATETIMEEDIT_OBJECT_NAME_STRING
00076                << "KLineEdit"
00077                << "KDateTimeWidget"
00078                << "KDatePicker";
00079 
00080   while ( it.current() ) {
00081     if ( allowedTypes.contains( it.current()->className() ) ) {
00082       TQString name = it.current()->name();
00083       if ( name.startsWith( "X_" ) ) {
00084         name = name.mid( 2 );
00085 
00086         TQWidget *widget = TQT_TQWIDGET( it.current() );
00087         if ( !name.isEmpty() )
00088           mWidgets.insert( name, widget );
00089 
00090         if ( it.current()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) )
00091           connect( it.current(), TQT_SIGNAL( textChanged( const TQString& ) ),
00092                    TQT_SIGNAL( modified() ) );
00093         else if ( it.current()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) )
00094           connect( it.current(), TQT_SIGNAL( valueChanged( int ) ),
00095                    TQT_SIGNAL( modified() ) );
00096         else if ( it.current()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) )
00097           connect( it.current(), TQT_SIGNAL( toggled( bool ) ),
00098                    TQT_SIGNAL( modified() ) );
00099         else if ( it.current()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) )
00100           connect( it.current(), TQT_SIGNAL( activated( const TQString& ) ),
00101                    TQT_SIGNAL( modified() ) );
00102         else if ( it.current()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) )
00103           connect( it.current(), TQT_SIGNAL( valueChanged( const TQDateTime& ) ),
00104                    TQT_SIGNAL( modified() ) );
00105         else if ( it.current()->inherits( "KDateTimeWidget" ) )
00106           connect( it.current(), TQT_SIGNAL( valueChanged( const TQDateTime& ) ),
00107                    TQT_SIGNAL( modified() ) );
00108         else if ( it.current()->inherits( "KDatePicker" ) )
00109           connect( it.current(), TQT_SIGNAL( dateChanged( TQDate ) ),
00110                    TQT_SIGNAL( modified() ) );
00111         else if ( it.current()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) )
00112           connect( it.current(), TQT_SIGNAL( textChanged() ),
00113                    TQT_SIGNAL( modified() ) );
00114 
00115         if ( !widget->isEnabled() )
00116           mDisabledWidgets.append( widget );
00117       }
00118     }
00119 
00120     ++it;
00121   }
00122 
00123   delete list;
00124 }
00125 
00126 TQString DesignerFields::identifier() const
00127 {
00128   return mIdentifier;
00129 }
00130 
00131 TQString DesignerFields::title() const
00132 {
00133   return mTitle;
00134 }
00135 
00136 void DesignerFields::load( DesignerFields::Storage *storage )
00137 {
00138   TQStringList 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   TQMap<TQString, TQWidget *>::ConstIterator widIt;
00144   for ( widIt = mWidgets.begin(); widIt != mWidgets.end(); ++widIt ) {
00145     TQString value;
00146     if ( widIt.data()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) {
00147       TQLineEdit *wdg = static_cast<TQLineEdit*>( widIt.data() );
00148       wdg->setText( TQString() );
00149     } else if ( widIt.data()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) {
00150       TQSpinBox *wdg = static_cast<TQSpinBox*>( widIt.data() );
00151       wdg->setValue( wdg->minValue() );
00152     } else if ( widIt.data()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) {
00153       TQCheckBox *wdg = static_cast<TQCheckBox*>( widIt.data() );
00154       wdg->setChecked( false );
00155     } else if ( widIt.data()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) {
00156       TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( widIt.data() );
00157       wdg->setDateTime( TQDateTime::currentDateTime() );
00158     } else if ( widIt.data()->inherits( "KDateTimeWidget" ) ) {
00159       KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.data() );
00160       wdg->setDateTime( TQDateTime::currentDateTime() );
00161     } else if ( widIt.data()->inherits( "KDatePicker" ) ) {
00162       KDatePicker *wdg = static_cast<KDatePicker*>( widIt.data() );
00163       wdg->setDate( TQDate::currentDate() );
00164     } else if ( widIt.data()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) ) {
00165       TQComboBox *wdg = static_cast<TQComboBox*>( widIt.data() );
00166       wdg->setCurrentItem( 0 );
00167     } else if ( widIt.data()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) {
00168       TQTextEdit *wdg = static_cast<TQTextEdit*>( widIt.data() );
00169       wdg->setText( TQString() );
00170     }
00171   }
00172 
00173   TQStringList::ConstIterator it2;
00174   for ( it2 = keys.begin(); it2 != keys.end(); ++it2 ) {
00175     TQString value = storage->read( *it2 );
00176 
00177     TQMap<TQString, TQWidget *>::ConstIterator it = mWidgets.find( *it2 );
00178     if ( it != mWidgets.end() ) {
00179       if ( it.data()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) {
00180         TQLineEdit *wdg = static_cast<TQLineEdit*>( it.data() );
00181         wdg->setText( value );
00182       } else if ( it.data()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) {
00183         TQSpinBox *wdg = static_cast<TQSpinBox*>( it.data() );
00184         wdg->setValue( value.toInt() );
00185       } else if ( it.data()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) {
00186         TQCheckBox *wdg = static_cast<TQCheckBox*>( it.data() );
00187         wdg->setChecked( value == "true" || value == "1" );
00188       } else if ( it.data()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) {
00189         TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( it.data() );
00190         wdg->setDateTime( TQDateTime::fromString( value, Qt::ISODate ) );
00191       } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
00192         KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
00193         wdg->setDateTime( TQDateTime::fromString( value, Qt::ISODate ) );
00194       } else if ( it.data()->inherits( "KDatePicker" ) ) {
00195         KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
00196         wdg->setDate( TQDate::fromString( value, Qt::ISODate ) );
00197       } else if ( it.data()->inherits( TQCOMBOBOX_OBJECT_NAME_STRING ) ) {
00198         TQComboBox *wdg = static_cast<TQComboBox*>( it.data() );
00199         wdg->setCurrentText( value );
00200       } else if ( it.data()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) {
00201         TQTextEdit *wdg = static_cast<TQTextEdit*>( it.data() );
00202         wdg->setText( value );
00203       }
00204     }
00205   }
00206 }
00207 
00208 void DesignerFields::save( DesignerFields::Storage *storage )
00209 {
00210   TQMap<TQString, TQWidget*>::Iterator it;
00211   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00212     TQString value;
00213     if ( it.data()->inherits( TQLINEEDIT_OBJECT_NAME_STRING ) ) {
00214       TQLineEdit *wdg = static_cast<TQLineEdit*>( it.data() );
00215       value = wdg->text();
00216     } else if ( it.data()->inherits( TQSPINBOX_OBJECT_NAME_STRING ) ) {
00217       TQSpinBox *wdg = static_cast<TQSpinBox*>( it.data() );
00218       value = TQString::number( wdg->value() );
00219     } else if ( it.data()->inherits( TQCHECKBOX_OBJECT_NAME_STRING ) ) {
00220       TQCheckBox *wdg = static_cast<TQCheckBox*>( it.data() );
00221       value = ( wdg->isChecked() ? "true" : "false" );
00222     } else if ( it.data()->inherits( TQDATETIMEEDIT_OBJECT_NAME_STRING ) ) {
00223       TQDateTimeEdit *wdg = static_cast<TQDateTimeEdit*>( 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( TQCOMBOBOX_OBJECT_NAME_STRING ) ) {
00232       TQComboBox *wdg = static_cast<TQComboBox*>( it.data() );
00233       value = wdg->currentText();
00234     } else if ( it.data()->inherits( TQTEXTEDIT_OBJECT_NAME_STRING ) ) {
00235       TQTextEdit *wdg = static_cast<TQTextEdit*>( 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   TQMap<TQString, TQWidget*>::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"