kitchensync

kwidgetlist.cpp
1/*
2 This file is part of KitchenSync.
3
4 Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include <tqvbox.h>
23
24#include <tdeglobalsettings.h>
25
26#include "kwidgetlist.h"
27
28class KWidgetList::Private
29{
30 public:
31 Private()
32 : mSelectedItem( 0 )
33 {
34 }
35
36 TQValueList<KWidgetListItem*> mItems;
37 KWidgetListItem *mSelectedItem;
38 TQVBox *mBox;
39};
40
41KWidgetList::KWidgetList( TQWidget *parent, const char *name )
42 : TQScrollView( parent, name ),
43 d( new Private )
44{
45 d->mBox = new TQVBox( viewport() );
46 addChild( d->mBox );
47
48 setResizePolicy( AutoOneFit );
49 setFocusPolicy( TQ_StrongFocus );
50
51 viewport()->setFocus();
52}
53
54KWidgetList::~KWidgetList()
55{
56 clear();
57
58 delete d;
59 d = 0;
60}
61
62uint KWidgetList::count() const
63{
64 return d->mItems.count();
65}
66
67void KWidgetList::appendItem( KWidgetListItem *item )
68{
69 if ( !item )
70 return;
71
72 if ( !d->mItems.contains( item ) ) {
73 d->mItems.append( item );
74 item->reparent( d->mBox, 0, TQPoint( 0, 0 ), true );
75 item->setSelected( false );
76 item->installEventFilter( this );
77
78 if ( d->mItems.count() == 1 ) {
79 d->mSelectedItem = item;
80 } else {
81 if ( !d->mSelectedItem )
82 setSelected( item );
83 else
84 d->mSelectedItem->setSelected( true );
85 }
86 }
87}
88
89void KWidgetList::removeItem( int index )
90{
91 if ( index < 0 || index >= (int)d->mItems.count() )
92 return;
93
94 KWidgetListItem *item = d->mItems[ index ];
95 d->mItems.remove( item );
96
97 if ( d->mSelectedItem == item ) {
98 // TODO: smarter selection
99 if ( !d->mItems.isEmpty() )
100 setSelected( d->mItems.first() );
101 else
102 d->mSelectedItem = 0;
103 }
104
105 delete item;
106
107 if ( d->mItems.count() == 1 )
108 d->mItems.first()->setSelected( false );
109}
110
111void KWidgetList::takeItem( KWidgetListItem *item )
112{
113 d->mItems.remove( item );
114 item->reparent( 0, 0, TQPoint( 0, 0 ) );
115 item->removeEventFilter( this );
116 item->hide();
117
118 if ( d->mSelectedItem == item ) {
119 // TODO: smarter selection
120 if ( !d->mItems.isEmpty() )
121 setSelected( d->mItems.first() );
122 else
123 d->mSelectedItem = 0;
124 }
125}
126
127void KWidgetList::setSelected( KWidgetListItem *item )
128{
129 if ( !item )
130 return;
131
132 if ( d->mItems.contains( item ) == 0 )
133 return;
134
135 if ( d->mSelectedItem )
136 d->mSelectedItem->setSelected( false );
137
138 item->setSelected( true );
139 d->mSelectedItem = item;
140}
141
142void KWidgetList::setSelected( int index )
143{
144 setSelected( item( index ) );
145}
146
147bool KWidgetList::isSelected( KWidgetListItem *item ) const
148{
149 return ( d->mSelectedItem == item );
150}
151
152bool KWidgetList::isSelected( int index ) const
153{
154 return isSelected( item( index ) );
155}
156
157KWidgetListItem *KWidgetList::selectedItem() const
158{
159 return d->mSelectedItem;
160}
161
162KWidgetListItem *KWidgetList::item( int index ) const
163{
164 if ( index < 0 || index >= (int)d->mItems.count() )
165 return 0;
166 else
167 return d->mItems[ index ];
168}
169
170int KWidgetList::index( KWidgetListItem *item ) const
171{
172 return d->mItems.findIndex( item );
173}
174
175void KWidgetList::clear()
176{
177 TQValueList<KWidgetListItem*>::Iterator it;
178 for ( it = d->mItems.begin(); it != d->mItems.end(); ++it )
179 delete *it;
180
181 d->mItems.clear();
182
183 d->mSelectedItem = 0;
184}
185
186void KWidgetList::setFocus()
187{
188 viewport()->setFocus();
189}
190
191bool KWidgetList::eventFilter( TQObject *object, TQEvent *event )
192{
193 if ( event->type() == TQEvent::MouseButtonPress ) {
194 TQMouseEvent *mouseEvent = TQT_TQMOUSEEVENT( event );
195 if ( mouseEvent->button() & Qt::LeftButton ) {
196 TQValueList<KWidgetListItem*>::Iterator it;
197 for ( it = d->mItems.begin(); it != d->mItems.end(); ++it ) {
198 if ( TQT_BASE_OBJECT(*it) == TQT_BASE_OBJECT(object) ) {
199 if ( d->mItems.count() != 1 ) {
200 setSelected( *it );
201 emit selectionChanged( *it );
202 }
203 return true;
204 }
205 }
206 }
207 } else if ( event->type() == TQEvent::MouseButtonDblClick ) {
208 TQValueList<KWidgetListItem*>::Iterator it;
209 for ( it = d->mItems.begin(); it != d->mItems.end(); ++it ) {
210 if ( TQT_BASE_OBJECT(*it) == TQT_BASE_OBJECT(object) ) {
211 if ( d->mItems.count() != 1 ) {
212 setSelected( *it );
213 emit doubleClicked( *it );
214 }
215 return true;
216 }
217 }
218 } else if ( event->type() == TQEvent::KeyPress ) {
219 TQKeyEvent *keyEvent = TQT_TQKEYEVENT( event );
220 if ( keyEvent->key() == TQt::Key_Up ) {
221 if ( d->mSelectedItem == 0 ) {
222 if ( !d->mItems.isEmpty() ) {
223 setSelected( d->mItems.first() );
224 return true;
225 }
226 }
227
228 for ( int i = 0; i < (int)d->mItems.count(); ++i ) {
229 if ( d->mItems[ i ] == d->mSelectedItem ) {
230 if ( ( i - 1 ) >= 0 ) {
231 setSelected( d->mItems[ i - 1 ] );
232 return true;
233 }
234 }
235 }
236 return true;
237 } else if ( keyEvent->key() == TQt::Key_Down ) {
238 if ( d->mSelectedItem == 0 ) {
239 if ( !d->mItems.isEmpty() ) {
240 setSelected( d->mItems.last() );
241 return true;
242 }
243 }
244
245 for ( int i = 0; i < (int)d->mItems.count(); ++i )
246 if ( d->mItems[ i ] == d->mSelectedItem ) {
247 if ( ( i + 1 ) < (int)d->mItems.count() ) {
248 setSelected( d->mItems[ i + 1 ] );
249 return true;
250 }
251 }
252 return true;
253 }
254 }
255
256 return TQScrollView::eventFilter( object, event );
257}
258
259KWidgetListItem::KWidgetListItem( KWidgetList *parent, const char *name )
260 : TQWidget( parent, name )
261{
262 mForegroundColor = TDEGlobalSettings::textColor();
263 mBackgroundColor = TDEGlobalSettings::baseColor();
264 mSelectionForegroundColor = TDEGlobalSettings::highlightedTextColor();
265 mSelectionBackgroundColor = TDEGlobalSettings::highlightColor();
266
267 setFocusPolicy( TQ_StrongFocus );
268}
269
270KWidgetListItem::~KWidgetListItem()
271{
272}
273
274void KWidgetListItem::setSelected( bool select )
275{
276 if ( select ) {
277 setPaletteForegroundColor( mSelectionForegroundColor );
278 setPaletteBackgroundColor( mSelectionBackgroundColor );
279 } else {
280 setPaletteForegroundColor( mForegroundColor );
281 setPaletteBackgroundColor( mBackgroundColor );
282 }
283}
284
285void KWidgetListItem::setForegroundColor( const TQColor &color )
286{
287 mForegroundColor = color;
288}
289
290void KWidgetListItem::setBackgroundColor( const TQColor &color )
291{
292 mBackgroundColor = color;
293}
294
295void KWidgetListItem::setSelectionForegroundColor( const TQColor &color )
296{
297 mSelectionForegroundColor = color;
298}
299
300void KWidgetListItem::setSelectionBackgroundColor( const TQColor &color )
301{
302 mSelectionBackgroundColor = color;
303}
304
305#include "kwidgetlist.moc"