navigationmodel.cc 3.56 KB
Newer Older
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
#include "radixwidgets/navigationmodel.hh"
#include "radixwidgets/navigationitem.hh"

#include "radixbug/bug.hh"

#include <QList>

namespace radix
{
class NavigationModel::PImpl
{
 public:
  NavigationItem* root = nullptr;

  ~PImpl();
};

NavigationModel::PImpl::~PImpl()
{
  if (root) delete root;
}
NavigationModel::NavigationModel(QObject* parent)
    : QAbstractItemModel(parent)
    , p(new PImpl(), [](PImpl* impl) { delete impl; })
{
26
  p->root = new NavigationItem();
27
28
29
30
31
32
33
34
}

QVariant NavigationModel::data(const QModelIndex& index, int role) const
{
  if (!index.isValid()) return QVariant();

  NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer());

35
36
37
38
  if (Qt::CheckStateRole == role && index.column() == 0)
    return static_cast<int>(item->isChecked() ? Qt::Checked : Qt::Unchecked);

  if (role != Qt::DisplayRole) return QVariant();
39
40
41
42
43
44
  return item->data(index.column());
}

bool NavigationModel::setData(const QModelIndex& index, const QVariant& value,
                              int role)
{
45
46
47
48
49
50
51
52
53
54
55
56
  if (Qt::CheckStateRole == role)
  {
    NavigationItem* item =
        static_cast<NavigationItem*>(index.internalPointer());
    item->setChecked(value.toBool());
    // signal selection/deselection
    if (item->isChecked())
      emit itemChecked(index);
    else
      emit itemUnChecked(index);
    return true;
  }
57
58
59
60
61
62
63
  return false;
}

Qt::ItemFlags NavigationModel::flags(const QModelIndex& index) const
{
  if (!index.isValid()) return QAbstractItemModel::flags(index);

64
65
66
67
  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  if (index.column() == 0) flags |= Qt::ItemIsUserCheckable;

  return flags;
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
}

QVariant NavigationModel::headerData(int section, Qt::Orientation orientation,
                                     int role) const
{
  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    return p->root->data(section);

  return QVariant();
}

QModelIndex NavigationModel::index(int row, int column,
                                   const QModelIndex& parent) const
{
  if (!hasIndex(row, column, parent)) return QModelIndex();

  NavigationItem* parentItem;

  if (!parent.isValid())
    parentItem = p->root;
  else
    parentItem = static_cast<NavigationItem*>(parent.internalPointer());

  NavigationItem* childItem = parentItem->child(row);
  if (childItem)
    return createIndex(row, column, childItem);
  else
    return QModelIndex();
}

QModelIndex NavigationModel::parent(const QModelIndex& index) const
{
  if (!index.isValid()) return QModelIndex();

  NavigationItem* childItem =
      static_cast<NavigationItem*>(index.internalPointer());
  NavigationItem* parentItem = childItem->parentItem();

  if (parentItem == p->root) return QModelIndex();

  return createIndex(parentItem->row(), 0, parentItem);
}

int NavigationModel::rowCount(const QModelIndex& parent) const
{
  NavigationItem* parentItem;
  if (parent.column() > 0) return 0;

  if (!parent.isValid())
    parentItem = p->root;
  else
    parentItem = static_cast<NavigationItem*>(parent.internalPointer());

  return parentItem->childCount();
}

int NavigationModel::columnCount(const QModelIndex& parent) const
{
  if (parent.isValid())
    return static_cast<NavigationItem*>(parent.internalPointer())
        ->columnCount();
  else
    return p->root->columnCount();
}

133
134
135
136
137
138
139
140
141
142
143
NavigationItem* NavigationModel::rootItem() { return p->root; }

NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent)
{
  beginResetModel();
  NavigationItem* newItem = new NavigationItem(value, parent);
  parent->addChild(newItem);
  endResetModel();
  return newItem;
}

144
}  // namespace radix