Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 24 | #include <QFont> |
| 25 | #include <QBrush> |
| 26 | #include <iostream> |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | #ifdef WAF |
| 30 | #include "key-tree-model.moc" |
| 31 | #endif |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace ncc { |
| 35 | |
| 36 | KeyTreeModel::KeyTreeModel(QObject *parent) |
| 37 | : QAbstractItemModel(parent) |
| 38 | , m_rootItem(new KeyTreeItem(QVariant(""))) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | KeyTreeModel::~KeyTreeModel() |
| 43 | { |
| 44 | delete m_rootItem; |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | KeyTreeModel::clear() |
| 49 | { |
| 50 | delete m_rootItem; |
| 51 | m_rootItem = new KeyTreeItem(QVariant("")); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | KeyTreeModel::addChild(KeyTreeItem* child) |
| 56 | { |
| 57 | child->setParent(m_rootItem); |
| 58 | m_rootItem->appendChild(child); |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | KeyTreeModel::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 | |
| 70 | QVariant |
| 71 | KeyTreeModel::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 | |
| 84 | QVariant |
| 85 | KeyTreeModel::data(const QModelIndex& index, int role) const |
| 86 | { |
| 87 | if (!index.isValid()) |
| 88 | return QVariant(); |
| 89 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 90 | |
| 91 | KeyTreeItem* item = static_cast<KeyTreeItem*>(index.internalPointer()); |
Yingdi Yu | 95dbc71 | 2016-03-21 09:56:57 -0700 | [diff] [blame] | 92 | 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 Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Qt::ItemFlags |
| 120 | KeyTreeModel::flags(const QModelIndex& index) const |
| 121 | { |
| 122 | if (!index.isValid()) |
| 123 | return 0; |
| 124 | |
| 125 | return QAbstractItemModel::flags(index); |
| 126 | } |
| 127 | |
| 128 | QVariant |
| 129 | KeyTreeModel::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 | |
| 137 | QModelIndex |
| 138 | KeyTreeModel::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 | |
| 157 | QModelIndex |
| 158 | KeyTreeModel::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 | |
| 172 | int |
| 173 | KeyTreeModel::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 |