blob: 56c4ec1fb875515f7c5f56b4afa700b39d86c09b [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>
Yingdi Yu95dbc712016-03-21 09:56:57 -070024#include <QFont>
25#include <QBrush>
26#include <iostream>
Yingdi Yu1f824642016-03-20 17:07:22 -070027
28
29#ifdef WAF
30#include "key-tree-model.moc"
31#endif
32
33namespace ndn {
34namespace ncc {
35
36KeyTreeModel::KeyTreeModel(QObject *parent)
37 : QAbstractItemModel(parent)
38 , m_rootItem(new KeyTreeItem(QVariant("")))
39{
40}
41
42KeyTreeModel::~KeyTreeModel()
43{
44 delete m_rootItem;
45}
46
47void
48KeyTreeModel::clear()
49{
50 delete m_rootItem;
51 m_rootItem = new KeyTreeItem(QVariant(""));
52}
53
54void
55KeyTreeModel::addChild(KeyTreeItem* child)
56{
57 child->setParent(m_rootItem);
58 m_rootItem->appendChild(child);
59}
60
61int
62KeyTreeModel::columnCount(const QModelIndex& parent) const
63{
64 if (parent.isValid())
65 return static_cast<KeyTreeItem*>(parent.internalPointer())->columnCount();
66 else
67 return m_rootItem->columnCount();
68}
69
70QVariant
71KeyTreeModel::name(const QModelIndex& index, int role) const
72{
73 if (!index.isValid())
74 return QVariant();
75
76 if (role != Qt::DisplayRole)
77 return QVariant();
78
79 KeyTreeItem* item = static_cast<KeyTreeItem*>(index.internalPointer());
80
81 return item->name();
82}
83
84QVariant
85KeyTreeModel::data(const QModelIndex& index, int role) const
86{
87 if (!index.isValid())
88 return QVariant();
89
Yingdi Yu1f824642016-03-20 17:07:22 -070090
91 KeyTreeItem* item = static_cast<KeyTreeItem*>(index.internalPointer());
Yingdi Yu95dbc712016-03-21 09:56:57 -070092 switch (role) {
93 case Qt::DisplayRole:
94 {
95 return item->data();
96 }
97 case Qt::FontRole:
98 {
99 QFont font;
100 if (item->isDefault()) {
101 font.setBold(true);
102 }
103 return font;
104 }
105 // case Qt::BackgroundRole:
106 // {
107 // std::cerr << "brush" << std::endl;
108 // QBrush brush(Qt::white, Qt::SolidPattern);
109 // if (index.row() % 2 == 0) {
110 // brush.setColor(Qt::cyan);
111 // }
112 // return brush;
113 // }
114 default:
115 return QVariant();
116 }
Yingdi Yu1f824642016-03-20 17:07:22 -0700117}
118
119Qt::ItemFlags
120KeyTreeModel::flags(const QModelIndex& index) const
121{
122 if (!index.isValid())
123 return 0;
124
125 return QAbstractItemModel::flags(index);
126}
127
128QVariant
129KeyTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
130{
131 // if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
132 // return m_rootItem->data();
133
134 return QVariant();
135}
136
137QModelIndex
138KeyTreeModel::index(int row, int column, const QModelIndex &parent) const
139{
140 if (!hasIndex(row, column, parent))
141 return QModelIndex();
142
143 KeyTreeItem *parentItem;
144
145 if (!parent.isValid())
146 parentItem = m_rootItem;
147 else
148 parentItem = static_cast<KeyTreeItem*>(parent.internalPointer());
149
150 KeyTreeItem *childItem = parentItem->child(row);
151 if (childItem)
152 return createIndex(row, column, childItem);
153 else
154 return QModelIndex();
155}
156
157QModelIndex
158KeyTreeModel::parent(const QModelIndex &index) const
159{
160 if (!index.isValid())
161 return QModelIndex();
162
163 KeyTreeItem *childItem = static_cast<KeyTreeItem*>(index.internalPointer());
164 KeyTreeItem *parentItem = childItem->parentItem();
165
166 if (parentItem == m_rootItem)
167 return QModelIndex();
168
169 return createIndex(parentItem->row(), 0, parentItem);
170}
171
172int
173KeyTreeModel::rowCount(const QModelIndex &parent) const
174{
175 KeyTreeItem *parentItem;
176 if (parent.column() > 0)
177 return 0;
178
179 if (!parent.isValid())
180 parentItem = m_rootItem;
181 else
182 parentItem = static_cast<KeyTreeItem*>(parent.internalPointer());
183
184 return parentItem->childCount();
185}
186
187// void
188// KeyTreeModel::setupModelData(const QStringList &lines, KeyTreeItem *parent)
189// {
190 // QList<KeyTreeItem*> parents;
191 // QList<int> indentations;
192 // parents << parent;
193 // indentations << 0;
194
195 // int number = 0;
196
197 // while (number < lines.count()) {
198 // int position = 0;
199 // while (position < lines[number].length()) {
200 // if (lines[number].at(position) != ' ')
201 // break;
202 // position++;
203 // }
204
205 // QString lineData = lines[number].mid(position).trimmed();
206
207 // if (!lineData.isEmpty()) {
208 // // Read the column data from the rest of the line.
209 // QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
210 // QList<QVariant> columnData;
211 // for (int column = 0; column < columnStrings.count(); ++column)
212 // columnData << columnStrings[column];
213
214 // if (position > indentations.last()) {
215 // // The last child of the current parent is now the new parent
216 // // unless the current parent has no children.
217
218 // if (parents.last()->childCount() > 0) {
219 // parents << parents.last()->child(parents.last()->childCount()-1);
220 // indentations << position;
221 // }
222 // } else {
223 // while (position < indentations.last() && parents.count() > 0) {
224 // parents.pop_back();
225 // indentations.pop_back();
226 // }
227 // }
228
229 // // Append a new item to the current parent's list of children.
230 // parents.last()->appendChild(new TreeItem(columnData, parents.last()));
231 // }
232
233 // ++number;
234 // }
235// }
236
237} // namespace ndn
238} // namespace ncc