Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COMPONENTS_H
- #define COMPONENTS_H
- #include <vector>
- #include <string>
- #include <memory>
- #include <QVariant>
- class Components
- {
- public:
- enum Types {
- kString,
- knumber,
- kDictionary,
- kArray
- };
- Components(Types type);
- Components(Types type, std::shared_ptr<Components> parent, std::string name);
- void setValue(std::string value);
- void addChild(std::shared_ptr<Components> child);
- Components *child(int row) const;
- Components *parent() const;
- int childCount() const;
- int columnCount() const;
- int row() const;
- QVariant data(int column) const;
- private:
- std::shared_ptr<Components> m_parent;
- enum Types m_type;
- QVariant m_name; // "key" in dictionary terms
- QVariant m_value;
- bool m_isValuePresent;
- std::vector<std::shared_ptr<Components>> m_children;
- };
- #endif
- // components.cpp
- #include "components.h"
- #include <stdexcept>
- #include <QString>
- Components::Components(Types type) {
- m_type = type;
- m_name = "root";
- m_isValuePresent = false;
- }
- Components::Components(Types type, std::shared_ptr<Components> parent, std::string name) {
- m_type = type;
- m_parent = parent;
- m_name = QVariant(QString::fromStdString(name));;
- m_isValuePresent = false;
- }
- void Components::setValue(std::string value) {
- if (m_children.size() != 0)
- throw std::invalid_argument("Value can't be assigned if children are present");
- m_value = QVariant(QString::fromStdString(value));
- m_isValuePresent = true;
- }
- void Components::addChild(std::shared_ptr<Components> child) {
- if (m_isValuePresent)
- throw std::invalid_argument("Can't add child if value is present");
- m_children.push_back(child);
- return;
- }
- Components* Components::child(int row) const {
- if (row >= m_children.size())
- return nullptr;
- return m_children[row].get();
- }
- Components* Components::parent() const {
- //if (m_parent.expired()) {
- // throw new std::invalid_argument("Parent pointer doesn't exist");
- //}
- return m_parent.get();
- }
- int Components::childCount() const {
- return m_children.size();
- }
- int Components::columnCount() const {
- // Apparently this goes for all children
- return 2;
- }
- QVariant Components::data(int column) const {
- if (column == 0)
- return m_name;
- if (column == 1 && m_isValuePresent)
- return m_value;
- return QVariant();
- }
- int Components::row() const {
- //if (m_parent.expired())
- // return 0;
- Components *parent = this->parent();
- for (int i = 0; i < parent->childCount(); ++i) {
- Components *child = parent->m_children[i].get();
- if (child == this)
- return i;
- }
- // Shouldn't happen
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement