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