Files
xtree.ng.zwo/model/xqnode.h
2025-08-13 18:30:47 +02:00

173 lines
3.4 KiB
C++

/***************************************************************************
source::worx xtree
Copyright © 2024-2025 c.holzheuer
christoph.holzheuer@gmail.com
This program 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 2 of the License, or
(at your option) any later version.
***************************************************************************/
#ifndef XQNODE_H
#define XQNODE_H
#include <iostream>
#include <memory>
#include <stack>
#include <iterator>
#include <QDebug>
#include <QModelIndex>
#include <znode.h>
#include <xqappdata.h>
#include <znode_factory.h>
// Overload the operator<< for QString and std::ostream
std::ostream& operator<<(std::ostream& os, const QString& obj);
// raw node
using XQNode = znode::zbasic_node<QString>;
// default shared node
using XQNodePtr = std::shared_ptr<znode::zbasic_node<QString>>;
// const ref on XQNodePtr, ok but what its good for?
//using const XQNodePtr& = const XQNodePtr&;
/*
class XQBasicSheetNode : public znode::zbasic_node<QString>
{
public:
//...
};
using XQSheetNode = std::shared_ptr<const XQBasicSheetNode>;
using XQSheetNodeCRef = const XQSheetNode&;
*/
///
/// __fix
/// sollte man einen XQNodePtr einführen, um 'setAttr<XQNodePtr>()' von
/// setAttr<XQNodePtr>() unterscheiden zu können?
///
/// nice try. (siehe oben). Haut aber so nicht hin, node.children() gibt wieder
/// Pointer auf die Basisklasse zurück, ( vgl. XQItemList -> QList<QStandardItem*>.
/// Erst dass behandeln!
///
// weak pointer to znode, used for parent()
using XQWeakNode = std::weak_ptr<znode::zbasic_node<QString>>;
// the node factory
using XQNodeFactory = znode::znode_factory<QString>;
class XQNodeList : public znode::zbasic_node<QString>::znode_list
{
friend class XQSimpleClipBoard;
public:
XQNodeList() = default;
virtual ~XQNodeList() = default;
};
class XNodeIterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = XQNode;
using difference_type = std::ptrdiff_t;
using pointer = XQNode*;
using reference = XQNode&;
XNodeIterator() = default;
XNodeIterator(XQNodePtr root)
{
if (root) {
_stack.push(root);
}
}
reference operator*() const {
return *_stack.top();
}
pointer operator->() const {
return _stack.top().get();
}
XNodeIterator& operator++() {
auto node = _stack.top();
_stack.pop();
for (auto it = node->children().rbegin(); it != node->children().rend(); ++it)
{
_stack.push(*it);
}
return *this;
}
XNodeIterator operator++(int) {
XNodeIterator tmp = *this;
++(*this);
return tmp;
}
bool operator==(const XNodeIterator& other) const {
return _stack == other._stack;
}
bool operator!=(const XNodeIterator& other) const {
return !(*this == other);
}
private:
std::stack<XQNodePtr> _stack;
};
// Define the tree class with begin and end methods
class XTree
{
public:
XTree(XQNodePtr root)
: _root(root)
{}
XNodeIterator begin()
{
return XNodeIterator(_root);
}
XNodeIterator end()
{
return XNodeIterator();
}
private:
XQNodePtr _root;
};
//void inspect( XQNodePtr node, int offSet=0 );
void inspect( const XQNodePtr& node, int indent=0 );
Q_DECLARE_METATYPE(XQNodePtr);
#endif // XQNODE_H