blob: ec4fc02833fd7ba7d88625d4c1bcbe79299f89c2 [file] [log] [blame]
Yingdi Yu1f824642016-03-20 17:07:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California,
4 *
5 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "key-tree-model.hpp"
21#include "key-tree-item.hpp"
22
23#include <QStringList>
24
25
26#ifdef WAF
27#include "key-tree-model.moc"
28#endif
29
30namespace ndn {
31namespace ncc {
32
33KeyTreeModel::KeyTreeModel(QObject *parent)
34 : QAbstractItemModel(parent)
35 , m_rootItem(new KeyTreeItem(QVariant("")))
36{
37}
38
39KeyTreeModel::~KeyTreeModel()
40{
41 delete m_rootItem;
42}
43
44void
45KeyTreeModel::clear()
46{
47 delete m_rootItem;
48 m_rootItem = new KeyTreeItem(QVariant(""));
49}
50
51void
52KeyTreeModel::addChild(KeyTreeItem* child)
53{
54 child->setParent(m_rootItem);
55 m_rootItem->appendChild(child);
56}
57
58int
59KeyTreeModel::columnCount(const QModelIndex& parent) const
60{
61 if (parent.isValid())
62 return static_cast<KeyTreeItem*>(parent.internalPointer())->columnCount();
63 else
64 return m_rootItem->columnCount();
65}
66
67QVariant
68KeyTreeModel::name(const QModelIndex& index, int role) const
69{
70 if (!index.isValid())
71 return QVariant();
72
73 if (role != Qt::DisplayRole)
74 return QVariant();
75
76 KeyTreeItem* item = static_cast<KeyTreeItem*>(index.internalPointer());
77
78 return item->name();
79}
80
81QVariant
82KeyTreeModel::data(const QModelIndex& index, int role) const
83{
84 if (!index.isValid())
85 return QVariant();
86
87 if (role != Qt::DisplayRole)
88 return QVariant();
89
90 KeyTreeItem* item = static_cast<KeyTreeItem*>(index.internalPointer());
91
92 return item->data();
93}
94
95Qt::ItemFlags
96KeyTreeModel::flags(const QModelIndex& index) const
97{
98 if (!index.isValid())
99 return 0;
100
101 return QAbstractItemModel::flags(index);
102}
103
104QVariant
105KeyTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
106{
107 // if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
108 // return m_rootItem->data();
109
110 return QVariant();
111}
112
113QModelIndex
114KeyTreeModel::index(int row, int column, const QModelIndex &parent) const
115{
116 if (!hasIndex(row, column, parent))
117 return QModelIndex();
118
119 KeyTreeItem *parentItem;
120
121 if (!parent.isValid())
122 parentItem = m_rootItem;
123 else
124 parentItem = static_cast<KeyTreeItem*>(parent.internalPointer());
125
126 KeyTreeItem *childItem = parentItem->child(row);
127 if (childItem)
128 return createIndex(row, column, childItem);
129 else
130 return QModelIndex();
131}
132
133QModelIndex
134KeyTreeModel::parent(const QModelIndex &index) const
135{
136 if (!index.isValid())
137 return QModelIndex();
138
139 KeyTreeItem *childItem = static_cast<KeyTreeItem*>(index.internalPointer());
140 KeyTreeItem *parentItem = childItem->parentItem();
141
142 if (parentItem == m_rootItem)
143 return QModelIndex();
144
145 return createIndex(parentItem->row(), 0, parentItem);
146}
147
148int
149KeyTreeModel::rowCount(const QModelIndex &parent) const
150{
151 KeyTreeItem *parentItem;
152 if (parent.column() > 0)
153 return 0;
154
155 if (!parent.isValid())
156 parentItem = m_rootItem;
157 else
158 parentItem = static_cast<KeyTreeItem*>(parent.internalPointer());
159
160 return parentItem->childCount();
161}
162
163// void
164// KeyTreeModel::setupModelData(const QStringList &lines, KeyTreeItem *parent)
165// {
166 // QList<KeyTreeItem*> parents;
167 // QList<int> indentations;
168 // parents << parent;
169 // indentations << 0;
170
171 // int number = 0;
172
173 // while (number < lines.count()) {
174 // int position = 0;
175 // while (position < lines[number].length()) {
176 // if (lines[number].at(position) != ' ')
177 // break;
178 // position++;
179 // }
180
181 // QString lineData = lines[number].mid(position).trimmed();
182
183 // if (!lineData.isEmpty()) {
184 // // Read the column data from the rest of the line.
185 // QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
186 // QList<QVariant> columnData;
187 // for (int column = 0; column < columnStrings.count(); ++column)
188 // columnData << columnStrings[column];
189
190 // if (position > indentations.last()) {
191 // // The last child of the current parent is now the new parent
192 // // unless the current parent has no children.
193
194 // if (parents.last()->childCount() > 0) {
195 // parents << parents.last()->child(parents.last()->childCount()-1);
196 // indentations << position;
197 // }
198 // } else {
199 // while (position < indentations.last() && parents.count() > 0) {
200 // parents.pop_back();
201 // indentations.pop_back();
202 // }
203 // }
204
205 // // Append a new item to the current parent's list of children.
206 // parents.last()->appendChild(new TreeItem(columnData, parents.last()));
207 // }
208
209 // ++number;
210 // }
211// }
212
213} // namespace ndn
214} // namespace ncc