Diffstat (limited to 'kaddressbook/views/kaddressbookiconview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/views/kaddressbookiconview.cpp | 378 |
1 files changed, 378 insertions, 0 deletions
diff --git a/kaddressbook/views/kaddressbookiconview.cpp b/kaddressbook/views/kaddressbookiconview.cpp new file mode 100644 index 0000000..50ff285 --- a/dev/null +++ b/kaddressbook/views/kaddressbookiconview.cpp | |||
@@ -0,0 +1,378 @@ | |||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | |||
19 | As a special exception, permission is given to link this program | ||
20 | with any edition of Qt, and distribute the resulting executable, | ||
21 | without including the source code for Qt in the source distribution. | ||
22 | */ | ||
23 | |||
24 | #ifndef KAB_EMBEDDED | ||
25 | #include <qiconview.h> | ||
26 | #include <qstringlist.h> | ||
27 | |||
28 | #include <kabc/addressee.h> | ||
29 | #include <kconfig.h> | ||
30 | #include <kdebug.h> | ||
31 | #include <kglobal.h> | ||
32 | #include <kiconloader.h> | ||
33 | #include <klocale.h> | ||
34 | |||
35 | #else //KAB_EMBEDDED | ||
36 | #endif //KAB_EMBEDDED | ||
37 | |||
38 | #include <kabc/addressbook.h> | ||
39 | #include "kabprefs.h" | ||
40 | #include "viewmanager.h" | ||
41 | #include "kaddressbookiconview.h" | ||
42 | #include <qlayout.h> | ||
43 | #include <kglobal.h> | ||
44 | /*US transfered to the headerfile | ||
45 | class IconViewFactory : public ViewFactory | ||
46 | { | ||
47 | public: | ||
48 | KAddressBookView *view( KABC::AddressBook *ab, QWidget *parent, const char *name ) | ||
49 | { | ||
50 | return new KAddressBookIconView( ab, parent, name ); | ||
51 | } | ||
52 | |||
53 | QString type() const { return "Icon"; } | ||
54 | |||
55 | QString description() const { return i18n( "Icons represent contacts. Very simple view." ); } | ||
56 | }; | ||
57 | |||
58 | */ | ||
59 | |||
60 | extern "C" { | ||
61 | void *init_libkaddrbk_iconview() | ||
62 | { | ||
63 | return ( new IconViewFactory ); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | //////////////////////////////// | ||
68 | // AddresseeIconView (internal class) | ||
69 | #ifndef KAB_EMBEDDED | ||
70 | AddresseeIconView::AddresseeIconView(QWidget *parent, const char *name) | ||
71 | : KIconView(parent, name) | ||
72 | #else //KAB_EMBEDDED | ||
73 | AddresseeIconView::AddresseeIconView(QWidget *parent, const char *name) | ||
74 | : QIconView(parent, name) | ||
75 | #endif //KAB_EMBEDDED | ||
76 | |||
77 | { | ||
78 | setSelectionMode( QIconView::Extended ); | ||
79 | setResizeMode( QIconView::Adjust ); | ||
80 | setWordWrapIconText( true ); | ||
81 | setGridX( 100 ); | ||
82 | setItemsMovable(false); | ||
83 | setSorting(true, true); | ||
84 | |||
85 | |||
86 | //US ??? setMode( KIconView::Select ); | ||
87 | |||
88 | #ifndef KAB_EMBEDDED | ||
89 | |||
90 | connect(this, SIGNAL(dropped(QDropEvent*, const QValueList<QIconDragItem>&)), | ||
91 | this, SLOT(itemDropped(QDropEvent*, const QValueList<QIconDragItem>&))); | ||
92 | #endif //KAB_EMBEDDED | ||
93 | } | ||
94 | |||
95 | AddresseeIconView::~AddresseeIconView() | ||
96 | { | ||
97 | } | ||
98 | |||
99 | void AddresseeIconView::itemDropped(QDropEvent *e, | ||
100 | const QValueList<QIconDragItem> &) | ||
101 | { | ||
102 | emit addresseeDropped(e); | ||
103 | } | ||
104 | |||
105 | QDragObject *AddresseeIconView::dragObject() | ||
106 | { | ||
107 | emit startAddresseeDrag(); | ||
108 | |||
109 | // We never want IconView to start the drag | ||
110 | return 0; | ||
111 | } | ||
112 | //////////////////////////////// | ||
113 | // AddresseeIconViewItem (internal class) | ||
114 | #ifndef KAB_EMBEDDED | ||
115 | class AddresseeIconViewItem : public KIconViewItem | ||
116 | #else //KAB_EMBEDDED | ||
117 | class AddresseeIconViewItem : public QIconViewItem | ||
118 | #endif //KAB_EMBEDDED | ||
119 | { | ||
120 | public: | ||
121 | #ifndef KAB_EMBEDDED | ||
122 | AddresseeIconViewItem(const KABC::Field::List &fields, | ||
123 | KABC::AddressBook *doc, const KABC::Addressee &a, | ||
124 | QIconView *parent) | ||
125 | : KIconViewItem(parent), mFields( fields ), mDocument(doc), mAddressee(a) | ||
126 | #else //KAB_EMBEDDED | ||
127 | AddresseeIconViewItem(const KABC::Field::List &fields, | ||
128 | KABC::AddressBook *doc, const KABC::Addressee &a, | ||
129 | QIconView *parent) | ||
130 | : QIconViewItem(parent), mFields( fields ), mDocument(doc), mAddressee(a) | ||
131 | #endif //KAB_EMBEDDED | ||
132 | { | ||
133 | if ( mFields.isEmpty() ) { | ||
134 | mFields = KABC::Field::defaultFields(); | ||
135 | } | ||
136 | refresh(); | ||
137 | } | ||
138 | |||
139 | const KABC::Addressee &addressee() const { return mAddressee; } | ||
140 | |||
141 | void refresh() | ||
142 | { | ||
143 | // Update our addressee, since it may have changed elsewhere | ||
144 | mAddressee = mDocument->findByUid(mAddressee.uid()); | ||
145 | |||
146 | if (!mAddressee.isEmpty()) | ||
147 | setText( mAddressee.givenName() + " " + mAddressee.familyName() ); | ||
148 | |||
149 | QPixmap icon; | ||
150 | QPixmap defaultIcon( KGlobal::iconLoader()->loadIcon( "vcard", KIcon::Desktop, 128 ) ); | ||
151 | KABC::Picture pic = mAddressee.photo(); | ||
152 | if ( pic.data().isNull() ) | ||
153 | pic = mAddressee.logo(); | ||
154 | |||
155 | if ( pic.isIntern() && !pic.data().isNull() ) { | ||
156 | QImage img = pic.data(); | ||
157 | #ifndef KAB_EMBEDDED | ||
158 | if ( img.width() > img.height() ) | ||
159 | icon = img.scaleWidth( 32 ); | ||
160 | else | ||
161 | icon = img.scaleHeight( 32 ); | ||
162 | #else //KAB_EMBEDDED | ||
163 | qDebug("AddresseeIconViewItem::refresh - scale here dependend of the displaysize and the right factor"); | ||
164 | icon.convertFromImage(img.smoothScale(32, 32)); | ||
165 | #endif //KAB_EMBEDDED | ||
166 | |||
167 | } else | ||
168 | icon = defaultIcon; | ||
169 | |||
170 | setPixmap( icon ); | ||
171 | } | ||
172 | |||
173 | private: | ||
174 | KABC::Field::List mFields; | ||
175 | KABC::AddressBook *mDocument; | ||
176 | KABC::Addressee mAddressee; | ||
177 | }; | ||
178 | |||
179 | /////////////////////////////// | ||
180 | // KAddressBookView | ||
181 | |||
182 | KAddressBookIconView::KAddressBookIconView( KABC::AddressBook *ab, | ||
183 | QWidget *parent, const char *name) | ||
184 | : KAddressBookView( ab, parent, name ) | ||
185 | { | ||
186 | // Init the GUI | ||
187 | QVBoxLayout *layout = new QVBoxLayout(viewWidget()); | ||
188 | |||
189 | mIconView = new AddresseeIconView(viewWidget(), "mIconView"); | ||
190 | layout->addWidget(mIconView); | ||
191 | |||
192 | // Connect up the signals | ||
193 | |||
194 | //US method executed is part of KIconView | ||
195 | //US connect(mIconView, SIGNAL(executed(QIconViewItem *)), | ||
196 | //US this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
197 | connect(mIconView, SIGNAL(selectionChanged(QIconViewItem *)), | ||
198 | this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
199 | |||
200 | connect(mIconView, SIGNAL(selectionChanged()), | ||
201 | this, SLOT(addresseeSelected())); | ||
202 | connect(mIconView, SIGNAL(addresseeDropped(QDropEvent*)), | ||
203 | this, SIGNAL(dropped(QDropEvent*))); | ||
204 | connect(mIconView, SIGNAL(startAddresseeDrag()), | ||
205 | this, SIGNAL(startDrag())); | ||
206 | } | ||
207 | |||
208 | KAddressBookIconView::~KAddressBookIconView() | ||
209 | { | ||
210 | } | ||
211 | |||
212 | void KAddressBookIconView::readConfig(KConfig *config) | ||
213 | { | ||
214 | KAddressBookView::readConfig(config); | ||
215 | |||
216 | //US method executed is part of KIconView | ||
217 | //US disconnect(mIconView, SIGNAL(executed(QIconViewItem *)), | ||
218 | //US this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
219 | disconnect(mIconView, SIGNAL(selectionChanged(QIconViewItem *)), | ||
220 | this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
221 | |||
222 | //US method executed is part of KIconView. Use selectionChanged instead | ||
223 | /*US | ||
224 | if (KABPrefs::instance()->mHonorSingleClick) | ||
225 | connect(mIconView, SIGNAL(executed(QIconViewItem *)), | ||
226 | this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
227 | else | ||
228 | connect(mIconView, SIGNAL(doubleClicked(QIconViewItem *)), | ||
229 | this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
230 | */ | ||
231 | connect(mIconView, SIGNAL(selectionChanged(QIconViewItem *)), | ||
232 | this, SLOT(addresseeExecuted(QIconViewItem *))); | ||
233 | |||
234 | } | ||
235 | |||
236 | QStringList KAddressBookIconView::selectedUids() | ||
237 | { | ||
238 | QStringList uidList; | ||
239 | QIconViewItem *item; | ||
240 | AddresseeIconViewItem *aItem; | ||
241 | |||
242 | for (item = mIconView->firstItem(); item; item = item->nextItem()) | ||
243 | { | ||
244 | if (item->isSelected()) | ||
245 | { | ||
246 | #ifndef KAB_EMBEDDED | ||
247 | aItem = dynamic_cast<AddresseeIconViewItem*>(item); | ||
248 | #else //KAB_EMBEDDED | ||
249 | aItem = (AddresseeIconViewItem*)(item); | ||
250 | #endif //KAB_EMBEDDED | ||
251 | if (aItem) | ||
252 | uidList << aItem->addressee().uid(); | ||
253 | } | ||
254 | } | ||
255 | |||
256 | return uidList; | ||
257 | } | ||
258 | |||
259 | void KAddressBookIconView::refresh(QString uid) | ||
260 | { | ||
261 | QIconViewItem *item; | ||
262 | AddresseeIconViewItem *aItem; | ||
263 | |||
264 | if ( uid.isNull() ) { | ||
265 | // Rebuild the view | ||
266 | mIconView->clear(); | ||
267 | mIconList.clear(); | ||
268 | |||
269 | KABC::Addressee::List addresseeList = addressees(); | ||
270 | KABC::Addressee::List::Iterator iter; | ||
271 | for ( iter = addresseeList.begin(); iter != addresseeList.end(); ++iter ) | ||
272 | aItem = new AddresseeIconViewItem( fields(), addressBook(), *iter, mIconView ); | ||
273 | |||
274 | mIconView->arrangeItemsInGrid( true ); | ||
275 | |||
276 | for ( item = mIconView->firstItem(); item; item = item->nextItem() ) | ||
277 | { | ||
278 | #ifndef KAB_EMBEDDED | ||
279 | AddresseeIconViewItem* aivi = dynamic_cast<AddresseeIconViewItem*>( item ); | ||
280 | #else //KAB_EMBEDDED | ||
281 | AddresseeIconViewItem* aivi = (AddresseeIconViewItem*)( item ); | ||
282 | #endif //KAB_EMBEDDED | ||
283 | mIconList.append( aivi ); | ||
284 | } | ||
285 | |||
286 | } else { | ||
287 | // Try to find the one to refresh | ||
288 | for ( item = mIconView->firstItem(); item; item = item->nextItem() ) { | ||
289 | #ifndef KAB_EMBEDDED | ||
290 | aItem = dynamic_cast<AddresseeIconViewItem*>(item); | ||
291 | #else //KAB_EMBEDDED | ||
292 | aItem = (AddresseeIconViewItem*)(item); | ||
293 | #endif //KAB_EMBEDDED | ||
294 | if ((aItem) && (aItem->addressee().uid() == uid)) { | ||
295 | aItem->refresh(); | ||
296 | mIconView->arrangeItemsInGrid( true ); | ||
297 | return; | ||
298 | } | ||
299 | } | ||
300 | refresh( QString::null ); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | void KAddressBookIconView::setSelected(QString uid, bool selected) | ||
305 | { | ||
306 | QIconViewItem *item; | ||
307 | AddresseeIconViewItem *aItem; | ||
308 | |||
309 | if (uid.isNull()) | ||
310 | { | ||
311 | mIconView->selectAll(selected); | ||
312 | } | ||
313 | else | ||
314 | { | ||
315 | bool found = false; | ||
316 | for (item = mIconView->firstItem(); item && !found; | ||
317 | item = item->nextItem()) | ||
318 | { | ||
319 | #ifndef KAB_EMBEDDED | ||
320 | aItem = dynamic_cast<AddresseeIconViewItem*>(item); | ||
321 | #else //KAB_EMBEDDED | ||
322 | aItem = (AddresseeIconViewItem*)(item); | ||
323 | #endif //KAB_EMBEDDED | ||
324 | |||
325 | if ((aItem) && (aItem->addressee().uid() == uid)) | ||
326 | { | ||
327 | mIconView->setSelected(aItem, selected); | ||
328 | mIconView->ensureItemVisible( aItem ); | ||
329 | found = true; | ||
330 | } | ||
331 | } | ||
332 | } | ||
333 | } | ||
334 | |||
335 | void KAddressBookIconView::addresseeExecuted(QIconViewItem *item) | ||
336 | { | ||
337 | #ifndef KAB_EMBEDDED | ||
338 | AddresseeIconViewItem *aItem = dynamic_cast<AddresseeIconViewItem*>(item); | ||
339 | #else //KAB_EMBEDDED | ||
340 | AddresseeIconViewItem *aItem = (AddresseeIconViewItem*)(item); | ||
341 | #endif //KAB_EMBEDDED | ||
342 | |||
343 | if (aItem) { | ||
344 | emit executed(aItem->addressee().uid()); | ||
345 | } | ||
346 | } | ||
347 | |||
348 | void KAddressBookIconView::addresseeSelected() | ||
349 | { | ||
350 | QIconViewItem *item; | ||
351 | AddresseeIconViewItem *aItem; | ||
352 | |||
353 | bool found = false; | ||
354 | for (item = mIconView->firstItem(); item && !found; | ||
355 | item = item->nextItem()) | ||
356 | { | ||
357 | if (item->isSelected()) | ||
358 | { | ||
359 | #ifndef KAB_EMBEDDED | ||
360 | aItem = dynamic_cast<AddresseeIconViewItem*>(item); | ||
361 | #else //KAB_EMBEDDED | ||
362 | aItem = (AddresseeIconViewItem*)(item); | ||
363 | #endif //KAB_EMBEDDED | ||
364 | if (aItem) | ||
365 | { | ||
366 | emit selected(aItem->addressee().uid()); | ||
367 | found = true; | ||
368 | } | ||
369 | } | ||
370 | } | ||
371 | |||
372 | if (!found) | ||
373 | emit selected(QString::null); | ||
374 | } | ||
375 | |||
376 | #ifndef KAB_EMBEDDED | ||
377 | #include "kaddressbookiconview.moc" | ||
378 | #endif //KAB_EMBEDDED | ||