kaddressbookiconview.cpp
00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (c) 2002 Mike Pilone <mpilone@slac.com> 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 TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 00024 #include <tqapplication.h> 00025 #include <tqiconview.h> 00026 #include <tqlayout.h> 00027 #include <tqstringlist.h> 00028 00029 #include <tdeabc/addressbook.h> 00030 #include <tdeabc/addressee.h> 00031 #include <tdeconfig.h> 00032 #include <kdebug.h> 00033 #include <tdeglobal.h> 00034 #include <kiconloader.h> 00035 #include <tdelocale.h> 00036 00037 #include "core.h" 00038 #include "kabprefs.h" 00039 00040 #include "kaddressbookiconview.h" 00041 00042 class IconViewFactory : public ViewFactory 00043 { 00044 public: 00045 KAddressBookView *view( KAB::Core *core, TQWidget *parent, const char *name ) 00046 { 00047 return new KAddressBookIconView( core, parent, name ); 00048 } 00049 00050 TQString type() const { return I18N_NOOP( "Icon" ); } 00051 00052 TQString description() const { return i18n( "Icons represent contacts. Very simple view." ); } 00053 }; 00054 00055 extern "C" { 00056 void *init_libkaddrbk_iconview() 00057 { 00058 return ( new IconViewFactory ); 00059 } 00060 } 00061 00062 AddresseeIconView::AddresseeIconView( TQWidget *parent, const char *name ) 00063 : TDEIconView( parent, name ) 00064 { 00065 setSelectionMode( TQIconView::Extended ); 00066 setResizeMode( TQIconView::Adjust ); 00067 setWordWrapIconText( true ); 00068 setGridX( 100 ); 00069 setItemsMovable( false ); 00070 setSorting( true, true ); 00071 setMode( TDEIconView::Select ); 00072 00073 connect( this, TQT_SIGNAL( dropped( TQDropEvent*, const TQValueList<TQIconDragItem>& ) ), 00074 this, TQT_SLOT( itemDropped( TQDropEvent*, const TQValueList<TQIconDragItem>& ) ) ); 00075 } 00076 00077 AddresseeIconView::~AddresseeIconView() 00078 { 00079 } 00080 00081 void AddresseeIconView::itemDropped( TQDropEvent *event, const TQValueList<TQIconDragItem>& ) 00082 { 00083 emit addresseeDropped( event ); 00084 } 00085 00086 TQDragObject *AddresseeIconView::dragObject() 00087 { 00088 emit startAddresseeDrag(); 00089 00090 // We never want IconView to start the drag 00091 return 0; 00092 } 00093 00094 00095 class AddresseeIconViewItem : public TDEIconViewItem 00096 { 00097 public: 00098 AddresseeIconViewItem( const TDEABC::Field::List&, TDEABC::AddressBook *doc, 00099 const TDEABC::Addressee &addr, TQIconView *parent ) 00100 : TDEIconViewItem( parent ), mDocument( doc ), mAddressee( addr ) 00101 { 00102 refresh(); 00103 } 00104 00105 const TDEABC::Addressee &addressee() const { return mAddressee; } 00106 00107 void refresh() 00108 { 00109 mAddressee = mDocument->findByUid( mAddressee.uid() ); 00110 00111 if ( !mAddressee.isEmpty() ) 00112 setText( mAddressee.givenName() + " " + mAddressee.familyName() ); 00113 00114 TQPixmap icon; 00115 TQPixmap defaultIcon( TDEGlobal::iconLoader()->loadIcon( "x-office-address-book", TDEIcon::Desktop ) ); 00116 TDEABC::Picture pic = mAddressee.photo(); 00117 if ( pic.data().isNull() ) 00118 pic = mAddressee.logo(); 00119 00120 if ( pic.isIntern() && !pic.data().isNull() ) { 00121 TQImage img = pic.data(); 00122 if ( img.width() > img.height() ) 00123 icon = img.scaleWidth( 32 ); 00124 else 00125 icon = img.scaleHeight( 32 ); 00126 } else 00127 icon = defaultIcon; 00128 00129 setPixmap( icon ); 00130 } 00131 00132 private: 00133 TDEABC::AddressBook *mDocument; 00134 TDEABC::Addressee mAddressee; 00135 }; 00136 00137 00138 KAddressBookIconView::KAddressBookIconView( KAB::Core *core, 00139 TQWidget *parent, const char *name) 00140 : KAddressBookView( core, parent, name ) 00141 { 00142 TQVBoxLayout *layout = new TQVBoxLayout( viewWidget() ); 00143 00144 mIconView = new AddresseeIconView( viewWidget(), "mIconView" ); 00145 layout->addWidget( mIconView ); 00146 00147 // Connect up the signals 00148 connect( mIconView, TQT_SIGNAL( executed( TQIconViewItem* ) ), 00149 this, TQT_SLOT( addresseeExecuted( TQIconViewItem* ) ) ); 00150 connect( mIconView, TQT_SIGNAL( selectionChanged() ), 00151 this, TQT_SLOT( addresseeSelected() ) ); 00152 connect( mIconView, TQT_SIGNAL( addresseeDropped( TQDropEvent* ) ), 00153 this, TQT_SIGNAL( dropped( TQDropEvent* ) ) ); 00154 connect( mIconView, TQT_SIGNAL( startAddresseeDrag() ), 00155 this, TQT_SIGNAL( startDrag() ) ); 00156 connect( mIconView, TQT_SIGNAL( contextMenuRequested( TQIconViewItem*, const TQPoint& ) ), 00157 this, TQT_SLOT( rmbClicked( TQIconViewItem*, const TQPoint& ) ) ); 00158 } 00159 00160 KAddressBookIconView::~KAddressBookIconView() 00161 { 00162 } 00163 00164 TDEABC::Field *KAddressBookIconView::sortField() const 00165 { 00166 // we have hardcoded sorting, so we have to return a hardcoded field :( 00167 return TDEABC::Field::allFields()[ 2 ]; 00168 } 00169 00170 void KAddressBookIconView::readConfig( TDEConfig *config ) 00171 { 00172 KAddressBookView::readConfig( config ); 00173 00174 disconnect( mIconView, TQT_SIGNAL( executed( TQIconViewItem* ) ), 00175 this, TQT_SLOT( addresseeExecuted( TQIconViewItem* ) ) ); 00176 00177 if ( KABPrefs::instance()->honorSingleClick() ) 00178 connect( mIconView, TQT_SIGNAL( executed( TQIconViewItem* ) ), 00179 this, TQT_SLOT( addresseeExecuted( TQIconViewItem* ) ) ); 00180 else 00181 connect( mIconView, TQT_SIGNAL( doubleClicked( TQIconViewItem* ) ), 00182 this, TQT_SLOT( addresseeExecuted( TQIconViewItem* ) ) ); 00183 } 00184 00185 TQStringList KAddressBookIconView::selectedUids() 00186 { 00187 TQStringList uidList; 00188 TQIconViewItem *item; 00189 AddresseeIconViewItem *aItem; 00190 00191 for ( item = mIconView->firstItem(); item; item = item->nextItem() ) { 00192 if ( item->isSelected() ) { 00193 aItem = dynamic_cast<AddresseeIconViewItem*>( item ); 00194 if ( aItem ) 00195 uidList << aItem->addressee().uid(); 00196 } 00197 } 00198 00199 return uidList; 00200 } 00201 00202 void KAddressBookIconView::refresh( const TQString &uid ) 00203 { 00204 TQIconViewItem *item; 00205 AddresseeIconViewItem *aItem; 00206 00207 if ( uid.isEmpty() ) { 00208 // Rebuild the view 00209 mIconView->clear(); 00210 mIconList.clear(); 00211 00212 const TDEABC::Addressee::List addresseeList( addressees() ); 00213 TDEABC::Addressee::List::ConstIterator it( addresseeList.begin() ); 00214 const TDEABC::Addressee::List::ConstIterator endIt( addresseeList.end() ); 00215 for ( ; it != endIt; ++it ) 00216 aItem = new AddresseeIconViewItem( fields(), core()->addressBook(), *it, mIconView ); 00217 00218 mIconView->arrangeItemsInGrid( true ); 00219 00220 for ( item = mIconView->firstItem(); item; item = item->nextItem() ) { 00221 AddresseeIconViewItem* aivi = dynamic_cast<AddresseeIconViewItem*>( item ); 00222 mIconList.append( aivi ); 00223 } 00224 00225 } else { 00226 // Try to find the one to refresh 00227 for ( item = mIconView->firstItem(); item; item = item->nextItem() ) { 00228 aItem = dynamic_cast<AddresseeIconViewItem*>( item ); 00229 if ( aItem && (aItem->addressee().uid() == uid) ) { 00230 aItem->refresh(); 00231 mIconView->arrangeItemsInGrid( true ); 00232 return; 00233 } 00234 } 00235 00236 refresh( TQString() ); 00237 } 00238 } 00239 00240 void KAddressBookIconView::setSelected( const TQString &uid, bool selected ) 00241 { 00242 TQIconViewItem *item; 00243 AddresseeIconViewItem *aItem; 00244 00245 if ( uid.isEmpty() ) { 00246 mIconView->selectAll( selected ); 00247 } else { 00248 bool found = false; 00249 for ( item = mIconView->firstItem(); item && !found; item = item->nextItem() ) { 00250 00251 aItem = dynamic_cast<AddresseeIconViewItem*>( item ); 00252 if ( aItem && (aItem->addressee().uid() == uid) ) { 00253 mIconView->setSelected( aItem, selected ); 00254 mIconView->ensureItemVisible( aItem ); 00255 found = true; 00256 } 00257 } 00258 } 00259 } 00260 00261 void KAddressBookIconView::setFirstSelected( bool selected ) 00262 { 00263 if ( mIconView->firstItem() ) { 00264 mIconView->setSelected( mIconView->firstItem(), selected ); 00265 mIconView->ensureItemVisible( mIconView->firstItem() ); 00266 } 00267 } 00268 00269 void KAddressBookIconView::addresseeExecuted( TQIconViewItem *item ) 00270 { 00271 AddresseeIconViewItem *aItem = dynamic_cast<AddresseeIconViewItem*>( item ); 00272 00273 if ( aItem ) 00274 emit executed( aItem->addressee().uid() ); 00275 } 00276 00277 void KAddressBookIconView::addresseeSelected() 00278 { 00279 TQIconViewItem *item; 00280 AddresseeIconViewItem *aItem; 00281 00282 bool found = false; 00283 for ( item = mIconView->firstItem(); item && !found; item = item->nextItem() ) { 00284 if ( item->isSelected() ) { 00285 aItem = dynamic_cast<AddresseeIconViewItem*>( item ); 00286 if ( aItem ) { 00287 emit selected( aItem->addressee().uid() ); 00288 found = true; 00289 } 00290 } 00291 } 00292 00293 if ( !found ) 00294 emit selected( TQString() ); 00295 } 00296 00297 void KAddressBookIconView::rmbClicked( TQIconViewItem*, const TQPoint &point ) 00298 { 00299 popup( point ); 00300 } 00301 00302 void KAddressBookIconView::scrollUp() 00303 { 00304 TQApplication::postEvent( mIconView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Up, 0, 0 ) ); 00305 } 00306 00307 void KAddressBookIconView::scrollDown() 00308 { 00309 TQApplication::postEvent( mIconView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Down, 0, 0 ) ); 00310 } 00311 00312 #include "kaddressbookiconview.moc"