author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kaddressbook/views/contactlistview.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'kaddressbook/views/contactlistview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/views/contactlistview.cpp | 340 |
1 files changed, 340 insertions, 0 deletions
diff --git a/kaddressbook/views/contactlistview.cpp b/kaddressbook/views/contactlistview.cpp new file mode 100644 index 0000000..98b2fb2 --- a/dev/null +++ b/kaddressbook/views/contactlistview.cpp | |||
@@ -0,0 +1,340 @@ | |||
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 <qheader.h> | ||
25 | #include <qiconset.h> | ||
26 | #include <qimage.h> | ||
27 | #include <qdragobject.h> | ||
28 | #include <qcombobox.h> | ||
29 | #include <qpainter.h> | ||
30 | #include <qbrush.h> | ||
31 | #include <qevent.h> | ||
32 | |||
33 | #include <klocale.h> | ||
34 | #include <kglobalsettings.h> | ||
35 | #include <kiconloader.h> | ||
36 | #include <kdebug.h> | ||
37 | #include <kconfig.h> | ||
38 | #include <kapplication.h> | ||
39 | #include <kurl.h> | ||
40 | |||
41 | #include "kaddressbooktableview.h" | ||
42 | |||
43 | #include "contactlistview.h" | ||
44 | |||
45 | ///////////////////////////////// | ||
46 | // DynamicTip Methods | ||
47 | |||
48 | DynamicTip::DynamicTip( ContactListView *parent) | ||
49 | : QToolTip( parent ) | ||
50 | { | ||
51 | } | ||
52 | |||
53 | void DynamicTip::maybeTip( const QPoint &pos ) | ||
54 | { | ||
55 | static bool ishidden = true; | ||
56 | if (!parentWidget()->inherits( "ContactListView" )) | ||
57 | return; | ||
58 | |||
59 | ContactListView *plv = (ContactListView*)parentWidget(); | ||
60 | if (!plv->tooltips()) | ||
61 | return; | ||
62 | |||
63 | QPoint posVp = plv->viewport()->pos(); | ||
64 | |||
65 | QListViewItem *lvi = plv->itemAt( pos - posVp ); | ||
66 | if (!lvi) | ||
67 | return; | ||
68 | |||
69 | #ifndef KAB_EMBEDDED | ||
70 | ContactListViewItem *plvi = dynamic_cast< ContactListViewItem* >(lvi); | ||
71 | #else //KAB_EMBEDDED | ||
72 | ContactListViewItem *plvi = (ContactListViewItem*)(lvi); | ||
73 | #endif //KAB_EMBEDDED | ||
74 | |||
75 | if (!plvi) | ||
76 | return; | ||
77 | |||
78 | if (ishidden) { | ||
79 | QString s; | ||
80 | QRect r = plv->itemRect( lvi ); | ||
81 | r.moveBy( posVp.x(), posVp.y() ); | ||
82 | |||
83 | //kdDebug() << "Tip rec: " << r.x() << "," << r.y() << "," << r.width() | ||
84 | // << "," << r.height() << endl; | ||
85 | |||
86 | KABC::Addressee a = plvi->addressee(); | ||
87 | if (a.isEmpty()) | ||
88 | return; | ||
89 | |||
90 | s += i18n("label: value", "%1: %2").arg(a.formattedNameLabel()) | ||
91 | .arg(a.formattedName()); | ||
92 | |||
93 | s += '\n'; | ||
94 | s += i18n("label: value", "%1: %2").arg(a.organizationLabel()) | ||
95 | .arg(a.organization()); | ||
96 | |||
97 | QString notes = a.note().stripWhiteSpace(); | ||
98 | if ( !notes.isEmpty() ) { | ||
99 | notes += '\n'; | ||
100 | s += '\n' + i18n("label: value", "%1: \n").arg(a.noteLabel()); | ||
101 | QFontMetrics fm( font() ); | ||
102 | |||
103 | // Begin word wrap code based on QMultiLineEdit code | ||
104 | int i = 0; | ||
105 | bool doBreak = false; | ||
106 | int linew = 0; | ||
107 | int lastSpace = -1; | ||
108 | int a = 0; | ||
109 | int lastw = 0; | ||
110 | |||
111 | while ( i < int(notes.length()) ) { | ||
112 | doBreak = FALSE; | ||
113 | if ( notes[i] != '\n' ) | ||
114 | linew += fm.width( notes[i] ); | ||
115 | |||
116 | if ( lastSpace >= a && notes[i] != '\n' ) | ||
117 | if (linew >= parentWidget()->width()) { | ||
118 | doBreak = TRUE; | ||
119 | if ( lastSpace > a ) { | ||
120 | i = lastSpace; | ||
121 | linew = lastw; | ||
122 | } | ||
123 | else | ||
124 | i = QMAX( a, i-1 ); | ||
125 | } | ||
126 | |||
127 | if ( notes[i] == '\n' || doBreak ) { | ||
128 | s += notes.mid( a, i - a + (doBreak?1:0) ) +"\n"; | ||
129 | |||
130 | a = i + 1; | ||
131 | lastSpace = a; | ||
132 | linew = 0; | ||
133 | } | ||
134 | |||
135 | if ( notes[i].isSpace() ) { | ||
136 | lastSpace = i; | ||
137 | lastw = linew; | ||
138 | } | ||
139 | |||
140 | if ( lastSpace <= a ) { | ||
141 | lastw = linew; | ||
142 | } | ||
143 | |||
144 | ++i; | ||
145 | } | ||
146 | } | ||
147 | |||
148 | tip( r, s ); | ||
149 | } | ||
150 | else | ||
151 | hide(); | ||
152 | ishidden = !ishidden; | ||
153 | |||
154 | } | ||
155 | |||
156 | /////////////////////////// | ||
157 | // ContactListViewItem Methods | ||
158 | |||
159 | ContactListViewItem::ContactListViewItem(const KABC::Addressee &a, | ||
160 | ContactListView *parent, | ||
161 | KABC::AddressBook *doc, | ||
162 | const KABC::Field::List &fields ) | ||
163 | : KListViewItem(parent), mAddressee(a), mFields( fields ), | ||
164 | parentListView( parent ), mDocument(doc) | ||
165 | { | ||
166 | refresh(); | ||
167 | } | ||
168 | |||
169 | QString ContactListViewItem::key(int column, bool ascending) const | ||
170 | { | ||
171 | return QListViewItem::key(column, ascending).lower(); | ||
172 | } | ||
173 | |||
174 | void ContactListViewItem::paintCell(QPainter * p, | ||
175 | const QColorGroup & cg, | ||
176 | int column, | ||
177 | int width, | ||
178 | int align) | ||
179 | { | ||
180 | KListViewItem::paintCell(p, cg, column, width, align); | ||
181 | |||
182 | if ( !p ) | ||
183 | return; | ||
184 | |||
185 | if (parentListView->singleLine()) { | ||
186 | p->setPen( parentListView->alternateColor() ); | ||
187 | p->drawLine( 0, height() - 1, width, height() - 1 ); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | |||
192 | ContactListView *ContactListViewItem::parent() | ||
193 | { | ||
194 | return parentListView; | ||
195 | } | ||
196 | |||
197 | |||
198 | void ContactListViewItem::refresh() | ||
199 | { | ||
200 | // Update our addressee, since it may have changed else were | ||
201 | mAddressee = mDocument->findByUid(mAddressee.uid()); | ||
202 | if (mAddressee.isEmpty()) | ||
203 | return; | ||
204 | |||
205 | int i = 0; | ||
206 | KABC::Field::List::ConstIterator it; | ||
207 | for( it = mFields.begin(); it != mFields.end(); ++it ) { | ||
208 | setText( i++, (*it)->value( mAddressee ) ); | ||
209 | } | ||
210 | } | ||
211 | |||
212 | /////////////////////////////// | ||
213 | // ContactListView | ||
214 | |||
215 | ContactListView::ContactListView(KAddressBookTableView *view, | ||
216 | KABC::AddressBook* /* doc */, | ||
217 | QWidget *parent, | ||
218 | const char *name ) | ||
219 | : KListView( parent, name ), | ||
220 | pabWidget( view ), | ||
221 | oldColumn( 0 ) | ||
222 | { | ||
223 | mABackground = true; | ||
224 | mSingleLine = false; | ||
225 | mToolTips = true; | ||
226 | #ifndef KAB_EMBEDDED | ||
227 | mAlternateColor = KGlobalSettings::alternateBackgroundColor(); | ||
228 | #else //KAB_EMBEDDED | ||
229 | mAlternateColor = QColor(240, 240, 240); | ||
230 | #endif //KAB_EMBEDDED | ||
231 | |||
232 | setAlternateBackgroundEnabled(mABackground); | ||
233 | setAcceptDrops( true ); | ||
234 | viewport()->setAcceptDrops( true ); | ||
235 | setAllColumnsShowFocus( true ); | ||
236 | setShowSortIndicator(true); | ||
237 | |||
238 | setSelectionModeExt( KListView::Extended ); | ||
239 | setDropVisualizer(false); | ||
240 | // setFrameStyle(QFrame::NoFrame); | ||
241 | setLineWidth ( 0 ); | ||
242 | setMidLineWidth ( 0 ); | ||
243 | setMargin ( 0 ); | ||
244 | #ifndef KAB_EMBEDDED | ||
245 | connect(this, SIGNAL(dropped(QDropEvent*)), | ||
246 | this, SLOT(itemDropped(QDropEvent*))); | ||
247 | #endif //KAB_EMBEDDED | ||
248 | |||
249 | |||
250 | new DynamicTip( this ); | ||
251 | } | ||
252 | |||
253 | void ContactListView::paintEmptyArea( QPainter * p, const QRect & rect ) | ||
254 | { | ||
255 | QBrush b = palette().brush(QPalette::Active, QColorGroup::Base); | ||
256 | |||
257 | // Get the brush, which will have the background pixmap if there is one. | ||
258 | if (b.pixmap()) | ||
259 | { | ||
260 | p->drawTiledPixmap( rect.left(), rect.top(), rect.width(), rect.height(), | ||
261 | *(b.pixmap()), | ||
262 | rect.left() + contentsX(), | ||
263 | rect.top() + contentsY() ); | ||
264 | } | ||
265 | |||
266 | else | ||
267 | { | ||
268 | // Do a normal paint | ||
269 | KListView::paintEmptyArea(p, rect); | ||
270 | } | ||
271 | } | ||
272 | |||
273 | void ContactListView::contentsMousePressEvent(QMouseEvent* e) | ||
274 | { | ||
275 | presspos = e->pos(); | ||
276 | KListView::contentsMousePressEvent(e); | ||
277 | } | ||
278 | |||
279 | |||
280 | // To initiate a drag operation | ||
281 | void ContactListView::contentsMouseMoveEvent( QMouseEvent *e ) | ||
282 | { | ||
283 | if ((e->state() & LeftButton) && (e->pos() - presspos).manhattanLength() > 4 ) { | ||
284 | emit startAddresseeDrag(); | ||
285 | } | ||
286 | else | ||
287 | KListView::contentsMouseMoveEvent( e ); | ||
288 | } | ||
289 | |||
290 | bool ContactListView::acceptDrag(QDropEvent *e) const | ||
291 | { | ||
292 | #ifndef KAB_EMBEDDED | ||
293 | return QTextDrag::canDecode(e); | ||
294 | #else //KAB_EMBEDDED | ||
295 | qDebug("ContactListView::acceptDrag has to be fixed"); | ||
296 | return false; | ||
297 | #endif //KAB_EMBEDDED | ||
298 | } | ||
299 | |||
300 | void ContactListView::itemDropped(QDropEvent *e) | ||
301 | { | ||
302 | contentsDropEvent(e); | ||
303 | } | ||
304 | |||
305 | void ContactListView::contentsDropEvent( QDropEvent *e ) | ||
306 | { | ||
307 | emit addresseeDropped(e); | ||
308 | } | ||
309 | |||
310 | void ContactListView::setAlternateBackgroundEnabled(bool enabled) | ||
311 | { | ||
312 | mABackground = enabled; | ||
313 | |||
314 | if (mABackground) | ||
315 | { | ||
316 | setAlternateBackground(mAlternateColor); | ||
317 | } | ||
318 | else | ||
319 | { | ||
320 | setAlternateBackground(QColor()); | ||
321 | } | ||
322 | } | ||
323 | |||
324 | void ContactListView::setBackgroundPixmap(const QString &filename) | ||
325 | { | ||
326 | if (filename.isEmpty()) | ||
327 | { | ||
328 | unsetPalette(); | ||
329 | } | ||
330 | else | ||
331 | { | ||
332 | qDebug("ContactListView::setBackgroundPixmap has to be verified"); | ||
333 | //US setPaletteBackgroundPixmap(QPixmap(filename)); | ||
334 | KListView::setBackgroundPixmap((const QPixmap&)QPixmap(filename)); | ||
335 | } | ||
336 | |||
337 | } | ||
338 | #ifndef KAB_EMBEDDED | ||
339 | #include "contactlistview.moc" | ||
340 | #endif //KAB_EMBEDDED | ||