kaddressbook

kaddressbookiconview.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tqapplication.h>
25#include <tqiconview.h>
26#include <tqlayout.h>
27#include <tqstringlist.h>
28
29#include <tdeabc/addressbook.h>
30#include <tdeabc/addressee.h>
31#include <tdeconfig.h>
32#include <kdebug.h>
33#include <tdeglobal.h>
34#include <kiconloader.h>
35#include <tdelocale.h>
36
37#include "core.h"
38#include "kabprefs.h"
39
40#include "kaddressbookiconview.h"
41
42class IconViewFactory : public ViewFactory
43{
44 public:
45 KAddressBookView *view( KAB::Core *core, TQWidget *parent, const char *name )
46 {
47 return new KAddressBookIconView( core, parent, name );
48 }
49
50 TQString type() const { return I18N_NOOP( "Icon" ); }
51
52 TQString description() const { return i18n( "Icons represent contacts. Very simple view." ); }
53};
54
55extern "C" {
56 void *init_libkaddrbk_iconview()
57 {
58 return ( new IconViewFactory );
59 }
60}
61
62AddresseeIconView::AddresseeIconView( TQWidget *parent, const char *name )
63 : TDEIconView( parent, name )
64{
65 setSelectionMode( TQIconView::Extended );
66 setResizeMode( TQIconView::Adjust );
67 setWordWrapIconText( true );
68 setGridX( 100 );
69 setItemsMovable( false );
70 setSorting( true, true );
71 setMode( TDEIconView::Select );
72
73 connect( this, TQ_SIGNAL( dropped( TQDropEvent*, const TQValueList<TQIconDragItem>& ) ),
74 this, TQ_SLOT( itemDropped( TQDropEvent*, const TQValueList<TQIconDragItem>& ) ) );
75}
76
77AddresseeIconView::~AddresseeIconView()
78{
79}
80
81void AddresseeIconView::itemDropped( TQDropEvent *event, const TQValueList<TQIconDragItem>& )
82{
83 emit addresseeDropped( event );
84}
85
86TQDragObject *AddresseeIconView::dragObject()
87{
88 emit startAddresseeDrag();
89
90 // We never want IconView to start the drag
91 return 0;
92}
93
94
95class AddresseeIconViewItem : public TDEIconViewItem
96{
97 public:
98 AddresseeIconViewItem( const TDEABC::Field::List&, TDEABC::AddressBook *doc,
99 const TDEABC::Addressee &addr, TQIconView *parent )
100 : TDEIconViewItem( parent ), mDocument( doc ), mAddressee( addr )
101 {
102 refresh();
103 }
104
105 const TDEABC::Addressee &addressee() const { return mAddressee; }
106
107 void refresh()
108 {
109 mAddressee = mDocument->findByUid( mAddressee.uid() );
110
111 if ( !mAddressee.isEmpty() )
112 setText( mAddressee.givenName() + " " + mAddressee.familyName() );
113
114 TQPixmap icon;
115 TQPixmap defaultIcon( TDEGlobal::iconLoader()->loadIcon( "x-office-address-book", TDEIcon::Desktop ) );
116 TDEABC::Picture pic = mAddressee.photo();
117 if ( pic.data().isNull() )
118 pic = mAddressee.logo();
119
120 if ( pic.isIntern() && !pic.data().isNull() ) {
121 TQImage img = pic.data();
122 if ( img.width() > img.height() )
123 icon = img.scaleWidth( 32 );
124 else
125 icon = img.scaleHeight( 32 );
126 } else
127 icon = defaultIcon;
128
129 setPixmap( icon );
130 }
131
132 private:
133 TDEABC::AddressBook *mDocument;
134 TDEABC::Addressee mAddressee;
135};
136
137
138KAddressBookIconView::KAddressBookIconView( KAB::Core *core,
139 TQWidget *parent, const char *name)
140 : KAddressBookView( core, parent, name )
141{
142 TQVBoxLayout *layout = new TQVBoxLayout( viewWidget() );
143
144 mIconView = new AddresseeIconView( viewWidget(), "mIconView" );
145 layout->addWidget( mIconView );
146
147 // Connect up the signals
148 connect( mIconView, TQ_SIGNAL( executed( TQIconViewItem* ) ),
149 this, TQ_SLOT( addresseeExecuted( TQIconViewItem* ) ) );
150 connect( mIconView, TQ_SIGNAL( selectionChanged() ),
151 this, TQ_SLOT( addresseeSelected() ) );
152 connect( mIconView, TQ_SIGNAL( addresseeDropped( TQDropEvent* ) ),
153 this, TQ_SIGNAL( dropped( TQDropEvent* ) ) );
154 connect( mIconView, TQ_SIGNAL( startAddresseeDrag() ),
155 this, TQ_SIGNAL( startDrag() ) );
156 connect( mIconView, TQ_SIGNAL( contextMenuRequested( TQIconViewItem*, const TQPoint& ) ),
157 this, TQ_SLOT( rmbClicked( TQIconViewItem*, const TQPoint& ) ) );
158}
159
160KAddressBookIconView::~KAddressBookIconView()
161{
162}
163
165{
166 // we have hardcoded sorting, so we have to return a hardcoded field :(
167 return TDEABC::Field::allFields()[ 2 ];
168}
169
170void KAddressBookIconView::readConfig( TDEConfig *config )
171{
173
174 disconnect( mIconView, TQ_SIGNAL( executed( TQIconViewItem* ) ),
175 this, TQ_SLOT( addresseeExecuted( TQIconViewItem* ) ) );
176
177 if ( KABPrefs::instance()->honorSingleClick() )
178 connect( mIconView, TQ_SIGNAL( executed( TQIconViewItem* ) ),
179 this, TQ_SLOT( addresseeExecuted( TQIconViewItem* ) ) );
180 else
181 connect( mIconView, TQ_SIGNAL( doubleClicked( TQIconViewItem* ) ),
182 this, TQ_SLOT( addresseeExecuted( TQIconViewItem* ) ) );
183}
184
186{
187 TQStringList uidList;
188 TQIconViewItem *item;
189 AddresseeIconViewItem *aItem;
190
191 for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
192 if ( item->isSelected() ) {
193 aItem = dynamic_cast<AddresseeIconViewItem*>( item );
194 if ( aItem )
195 uidList << aItem->addressee().uid();
196 }
197 }
198
199 return uidList;
200}
201
202void KAddressBookIconView::refresh( const TQString &uid )
203{
204 TQIconViewItem *item;
205 AddresseeIconViewItem *aItem;
206
207 if ( uid.isEmpty() ) {
208 // Rebuild the view
209 mIconView->clear();
210 mIconList.clear();
211
212 const TDEABC::Addressee::List addresseeList( addressees() );
213 TDEABC::Addressee::List::ConstIterator it( addresseeList.begin() );
214 const TDEABC::Addressee::List::ConstIterator endIt( addresseeList.end() );
215 for ( ; it != endIt; ++it )
216 aItem = new AddresseeIconViewItem( fields(), core()->addressBook(), *it, mIconView );
217
218 mIconView->arrangeItemsInGrid( true );
219
220 for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
221 AddresseeIconViewItem* aivi = dynamic_cast<AddresseeIconViewItem*>( item );
222 mIconList.append( aivi );
223 }
224
225 } else {
226 // Try to find the one to refresh
227 for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
228 aItem = dynamic_cast<AddresseeIconViewItem*>( item );
229 if ( aItem && (aItem->addressee().uid() == uid) ) {
230 aItem->refresh();
231 mIconView->arrangeItemsInGrid( true );
232 return;
233 }
234 }
235
236 refresh( TQString() );
237 }
238}
239
240void KAddressBookIconView::setSelected( const TQString &uid, bool selected )
241{
242 TQIconViewItem *item;
243 AddresseeIconViewItem *aItem;
244
245 if ( uid.isEmpty() ) {
246 mIconView->selectAll( selected );
247 } else {
248 bool found = false;
249 for ( item = mIconView->firstItem(); item && !found; item = item->nextItem() ) {
250
251 aItem = dynamic_cast<AddresseeIconViewItem*>( item );
252 if ( aItem && (aItem->addressee().uid() == uid) ) {
253 mIconView->setSelected( aItem, selected );
254 mIconView->ensureItemVisible( aItem );
255 found = true;
256 }
257 }
258 }
259}
260
261void KAddressBookIconView::setFirstSelected( bool selected )
262{
263 if ( mIconView->firstItem() ) {
264 mIconView->setSelected( mIconView->firstItem(), selected );
265 mIconView->ensureItemVisible( mIconView->firstItem() );
266 }
267}
268
269void KAddressBookIconView::addresseeExecuted( TQIconViewItem *item )
270{
271 AddresseeIconViewItem *aItem = dynamic_cast<AddresseeIconViewItem*>( item );
272
273 if ( aItem )
274 emit executed( aItem->addressee().uid() );
275}
276
277void KAddressBookIconView::addresseeSelected()
278{
279 TQIconViewItem *item;
280 AddresseeIconViewItem *aItem;
281
282 bool found = false;
283 for ( item = mIconView->firstItem(); item && !found; item = item->nextItem() ) {
284 if ( item->isSelected() ) {
285 aItem = dynamic_cast<AddresseeIconViewItem*>( item );
286 if ( aItem ) {
287 emit selected( aItem->addressee().uid() );
288 found = true;
289 }
290 }
291 }
292
293 if ( !found )
294 emit selected( TQString() );
295}
296
297void KAddressBookIconView::rmbClicked( TQIconViewItem*, const TQPoint &point )
298{
299 popup( point );
300}
301
302void KAddressBookIconView::scrollUp()
303{
304 TQApplication::postEvent( mIconView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Up, 0, 0 ) );
305}
306
307void KAddressBookIconView::scrollDown()
308{
309 TQApplication::postEvent( mIconView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Down, 0, 0 ) );
310}
311
312#include "kaddressbookiconview.moc"
This is an example kaddressbook view that is implemented using TDEIconView.
virtual void readConfig(TDEConfig *config)
Called whenever this view should read the config.
virtual TQStringList selectedUids()
Must be overloaded in subclasses.
virtual TDEABC::Field * sortField() const
Base class for all views in kaddressbook.
void popup(const TQPoint &point)
Call this slot to popup a rmb menu.
KAB::Core * core() const
void selected(const TQString &uid)
This signal should be emitted by a subclass whenever an addressee is selected.
void executed(const TQString &uid)
This signal should be emitted by a subclass whenever an addressee is executed.
TDEABC::Addressee::List addressees()
Returns a list of the addressees that should be displayed.
virtual void readConfig(TDEConfig *config)
Called whenever this view should read the config.
TDEABC::Field::List fields() const
Returns a list of the fields that should be displayed.