diff options
author | Slávek Banko <[email protected]> | 2013-07-27 16:34:45 +0200 |
---|---|---|
committer | Slávek Banko <[email protected]> | 2013-07-27 16:34:45 +0200 |
commit | d76ff81b7c1beffef0b84e570914c8f2d47834e6 (patch) | |
tree | 284b80ce7c5456fbb041f7979ac2c0baeead8902 /src/kwidgetlistbox.cpp | |
download | tork-d76ff81b7c1beffef0b84e570914c8f2d47834e6.tar.gz tork-d76ff81b7c1beffef0b84e570914c8f2d47834e6.zip |
Initial import of tork 0.33
Diffstat (limited to 'src/kwidgetlistbox.cpp')
-rw-r--r-- | src/kwidgetlistbox.cpp | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/src/kwidgetlistbox.cpp b/src/kwidgetlistbox.cpp new file mode 100644 index 0000000..1c32868 --- /dev/null +++ b/src/kwidgetlistbox.cpp @@ -0,0 +1,223 @@ +/*************************************************************************** + * Copyright (C) 2005 Petri Damst� <[email protected]> * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * + ***************************************************************************/ + +#include "kwidgetlistbox.h" +#include <kdebug.h> +#include <kglobalsettings.h> +#include <hitwidget.h> + +KWidgetListbox::KWidgetListbox(QWidget *parent, const char *name) + : QTable(parent, name) +{ + setNumRows(0); + setNumCols(1); + setColumnStretchable(0, true); + setLeftMargin(0); + setTopMargin(0); + horizontalHeader()->hide(); + verticalHeader()->hide(); + setSelectionMode(QTable::NoSelection); + setFocusStyle(QTable::FollowStyle); + connect(this, SIGNAL(currentChanged(int, int)), + this, SLOT(selectionChanged(int, int))); + setHScrollBarMode(QScrollView::AlwaysOff); + setVScrollBarMode(QScrollView::Auto); +} + +KWidgetListbox::~KWidgetListbox() +{ + clear(); +} + +void KWidgetListbox::clear() +{ + for(int i = 0; i < numRows(); ++i) + clearCellWidget(i, 0); + setNumRows(0); +} + +int KWidgetListbox::insertItem(QWidget* item, int index) +{ + int row; + + if(index == -1 || numRows()==0) + { + row = numRows(); + setNumRows(row + 1); + } + else { + row = index; + insertRows(row); + } + + item->setMinimumWidth( width() ); + item->adjustSize(); + + HitWidget* hit = dynamic_cast<HitWidget*>(item); + if (hit && hit->isCollapsed()) +//#warning fixme + setRowHeight(row, 30); + else + setRowHeight(row, item->sizeHint().height()); + setCellWidget(row, 0, item); + setItemColors(row, even(row)); + return row; +} + +void KWidgetListbox::adjustSize(QWidget* item) +{ + item->setMinimumWidth( columnWidth(0) ); + item->adjustSize(); + HitWidget* hit = (HitWidget*)item; + if (hit->isCollapsed()) + setRowHeight(index(item), 28); + else + setRowHeight(index(item), item->height()); +} + +void KWidgetListbox::setSelected(QWidget* item) +{ + setSelected(index(item)); +} + +void KWidgetListbox::selectionChanged(int row, int col) +{ + ensureCellVisible(row, col); + updateColors(); + emit selected(row); +} + +void KWidgetListbox::removeItem(QWidget* item) +{ + removeItem(index(item)); +} + +void KWidgetListbox::removeItem(int index) +{ + removeRow(index); + updateColors(); +} + +void KWidgetListbox::setSelected(int index) +{ + setCurrentCell(index, 0); +} + +int KWidgetListbox::selected() const +{ + return currentRow(); +} + +QWidget* KWidgetListbox::selectedItem() const +{ + return item(selected()); +} + +QWidget* KWidgetListbox::item(int index) const +{ + return cellWidget(index, 0); +} + +int KWidgetListbox::index(QWidget* itm) const +{ + for(int i = 0; i < numRows(); ++i) + if(item(i) == itm) + return i; + return -1; +} + +bool KWidgetListbox::even(int index) +{ + int v = 0; + for(int i = 0; i < numRows(); ++i) + { + if(index == i) + break; + if(!isRowHidden(i)) + ++v; + } + return (v%2 == 0); +} + +void KWidgetListbox::updateColors() +{ + int v = 0; + for(int i = 0; i < numRows(); ++i) + { + if(!isRowHidden(i)) + { + setItemColors(i, (v%2 == 0)); + ++v; + } + } +} + +void KWidgetListbox::setItemColors(int index, bool even) +{ + QWidget* itm = item(index); + + if (!itm) + return; + + if(index == selected()) + { + itm->setPaletteBackgroundColor(KGlobalSettings::highlightColor()); + itm->setPaletteForegroundColor(KGlobalSettings::highlightedTextColor()); + } + else if(even) + { + itm->setPaletteBackgroundColor(KGlobalSettings::baseColor()); + itm->setPaletteForegroundColor(KGlobalSettings::textColor()); + } + else + { + itm->setPaletteBackgroundColor( + KGlobalSettings::alternateBackgroundColor()); + itm->setPaletteForegroundColor(KGlobalSettings::textColor()); + } +} + +void KWidgetListbox::showItems(show_callback func, void* data) +{ + for(int i = 0; i < numRows(); ++i) + { + if(func == 0) + showRow(i); + else + { + if(func(i, item(i), data)) + showRow(i); + else + hideRow(i); + } + } + updateColors(); +} + +void KWidgetListbox::showEvent(QShowEvent*) +{ + repaintContents(false); +} + +void KWidgetListbox::paintCell(QPainter*, int, int, const QRect&, + bool, const QColorGroup&) +{ +} + +#include "kwidgetlistbox.moc" |