navigationitem.cc 2.16 KB
Newer Older
1
2
3
#include "radixwidgets/navigationitem.hh"
#include "radixbug/bug.hh"

4
#include <QList>
5
6
7

namespace radix
{
8
class NavigationItem::PImpl
9
{
10
11
12
13
 public:
  NavigationItem *parent = nullptr;
  QList<NavigationItem *> children;
  QList<QVariant> data;
14
  bool checked = false;
15
  int type     = 0;
16
  ~PImpl();
17
18
};

19
NavigationItem::PImpl::~PImpl() { qDeleteAll(children); }
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
NavigationItem::NavigationItem(QVariant data, NavigationItem *parentItem)
    : p(new PImpl(), [](PImpl *impl) { delete impl; })
{
  p->parent = parentItem;
  p->data.append(data);
}

NavigationItem::NavigationItem(QList<QVariant> data, NavigationItem *parentItem)
    : p(new PImpl(), [](PImpl *impl) { delete impl; })
{
  p->parent = parentItem;
  p->data   = data;
}

NavigationItem::NavigationItem(NavigationItem *parentItem)
    : p(new PImpl(), [](PImpl *impl) { delete impl; })
{
  p->parent = parentItem;
}

41
42
43
44
int NavigationItem::type() const { return p->type; }

void NavigationItem::setType(int type) { p->type = type; }

45
46
47
48
49
50
51
void NavigationItem::addChild(NavigationItem *child)
{
  p->children.append(child);
}

int NavigationItem::childCount() const { return p->children.size(); }

52
53
54
55
56
57
58
59
60
61
int NavigationItem::columnCount() const
{
  // recursively find the largest number of columns from children
  int count = p->data.size();
  for (int ci = 0; ci < p->children.size(); ++ci)
  {
    count = std::max(count, p->children[ci]->columnCount());
  }
  return count;
}
62
63
64
65

QVariant NavigationItem::data(int column) const
{
  if (column >= p->data.size()) return QVariant();
66
  return p->data.value(column);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}

void NavigationItem::setData(int column, QVariant value)
{
  int size = p->data.size();
  for (int i = size; i <= column; ++i)
  {
    p->data.append(QVariant());
  }
  p->data[column] = value;
}

int NavigationItem::row() const
{
  if (p->parent)
  {
    return p->parent->p->children.indexOf(const_cast<NavigationItem *>(this));
  }
}

NavigationItem *NavigationItem::parentItem() { return p->parent; }

89
90
91
92
bool NavigationItem::isChecked() const { return p->checked; }

void NavigationItem::setChecked(bool checked) { p->checked = checked; }

93
94
95
96
NavigationItem *NavigationItem::child(int row)
{
  return p->children.value(row);
}
97
98

}  // namespace radix