kaddressbook
contacteditorwidgetmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QtGui/QFrame>
00025 #include <QtGui/QGridLayout>
00026
00027 #include <kapplication.h>
00028 #include <kdialog.h>
00029 #include <klibloader.h>
00030 #include <kservicetypetrader.h>
00031
00032
00033 #include "customfieldswidget.h"
00034 #include "freebusywidget.h"
00035 #include "geowidget.h"
00036 #include "imagewidget.h"
00037 #include "soundwidget.h"
00038
00039 #include "contacteditorwidget.h"
00040 #include "contacteditorwidgetmanager.h"
00041
00042 ContactEditorWidgetManager *ContactEditorWidgetManager::mSelf = 0;
00043
00044 ContactEditorWidgetManager::ContactEditorWidgetManager()
00045 : QObject( qApp )
00046 {
00047 reload();
00048 }
00049
00050 ContactEditorWidgetManager::~ContactEditorWidgetManager()
00051 {
00052 mFactories.clear();
00053 }
00054
00055 ContactEditorWidgetManager *ContactEditorWidgetManager::self()
00056 {
00057 kWarning( !kapp, 7520 ) <<"No QApplication object available!";
00058
00059 if ( !mSelf )
00060 mSelf = new ContactEditorWidgetManager();
00061
00062 return mSelf;
00063 }
00064
00065 int ContactEditorWidgetManager::count() const
00066 {
00067 return mFactories.count();
00068 }
00069
00070 KAB::ContactEditorWidgetFactory *ContactEditorWidgetManager::factory( int pos ) const
00071 {
00072 return mFactories[ pos ];
00073 }
00074
00075 void ContactEditorWidgetManager::reload()
00076 {
00077 mFactories.clear();
00078 kDebug(5720) <<"ContactEditorWidgetManager::reload()";
00079 const KService::List plugins = KServiceTypeTrader::self()->query( "KAddressBook/ContactEditorWidget",
00080 QString( "[X-KDE-KAddressBook-CEWPluginVersion] == %1" ).arg( KAB_CEW_PLUGIN_VERSION ) );
00081
00082 foreach ( KService::Ptr pluginService, plugins ) {
00083 KPluginFactory *factory = KPluginLoader( *pluginService ).factory();
00084 if ( !factory ) {
00085 kDebug(5720) <<"ContactEditorWidgetManager::reload(): Factory creation failed";
00086 continue;
00087 }
00088
00089 KAB::ContactEditorWidgetFactory *pageFactory =
00090 dynamic_cast<KAB::ContactEditorWidgetFactory*>( factory );
00091 if ( !pageFactory ) {
00092 kDebug(5720) <<"ContactEditorWidgetManager::reload(): Factory cast failed";
00093 continue;
00094 }
00095
00096 mFactories.append( pageFactory );
00097 }
00098
00099
00100 mFactories.append( new FreeBusyWidgetFactory );
00101 mFactories.append( new ImageWidgetFactory );
00102 mFactories.append( new SoundWidgetFactory );
00103 mFactories.append( new GeoWidgetFactory );
00104 mFactories.append( new CustomFieldsWidgetFactory );
00105 }
00106
00108
00109 ContactEditorTabPage::ContactEditorTabPage( QWidget *parent )
00110 : QWidget( parent )
00111 {
00112 mLayout = new QGridLayout( this );
00113 mLayout->setSpacing( KDialog::spacingHint() );
00114 mLayout->setMargin( KDialog::marginHint() );
00115 }
00116
00117 void ContactEditorTabPage::addWidget( KAB::ContactEditorWidget *widget )
00118 {
00119 if(!widget)
00120 return;
00121
00122 if ( widget->logicalWidth() == 2 ) {
00123 mWidgets.prepend( widget );
00124 connect( widget, SIGNAL( changed() ), SIGNAL( changed() ) );
00125 return;
00126 }
00127
00128
00129 KAB::ContactEditorWidget::List::Iterator it;
00130 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00131 if ( widget->logicalHeight() > (*it)->logicalHeight() &&
00132 (*it)->logicalWidth() == 1 ) {
00133 --it;
00134 break;
00135 }
00136 }
00137 mWidgets.insert( ++it, widget );
00138
00139 connect( widget, SIGNAL( changed() ), SIGNAL( changed() ) );
00140 }
00141
00142 void ContactEditorTabPage::loadContact( KABC::Addressee *addr )
00143 {
00144 KAB::ContactEditorWidget::List::Iterator it;
00145 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00146 (*it)->setModified( false );
00147 (*it)->loadContact( addr );
00148 }
00149 }
00150
00151 void ContactEditorTabPage::storeContact( KABC::Addressee *addr )
00152 {
00153 KAB::ContactEditorWidget::List::Iterator it;
00154 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00155 if ( (*it)->modified() ) {
00156 (*it)->storeContact( addr );
00157 (*it)->setModified( false );
00158 }
00159 }
00160 }
00161
00162 void ContactEditorTabPage::setReadOnly( bool readOnly )
00163 {
00164 KAB::ContactEditorWidget::List::Iterator it;
00165 for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
00166 (*it)->setReadOnly( readOnly );
00167 }
00168
00169 void ContactEditorTabPage::updateLayout()
00170 {
00171 KAB::ContactEditorWidget::List::ConstIterator it, last;
00172 int row = 0;
00173 if( mWidgets.isEmpty() )
00174 return;
00175 last = mWidgets.end();
00176 --last;
00177 for ( it = mWidgets.begin(); it != mWidgets.end() ; ++it ) {
00178 if ( (*it)->logicalWidth() == 2 ) {
00179 mLayout->addWidget( *it, row, 0, (*it)->logicalHeight(), 2 );
00180 row += (*it)->logicalHeight();
00181 if ( it != mWidgets.end() ) {
00182 QFrame *frame = new QFrame( this );
00183 frame->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00184 mLayout->addWidget( frame, row, 0, 1, 2 );
00185 row++;
00186 }
00187 continue;
00188 }
00189
00190
00191 int leftHeight = (*it)->logicalHeight();
00192
00193 if ( it == last ) {
00194 mLayout->addWidget( *it, row, 0, leftHeight, 2 );
00195 return;
00196 } else {
00197 mLayout->addWidget( *it, row, 0, leftHeight, 1);
00198 QFrame *frame = new QFrame( this );
00199 frame->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00200 mLayout->addWidget( frame, row + leftHeight, 0, 1, 2 );
00201 }
00202
00203
00204 for ( int i = 0; i < leftHeight; ++i ) {
00205 ++it;
00206 if ( it == last )
00207 break;
00208
00209 int rightHeight = (*it)->logicalHeight();
00210 if ( rightHeight + i <= leftHeight )
00211 mLayout->addWidget( *it, row + i, 1, rightHeight, 1);
00212 else {
00213 --it;
00214 break;
00215 }
00216 }
00217
00218 row += 2;
00219 }
00220 }
00221
00222 #include "contacteditorwidgetmanager.moc"