summaryrefslogtreecommitdiffstats
path: root/src/common/gui/list_container.cpp
blob: 01031757ef36063f8e1038f27ee7c6d91e53ebf2 (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
/***************************************************************************
 *   Copyright (C) 2007 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
#include "list_container.h"

//----------------------------------------------------------------------------
PopupContainer::PopupContainer(const QString &title, QWidget *parent, const char *name)
  : KPopupMenu(parent, name)
{
  if ( !title.isEmpty() ) insertTitle(title);
}

ListContainer *PopupContainer::appendBranch(const QString &title)
{
  PopupContainer *branch = new PopupContainer(title, this);
  insertItem(title, branch);
  return branch;
}

ListContainer *PopupContainer::appendBranch(const QPixmap &pixmap, const QString &title)
{
  PopupContainer *branch = new PopupContainer(title, this);
  insertItem(pixmap, title, branch);
  return branch;
}

void PopupContainer::appendItem(const QPixmap &icon, const QString &label, uint id, ItemState state)
{
  insertItem(icon, label, id);
  switch (state) {
    case Normal:    break;
    case Checked:   setItemChecked(id, true); break;
    case UnChecked: setItemChecked(id, false); break;
    case Disabled:  setItemEnabled(id, false); break;
  }
}

//----------------------------------------------------------------------------
ListViewItemContainer::ListViewItemContainer(const QString &title, KListView *parent)
  : KListViewItem(parent, title), _parent(0), _column(0)
{
  _ids = new QMap<const QListViewItem *, uint>;
}

ListViewItemContainer::ListViewItemContainer(const QString &title, ListViewItemContainer *parent)
  : KListViewItem(parent, title), _parent(parent), _column(0)
{
  _ids = parent->_ids;
}

ListViewItemContainer::~ListViewItemContainer()
{
  if ( _parent==0 ) delete _ids;
}

ListContainer *ListViewItemContainer::appendBranch(const QString &title)
{
  ListViewItemContainer *branch = new ListViewItemContainer(title, this);
  branch->setColumn(_column);
  branch->setSelectable(false);
  // append instead of prepend
  QListViewItem *litem=firstChild();
  while ( litem && litem->nextSibling() ) litem = litem->nextSibling();
  if (litem) branch->moveItem(litem);
  return branch;
}

void ListViewItemContainer::appendItem(const QPixmap &icon, const QString &title, uint id, ItemState state)
{
  QListViewItem *item = 0;
  if ( state==Normal || state==Disabled ) {
    item = new KListViewItem(this);
    item->setText(_column, title);
  } else {
    item = new QCheckListItem(this, title, QCheckListItem::CheckBox);
    static_cast<QCheckListItem *>(item)->setState(state==Checked ? QCheckListItem::On : QCheckListItem::Off);
  }
  item->setPixmap(_column, icon);
  item->setSelectable(state==Normal);
  // append instead of prepend
  QListViewItem *litem=firstChild();
  while ( litem && litem->nextSibling() ) litem = litem->nextSibling();
  if (litem) item->moveItem(litem);
  (*_ids)[item] = id;
}

int ListViewItemContainer::id(const QListViewItem *item) const
{
  return (_ids->contains(item) ? int((*_ids)[item]) : -1);
}