summaryrefslogtreecommitdiff
path: root/src/data/stylelist.cpp
blob: 14302c75a1d9b926e31539a41aa61355e123e635 (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
#include "stylelist.h"

#include <QTextStream>

StyleList::StyleList(bool edgeStyles, QObject *parent) : QAbstractListModel(parent), _edgeStyles(edgeStyles)
{
    if (edgeStyles) {
        _styles << noneEdgeStyle;
    } else {
        _styles << noneStyle;
    }
}

Style *StyleList::style(QString name)
{
    foreach (Style *s, _styles)
        if (s->name() == name) return s;
    return nullptr;
}

Style *StyleList::style(int i)
{
    return _styles[i];
}

int StyleList::length() const
{
    return _styles.length();
}

void StyleList::addStyle(Style *s)
{
    s->setParent(this);
    if (s->category() == _category) {
        int n = numInCategory();
        beginInsertRows(QModelIndex(), n, n);
        _styles << s;
        endInsertRows();
    } else {
        _styles << s;
    }
}

void StyleList::removeNthStyle(int n)
{
    beginRemoveRows(QModelIndex(), n, n);
    _styles.remove(nthInCategory(n));
    endRemoveRows();
}

void StyleList::clear()
{
    int n = numInCategory();
    if (n > 0) {
        beginRemoveRows(QModelIndex(), 1, n - 1);
        _styles.clear();
        if (_edgeStyles) _styles << noneEdgeStyle;
        else _styles << noneStyle;
        endRemoveRows();
    } else {
        _styles.clear();
        if (_edgeStyles) _styles << noneEdgeStyle;
        else _styles << noneStyle;
    }

    _category = "";
}

QString StyleList::tikz()
{
    QString str;
    QTextStream code(&str);
    for (int i = 1; i < _styles.length(); ++i)
        code << _styles[i]->tikz() << "\n";
    code.flush();
    return str;
}

int StyleList::numInCategory() const
{
    int c = 0;
    foreach (Style *s, _styles) {
        if (_category == "" || s->isNone() || s->category() == _category) {
            ++c;
        }
    }
    return c;
}

int StyleList::nthInCategory(int n) const
{
    int c = 0;
    for (int j = 0; j < _styles.length(); ++j) {
        if (_category == "" || _styles[j]->isNone() || _styles[j]->category() == _category) {
            if (c == n) return j;
            else ++c;
        }
    }
    return -1;
}

Style *StyleList::styleInCategory(int n) const
{
    return _styles[nthInCategory(n)];
}

QVariant StyleList::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
        return QVariant(styleInCategory(index.row())->name());
    } else if (role == Qt::DecorationRole) {
        return QVariant(styleInCategory(index.row())->icon());
    } else {
        return QVariant();
    }
}

int StyleList::rowCount(const QModelIndex &/*parent*/) const
{
    return numInCategory();
}

bool StyleList::moveRows(const QModelIndex &sourceParent,
                                int sourceRow,
                                int /*count*/,
                                const QModelIndex &destinationParent,
                                int destinationRow)
{
    if (sourceRow >= 1 && sourceRow < numInCategory() &&
        destinationRow >= 1 && destinationRow <= numInCategory())
    {
        beginMoveRows(sourceParent, sourceRow, sourceRow, destinationParent, destinationRow);
        int sourceIndex = nthInCategory(sourceRow);
        int destinationIndex = nthInCategory(destinationRow);
        if (destinationIndex == -1)
            destinationIndex = _styles.length();
        Style *s = _styles[sourceIndex];
        _styles.remove(sourceIndex);
        if (sourceIndex < destinationIndex) {
            _styles.insert(destinationIndex - 1, s);
        } else {
            _styles.insert(destinationIndex, s);
        }
        endMoveRows();
        return true;
    } else {
        return false;
    }
}

QString StyleList::category() const
{
    return _category;
}

void StyleList::setCategory(const QString &category)
{
    if (category != _category) {
        beginResetModel();
        _category = category;
        endResetModel();
    }
}