From ea39cc542e17ce592dc3c4f2053d534bc458d88e Mon Sep 17 00:00:00 2001 From: "Felix (xq) Queißner" Date: Sat, 30 May 2020 19:33:47 +0200 Subject: More usability, survives conmans torture nearly with 100% --- documentoutlinemodel.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 documentoutlinemodel.cpp (limited to 'documentoutlinemodel.cpp') diff --git a/documentoutlinemodel.cpp b/documentoutlinemodel.cpp new file mode 100644 index 0000000..1787d9e --- /dev/null +++ b/documentoutlinemodel.cpp @@ -0,0 +1,131 @@ +#include "documentoutlinemodel.hpp" + +#include + +DocumentOutlineModel::DocumentOutlineModel() : + QAbstractItemModel(), + root() +{ + +} + +void DocumentOutlineModel::beginBuild() +{ + beginResetModel(); + root = Node { + nullptr, + "", + 0, + QVector { }, +}; +} + +void DocumentOutlineModel::appendH1(const QString &title) +{ + root.children.append(Node { + &root, + title, + 1, + QVector { }, + }); +} + +void DocumentOutlineModel::appendH2(const QString &title) +{ + if(root.children.size() == 0) { + root.children.append(Node { + &root, + "", + 1, + QVector { }, + }); + } + auto & parent = root.children.last(); + parent.children.append(Node { + &parent, + title, + 2, + QVector { }, + }); +} + +void DocumentOutlineModel::appendH3(const QString &title) +{ + +} + +void DocumentOutlineModel::endBuild() +{ + endResetModel(); +} + +QModelIndex DocumentOutlineModel::index(int row, int column, const QModelIndex &parent) const +{ + if (not hasIndex(row, column, parent)) + return QModelIndex(); + + Node const * parentItem; + + if (!parent.isValid()) + parentItem = &this->root; + else + parentItem = static_cast(parent.internalPointer()); + + Node const * childItem = &parentItem->children[row]; + if (childItem) + return createIndex(row, column, reinterpret_cast(childItem)); + return QModelIndex(); + +} + +QModelIndex DocumentOutlineModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) + return QModelIndex(); + + Node const *childItem = static_cast(child.internalPointer()); + Node const * parent = childItem->parent; + + if (parent == &root) + return QModelIndex(); + + return createIndex( + parent - parent->parent->children.data(), + 0, + reinterpret_cast(parent)); +} + +int DocumentOutlineModel::rowCount(const QModelIndex &parent) const +{ + Node const *parentItem; + if (parent.column() > 0) + return 0; + + if (!parent.isValid()) + parentItem = &root; + else + parentItem = static_cast(parent.internalPointer()); + + return parentItem->children.size(); +} + +int DocumentOutlineModel::columnCount(const QModelIndex &parent) const +{ + return 1; +} + +QVariant DocumentOutlineModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.column() != 0) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + Node const *item = static_cast(index.internalPointer()); + + return item->title; +} -- cgit v1.2.3