summaryrefslogtreecommitdiffstats
path: root/sigmodr/tree/AuthorGroupModel.cpp
blob: c17b3551f7268219b7f144265dab7b2e0fe39933 (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
/*
 * Copyright 2008-2009 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 "AuthorGroupModel.h"

// Sigmodr tree includes
#include "AuthorModel.h"

// Sigmod includes
#include <sigmod/Author.h>
#include <sigmod/Game.h>

// KDE includes
#include <KMenu>

using namespace Sigmod;
using namespace Sigmodr::Tree;

AuthorGroupModel::AuthorGroupModel(BaseModel* parent, Game* game) :
        GroupModel(parent, game, "Authors")
{
    for (int i = 0; i < game->authorCount(); ++i)
        addObject(game->author(i));
}

QVariant AuthorGroupModel::data(const int role) const
{
    if (role == BaseModel::ContextMenuRole)
    {
        KMenu* menu = new KMenu;
        menu->addAction("&Add Author", this, SLOT(addObject()));
        return QVariant::fromValue(menu);
    }
    return GroupModel::data(role);
}

bool AuthorGroupModel::setData(const QVariant& value, int role)
{
    if (role == BaseModel::XmlRole)
    {
        QString data = value.toString();
        if (!data.isEmpty())
        {
            QDomDocument xml;
            if (loadFromData(data, &xml) && (xml.doctype().name() == "Author"))
            {
                addObject(qobject_cast<Game*>(m_object)->newAuthor(xml.documentElement()));
                return true;
            }
        }
    }
    return false;
}

QString AuthorGroupModel::types() const
{
    return "Author";
}

void AuthorGroupModel::addObject(Object* object)
{
    if (!object)
        object = qobject_cast<Game*>(m_object)->newAuthor();
    if (object->className() == "Author")
        addChild(new AuthorModel(this, qobject_cast<Author*>(object)));
}

void AuthorGroupModel::deleteObject(BaseModel* model)
{
    const int index = find(model);
    if (0 <= index)
    {
        qobject_cast<Game*>(m_object)->deleteAuthor(index);
        removeChild(index);
    }
}