Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California, |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame] | 21 | #include "key-tree-model.moc" |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 22 | #include "key-tree-item.hpp" |
| 23 | |
| 24 | #include <QStringList> |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 25 | #include <QFont> |
| 26 | #include <QBrush> |
| 27 | #include <iostream> |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 29 | namespace ndn { |
| 30 | namespace ncc { |
| 31 | |
| 32 | KeyTreeModel::KeyTreeModel(QObject *parent) |
| 33 | : QAbstractItemModel(parent) |
| 34 | , m_rootItem(new KeyTreeItem(QVariant(""))) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | KeyTreeModel::~KeyTreeModel() |
| 39 | { |
| 40 | delete m_rootItem; |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | KeyTreeModel::clear() |
| 45 | { |
| 46 | delete m_rootItem; |
| 47 | m_rootItem = new KeyTreeItem(QVariant("")); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | KeyTreeModel::addChild(KeyTreeItem* child) |
| 52 | { |
| 53 | child->setParent(m_rootItem); |
| 54 | m_rootItem->appendChild(child); |
| 55 | } |
| 56 | |
| 57 | int |
| 58 | KeyTreeModel::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 | |
| 66 | QVariant |
| 67 | KeyTreeModel::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 | |
| 80 | QVariant |
| 81 | KeyTreeModel::data(const QModelIndex& index, int role) const |
| 82 | { |
| 83 | if (!index.isValid()) |
| 84 | return QVariant(); |
| 85 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 86 | |
| 87 | KeyTreeItem* item = static_cast<KeyTreeItem*>(index.internalPointer()); |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 88 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Qt::ItemFlags |
| 116 | KeyTreeModel::flags(const QModelIndex& index) const |
| 117 | { |
| 118 | if (!index.isValid()) |
| 119 | return 0; |
| 120 | |
| 121 | return QAbstractItemModel::flags(index); |
| 122 | } |
| 123 | |
| 124 | QVariant |
| 125 | KeyTreeModel::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 | |
| 133 | QModelIndex |
| 134 | KeyTreeModel::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 | |
| 153 | QModelIndex |
| 154 | KeyTreeModel::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 | |
| 168 | int |
| 169 | KeyTreeModel::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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 233 | } // namespace ncc |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame] | 234 | } // namespace ndn |