summaryrefslogtreecommitdiffstats
path: root/src/kwidgetlistbox.cpp
blob: 9249866dee10f0adec9c5fa85cbd3c2b8ce31353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/***************************************************************************
 *   Copyright (C) 2005 Petri Damst� <petri.damsten@iki.fi>                *
 *                                                                         *
 *   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 <kdebug.h>
#include <tdeglobalsettings.h>
#include <hitwidget.h>

#include "kwidgetlistbox.h"


KWidgetListbox::KWidgetListbox(TQWidget *parent, const char *name)
 : TQTable(parent, name)
{
  setNumRows(0);
  setNumCols(1);
  setColumnStretchable(0, true);
  setLeftMargin(0);
  setTopMargin(0);
  horizontalHeader()->hide();
  verticalHeader()->hide();
  setSelectionMode(TQTable::NoSelection);
  setFocusStyle(TQTable::FollowStyle);
  connect(this, TQ_SIGNAL(currentChanged(int, int)),
          this, TQ_SLOT(selectionChanged(int, int)));
  setHScrollBarMode(TQScrollView::AlwaysOff);
  setVScrollBarMode(TQScrollView::Auto);
}

KWidgetListbox::~KWidgetListbox()
{
  clear();
}

void KWidgetListbox::clear()
{
  for(int i = 0; i < numRows(); ++i)
    clearCellWidget(i, 0);
  setNumRows(0);
}

int KWidgetListbox::insertItem(TQWidget* 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(TQWidget* 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(TQWidget* item)
{
  setSelected(index(item));
}

void KWidgetListbox::selectionChanged(int row, int col)
{
  ensureCellVisible(row, col);
  updateColors();
  emit selected(row);
}

void KWidgetListbox::removeItem(TQWidget* 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();
}

TQWidget* KWidgetListbox::selectedItem() const
{
  return item(selected());
}

TQWidget* KWidgetListbox::item(int index) const
{
  return cellWidget(index, 0);
}

int KWidgetListbox::index(TQWidget* 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)
{
  TQWidget* itm = item(index);

  if (!itm)
    return;

  if(index == selected())
  {
    itm->setPaletteBackgroundColor(TDEGlobalSettings::highlightColor());
    itm->setPaletteForegroundColor(TDEGlobalSettings::highlightedTextColor());
  }
  else if(even)
  {
    itm->setPaletteBackgroundColor(TDEGlobalSettings::baseColor());
    itm->setPaletteForegroundColor(TDEGlobalSettings::textColor());
  }
  else
  {
    itm->setPaletteBackgroundColor(
        TDEGlobalSettings::alternateBackgroundColor());
    itm->setPaletteForegroundColor(TDEGlobalSettings::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(TQShowEvent*)
{
  repaintContents(false);
}

void KWidgetListbox::paintCell(TQPainter*, int, int, const TQRect&,
                               bool, const TQColorGroup&)
{
}

#include "kwidgetlistbox.moc"