kwidgetlist.cpp
00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2005 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include <tqvbox.h> 00023 00024 #include <tdeglobalsettings.h> 00025 00026 #include "kwidgetlist.h" 00027 00028 class KWidgetList::Private 00029 { 00030 public: 00031 Private() 00032 : mSelectedItem( 0 ) 00033 { 00034 } 00035 00036 TQValueList<KWidgetListItem*> mItems; 00037 KWidgetListItem *mSelectedItem; 00038 TQVBox *mBox; 00039 }; 00040 00041 KWidgetList::KWidgetList( TQWidget *parent, const char *name ) 00042 : TQScrollView( parent, name ), 00043 d( new Private ) 00044 { 00045 d->mBox = new TQVBox( viewport() ); 00046 addChild( d->mBox ); 00047 00048 setResizePolicy( AutoOneFit ); 00049 setFocusPolicy( TQ_StrongFocus ); 00050 00051 viewport()->setFocus(); 00052 } 00053 00054 KWidgetList::~KWidgetList() 00055 { 00056 clear(); 00057 00058 delete d; 00059 d = 0; 00060 } 00061 00062 uint KWidgetList::count() const 00063 { 00064 return d->mItems.count(); 00065 } 00066 00067 void KWidgetList::appendItem( KWidgetListItem *item ) 00068 { 00069 if ( !item ) 00070 return; 00071 00072 if ( !d->mItems.contains( item ) ) { 00073 d->mItems.append( item ); 00074 item->reparent( d->mBox, 0, TQPoint( 0, 0 ), true ); 00075 item->setSelected( false ); 00076 item->installEventFilter( this ); 00077 00078 if ( d->mItems.count() == 1 ) { 00079 d->mSelectedItem = item; 00080 } else { 00081 if ( !d->mSelectedItem ) 00082 setSelected( item ); 00083 else 00084 d->mSelectedItem->setSelected( true ); 00085 } 00086 } 00087 } 00088 00089 void KWidgetList::removeItem( int index ) 00090 { 00091 if ( index < 0 || index >= (int)d->mItems.count() ) 00092 return; 00093 00094 KWidgetListItem *item = d->mItems[ index ]; 00095 d->mItems.remove( item ); 00096 00097 if ( d->mSelectedItem == item ) { 00098 // TODO: smarter selection 00099 if ( !d->mItems.isEmpty() ) 00100 setSelected( d->mItems.first() ); 00101 else 00102 d->mSelectedItem = 0; 00103 } 00104 00105 delete item; 00106 00107 if ( d->mItems.count() == 1 ) 00108 d->mItems.first()->setSelected( false ); 00109 } 00110 00111 void KWidgetList::takeItem( KWidgetListItem *item ) 00112 { 00113 d->mItems.remove( item ); 00114 item->reparent( 0, 0, TQPoint( 0, 0 ) ); 00115 item->removeEventFilter( this ); 00116 item->hide(); 00117 00118 if ( d->mSelectedItem == item ) { 00119 // TODO: smarter selection 00120 if ( !d->mItems.isEmpty() ) 00121 setSelected( d->mItems.first() ); 00122 else 00123 d->mSelectedItem = 0; 00124 } 00125 } 00126 00127 void KWidgetList::setSelected( KWidgetListItem *item ) 00128 { 00129 if ( !item ) 00130 return; 00131 00132 if ( d->mItems.contains( item ) == 0 ) 00133 return; 00134 00135 if ( d->mSelectedItem ) 00136 d->mSelectedItem->setSelected( false ); 00137 00138 item->setSelected( true ); 00139 d->mSelectedItem = item; 00140 } 00141 00142 void KWidgetList::setSelected( int index ) 00143 { 00144 setSelected( item( index ) ); 00145 } 00146 00147 bool KWidgetList::isSelected( KWidgetListItem *item ) const 00148 { 00149 return ( d->mSelectedItem == item ); 00150 } 00151 00152 bool KWidgetList::isSelected( int index ) const 00153 { 00154 return isSelected( item( index ) ); 00155 } 00156 00157 KWidgetListItem *KWidgetList::selectedItem() const 00158 { 00159 return d->mSelectedItem; 00160 } 00161 00162 KWidgetListItem *KWidgetList::item( int index ) const 00163 { 00164 if ( index < 0 || index >= (int)d->mItems.count() ) 00165 return 0; 00166 else 00167 return d->mItems[ index ]; 00168 } 00169 00170 int KWidgetList::index( KWidgetListItem *item ) const 00171 { 00172 return d->mItems.findIndex( item ); 00173 } 00174 00175 void KWidgetList::clear() 00176 { 00177 TQValueList<KWidgetListItem*>::Iterator it; 00178 for ( it = d->mItems.begin(); it != d->mItems.end(); ++it ) 00179 delete *it; 00180 00181 d->mItems.clear(); 00182 00183 d->mSelectedItem = 0; 00184 } 00185 00186 void KWidgetList::setFocus() 00187 { 00188 viewport()->setFocus(); 00189 } 00190 00191 bool KWidgetList::eventFilter( TQObject *object, TQEvent *event ) 00192 { 00193 if ( event->type() == TQEvent::MouseButtonPress ) { 00194 TQMouseEvent *mouseEvent = TQT_TQMOUSEEVENT( event ); 00195 if ( mouseEvent->button() & Qt::LeftButton ) { 00196 TQValueList<KWidgetListItem*>::Iterator it; 00197 for ( it = d->mItems.begin(); it != d->mItems.end(); ++it ) { 00198 if ( TQT_BASE_OBJECT(*it) == TQT_BASE_OBJECT(object) ) { 00199 if ( d->mItems.count() != 1 ) { 00200 setSelected( *it ); 00201 emit selectionChanged( *it ); 00202 } 00203 return true; 00204 } 00205 } 00206 } 00207 } else if ( event->type() == TQEvent::MouseButtonDblClick ) { 00208 TQValueList<KWidgetListItem*>::Iterator it; 00209 for ( it = d->mItems.begin(); it != d->mItems.end(); ++it ) { 00210 if ( TQT_BASE_OBJECT(*it) == TQT_BASE_OBJECT(object) ) { 00211 if ( d->mItems.count() != 1 ) { 00212 setSelected( *it ); 00213 emit doubleClicked( *it ); 00214 } 00215 return true; 00216 } 00217 } 00218 } else if ( event->type() == TQEvent::KeyPress ) { 00219 TQKeyEvent *keyEvent = TQT_TQKEYEVENT( event ); 00220 if ( keyEvent->key() == TQt::Key_Up ) { 00221 if ( d->mSelectedItem == 0 ) { 00222 if ( !d->mItems.isEmpty() ) { 00223 setSelected( d->mItems.first() ); 00224 return true; 00225 } 00226 } 00227 00228 for ( int i = 0; i < (int)d->mItems.count(); ++i ) { 00229 if ( d->mItems[ i ] == d->mSelectedItem ) { 00230 if ( ( i - 1 ) >= 0 ) { 00231 setSelected( d->mItems[ i - 1 ] ); 00232 return true; 00233 } 00234 } 00235 } 00236 return true; 00237 } else if ( keyEvent->key() == TQt::Key_Down ) { 00238 if ( d->mSelectedItem == 0 ) { 00239 if ( !d->mItems.isEmpty() ) { 00240 setSelected( d->mItems.last() ); 00241 return true; 00242 } 00243 } 00244 00245 for ( int i = 0; i < (int)d->mItems.count(); ++i ) 00246 if ( d->mItems[ i ] == d->mSelectedItem ) { 00247 if ( ( i + 1 ) < (int)d->mItems.count() ) { 00248 setSelected( d->mItems[ i + 1 ] ); 00249 return true; 00250 } 00251 } 00252 return true; 00253 } 00254 } 00255 00256 return TQScrollView::eventFilter( object, event ); 00257 } 00258 00259 KWidgetListItem::KWidgetListItem( KWidgetList *parent, const char *name ) 00260 : TQWidget( parent, name ) 00261 { 00262 mForegroundColor = TDEGlobalSettings::textColor(); 00263 mBackgroundColor = TDEGlobalSettings::baseColor(); 00264 mSelectionForegroundColor = TDEGlobalSettings::highlightedTextColor(); 00265 mSelectionBackgroundColor = TDEGlobalSettings::highlightColor(); 00266 00267 setFocusPolicy( TQ_StrongFocus ); 00268 } 00269 00270 KWidgetListItem::~KWidgetListItem() 00271 { 00272 } 00273 00274 void KWidgetListItem::setSelected( bool select ) 00275 { 00276 if ( select ) { 00277 setPaletteForegroundColor( mSelectionForegroundColor ); 00278 setPaletteBackgroundColor( mSelectionBackgroundColor ); 00279 } else { 00280 setPaletteForegroundColor( mForegroundColor ); 00281 setPaletteBackgroundColor( mBackgroundColor ); 00282 } 00283 } 00284 00285 void KWidgetListItem::setForegroundColor( const TQColor &color ) 00286 { 00287 mForegroundColor = color; 00288 } 00289 00290 void KWidgetListItem::setBackgroundColor( const TQColor &color ) 00291 { 00292 mBackgroundColor = color; 00293 } 00294 00295 void KWidgetListItem::setSelectionForegroundColor( const TQColor &color ) 00296 { 00297 mSelectionForegroundColor = color; 00298 } 00299 00300 void KWidgetListItem::setSelectionBackgroundColor( const TQColor &color ) 00301 { 00302 mSelectionBackgroundColor = color; 00303 } 00304 00305 #include "kwidgetlist.moc"