navigationmodel.cc 3.47 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
26
#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; })
{
  radix_tagged_line("Navigation Model");
27
  p->root = new NavigationItem({"Header 1", "Header 2"});
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
  p->root->addChild(new NavigationItem("Hello", p->root));
  NavigationItem* world = new NavigationItem("World", p->root);
  p->root->addChild(world);
  NavigationItem* child = new NavigationItem("My name is edward", world);
  world->addChild(child);
}

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

  if (role != Qt::DisplayRole) return QVariant();

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

  return item->data(index.column());
}

bool NavigationModel::setData(const QModelIndex& index, const QVariant& value,
                              int role)
{
  return false;
}

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

  return QAbstractItemModel::flags(index);
}

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
{
  radix_tagged_line("index(" << row << "," << column << ")");
  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
{
  radix_tagged_line("parent(" << index.row() << "," << index.column() << ")");
  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
{
  radix_tagged_line("rowCount(" << parent.row() << "," << parent.column()
                                << ")");
  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
{
  radix_tagged_line("columnCount(" << parent.row() << "," << parent.column()
                                   << ")");
  if (parent.isValid())
    return static_cast<NavigationItem*>(parent.internalPointer())
        ->columnCount();
  else
    return p->root->columnCount();
}

}  // namespace radix