remarkable_tool/notebook_tree_model.cpp

91 lines
2.7 KiB
C++

/* Copyright 2021, Michele Santullo
* This file is part of remarkable_tool.
*
* remarkable_tool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Remarkable_tool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with remarkable_tool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "notebook_tree_model.hpp"
#include "notebook_tree.hpp"
namespace duck {
NotebookTreeModel::NotebookTreeModel (Node* root, QObject* parent) :
QAbstractItemModel(parent),
m_tree(root)
{
}
NotebookTreeModel::~NotebookTreeModel() noexcept = default;
int NotebookTreeModel::rowCount (const QModelIndex& parent) const {
if (parent.column() > 0)
return 0;
if (not parent.isValid())
return static_cast<int>(m_tree->children.size());
else
return static_cast<int>(static_cast<Node*>(parent.internalPointer())->children.size());
}
int NotebookTreeModel::columnCount (const QModelIndex&) const {
return 1;
}
Qt::ItemFlags NotebookTreeModel::flags (const QModelIndex& index) const {
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
QModelIndex NotebookTreeModel::index (
int row,
int column,
const QModelIndex& parent
) const {
if (not hasIndex(row, column, parent))
return {};
Node* const parent_item = (parent.isValid() ? static_cast<Node*>(parent.internalPointer()) : m_tree);
Node* child_item = &parent_item->children[row];
return createIndex(row, column, child_item);
}
QVariant NotebookTreeModel::data (const QModelIndex& index, int role) const {
if (not index.isValid() or role != Qt::DisplayRole)
return {};
Node* const item = static_cast<Node*>(index.internalPointer());
return QString::fromUtf8(item->info->visible_name.data(), static_cast<int>(item->info->visible_name.size()));
}
QVariant NotebookTreeModel::headerData (int section, Qt::Orientation orientation, int role) const {
return {};
}
QModelIndex NotebookTreeModel::parent (const QModelIndex& index) const {
if (not index.isValid())
return {};
Node* const curr_node = static_cast<Node*>(index.internalPointer());
if (curr_node->parent == m_tree) {
return {};
}
else {
const Node* const parent_node = curr_node->parent;
const int parent_index = static_cast<int>(parent_node - parent_node->parent->children.data());
return createIndex(parent_index, 0, const_cast<Node*>(parent_node));
}
}
} //namespace duck