/* * Copyright 2008 Ben Boeckel * * 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 . */ // Header include #include "BadgeGroupModel.h" // Model includes #include "BadgeModel.h" // Sigmod includes #include "../../sigmod/Badge.h" #include "../../sigmod/Sigmod.h" // KDE includes #include Sigmodr::BadgeGroupModel::BadgeGroupModel(BaseModel* parent, Sigmod::Sigmod* sigmod) : GroupModel(parent, sigmod, "Badges") { for (int i = 0; i < sigmod->badgeCount(); ++i) addObject(sigmod->badge(i)); } Sigmodr::BadgeGroupModel::~BadgeGroupModel() { } QVariant Sigmodr::BadgeGroupModel::data(const int role) const { if (role == Sigmodr::BaseModel::ContextMenuRole) { KMenu* menu = new KMenu; menu->addAction("&Add Badge", this, SLOT(addObject())); return QVariant::fromValue(menu); } return Sigmodr::GroupModel::data(role); } bool Sigmodr::BadgeGroupModel::setData(const QVariant& value, int role) { if (role == Sigmodr::BaseModel::XmlRole) { QString data = value.toString(); if (!data.isEmpty()) { QDomDocument xml; if (loadFromData(data, &xml) && (xml.doctype().name() == "Badge")) { addObject(qobject_cast(m_object)->newBadge(xml.documentElement())); return true; } } } return false; } QString Sigmodr::BadgeGroupModel::types() const { return "Badge"; } void Sigmodr::BadgeGroupModel::addObject(Sigmod::Object* object) { if (!object) object = qobject_cast(m_object)->newBadge(); if (object->className() == "Badge") addChild(new BadgeModel(this, qobject_cast(object))); } void Sigmodr::BadgeGroupModel::deleteObject(BaseModel* model) { const int index = find(model); if (0 <= index) { qobject_cast(m_object)->deleteBadge(index); m_objects[index]->deleteLater(); m_objects.removeAt(index); childRowChanged(index); } }