示例#1
0
PsiContact* ContactListModel::contactFor(const QModelIndex& index) const
{
    ContactListItem *item = static_cast<ContactListItem*>(index.internalPointer());
    if (item->type() != ContactListItem::Type::ContactType)
        return nullptr;

    return item->contact();
}
示例#2
0
bool ContactListModel::setData(const QModelIndex &index, const QVariant &data, int role)
{
    if (!index.isValid())
        return false;

    ContactListItem *item = toItem(index);
    if (!item)
        return false;
    PsiContact *contact = item->isContact() ? item->contact() : nullptr;

    if (role == ActivateRole) {
        if (!contact)
            return false;

        contact->activate();
        return true;
    }
    else if (role == Qt::EditRole) {
        QString name = data.toString();
        if (contact) {
            item->setName(name);
            emit dataChanged(index, index);
        }
        else if (item->isGroup() && !name.isEmpty()) {
            QString oldName = item->name();
            QList<PsiContact*> contacts;
            for (int i = 0; i < item->childCount(); ++i) {
                if (item->child(i)->isContact())
                contacts << item->child(i)->contact();
            }

            for (PsiContact *contact: contacts) {
                QStringList groups = contact->groups();
                groups.removeOne(oldName);
                groups << name;
                contact->setGroups(groups);
            }

        }

        return true;
    }
    else if (role == ExpandedRole) {
        if (!item->isContact()) {
            item->setExpanded(data.toBool());
        }
    }

    return true;
}