navigationitem.cc 2.69 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;
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
14
  QList<QString> tooltips;
15
  bool checked = false;
16
  int type     = 0;
17
  ~PImpl();
18
19
};

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

22
NavigationItem::NavigationItem(QVariant data, NavigationItem *parentItem)
23
    : p(new PImpl())
24
25
26
27
28
29
{
  p->parent = parentItem;
  p->data.append(data);
}

NavigationItem::NavigationItem(QList<QVariant> data, NavigationItem *parentItem)
30
    : p(new PImpl())
31
32
33
34
35
36
{
  p->parent = parentItem;
  p->data   = data;
}

NavigationItem::NavigationItem(NavigationItem *parentItem)
37
    : p(new PImpl())
38
39
40
41
{
  p->parent = parentItem;
}

42
NavigationItem::~NavigationItem() { delete p; }
Huff, Israel's avatar
Huff, Israel committed
43

44
45
46
47
int NavigationItem::type() const { return p->type; }

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

48
49
50
51
52
53
54
void NavigationItem::addChild(NavigationItem *child)
{
  p->children.append(child);
}

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

55
56
57
58
59
60
61
62
63
64
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;
}
65

Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
66
QVariant NavigationItem::data(int column, int role) const
67
{
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  if (Qt::DisplayRole == role)
  {
    if (column >= p->data.size()) return QVariant();
    return p->data.value(column);
  }
  else if (Qt::ToolTipRole == role)
  {
    if (column < p->tooltips.size()) return p->tooltips.value(column);
  }
  else if (Qt::CheckStateRole == role)
  {
    return static_cast<int>(isChecked() ? Qt::Checked : Qt::Unchecked);
  }
  return QVariant();
82
83
}

Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
84
void NavigationItem::setData(int column, QVariant value, int role)
85
{
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
86
  if (Qt::DisplayRole == role)
87
  {
Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
88
89
90
91
92
93
94
95
96
97
    int size = p->data.size();
    for (int i = size; i <= column; ++i)
    {
      p->data.append(QVariant());
    }
    p->data[column] = value;
  }
  else if (Qt::ToolTipRole == role)
  {
    p->tooltips.insert(column, value.toString());
98
99
100
101
102
103
104
105
106
  }
}

int NavigationItem::row() const
{
  if (p->parent)
  {
    return p->parent->p->children.indexOf(const_cast<NavigationItem *>(this));
  }
107
  return 0;
108
109
110
111
}

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

112
113
114
115
bool NavigationItem::isChecked() const { return p->checked; }

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

116
117
118
119
NavigationItem *NavigationItem::child(int row)
{
  return p->children.value(row);
}
120

Lefebvre, Jordan's avatar
Lefebvre, Jordan committed
121
122
123
124
125
bool NavigationItem::disown(NavigationItem *child)
{
  return p->children.removeOne(child);
}

126
}  // namespace radix