kwidgetlistbox.cpp

00001 /*
00002  * Copyright (C) 2005 Petri Damstén <petri.damsten@iki.fi>
00003  *
00004  * This file is part of SuperKaramba.
00005  *
00006  *  SuperKaramba is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  SuperKaramba 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
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with SuperKaramba; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  ****************************************************************************/
00020 #include "kwidgetlistbox.h"
00021 #include <kdebug.h>
00022 #include <kglobalsettings.h>
00023 
00024 KWidgetListbox::KWidgetListbox(QWidget *parent, const char *name)
00025  : QTable(parent, name)
00026 {
00027   setNumRows(0);
00028   setNumCols(1);
00029   setColumnStretchable(0, true);
00030   setLeftMargin(0);
00031   setTopMargin(0);
00032   horizontalHeader()->hide();
00033   verticalHeader()->hide();
00034   setSelectionMode(QTable::NoSelection);
00035   setFocusStyle(QTable::FollowStyle);
00036   connect(this, SIGNAL(currentChanged(int, int)),
00037           this, SLOT(selectionChanged(int, int)));
00038   setHScrollBarMode(QScrollView::AlwaysOff);
00039   setVScrollBarMode(QScrollView::Auto);
00040 }
00041 
00042 KWidgetListbox::~KWidgetListbox()
00043 {
00044   clear();
00045 }
00046 
00047 void KWidgetListbox::clear()
00048 {
00049   for(int i = 0; i < numRows(); ++i)
00050     clearCellWidget(i, 0);
00051   setNumRows(0);
00052 }
00053 
00054 int KWidgetListbox::insertItem(QWidget* item, int index)
00055 {
00056   int row;
00057 
00058   if(index == -1)
00059   {
00060     row = numRows();
00061     setNumRows(row + 1);
00062   }
00063   else
00064     return -1;
00065 
00066   setRowHeight(row, item->height());
00067   setCellWidget(row, 0, item);
00068   setItemColors(row, even(row));
00069   return row;
00070 }
00071 
00072 void KWidgetListbox::setSelected(QWidget* item)
00073 {
00074   setSelected(index(item));
00075 }
00076 
00077 void KWidgetListbox::selectionChanged(int row, int col)
00078 {
00079   ensureCellVisible(row, col);
00080   updateColors();
00081   emit selected(row);
00082 }
00083 
00084 void KWidgetListbox::removeItem(QWidget* item)
00085 {
00086   removeItem(index(item));
00087 }
00088 
00089 void KWidgetListbox::removeItem(int index)
00090 {
00091   removeRow(index);
00092   updateColors();
00093 }
00094 
00095 void KWidgetListbox::setSelected(int index)
00096 {
00097   setCurrentCell(index, 0);
00098 }
00099 
00100 int KWidgetListbox::selected() const
00101 {
00102   return currentRow();
00103 }
00104 
00105 QWidget* KWidgetListbox::selectedItem() const
00106 {
00107   return item(selected());
00108 }
00109 
00110 QWidget* KWidgetListbox::item(int index) const
00111 {
00112   return cellWidget(index, 0);
00113 }
00114 
00115 int KWidgetListbox::index(QWidget* itm) const
00116 {
00117   for(int i = 0; i < numRows(); ++i)
00118     if(item(i) == itm)
00119       return i;
00120   return -1;
00121 }
00122 
00123 bool KWidgetListbox::even(int index)
00124 {
00125   int v = 0;
00126   for(int i = 0; i < numRows(); ++i)
00127   {
00128     if(index == i)
00129       break;
00130     if(!isRowHidden(i))
00131       ++v;
00132   }
00133   return (v%2 == 0);
00134 }
00135 
00136 void KWidgetListbox::updateColors()
00137 {
00138   int v = 0;
00139   for(int i = 0; i < numRows(); ++i)
00140   {
00141     if(!isRowHidden(i))
00142     {
00143       setItemColors(i, (v%2 == 0));
00144       ++v;
00145     }
00146   }
00147 }
00148 
00149 void KWidgetListbox::setItemColors(int index, bool even)
00150 {
00151   QWidget* itm = item(index);
00152 
00153   if(index == selected())
00154   {
00155     itm->setPaletteBackgroundColor(KGlobalSettings::highlightColor());
00156     itm->setPaletteForegroundColor(KGlobalSettings::highlightedTextColor());
00157   }
00158   else if(even)
00159   {
00160     itm->setPaletteBackgroundColor(KGlobalSettings::baseColor());
00161     itm->setPaletteForegroundColor(KGlobalSettings::textColor());
00162   }
00163   else
00164   {
00165     itm->setPaletteBackgroundColor(
00166         KGlobalSettings::alternateBackgroundColor());
00167     itm->setPaletteForegroundColor(KGlobalSettings::textColor());
00168   }
00169 }
00170 
00171 void KWidgetListbox::showItems(show_callback func, void* data)
00172 {
00173   for(int i = 0; i < numRows(); ++i)
00174   {
00175     if(func == 0)
00176       showRow(i);
00177     else
00178     {
00179       if(func(i, item(i), data))
00180         showRow(i);
00181       else
00182         hideRow(i);
00183     }
00184   }
00185   updateColors();
00186 }
00187 
00188 void KWidgetListbox::showEvent(QShowEvent*)
00189 {
00190   //kdDebug() << k_funcinfo << endl;
00191   repaintContents(false);
00192 }
00193 
00194 void KWidgetListbox::paintCell(QPainter*, int, int, const QRect&,
00195                                bool, const QColorGroup&)
00196 {
00197   //kdDebug() << k_funcinfo << endl;
00198 }
00199 
00200 #include "kwidgetlistbox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys