superkaramba
kwidgetlistbox.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kwidgetlistbox.h"
00021 #include <kdebug.h>
00022 #include <kglobalsettings.h>
00023
00024 KWidgetListbox::KWidgetListbox(TQWidget *parent, const char *name)
00025 : TQTable(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(TQTable::NoSelection);
00035 setFocusStyle(TQTable::FollowStyle);
00036 connect(this, TQT_SIGNAL(currentChanged(int, int)),
00037 this, TQT_SLOT(selectionChanged(int, int)));
00038 setHScrollBarMode(TQScrollView::AlwaysOff);
00039 setVScrollBarMode(TQScrollView::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(TQWidget* 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(TQWidget* 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(TQWidget* 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 TQWidget* KWidgetListbox::selectedItem() const
00106 {
00107 return item(selected());
00108 }
00109
00110 TQWidget* KWidgetListbox::item(int index) const
00111 {
00112 return cellWidget(index, 0);
00113 }
00114
00115 int KWidgetListbox::index(TQWidget* 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 TQWidget* 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(TQShowEvent*)
00189 {
00190
00191 repaintContents(false);
00192 }
00193
00194 void KWidgetListbox::paintCell(TQPainter*, int, int, const TQRect&,
00195 bool, const TQColorGroup&)
00196 {
00197
00198 }
00199
00200 #include "kwidgetlistbox.moc"