kaddressbook

kaddressbookcardview.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 <tqdragobject.h>
26#include <tqevent.h>
27#include <tqiconview.h>
28#include <tqlayout.h>
29#include <tqstringlist.h>
30
31#include <tdeabc/addressbook.h>
32#include <tdeabc/addressee.h>
33#include <tdeconfig.h>
34#include <kdebug.h>
35#include <tdelocale.h>
36
37#include "core.h"
38#include "configurecardviewdialog.h"
39#include "kabprefs.h"
40
41#include "kaddressbookcardview.h"
42
43class CardViewFactory : public ViewFactory
44{
45 public:
46 KAddressBookView *view( KAB::Core *core, TQWidget *parent, const char *name )
47 {
48 return new KAddressBookCardView( core, parent, name );
49 }
50
51 TQString type() const { return I18N_NOOP("Card"); }
52
53 TQString description() const { return i18n( "Rolodex style cards represent contacts." ); }
54
55 ViewConfigureWidget *configureWidget( TDEABC::AddressBook *ab, TQWidget *parent,
56 const char *name = 0 )
57 {
58 return new ConfigureCardViewWidget( ab, parent, name );
59 }
60};
61
62extern "C" {
63 void *init_libkaddrbk_cardview()
64 {
65 return ( new CardViewFactory );
66 }
67}
68
69class AddresseeCardViewItem : public CardViewItem
70{
71 public:
72 AddresseeCardViewItem( const TDEABC::Field::List &fields,
73 bool showEmptyFields,
74 TDEABC::AddressBook *doc, const TDEABC::Addressee &addr,
75 CardView *parent )
76 : CardViewItem( parent, addr.realName() ),
77 mFields( fields ), mShowEmptyFields( showEmptyFields ),
78 mDocument( doc ), mAddressee( addr )
79 {
80 if ( mFields.isEmpty() )
81 mFields = TDEABC::Field::defaultFields();
82
83 refresh();
84 }
85
86 const TDEABC::Addressee &addressee() const { return mAddressee; }
87
88 void refresh()
89 {
90 mAddressee = mDocument->findByUid( mAddressee.uid() );
91
92 if ( !mAddressee.isEmpty() ) {
94
95 TDEABC::Field::List::ConstIterator it( mFields.begin() );
96 const TDEABC::Field::List::ConstIterator endIt( mFields.end() );
97 for ( ; it != endIt; ++it ) {
98 // insert empty fields or not? not doing so saves a bit of memory and CPU
99 // (during geometry calculations), but prevents having equally
100 // wide label columns in all cards, unless CardViewItem/CardView search
101 // globally for the widest label. (anders)
102
103 // if ( mShowEmptyFields || !(*it)->value( mAddressee ).isEmpty() )
104 insertField( (*it)->label(), (*it)->value( mAddressee ) );
105 }
106
107 setCaption( mAddressee.realName() );
108 }
109 }
110
111 private:
112 TDEABC::Field::List mFields;
113 bool mShowEmptyFields;
114 TDEABC::AddressBook *mDocument;
115 TDEABC::Addressee mAddressee;
116};
117
118
119AddresseeCardView::AddresseeCardView( TQWidget *parent, const char *name )
120 : CardView( parent, name )
121{
122 setAcceptDrops( true );
123}
124
125AddresseeCardView::~AddresseeCardView()
126{
127}
128
129void AddresseeCardView::dragEnterEvent( TQDragEnterEvent *event )
130{
131 if ( TQTextDrag::canDecode( event ) )
132 event->accept();
133}
134
135void AddresseeCardView::dropEvent( TQDropEvent *event )
136{
137 emit addresseeDropped( event );
138}
139
140void AddresseeCardView::startDrag()
141{
142 emit startAddresseeDrag();
143}
144
145
146KAddressBookCardView::KAddressBookCardView( KAB::Core *core,
147 TQWidget *parent, const char *name )
148 : KAddressBookView( core, parent, name )
149{
150 mShowEmptyFields = false;
151
152 TQVBoxLayout *layout = new TQVBoxLayout( viewWidget() );
153
154 mCardView = new AddresseeCardView( viewWidget(), "mCardView" );
155 mCardView->setSelectionMode( CardView::Extended );
156 layout->addWidget( mCardView );
157
158 // Connect up the signals
159 connect( mCardView, TQ_SIGNAL( executed( CardViewItem* ) ),
160 this, TQ_SLOT( addresseeExecuted( CardViewItem* ) ) );
161 connect( mCardView, TQ_SIGNAL( selectionChanged() ),
162 this, TQ_SLOT( addresseeSelected() ) );
163 connect( mCardView, TQ_SIGNAL( addresseeDropped( TQDropEvent* ) ),
164 this, TQ_SIGNAL( dropped( TQDropEvent* ) ) );
165 connect( mCardView, TQ_SIGNAL( startAddresseeDrag() ),
166 this, TQ_SIGNAL( startDrag() ) );
167 connect( mCardView, TQ_SIGNAL( contextMenuRequested( CardViewItem*, const TQPoint& ) ),
168 this, TQ_SLOT( rmbClicked( CardViewItem*, const TQPoint& ) ) );
169}
170
171KAddressBookCardView::~KAddressBookCardView()
172{
173}
174
176{
177 // we have hardcoded sorting, so we have to return a hardcoded field :(
178 return TDEABC::Field::allFields()[ 0 ];
179}
180
181void KAddressBookCardView::readConfig( TDEConfig *config )
182{
184
185 // costum colors?
186 if ( config->readBoolEntry( "EnableCustomColors", false ) ) {
187 TQPalette p( mCardView->palette() );
188 TQColor c = p.color( TQPalette::Active, TQColorGroup::Base );
189 p.setColor( TQPalette::Active, TQColorGroup::Base, config->readColorEntry( "BackgroundColor", &c ) );
190 c = p.color( TQPalette::Active, TQColorGroup::Text );
191 p.setColor( TQPalette::Active, TQColorGroup::Text, config->readColorEntry( "TextColor", &c ) );
192 c = p.color( TQPalette::Active, TQColorGroup::Button );
193 p.setColor( TQPalette::Active, TQColorGroup::Button, config->readColorEntry( "HeaderColor", &c ) );
194 c = p.color( TQPalette::Active, TQColorGroup::ButtonText );
195 p.setColor( TQPalette::Active, TQColorGroup::ButtonText, config->readColorEntry( "HeaderTextColor", &c ) );
196 c = p.color( TQPalette::Active, TQColorGroup::Highlight );
197 p.setColor( TQPalette::Active, TQColorGroup::Highlight, config->readColorEntry( "HighlightColor", &c ) );
198 c = p.color( TQPalette::Active, TQColorGroup::HighlightedText );
199 p.setColor( TQPalette::Active, TQColorGroup::HighlightedText, config->readColorEntry( "HighlightedTextColor", &c ) );
200 mCardView->viewport()->setPalette( p );
201 } else {
202 // needed if turned off during a session.
203 mCardView->viewport()->setPalette( mCardView->palette() );
204 }
205
206 //custom fonts?
207 TQFont f( font() );
208 if ( config->readBoolEntry( "EnableCustomFonts", false ) ) {
209 mCardView->setFont( config->readFontEntry( "TextFont", &f ) );
210 f.setBold( true );
211 mCardView->setHeaderFont( config->readFontEntry( "HeaderFont", &f ) );
212 } else {
213 mCardView->setFont( f );
214 f.setBold( true );
215 mCardView->setHeaderFont( f );
216 }
217
218 mCardView->setDrawCardBorder( config->readBoolEntry( "DrawBorder", true ) );
219 mCardView->setDrawColSeparators( config->readBoolEntry( "DrawSeparators", true ) );
220 mCardView->setDrawFieldLabels( config->readBoolEntry( "DrawFieldLabels", false ) );
221 mShowEmptyFields = config->readBoolEntry( "ShowEmptyFields", false );
222
223 mCardView->setShowEmptyFields( mShowEmptyFields );
224
225 mCardView->setItemWidth( config->readNumEntry( "ItemWidth", 200 ) );
226 mCardView->setItemMargin( config->readNumEntry( "ItemMargin", 0 ) );
227 mCardView->setItemSpacing( config->readNumEntry( "ItemSpacing", 10 ) );
228 mCardView->setSeparatorWidth( config->readNumEntry( "SeparatorWidth", 2 ) );
229
230 disconnect( mCardView, TQ_SIGNAL( executed( CardViewItem* ) ),
231 this, TQ_SLOT( addresseeExecuted( CardViewItem* ) ) );
232
233 if ( KABPrefs::instance()->honorSingleClick() )
234 connect( mCardView, TQ_SIGNAL( executed( CardViewItem* ) ),
235 this, TQ_SLOT( addresseeExecuted( CardViewItem* ) ) );
236 else
237 connect( mCardView, TQ_SIGNAL( doubleClicked( CardViewItem* ) ),
238 this, TQ_SLOT( addresseeExecuted( CardViewItem* ) ) );
239}
240
241void KAddressBookCardView::writeConfig( TDEConfig *config )
242{
243 config->writeEntry( "ItemWidth", mCardView->itemWidth() );
245}
246
248{
249 TQStringList uidList;
250 CardViewItem *item;
251 AddresseeCardViewItem *aItem;
252
253 for ( item = mCardView->firstItem(); item; item = item->nextItem() ) {
254 if ( item->isSelected() ) {
255 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
256 if ( aItem )
257 uidList << aItem->addressee().uid();
258 }
259 }
260
261 return uidList;
262}
263
264void KAddressBookCardView::refresh( const TQString &uid )
265{
266 CardViewItem *item;
267 AddresseeCardViewItem *aItem;
268
269 if ( uid.isEmpty() ) {
270 // Rebuild the view
271 mCardView->viewport()->setUpdatesEnabled( false );
272 mCardView->clear();
273
274 const TDEABC::Addressee::List addresseeList( addressees() );
275 TDEABC::Addressee::List::ConstIterator it( addresseeList.begin() );
276 const TDEABC::Addressee::List::ConstIterator endIt( addresseeList.end() );
277 for ( ; it != endIt; ++it ) {
278 aItem = new AddresseeCardViewItem( fields(), mShowEmptyFields,
279 core()->addressBook(), *it, mCardView );
280 }
281 mCardView->viewport()->setUpdatesEnabled( true );
282 mCardView->viewport()->update();
283
284 // by default nothing is selected
285 emit selected( TQString() );
286 } else {
287 // Try to find the one to refresh
288 bool found = false;
289 for ( item = mCardView->firstItem(); item && !found; item = item->nextItem() ) {
290 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
291 if ( aItem && (aItem->addressee().uid() == uid) ) {
292 aItem->refresh();
293 found = true;
294 }
295 }
296 }
297}
298
299void KAddressBookCardView::setSelected( const TQString &uid, bool selected )
300{
301 CardViewItem *item;
302 AddresseeCardViewItem *aItem;
303
304 if ( uid.isEmpty() ) {
305 mCardView->selectAll( selected );
306 } else {
307 bool found = false;
308 for ( item = mCardView->firstItem(); item && !found; item = item->nextItem() ) {
309 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
310
311 if ( aItem && (aItem->addressee().uid() == uid) ) {
312 mCardView->setSelected( aItem, selected );
313 mCardView->ensureItemVisible( item );
314 found = true;
315 }
316 }
317 }
318}
319
320void KAddressBookCardView::setFirstSelected( bool selected )
321{
322 if ( mCardView->firstItem() ) {
323 mCardView->setSelected( mCardView->firstItem(), selected );
324 mCardView->ensureItemVisible( mCardView->firstItem() );
325 }
326}
327
328void KAddressBookCardView::addresseeExecuted( CardViewItem *item )
329{
330 AddresseeCardViewItem *aItem = dynamic_cast<AddresseeCardViewItem*>( item );
331 if ( aItem )
332 emit executed( aItem->addressee().uid() );
333}
334
335void KAddressBookCardView::addresseeSelected()
336{
337 CardViewItem *item;
338 AddresseeCardViewItem *aItem;
339
340 bool found = false;
341 for ( item = mCardView->firstItem(); item && !found; item = item->nextItem() ) {
342 if ( item->isSelected() ) {
343 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
344 if ( aItem ) {
345 emit selected( aItem->addressee().uid() );
346 found = true;
347 }
348 }
349 }
350
351 if ( !found )
352 emit selected( TQString() );
353}
354
355void KAddressBookCardView::rmbClicked( CardViewItem*, const TQPoint &point )
356{
357 popup( point );
358}
359
360void KAddressBookCardView::scrollUp()
361{
362 TQApplication::postEvent( mCardView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Up, 0, 0 ) );
363}
364
365void KAddressBookCardView::scrollDown()
366{
367 TQApplication::postEvent( mCardView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Down, 0, 0 ) );
368}
369
370#include "kaddressbookcardview.moc"
Represents a single card (item) in the card view.
Definition: cardview.h:50
CardViewItem * nextItem() const
Definition: cardview.cpp:441
void clearFields()
Removes all the fields from this card.
Definition: cardview.cpp:409
void setCaption(const TQString &caption)
Sets the caption of the card.
Definition: cardview.cpp:457
bool isSelected() const
Definition: cardview.cpp:368
void insertField(const TQString &label, const TQString &value)
Adds a field to the card.
Definition: cardview.cpp:378
The CardView is a method of displaying data in cards.
Definition: cardview.h:195
Configure dialog for the card view.
This view uses the CardView class to create a card view.
virtual void readConfig(TDEConfig *config)
Called whenever this view should read the config.
virtual TQStringList selectedUids()
Must be overloaded in subclasses.
virtual void writeConfig(TDEConfig *config)
Called whenever this view should write the config.
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.
virtual void writeConfig(TDEConfig *)
Called whenever this view should write the config.
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.
This widget is the base class for all view configuration widgets.