Diffstat (limited to 'kaddressbook/views/kaddressbookcardview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/views/kaddressbookcardview.cpp | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/kaddressbook/views/kaddressbookcardview.cpp b/kaddressbook/views/kaddressbookcardview.cpp new file mode 100644 index 0000000..239429f --- a/dev/null +++ b/kaddressbook/views/kaddressbookcardview.cpp | |||
@@ -0,0 +1,394 @@ | |||
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 | #include <qdragobject.h> | ||
25 | #include <qevent.h> | ||
26 | #include <qiconview.h> | ||
27 | #include <qlayout.h> | ||
28 | #include <qstringlist.h> | ||
29 | |||
30 | #include <kabc/addressbook.h> | ||
31 | #include <kabc/addressee.h> | ||
32 | #include <kconfig.h> | ||
33 | #include <kdebug.h> | ||
34 | #include <klocale.h> | ||
35 | |||
36 | #include "kabprefs.h" | ||
37 | #include "viewmanager.h" | ||
38 | |||
39 | #include "kaddressbookcardview.h" | ||
40 | |||
41 | #ifndef KAB_EMBEDDED | ||
42 | extern "C" { | ||
43 | void *init_libkaddrbk_cardview() | ||
44 | { | ||
45 | return ( new CardViewFactory ); | ||
46 | } | ||
47 | } | ||
48 | #endif //KAB_EMBEDDED | ||
49 | |||
50 | //////////////////////////////// | ||
51 | // AddresseeCardViewItem (internal class) | ||
52 | class AddresseeCardViewItem : public CardViewItem | ||
53 | { | ||
54 | public: | ||
55 | AddresseeCardViewItem(const KABC::Field::List &fields, | ||
56 | bool showEmptyFields, | ||
57 | KABC::AddressBook *doc, const KABC::Addressee &a, | ||
58 | CardView *parent) | ||
59 | : CardViewItem(parent, a.formattedName()), | ||
60 | mFields( fields ), mShowEmptyFields(showEmptyFields), | ||
61 | mDocument(doc), mAddressee(a) | ||
62 | { | ||
63 | if ( mFields.isEmpty() ) { | ||
64 | mFields = KABC::Field::defaultFields(); | ||
65 | } | ||
66 | refresh(); | ||
67 | } | ||
68 | |||
69 | const KABC::Addressee &addressee() const { return mAddressee; } | ||
70 | |||
71 | void refresh() | ||
72 | { | ||
73 | // Update our addressee, since it may have changed elsewhere | ||
74 | mAddressee = mDocument->findByUid(mAddressee.uid()); | ||
75 | |||
76 | if (!mAddressee.isEmpty()) | ||
77 | { | ||
78 | clearFields(); | ||
79 | |||
80 | // Try all the selected fields until we find one with text. | ||
81 | // This will limit the number of unlabeled icons in the view | ||
82 | KABC::Field::List::Iterator iter; | ||
83 | for (iter = mFields.begin(); iter != mFields.end(); ++iter) | ||
84 | { | ||
85 | // insert empty fields or not? not doing so saves a bit of memory and CPU | ||
86 | // (during geometry calculations), but prevents having equally | ||
87 | // wide label columns in all cards, unless CardViewItem/CardView search | ||
88 | // globally for the widest label. (anders) | ||
89 | //if (mShowEmptyFields || !(*iter)->value( mAddressee ).isEmpty()) | ||
90 | insertField((*iter)->label(), (*iter)->value( mAddressee )); | ||
91 | } | ||
92 | |||
93 | // We might want to make this the first field. hmm... -mpilone | ||
94 | setCaption( mAddressee.realName() ); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | private: | ||
99 | KABC::Field::List mFields; | ||
100 | bool mShowEmptyFields; | ||
101 | KABC::AddressBook *mDocument; | ||
102 | KABC::Addressee mAddressee; | ||
103 | }; | ||
104 | |||
105 | /////////////////////////////// | ||
106 | // AddresseeCardView | ||
107 | |||
108 | AddresseeCardView::AddresseeCardView(QWidget *parent, const char *name) | ||
109 | : CardView(parent, name) | ||
110 | { | ||
111 | setAcceptDrops(true); | ||
112 | } | ||
113 | |||
114 | AddresseeCardView::~AddresseeCardView() | ||
115 | { | ||
116 | } | ||
117 | |||
118 | void AddresseeCardView::dragEnterEvent(QDragEnterEvent *e) | ||
119 | { | ||
120 | #ifndef KAB_EMBEDDED | ||
121 | if (QTextDrag::canDecode(e)) | ||
122 | e->accept(); | ||
123 | #else //KAB_EMBEDDED | ||
124 | qDebug("AddresseeCardView::dragEnterEvent drag&drop is not implemented"); | ||
125 | #endif //KAB_EMBEDDED | ||
126 | } | ||
127 | |||
128 | void AddresseeCardView::dropEvent(QDropEvent *e) | ||
129 | { | ||
130 | emit addresseeDropped(e); | ||
131 | } | ||
132 | |||
133 | void AddresseeCardView::startDrag() | ||
134 | { | ||
135 | emit startAddresseeDrag(); | ||
136 | } | ||
137 | |||
138 | |||
139 | /////////////////////////////// | ||
140 | // KAddressBookCardView | ||
141 | |||
142 | KAddressBookCardView::KAddressBookCardView( KABC::AddressBook *ab, | ||
143 | QWidget *parent, const char *name ) | ||
144 | : KAddressBookView( ab, parent, name ) | ||
145 | { | ||
146 | mShowEmptyFields = false; | ||
147 | |||
148 | // Init the GUI | ||
149 | QVBoxLayout *layout = new QVBoxLayout(viewWidget()); | ||
150 | |||
151 | mCardView = new AddresseeCardView(viewWidget(), "mCardView"); | ||
152 | mCardView->setSelectionMode(CardView::Extended); | ||
153 | layout->addWidget(mCardView); | ||
154 | |||
155 | // Connect up the signals | ||
156 | connect(mCardView, SIGNAL(executed(CardViewItem *)), | ||
157 | this, SLOT(addresseeExecuted(CardViewItem *))); | ||
158 | connect(mCardView, SIGNAL(selectionChanged()), | ||
159 | this, SLOT(addresseeSelected())); | ||
160 | connect(mCardView, SIGNAL(addresseeDropped(QDropEvent*)), | ||
161 | this, SIGNAL(dropped(QDropEvent*))); | ||
162 | connect(mCardView, SIGNAL(startAddresseeDrag()), | ||
163 | this, SIGNAL(startDrag())); | ||
164 | } | ||
165 | |||
166 | KAddressBookCardView::~KAddressBookCardView() | ||
167 | { | ||
168 | } | ||
169 | |||
170 | void KAddressBookCardView::readConfig(KConfig *config) | ||
171 | { | ||
172 | KAddressBookView::readConfig(config); | ||
173 | |||
174 | // costum colors? | ||
175 | if ( config->readBoolEntry( "EnableCustomColors", false ) ) | ||
176 | { | ||
177 | QPalette p( mCardView->palette() ); | ||
178 | QColor c = p.color(QPalette::Normal, QColorGroup::Base ); | ||
179 | p.setColor( QPalette::Normal, QColorGroup::Base, config->readColorEntry( "BackgroundColor", &c ) ); | ||
180 | c = p.color(QPalette::Normal, QColorGroup::Text ); | ||
181 | p.setColor( QPalette::Normal, QColorGroup::Text, config->readColorEntry( "TextColor", &c ) ); | ||
182 | c = p.color(QPalette::Normal, QColorGroup::Button ); | ||
183 | p.setColor( QPalette::Normal, QColorGroup::Button, config->readColorEntry( "HeaderColor", &c ) ); | ||
184 | c = p.color(QPalette::Normal, QColorGroup::ButtonText ); | ||
185 | p.setColor( QPalette::Normal, QColorGroup::ButtonText, config->readColorEntry( "HeaderTextColor", &c ) ); | ||
186 | c = p.color(QPalette::Normal, QColorGroup::Highlight ); | ||
187 | p.setColor( QPalette::Normal, QColorGroup::Highlight, config->readColorEntry( "HighlightColor", &c ) ); | ||
188 | c = p.color(QPalette::Normal, QColorGroup::HighlightedText ); | ||
189 | p.setColor( QPalette::Normal, QColorGroup::HighlightedText, config->readColorEntry( "HighlightedTextColor", &c ) ); | ||
190 | mCardView->viewport()->setPalette( p ); | ||
191 | } | ||
192 | else | ||
193 | { | ||
194 | // needed if turned off during a session. | ||
195 | mCardView->viewport()->setPalette( mCardView->palette() ); | ||
196 | } | ||
197 | |||
198 | //custom fonts? | ||
199 | QFont f( font() ); | ||
200 | if ( config->readBoolEntry( "EnableCustomFonts", false ) ) | ||
201 | { | ||
202 | mCardView->setFont( config->readFontEntry( "TextFont", &f) ); | ||
203 | f.setBold( true ); | ||
204 | mCardView->setHeaderFont( config->readFontEntry( "HeaderFont", &f ) ); | ||
205 | } | ||
206 | else | ||
207 | { | ||
208 | mCardView->setFont( f ); | ||
209 | f.setBold( true ); | ||
210 | mCardView->setHeaderFont( f ); | ||
211 | } | ||
212 | |||
213 | mCardView->setDrawCardBorder(config->readBoolEntry("DrawBorder", true)); | ||
214 | mCardView->setDrawColSeparators(config->readBoolEntry("DrawSeparators", | ||
215 | true)); | ||
216 | mCardView->setDrawFieldLabels(config->readBoolEntry("DrawFieldLabels",false)); | ||
217 | mShowEmptyFields = config->readBoolEntry("ShowEmptyFields", false); | ||
218 | |||
219 | mCardView->setShowEmptyFields( mShowEmptyFields ); | ||
220 | |||
221 | mCardView->setItemWidth( config->readNumEntry( "ItemWidth", 200 ) ); | ||
222 | mCardView->setItemMargin( config->readNumEntry( "ItemMargin", 0 ) ); | ||
223 | mCardView->setItemSpacing( config->readNumEntry( "ItemSpacing", 10 ) ); | ||
224 | mCardView->setSeparatorWidth( config->readNumEntry( "SeparatorWidth", 2 ) ); | ||
225 | |||
226 | disconnect(mCardView, SIGNAL(executed(CardViewItem *)), | ||
227 | this, SLOT(addresseeExecuted(CardViewItem *))); | ||
228 | |||
229 | if (KABPrefs::instance()->mHonorSingleClick) | ||
230 | connect(mCardView, SIGNAL(executed(CardViewItem *)), | ||
231 | this, SLOT(addresseeExecuted(CardViewItem *))); | ||
232 | else | ||
233 | connect(mCardView, SIGNAL(doubleClicked(CardViewItem *)), | ||
234 | this, SLOT(addresseeExecuted(CardViewItem *))); | ||
235 | |||
236 | } | ||
237 | |||
238 | void KAddressBookCardView::writeConfig( KConfig *config ) | ||
239 | { | ||
240 | config->writeEntry( "ItemWidth", mCardView->itemWidth() ); | ||
241 | KAddressBookView::writeConfig( config ); | ||
242 | } | ||
243 | |||
244 | QStringList KAddressBookCardView::selectedUids() | ||
245 | { | ||
246 | QStringList uidList; | ||
247 | CardViewItem *item; | ||
248 | AddresseeCardViewItem *aItem; | ||
249 | |||
250 | for (item = mCardView->firstItem(); item; item = item->nextItem()) | ||
251 | { | ||
252 | if (item->isSelected()) | ||
253 | { | ||
254 | #ifndef KAB_EMBEDDED | ||
255 | aItem = dynamic_cast<AddresseeCardViewItem*>(item); | ||
256 | #else //KAB_EMBEDDED | ||
257 | aItem = (AddresseeCardViewItem*)(item); | ||
258 | #endif //KAB_EMBEDDED | ||
259 | if (aItem) | ||
260 | uidList << aItem->addressee().uid(); | ||
261 | } | ||
262 | } | ||
263 | |||
264 | return uidList; | ||
265 | } | ||
266 | |||
267 | void KAddressBookCardView::refresh(QString uid) | ||
268 | { | ||
269 | CardViewItem *item; | ||
270 | AddresseeCardViewItem *aItem; | ||
271 | |||
272 | if (uid.isNull()) | ||
273 | { | ||
274 | // Rebuild the view | ||
275 | mCardView->viewport()->setUpdatesEnabled( false ); | ||
276 | mCardView->clear(); | ||
277 | |||
278 | KABC::Addressee::List addresseeList = addressees(); | ||
279 | KABC::Addressee::List::Iterator iter; | ||
280 | for (iter = addresseeList.begin(); iter != addresseeList.end(); ++iter) | ||
281 | { | ||
282 | aItem = new AddresseeCardViewItem(fields(), mShowEmptyFields, | ||
283 | addressBook(), *iter, mCardView); | ||
284 | } | ||
285 | mCardView->viewport()->setUpdatesEnabled( true ); | ||
286 | mCardView->viewport()->update(); | ||
287 | |||
288 | // by default nothing is selected | ||
289 | emit selected(QString::null); | ||
290 | } | ||
291 | else | ||
292 | { | ||
293 | // Try to find the one to refresh | ||
294 | bool found = false; | ||
295 | for (item = mCardView->firstItem(); item && !found; | ||
296 | item = item->nextItem()) | ||
297 | { | ||
298 | #ifndef KAB_EMBEDDED | ||
299 | aItem = dynamic_cast<AddresseeCardViewItem*>(item); | ||
300 | #else //KAB_EMBEDDED | ||
301 | aItem = (AddresseeCardViewItem*)(item); | ||
302 | #endif //KAB_EMBEDDED | ||
303 | |||
304 | if ((aItem) && (aItem->addressee().uid() == uid)) | ||
305 | { | ||
306 | aItem->refresh(); | ||
307 | found = true; | ||
308 | } | ||
309 | } | ||
310 | } | ||
311 | } | ||
312 | |||
313 | void KAddressBookCardView::setSelected(QString uid, bool selected) | ||
314 | { | ||
315 | CardViewItem *item; | ||
316 | AddresseeCardViewItem *aItem; | ||
317 | |||
318 | if (uid.isNull()) | ||
319 | { | ||
320 | mCardView->selectAll(selected); | ||
321 | } | ||
322 | else | ||
323 | { | ||
324 | bool found = false; | ||
325 | for (item = mCardView->firstItem(); item && !found; | ||
326 | item = item->nextItem()) | ||
327 | { | ||
328 | #ifndef KAB_EMBEDDED | ||
329 | aItem = dynamic_cast<AddresseeCardViewItem*>(item); | ||
330 | #else //KAB_EMBEDDED | ||
331 | aItem = (AddresseeCardViewItem*)(item); | ||
332 | #endif //KAB_EMBEDDED | ||
333 | |||
334 | if ((aItem) && (aItem->addressee().uid() == uid)) | ||
335 | { | ||
336 | mCardView->setSelected(aItem, selected); | ||
337 | mCardView->ensureItemVisible(item); | ||
338 | found = true; | ||
339 | } | ||
340 | } | ||
341 | } | ||
342 | } | ||
343 | |||
344 | //US added an additional method without parameter | ||
345 | void KAddressBookCardView::setSelected() | ||
346 | { | ||
347 | setSelected(QString::null, true); | ||
348 | } | ||
349 | |||
350 | void KAddressBookCardView::addresseeExecuted(CardViewItem *item) | ||
351 | { | ||
352 | #ifndef KAB_EMBEDDED | ||
353 | AddresseeCardViewItem *aItem = dynamic_cast<AddresseeCardViewItem*>(item); | ||
354 | #else //KAB_EMBEDDED | ||
355 | AddresseeCardViewItem *aItem = (AddresseeCardViewItem*)(item); | ||
356 | #endif //KAB_EMBEDDED | ||
357 | if (aItem) | ||
358 | { | ||
359 | //kdDebug()<<"... even has a valid item:)"<<endl; | ||
360 | emit executed(aItem->addressee().uid()); | ||
361 | } | ||
362 | } | ||
363 | |||
364 | void KAddressBookCardView::addresseeSelected() | ||
365 | { | ||
366 | CardViewItem *item; | ||
367 | AddresseeCardViewItem *aItem; | ||
368 | |||
369 | bool found = false; | ||
370 | for (item = mCardView->firstItem(); item && !found; | ||
371 | item = item->nextItem()) | ||
372 | { | ||
373 | if (item->isSelected()) | ||
374 | { | ||
375 | #ifndef KAB_EMBEDDED | ||
376 | aItem = dynamic_cast<AddresseeCardViewItem*>(item); | ||
377 | #else //KAB_EMBEDDED | ||
378 | aItem = (AddresseeCardViewItem*)(item); | ||
379 | #endif //KAB_EMBEDDED | ||
380 | if ( aItem ) | ||
381 | { | ||
382 | emit selected(aItem->addressee().uid()); | ||
383 | found = true; | ||
384 | } | ||
385 | } | ||
386 | } | ||
387 | |||
388 | if (!found) | ||
389 | emit selected(QString::null); | ||
390 | |||
391 | } | ||
392 | #ifndef KAB_EMBEDDED | ||
393 | #include "kaddressbookcardview.moc" | ||
394 | #endif //KAB_EMBEDDED | ||