kaddressbook

contacteditorwidgetmanager.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program 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
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qlayout.h>
00025 
00026 #include <kapplication.h>
00027 #include <kdialog.h>
00028 #include <klibloader.h>
00029 #include <ktrader.h>
00030 
00031 // include non-plugin contact editor widgets
00032 #include "customfieldswidget.h"
00033 #include "freebusywidget.h"
00034 #include "geowidget.h"
00035 #include "imagewidget.h"
00036 #include "soundwidget.h"
00037 
00038 #include "contacteditorwidget.h"
00039 #include "contacteditorwidgetmanager.h"
00040 
00041 ContactEditorWidgetManager *ContactEditorWidgetManager::mSelf = 0;
00042 
00043 ContactEditorWidgetManager::ContactEditorWidgetManager()
00044   : QObject( qApp )
00045 {
00046   reload();
00047 }
00048 
00049 ContactEditorWidgetManager::~ContactEditorWidgetManager()
00050 {
00051   mFactories.clear();
00052 }
00053 
00054 ContactEditorWidgetManager *ContactEditorWidgetManager::self()
00055 {
00056   kdWarning( !kapp, 7520 ) << "No QApplication object available!" << endl;
00057 
00058   if ( !mSelf )
00059     mSelf = new ContactEditorWidgetManager();
00060 
00061   return mSelf;
00062 }
00063 
00064 int ContactEditorWidgetManager::count() const
00065 {
00066   return mFactories.count();
00067 }
00068 
00069 KAB::ContactEditorWidgetFactory *ContactEditorWidgetManager::factory( int pos ) const
00070 {
00071   return mFactories[ pos ];
00072 }
00073 
00074 void ContactEditorWidgetManager::reload()
00075 {
00076   mFactories.clear();
00077   kdDebug(5720) << "ContactEditorWidgetManager::reload()" << endl;
00078   const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/ContactEditorWidget",
00079     QString( "[X-KDE-KAddressBook-CEWPluginVersion] == %1" ).arg( KAB_CEW_PLUGIN_VERSION ) );
00080 
00081   KTrader::OfferList::ConstIterator it;
00082   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00083     KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
00084     if ( !factory ) {
00085       kdDebug(5720) << "ContactEditorWidgetManager::reload(): Factory creation failed" << endl;
00086       continue;
00087     }
00088 
00089     KAB::ContactEditorWidgetFactory *pageFactory =
00090                           static_cast<KAB::ContactEditorWidgetFactory*>( factory );
00091 
00092     if ( !pageFactory ) {
00093       kdDebug(5720) << "ContactEditorWidgetManager::reload(): Cast failed" << endl;
00094       continue;
00095     }
00096 
00097     mFactories.append( pageFactory );
00098   }
00099 
00100   // add all non-plugin contact editor factories
00101   mFactories.append( new FreeBusyWidgetFactory );
00102   mFactories.append( new ImageWidgetFactory );
00103   mFactories.append( new SoundWidgetFactory );
00104   mFactories.append( new GeoWidgetFactory );
00105   mFactories.append( new CustomFieldsWidgetFactory );
00106 }
00107 
00109 
00110 ContactEditorTabPage::ContactEditorTabPage( QWidget *parent, const char *name )
00111   : QWidget( parent, name )
00112 {
00113   mLayout = new QGridLayout( this, 0, 2, KDialog::marginHint(),
00114                              KDialog::spacingHint() );
00115 }
00116 
00117 void ContactEditorTabPage::addWidget( KAB::ContactEditorWidget *widget )
00118 {
00119   if ( widget->logicalWidth() == 2 ) {
00120     mWidgets.prepend( widget );
00121     connect( widget, SIGNAL( changed() ), SIGNAL( changed() ) );
00122     return;
00123   }
00124 
00125   // insert it in descending order
00126   KAB::ContactEditorWidget::List::Iterator it;
00127   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00128     if ( widget->logicalHeight() > (*it)->logicalHeight() &&
00129          (*it)->logicalWidth() == 1 ) {
00130       --it;
00131       break;
00132     }
00133   }
00134   mWidgets.insert( ++it, widget );
00135 
00136   connect( widget, SIGNAL( changed() ), SIGNAL( changed() ) );
00137 }
00138 
00139 void ContactEditorTabPage::loadContact( KABC::Addressee *addr )
00140 {
00141   KAB::ContactEditorWidget::List::Iterator it;
00142   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00143     (*it)->setModified( false );
00144     (*it)->loadContact( addr );
00145   }
00146 }
00147 
00148 void ContactEditorTabPage::storeContact( KABC::Addressee *addr )
00149 {
00150   KAB::ContactEditorWidget::List::Iterator it;
00151   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00152     if ( (*it)->modified() ) {
00153       (*it)->storeContact( addr );
00154       (*it)->setModified( false );
00155     }
00156   }
00157 }
00158 
00159 void ContactEditorTabPage::setReadOnly( bool readOnly )
00160 {
00161   KAB::ContactEditorWidget::List::Iterator it;
00162   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
00163     (*it)->setReadOnly( readOnly );
00164 }
00165 
00166 void ContactEditorTabPage::updateLayout()
00167 {
00168   KAB::ContactEditorWidget::List::ConstIterator it;
00169 
00170   int row = 0;
00171   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00172     if ( (*it)->logicalWidth() == 2 ) {
00173       mLayout->addMultiCellWidget( *it, row, row + (*it)->logicalHeight() - 1, 0, 1 );
00174       row += (*it)->logicalHeight();
00175 
00176       if ( it != mWidgets.fromLast() ) {
00177         QFrame *frame = new QFrame( this );
00178         frame->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00179         mLayout->addMultiCellWidget( frame, row, row, 0, 1 );
00180         row++;
00181       }
00182       continue;
00183     }
00184 
00185     // fill left side
00186     int leftHeight = (*it)->logicalHeight();
00187 
00188     if ( it == mWidgets.fromLast() ) { // last widget gets full width
00189       mLayout->addMultiCellWidget( *it, row, row + leftHeight - 1, 0, 1 );
00190       return;
00191     } else {
00192       mLayout->addMultiCellWidget( *it, row, row + leftHeight - 1, 0, 0 );
00193       QFrame *frame = new QFrame( this );
00194       frame->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00195       mLayout->addMultiCellWidget( frame, row + leftHeight, row + leftHeight, 0, 1 );
00196     }
00197 
00198     // fill right side
00199     for ( int i = 0; i < leftHeight; ++i ) {
00200       ++it;
00201       if ( it == mWidgets.end() )
00202         break;
00203 
00204       int rightHeight = (*it)->logicalHeight();
00205       if ( rightHeight + i <= leftHeight )
00206         mLayout->addMultiCellWidget( *it, row + i, row + i + rightHeight - 1, 1, 1 );
00207       else {
00208         --i;
00209         break;
00210       }
00211     }
00212 
00213     row += 2;
00214   }
00215 }
00216 
00217 #include "contacteditorwidgetmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys