diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-05-30 19:33:47 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-05-30 19:33:47 +0200 |
| commit | ea39cc542e17ce592dc3c4f2053d534bc458d88e (patch) | |
| tree | c3c6a369d5b6d8a6a4e0b3e3667a56ca19e93173 /documentoutlinemodel.cpp | |
| parent | 79ff338a3427a236ef53adf806c56616faa3426c (diff) | |
| download | kristall-ea39cc542e17ce592dc3c4f2053d534bc458d88e.tar.gz | |
More usability, survives conmans torture nearly with 100%
Diffstat (limited to 'documentoutlinemodel.cpp')
| -rw-r--r-- | documentoutlinemodel.cpp | 131 |
1 files changed, 131 insertions, 0 deletions
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 <QModelIndex> + +DocumentOutlineModel::DocumentOutlineModel() : + QAbstractItemModel(), + root() +{ + +} + +void DocumentOutlineModel::beginBuild() +{ + beginResetModel(); + root = Node { + nullptr, + "<ROOT>", + 0, + QVector<Node> { }, +}; +} + +void DocumentOutlineModel::appendH1(const QString &title) +{ + root.children.append(Node { + &root, + title, + 1, + QVector<Node> { }, + }); +} + +void DocumentOutlineModel::appendH2(const QString &title) +{ + if(root.children.size() == 0) { + root.children.append(Node { + &root, + "<missing layer>", + 1, + QVector<Node> { }, + }); + } + auto & parent = root.children.last(); + parent.children.append(Node { + &parent, + title, + 2, + QVector<Node> { }, + }); +} + +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<Node*>(parent.internalPointer()); + + Node const * childItem = &parentItem->children[row]; + if (childItem) + return createIndex(row, column, reinterpret_cast<quintptr>(childItem)); + return QModelIndex(); + +} + +QModelIndex DocumentOutlineModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) + return QModelIndex(); + + Node const *childItem = static_cast<Node const *>(child.internalPointer()); + Node const * parent = childItem->parent; + + if (parent == &root) + return QModelIndex(); + + return createIndex( + parent - parent->parent->children.data(), + 0, + reinterpret_cast<quintptr>(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<Node const *>(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<Node const*>(index.internalPointer()); + + return item->title; +} |
