summaryrefslogtreecommitdiffstats
path: root/sigmodr/SigmodTreeModel.cpp
blob: 51bea45d6889a07ce7d59ecaa7c66bbcbfd218ba (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2008 Ben Boeckel <MathStuf@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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Header include
#include "SigmodTreeModel.h"

// Model includes
#include "models/RootModel.h"

// Sigmod includes
#include "../sigmod/Sigmod.h"

// Qt includes
#include <QtCore/QMimeData>
#include <QtCore/QStringList>
#include <QtCore/QUrl>
#include <QtXml/QDomDocument>

Sigmodr::SigmodTreeModel::SigmodTreeModel(QObject* parent) :
        QAbstractItemModel(parent),
        m_root(new RootModel)
{
    connect(m_root, SIGNAL(rowsChanged(QList<int>)), this, SLOT(rowsChanged(QList<int>)));
}

Sigmodr::SigmodTreeModel::~SigmodTreeModel()
{
    delete m_root;
}

QVariant Sigmodr::SigmodTreeModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
        return QVariant();
    BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
    return object->data(role);
}

QVariant Sigmodr::SigmodTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    Q_UNUSED(section)
    Q_UNUSED(orientation)
    Q_UNUSED(role)
    return QVariant();
}

QModelIndex Sigmodr::SigmodTreeModel::index(int row, int column, const QModelIndex& parent) const
{
    if ((row < -1) || (column < -1))
        return QModelIndex();
    if (row == -1)
        return createIndex(row, 0, m_root);
    BaseModel* model = getItem(parent);
    if ((model->rowCount() <= row) || (1 <= column))
        return QModelIndex();
    BaseModel* object = model->childItem(row);
    return createIndex(row, 0, object);
}

QModelIndex Sigmodr::SigmodTreeModel::parent(const QModelIndex& index) const
{
    if (!index.isValid())
        return QModelIndex();
    BaseModel* parent = static_cast<BaseModel*>(index.internalPointer())->parent();
    if (!parent || (parent == m_root))
        return QModelIndex();
    return createIndex(parent->indexNumber(), 0, parent);
}

int Sigmodr::SigmodTreeModel::rowCount(const QModelIndex& parent) const
{
    BaseModel* object = getItem(parent);
    return object->rowCount();
}

int Sigmodr::SigmodTreeModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)
    return 1;
}

Qt::ItemFlags Sigmodr::SigmodTreeModel::flags(const QModelIndex& index) const
{
    if (!index.isValid())
        return 0;
    BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
    return object->flags();
}

bool Sigmodr::SigmodTreeModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
    if (!index.isValid())
        return false;
    BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
    emit(layoutAboutToBeChanged());
    bool success = object->setData(value, role);
    emit(layoutChanged());
    if (success)
    {
        emit(dataChanged(index, index));
        emit(dirty(findSigmod(index), true));
    }
    return success;
}

Sigmodr::BaseModel* Sigmodr::SigmodTreeModel::getItem(const QModelIndex& index) const
{
    if (index.isValid())
    {
        BaseModel* object = static_cast<BaseModel*>(index.internalPointer());
        if (object)
            return object;
    }
    return m_root;
}

QStringList Sigmodr::SigmodTreeModel::mimeTypes() const
{
    return QStringList() << "application/x-sigmod+xml" << "text/uri-list";
}

QMimeData* Sigmodr::SigmodTreeModel::mimeData(const QModelIndexList& indexes) const
{
    QMimeData *mimeData = new QMimeData;
    if ((indexes.size() == 1) && indexes[0].isValid())
    {
        QDomDocument xml;
        xml.setContent(data(indexes[0], Sigmodr::BaseModel::XmlRole).toString());
        mimeData->setData("application/x-sigmod+xml", xml.toByteArray());
    }
    return mimeData;
}

bool Sigmodr::SigmodTreeModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
{
    Q_UNUSED(row)
    Q_UNUSED(column)
    if (action == Qt::IgnoreAction)
        return true;
    QString format;
    bool success = false;
    if (data->hasFormat("application/x-sigmod+xml"))
    {
        QByteArray mimeData = data->data("application/x-sigmod+xml");
        emit(layoutAboutToBeChanged());
        success = parent.isValid() ? setData(parent, mimeData, Sigmodr::BaseModel::XmlRole) : m_root->setData(mimeData, Sigmodr::BaseModel::XmlRole);
        emit(layoutChanged());
    }
    else if (data->hasFormat("text/uri-list"))
    {
        QList<QUrl> urls = data->urls();
        foreach (const QUrl& url, urls)
        {
            emit(layoutAboutToBeChanged());
            success = parent.isValid() ? setData(parent, url.toEncoded(), Sigmodr::BaseModel::XmlRole) : m_root->setData(url.toEncoded(), Sigmodr::BaseModel::XmlRole);
            emit(layoutChanged());
        }
    }
    if (success)
        emit(dirty(findSigmod(parent), true));
    return success;
}

void Sigmodr::SigmodTreeModel::addSigmod(Sigmod::Sigmod* sigmod)
{
    m_root->addSigmod(sigmod);
    reset();
}

void Sigmodr::SigmodTreeModel::deleteSigmod(const Sigmod::Sigmod* sigmod)
{
    m_root->deleteSigmod(sigmod);
    reset();
}

const Sigmod::Sigmod* Sigmodr::SigmodTreeModel::findSigmod(const QModelIndex& index) const
{
    QModelIndex curIndex = index;
    QModelIndex parIndex = parent(curIndex);
    while (parIndex.isValid())
    {
        curIndex = parIndex;
        parIndex = parent(curIndex);
    }
    return qobject_cast<const Sigmod::Sigmod*>(getItem(curIndex)->object());
}

void Sigmodr::SigmodTreeModel::rowsChanged(const QList<int>& rows)
{
    QModelIndex curIndex;
    foreach (int row, rows)
        curIndex = index(row, 0, curIndex);
    if (curIndex.isValid())
    {
        emit(dataChanged(curIndex, curIndex));
        emit(dirty(findSigmod(curIndex), true));
    }
    else
        reset();
}