summaryrefslogtreecommitdiffstats
path: root/src/gui/listview.cpp
blob: 37673e0c3f2cc5e1393b2a22b9ec2a4cc70da32e (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/***************************************************************************
    copyright            : (C) 2001-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "listview.h"
#include "../controller.h"
#include "../tellico_utils.h"
#include "../tellico_debug.h"

#include <tdeapplication.h>

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqheader.h>

using Tellico::GUI::ListView;
using Tellico::GUI::ListViewItem;

ListView::ListView(TQWidget* parent_, const char* name_) : TDEListView(parent_, name_/*=0*/),
    m_sortStyle(SortByText), m_isClear(true) {
  setSelectionMode(TQListView::Extended);
  connect(this, TQT_SIGNAL(selectionChanged()),
          TQT_SLOT(slotSelectionChanged()));
  connect(this, TQT_SIGNAL(doubleClicked(TQListViewItem*)),
          TQT_SLOT(slotDoubleClicked(TQListViewItem*)));
#if !KDE_IS_VERSION(3,3,90)
  m_shadeSortColumn = false;
  // call it once to initialize
  slotUpdateColors();
#endif
  connect(kapp, TQT_SIGNAL(tdedisplayPaletteChanged()), TQT_SLOT(slotUpdateColors()));
  m_comparisons.setAutoDelete(true);
}

ListView::~ListView() {
}

void ListView::clearSelection() {
  if(m_selectedItems.isEmpty()) {
    // nothing to do;
    return;
  }
  bool b = signalsBlocked();
  blockSignals(true);
  selectAll(false);
  blockSignals(b);
  emit selectionChanged();
}

void ListView::updateSelected(ListViewItem* item_, bool selected_) {
  if(selected_) {
    m_selectedItems.append(item_);
  } else {
    m_selectedItems.removeRef(item_);
  }
}

bool ListView::isSelectable(ListViewItem* item_) const {
  // don't allow hidden items to be selected
  if(!item_->isVisible()) {
    return false;
  }

  // selecting multiple items is ok
  // only when parent is open. Be careful to check for existence of parent
  if(item_->parent() && !item_->parent()->isOpen()) {
    return false;
  }

  // just selecting a single item is always ok
  if(m_selectedItems.isEmpty()) {
    return true;
  }

  // not allowed is something other than an entry is selected and current is entry
  if(m_selectedItems.getFirst()->isEntryItem() != item_->isEntryItem()) {
    return false;
  }

  return true;
}

int ListView::firstVisibleColumn() const {
  int col = 0;
  while(col < columns() && columnWidth(header()->mapToSection(col)) == 0) {
    ++col;
  }
  if(col == columns()) {
    return -1;
  }
  return header()->mapToSection(col);
}

int ListView::lastVisibleColumn() const {
  int col = columns()-1;
  while(col < columns() && columnWidth(header()->mapToSection(col)) == 0) {
    --col;
  }
  if(col == columns()) {
    return -1;
  }
  return header()->mapToSection(col);
}

void ListView::setColumnText(int column, const TQString& label) {
  ListViewComparison* comp = m_comparisons.take(columnText(column));
  TDEListView::setColumnText(column, label);
  if(comp) {
    m_comparisons.insert(columnText(column), comp);
  }
}

void ListView::setComparison(int column, ListViewComparison* comp) {
  if(comp) {
    m_comparisons.replace(columnText(column), comp);
  }
}

void ListView::removeComparison(int column) {
  m_comparisons.remove(columnText(column));
}

void ListView::clearComparisons() {
  m_comparisons.clear();
}

int ListView::compare(int col, const GUI::ListViewItem* item1, GUI::ListViewItem* item2, bool asc) {
  if(col >= 0 && col < static_cast<int>(m_comparisons.count())) {
    ListViewComparison* com = m_comparisons.find(columnText(col));
    if(com) {
      return com->compare(col, item1, item2, asc);
    }
  }
  return 0;
}

#if !KDE_IS_VERSION(3,3,90)
void ListView::setShadeSortColumn(bool shade_) {
  if(m_shadeSortColumn != shade_) {
    m_shadeSortColumn = shade_;
    repaint();
  }
}
#endif

void ListView::slotUpdateColors() {
#if !KDE_IS_VERSION(3,3,90)
  m_backColor2 = viewport()->colorGroup().base();
  if(m_backColor2 == TQt::black) {
    m_backColor2 = TQColor(50, 50, 50);  // dark gray
  } else {
    int h,s,v;
    m_backColor2.hsv(&h, &s, &v);
    if(v > 175) {
      m_backColor2 = m_backColor2.dark(105);
    } else {
      m_backColor2 = m_backColor2.light(120);
    }
  }

  m_altColor2 = alternateBackground();
  if(m_altColor2 == TQt::black) {
    m_altColor2 = TQColor(50, 50, 50);  // dark gray
  } else {
    int h,s,v;
    m_altColor2.hsv(&h, &s, &v);
    if(v > 175) {
      m_altColor2 = m_altColor2.dark(105);
    } else {
      m_altColor2 = m_altColor2.light(120);
    }
  }
#endif
  Tellico::updateContrastColor(viewport()->colorGroup());
  repaint();
}

void ListView::slotSelectionChanged() {
  if(m_selectedItems.isEmpty()) {
    if(m_isClear) {
      return; // nothing to do
    }
    m_isClear = true;
    Controller::self()->slotClearSelection();
    return;
  }
  m_isClear = false;

  Data::EntryVec entries;
  // now just find all the children or grandchildren that are entry items
  for(GUI::ListViewItemListIt it(m_selectedItems); it.current(); ++it) {
    Data::EntryVec more = it.current()->entries();
    for(Data::EntryVecIt entry = more.begin(); entry != more.end(); ++entry) {
      if(!entries.contains(entry)) {
        entries.append(entry);
      }
    }
  }
//  Controller::self()->slotUpdateCurrent(entries); // just update current, don't change selection
  Controller::self()->slotUpdateSelection(this, entries);
}

void ListView::slotDoubleClicked(TQListViewItem* item_) {
  if(!item_) {
    return;
  }

  // if it has children, just open it
  // but some items delay children creation
  if(static_cast<ListViewItem*>(item_)->realChildCount() > 0) {
    item_->setOpen(!item_->isOpen());
  }

  GUI::ListViewItem* item = static_cast<GUI::ListViewItem*>(item_);
  item->doubleClicked();
}

void ListView::drawContentsOffset(TQPainter* p, int ox, int oy, int cx, int cy, int cw, int ch) {
  bool oldUpdatesEnabled = isUpdatesEnabled();
  setUpdatesEnabled(false);
  TDEListView::drawContentsOffset(p, ox, oy, cx, cy, cw, ch);
  setUpdatesEnabled(oldUpdatesEnabled);
}

/* ****************** ListViewItem ********************* */

ListViewItem::~ListViewItem() {
  // I think there's a bug in qt where the children of this item are deleted after the item itself
  // as a result, there is no listView() pointer for the children, that obvious causes
  // a problem with updating the selection. So we MUST call clear() here ourselves!
  clear();
  // be sure to remove from selected list when it's deleted
  ListView* lv = listView();
  if(lv) {
    lv->updateSelected(this, false);
  }
}

void ListViewItem::clear() {
  TQListViewItem* item = firstChild();
  while(item) {
    delete item;
    item = firstChild();
  }
}

int ListViewItem::compare(TQListViewItem* item_, int col_, bool asc_) const {
  int res = compareWeight(item_, col_, asc_);
  if(res != 0) {
    return res;
  }
  res = listView()->compare(col_, this, static_cast<GUI::ListViewItem*>(item_), asc_);
  return res == 0 ? TDEListViewItem::compare(item_, col_, asc_) : res;
}

int ListViewItem::compareWeight(TQListViewItem* item_, int col_, bool asc_) const {
  Q_UNUSED(col_);
  // I want the sorting to be independent of sort order
  GUI::ListViewItem* i = static_cast<GUI::ListViewItem*>(item_);
  int res = 0;
  if(m_sortWeight < i->sortWeight()) {
    res = -1;
  } else if(m_sortWeight > i->sortWeight()) {
    res = 1;
  }
  if(asc_) {
    res *= -1; // reverse, heavier weights will come first always
  }
  return res;
}

void ListViewItem::setSelected(bool s_) {
  ListView* lv = listView();
  if(!lv) {
    return;
  }
  if(s_ && !lv->isSelectable(this)) {
    return;
  }
  if(s_ != isSelected()) {
    lv->updateSelected(this, s_);
    TDEListViewItem::setSelected(s_);
  }
}

TQColor ListViewItem::backgroundColor(int column_) {
#if KDE_IS_VERSION(3,3,90)
  return TDEListViewItem::backgroundColor(column_);
#else
  ListView* view = listView();
  if(view->columns() > 1 && view->shadeSortColumn() && column_ == view->sortColumn()) {
    return isAlternate() ? view->alternateBackground2() : view->background2();
  }
  return isAlternate() ? view->alternateBackground() : view->viewport()->colorGroup().base();
#endif
}

void ListViewItem::paintCell(TQPainter* p_, const TQColorGroup& cg_,
                             int column_, int width_, int align_) {
  // taken from tdelistview.cpp
  // I can't call TDEListViewItem::paintCell since TDEListViewItem::backgroundCOlor(int) is
  // not virtual. I need to be sure to call ListViewItem::backgroundColor(int);
  TQColorGroup cg = cg_;
  const TQPixmap* pm = listView()->viewport()->backgroundPixmap();
  if(pm && !pm->isNull()) {
    cg.setBrush(TQColorGroup::Base, TQBrush(backgroundColor(column_), *pm));
    TQPoint o = p_->brushOrigin();
    p_->setBrushOrigin(o.x()-listView()->contentsX(), o.y()-listView()->contentsY());
  } else {
    cg.setColor(listView()->viewport()->backgroundMode() == TQt::FixedColor ?
                TQColorGroup::Background : TQColorGroup::Base,
                backgroundColor(column_));
  }

  // don't call TDEListViewItem::paintCell() since that also does alternate painting, etc...
  TQListViewItem::paintCell(p_, cg, column_, width_, align_);

  // borrowed from amarok, draw line to left of cell
  if(!isSelected()) {
    p_->setPen(TQPen(listView()->alternateBackground(), 0, TQt::SolidLine));
    p_->drawLine(width_-1, 0, width_-1, height()-1);
  }
}

Tellico::Data::EntryVec ListViewItem::entries() const {
  Data::EntryVec entries;
  for(TQListViewItem* child = firstChild(); child; child = child->nextSibling()) {
    Data::EntryVec more = static_cast<GUI::ListViewItem*>(child)->entries();
    for(Data::EntryVecIt entry = more.begin(); entry != more.end(); ++entry) {
      if(!entries.contains(entry)) {
        entries.append(entry);
      }
    }
  }
  return entries;
}

#include "listview.moc"