summaryrefslogtreecommitdiffabout
path: root/kaddressbook/views/kaddressbookiconview.cpp
blob: d6ddec3c93e39799c56313abcc0a8617ddb4e781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
    This file is part of KAddressBook.                                  
    Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
                                                                        
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or   
    (at your option) any later version.                                 
                                                                        
    This program is distributed in the hope that it will be useful,     
    but WITHOUT ANY WARRANTY; without even the implied warranty of      
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
    GNU General Public License for more details.                        
                                                                        
    You should have received a copy of the GNU General Public License   
    along with this program; if not, write to the Free Software         
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           
                                                                        
    As a special exception, permission is given to link this program    
    with any edition of Qt, and distribute the resulting executable,    
    without including the source code for Qt in the source distribution.
*/                                                                      

#ifndef KAB_EMBEDDED
#include <qiconview.h>
#include <qstringlist.h>

#include <kabc/addressee.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>

#else //KAB_EMBEDDED
#endif //KAB_EMBEDDED

#include <kabc/addressbook.h>
#include "kabprefs.h"
#include "viewmanager.h"
#include "kaddressbookiconview.h"
#include <qlayout.h>
#include <qregexp.h>
#include <qapplication.h>
#include <kglobal.h>
/*US transfered to the headerfile
class IconViewFactory : public ViewFactory
{
  public:
    KAddressBookView *view( KABC::AddressBook *ab, QWidget *parent, const char *name )
    {
      return new KAddressBookIconView( ab, parent, name );
    }

    QString type() const { return "Icon"; }
    
    QString description() const { return i18n( "Icons represent contacts. Very simple view." ); }
};

*/

extern "C" {
  void *init_libkaddrbk_iconview()
  {
    return ( new IconViewFactory );
  }
}

////////////////////////////////
// AddresseeIconView (internal class)
#ifndef KAB_EMBEDDED
AddresseeIconView::AddresseeIconView(QWidget *parent, const char *name)
  : KIconView(parent, name)
#else //KAB_EMBEDDED
AddresseeIconView::AddresseeIconView(QWidget *parent, const char *name)
  : QIconView(parent, name)
#endif //KAB_EMBEDDED

{
  setSelectionMode( QIconView::Extended );
  setResizeMode( QIconView::Adjust );
  setWordWrapIconText( true );
  setGridX( 100 );
  setItemsMovable(false);
  setSorting(true, true);

  
//US ???  setMode( KIconView::Select );
  
#ifndef KAB_EMBEDDED

  connect(this, SIGNAL(dropped(QDropEvent*, const QValueList<QIconDragItem>&)),
          this, SLOT(itemDropped(QDropEvent*, const QValueList<QIconDragItem>&)));
#endif //KAB_EMBEDDED
}

AddresseeIconView::~AddresseeIconView()
{
}


void AddresseeIconView::itemDropped(QDropEvent *e, 
                                    const QValueList<QIconDragItem> &)
{
  emit addresseeDropped(e);
}

QDragObject *AddresseeIconView::dragObject()
{
  emit startAddresseeDrag();
  
  // We never want IconView to start the drag
  return 0;
}
////////////////////////////////
// AddresseeIconViewItem  (internal class)
#ifndef KAB_EMBEDDED
class AddresseeIconViewItem : public KIconViewItem
#else //KAB_EMBEDDED
class AddresseeIconViewItem : public QIconViewItem
#endif //KAB_EMBEDDED
{
  public:
#ifndef KAB_EMBEDDED
    AddresseeIconViewItem(const KABC::Field::List &fields,
                          KABC::AddressBook *doc, const KABC::Addressee &a, 
                          QIconView *parent)
      : KIconViewItem(parent), mFields( fields ), mDocument(doc), mAddressee(a)
#else //KAB_EMBEDDED
    AddresseeIconViewItem(const KABC::Field::List &fields,
                          KABC::AddressBook *doc, const KABC::Addressee &a,
                          QIconView *parent)
      : QIconViewItem(parent), mFields( fields ), mDocument(doc), mAddressee(a)
#endif //KAB_EMBEDDED
      {
          if ( mFields.isEmpty() ) {
            mFields = KABC::Field::defaultFields();
          }
          refresh();
      }
      
    const KABC::Addressee &addressee() const { return mAddressee; }
    
    void refresh()
    {
        // Update our addressee, since it may have changed elsewhere
        mAddressee = mDocument->findByUid(mAddressee.uid());
        
        if (!mAddressee.isEmpty())
          setText( mAddressee.givenName() + " " + mAddressee.familyName() );

        QPixmap icon;
      
        KABC::Picture pic = mAddressee.photo();
        if ( pic.data().isNull() )
          pic = mAddressee.logo();

        if ( pic.isIntern() && !pic.data().isNull() ) {
          QImage img = pic.data();
#ifndef KAB_EMBEDDED
        if ( img.width() > img.height() )
          icon = img.scaleWidth( 32 );
        else
          icon = img.scaleHeight( 32 );
#else //KAB_EMBEDDED

        int wid = pic.data().width();
        int hei = pic.data().height(); 
        int max = 48; 
        if ( wid > max || hei > max ) {
            if ( wid > hei ) {
                hei = (hei*max)/wid;
                wid = max;
            } else {
                wid = (wid*max)/hei;
                hei = max;
            }
        }
        qDebug("AddresseeIconViewItem::refresh - scale here dependend of the displaysize and the right factor");
        icon.convertFromImage(img.smoothScale(wid, hei));
#endif //KAB_EMBEDDED
          
        } else {
            icon = KGlobal::iconLoader()->loadIcon( "vcard", KIcon::Desktop, 128 );
        }
      setPixmap( icon );
    }
    
  private:
    KABC::Field::List mFields;
    KABC::AddressBook *mDocument;
    KABC::Addressee mAddressee;
};

///////////////////////////////
// KAddressBookView

KAddressBookIconView::KAddressBookIconView( KABC::AddressBook *ab,
                                            QWidget *parent, const char *name)
    : KAddressBookView( ab, parent, name )
{
    // Init the GUI
    QVBoxLayout *layout = new QVBoxLayout(viewWidget());
    
    mIconView = new AddresseeIconView(viewWidget(), "mIconView");
    layout->addWidget(mIconView);
    
    // Connect up the signals
    
//US method executed is part of KIconView
//US    connect(mIconView, SIGNAL(executed(QIconViewItem *)),
//US            this, SLOT(addresseeExecuted(QIconViewItem *)));
    connect(mIconView, SIGNAL(selectionChanged(QIconViewItem *)),
            this, SLOT(addresseeExecuted(QIconViewItem *)));
    
    connect(mIconView, SIGNAL(selectionChanged()),
            this, SLOT(addresseeSelected()));
    connect(mIconView, SIGNAL(addresseeDropped(QDropEvent*)),
            this, SIGNAL(dropped(QDropEvent*)));
    connect(mIconView, SIGNAL(startAddresseeDrag()),
            this, SIGNAL(startDrag()));
}

KAddressBookIconView::~KAddressBookIconView()
{
}
void KAddressBookIconView::setFocusAV()
{
    if ( mIconView )
        mIconView->setFocus();
}


void KAddressBookIconView::scrollUP()
{
    QKeyEvent * ev = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Up, 0,0 );
    QApplication::postEvent( mIconView, ev ); 
}
void KAddressBookIconView::scrollDOWN()
{
    QKeyEvent * ev = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down, 0,0 );
    QApplication::postEvent( mIconView, ev );
}
void KAddressBookIconView::readConfig(KConfig *config)
{
  KAddressBookView::readConfig(config);
    
//US method executed is part of KIconView
//US  disconnect(mIconView, SIGNAL(executed(QIconViewItem *)),
//US             this, SLOT(addresseeExecuted(QIconViewItem *)));
  disconnect(mIconView, SIGNAL(selectionChanged(QIconViewItem *)),
             this, SLOT(addresseeExecuted(QIconViewItem *)));

//US method executed is part of KIconView. Use selectionChanged instead
/*US
  if (KABPrefs::instance()->mHonorSingleClick)
    connect(mIconView, SIGNAL(executed(QIconViewItem *)),
            this, SLOT(addresseeExecuted(QIconViewItem *)));
  else
    connect(mIconView, SIGNAL(doubleClicked(QIconViewItem *)),
            this, SLOT(addresseeExecuted(QIconViewItem *)));
*/
    connect(mIconView, SIGNAL(selectionChanged(QIconViewItem *)),
            this, SLOT(addresseeExecuted(QIconViewItem *)));
            
}
void KAddressBookIconView::doSearch( const QString&  s ,KABC::Field *field )
{
    mIconView->clear();
    mIconList.clear();
    if ( s.isEmpty() || s == "*" ) {
        refresh();
        return;
    }  
    QRegExp re = getRegExp( s );
    if (!re.isValid())
        return; 
    KABC::Addressee::List addresseeList = addressees();
    KABC::Addressee::List::Iterator it;  
    if (  field ) {
        for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
            if ( (*it).uid().left(2) == "la" && (*it).uid().left(19) == QString("last-syncAddressee-") )
                continue;
#if QT_VERSION >= 0x030000
            if (re.search(field->value( *it ).lower()) == 0)
#else
                if (re.match(field->value( *it ).lower()) == 0)
#endif
                    mIconList.append(new AddresseeIconViewItem( fields(), addressBook(), *it, mIconView ));
                 

        }
    } else {
        KABC::Field::List fieldList = allFields();
        KABC::Field::List::ConstIterator fieldIt;
        for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
            if ( (*it).uid().left(2) == "la" && (*it).uid().left(19) == QString("last-syncAddressee-") )
                continue;
            bool match = false;
            for ( fieldIt = fieldList.begin(); fieldIt != fieldList.end(); ++fieldIt ) {
#if QT_VERSION >= 0x030000
                if (re.search((*fieldIt)->value( *it ).lower()) == 0)
#else
                    if (re.match((*fieldIt)->value( *it ).lower()) == 0)
#endif   
                        { 
                            mIconList.append( new AddresseeIconViewItem( fields(), addressBook(), *it, mIconView ));
                            match = true;
                            break;
                        }
            } 
            if ( ! match ) {
                if ( (*it).matchPhoneNumber( &re ) ) { 
                    mIconList.append( new AddresseeIconViewItem( fields(), addressBook(), *it, mIconView ));
                    match = true;
                    break;
                }
            }
            if ( ! match ) {
                if ( (*it).matchAddress( &re ) ) { 
                    mIconList.append( new AddresseeIconViewItem( fields(), addressBook(), *it, mIconView ));
                    match = true;
                    break;
                }
            }
        }
    }
    mIconView->arrangeItemsInGrid( true ); 
    if ( mIconView->firstItem() ) {
        mIconView->setCurrentItem ( mIconView->firstItem() );
        mIconView->setSelected ( mIconView->firstItem() , true );
    }
    else
        emit selected(QString::null);
}
QStringList KAddressBookIconView::selectedUids()
{
    QStringList uidList;
    QIconViewItem *item;
    AddresseeIconViewItem *aItem;
    
    for (item = mIconView->firstItem(); item; item = item->nextItem())
    {
        if (item->isSelected())
        {
#ifndef KAB_EMBEDDED
            aItem = dynamic_cast<AddresseeIconViewItem*>(item);
#else //KAB_EMBEDDED
            aItem = (AddresseeIconViewItem*)(item);
#endif //KAB_EMBEDDED
            if (aItem)
                uidList << aItem->addressee().uid();
        }
    }
    
    return uidList;
}
    
void KAddressBookIconView::refresh(QString uid)
{
  QIconViewItem *item;
  AddresseeIconViewItem *aItem;
    
  if ( uid.isNull() ) {
    // Rebuild the view
    mIconView->clear();
    mIconList.clear();
        
    KABC::Addressee::List addresseeList = addressees();
    KABC::Addressee::List::Iterator iter;
    for ( iter = addresseeList.begin(); iter != addresseeList.end(); ++iter ) {
        if ( (*iter).uid().left(2) == "la" && (*iter).uid().left(19) == QString("last-syncAddressee-") )
                continue;
        aItem = new AddresseeIconViewItem( fields(), addressBook(), *iter, mIconView );
    }

    mIconView->arrangeItemsInGrid( true );

    for ( item = mIconView->firstItem(); item; item = item->nextItem() )
	{
#ifndef KAB_EMBEDDED
	  AddresseeIconViewItem* aivi = dynamic_cast<AddresseeIconViewItem*>( item );
#else //KAB_EMBEDDED
	  AddresseeIconViewItem* aivi = (AddresseeIconViewItem*)( item );
#endif //KAB_EMBEDDED
      mIconList.append( aivi );
	}

  } else {
    // Try to find the one to refresh
    for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
#ifndef KAB_EMBEDDED
      aItem = dynamic_cast<AddresseeIconViewItem*>(item);
#else //KAB_EMBEDDED
      aItem = (AddresseeIconViewItem*)(item);
#endif //KAB_EMBEDDED
      if ((aItem) && (aItem->addressee().uid() == uid)) {
        aItem->refresh();
        mIconView->arrangeItemsInGrid( true );
        return;
      }
    }
    refresh( QString::null );
  }
}

void KAddressBookIconView::setSelected(QString uid, bool selected)
{
    QIconViewItem *item;
    AddresseeIconViewItem *aItem;
    
    if (uid.isNull())
    {
        mIconView->selectAll(selected);
    }
    else
    {
        bool found = false;
        for (item = mIconView->firstItem(); item && !found; 
             item = item->nextItem())
         {
#ifndef KAB_EMBEDDED
             aItem = dynamic_cast<AddresseeIconViewItem*>(item);
#else //KAB_EMBEDDED
             aItem = (AddresseeIconViewItem*)(item);
#endif //KAB_EMBEDDED

             if ((aItem) && (aItem->addressee().uid() == uid))
             {
                 mIconView->setSelected(aItem, selected);
                 mIconView->ensureItemVisible( aItem );
                 found = true;
             }
         }
    }
}
   
void KAddressBookIconView::addresseeExecuted(QIconViewItem *item)
{
#ifndef KAB_EMBEDDED
    AddresseeIconViewItem *aItem = dynamic_cast<AddresseeIconViewItem*>(item);
#else //KAB_EMBEDDED
    AddresseeIconViewItem *aItem = (AddresseeIconViewItem*)(item);
#endif //KAB_EMBEDDED

    if (aItem) {
        emit executed(aItem->addressee().uid());
    }
}

void KAddressBookIconView::addresseeSelected()
{
    QIconViewItem *item;
    AddresseeIconViewItem *aItem;
    
    bool found = false;
    for (item = mIconView->firstItem(); item && !found; 
         item = item->nextItem())
    {
      if (item->isSelected())
      {
#ifndef KAB_EMBEDDED
        aItem = dynamic_cast<AddresseeIconViewItem*>(item);
#else //KAB_EMBEDDED
        aItem = (AddresseeIconViewItem*)(item);
#endif //KAB_EMBEDDED
        if (aItem)
        {
          emit selected(aItem->addressee().uid());
          found = true;
        }
      }
    }
    
    if (!found)
        emit selected(QString::null);
}

#ifndef KAB_EMBEDDED
#include "kaddressbookiconview.moc"
#endif //KAB_EMBEDDED