summaryrefslogtreecommitdiff
path: root/src/data/graphelementdata.cpp
blob: 01736b87544dd5f28458f17893335a3e23158285 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "graphelementdata.h"

#include <QDebug>
#include <QTextStream>

GraphElementData::GraphElementData(QVector<GraphElementProperty> init, QObject *parent) : QAbstractItemModel(parent)
{
    root = new GraphElementProperty();
    _properties = init;
}

GraphElementData::GraphElementData(QObject *parent) : QAbstractItemModel(parent) {
    root = new GraphElementProperty();
}

GraphElementData::~GraphElementData()
{
    delete root;
}

GraphElementData *GraphElementData::copy()
{
    return new GraphElementData(_properties);
}

void GraphElementData::setProperty(QString key, QString value)
{
    GraphElementProperty m(key, true);
    int i = _properties.indexOf(m);
    if (i != -1) {
        _properties[i].setValue(value);
    } else {
        GraphElementProperty p(key, value);
        _properties << p;
    }
}

void GraphElementData::unsetProperty(QString key)
{
    GraphElementProperty m(key, true);
    int i = _properties.indexOf(m);
    if (i != -1)
        _properties.remove(i);
}

void GraphElementData::add(GraphElementProperty p)
{
    _properties << p;
}

void GraphElementData::operator <<(GraphElementProperty p)
{
    add(p);
}

void GraphElementData::setAtom(QString atom)
{
    GraphElementProperty a(atom);
    int i = _properties.indexOf(a);
    if (i == -1)
        _properties << a;
}

void GraphElementData::unsetAtom(QString atom)
{
    GraphElementProperty a(atom);
    int i = _properties.indexOf(a);
    if (i != -1)
        _properties.remove(i);
}

QString GraphElementData::property(QString key)
{
    GraphElementProperty m(key, true);
    int i = _properties.indexOf(m);
    if (i != -1) {
        return _properties[i].value();
    } else {
        return QString(); // null QString
    }
}

bool GraphElementData::atom(QString atom)
{
    GraphElementProperty a(atom);
    return (_properties.indexOf(a) != -1);
}

QVariant GraphElementData::data(const QModelIndex &index, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (index.row() >= 0 && index.row() < _properties.length()) {
        const GraphElementProperty &p = _properties[index.row()];
        QString s = (index.column() == 0) ? p.key() : p.value();
        return QVariant(s);
    }

    return QVariant();
}

QVariant GraphElementData::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        if (section == 0) return QVariant("Key/Atom");
        else return QVariant("Value");
    }

    return QVariant();
}

QModelIndex GraphElementData::index(int row, int column, const QModelIndex &parent) const
{
    return createIndex(row, column, (void*)0);
}

QModelIndex GraphElementData::parent(const QModelIndex &index) const
{
    GraphElementProperty *p = static_cast<GraphElementProperty*>(index.internalPointer());
    if (p == root) return QModelIndex();
    else return createIndex(0,0,static_cast<void*>(root));
}

int GraphElementData::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    } else {
        return _properties.size();
    }
}

int GraphElementData::columnCount(const QModelIndex &) const
{
    return 2;
}

Qt::ItemFlags GraphElementData::flags(const QModelIndex &index) const
{
    return QAbstractItemModel::flags(index);
}

//bool GraphElementData::setData(const QModelIndex &index, const QVariant &value, int role)
//{

//}

//bool GraphElementData::insertRows(int position, int rows, const QModelIndex &parent)
//{

//}

//bool GraphElementData::removeRows(int position, int rows, const QModelIndex &parent)
//{

//}

QString GraphElementData::tikz() {
    if (_properties.length() == 0) return "";
    QString str;
    QTextStream code(&str);
    code << "[";

    GraphElementProperty p;
    bool first = true;
    foreach(p, _properties) {
        if (!first) code << ", ";
        code << p.tikz();
        first = false;
    }

    code << "]";

    code.flush();
    return str;
}

bool GraphElementData::isEmpty()
{
    return _properties.isEmpty();
}

QVector<GraphElementProperty> GraphElementData::properties() const
{
    return _properties;
}