bool SceneModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    int row = index.row();
    if (row >= mNovel->scenes().count()){
        qWarning() << "set scene data: row" << row << "> novel scene count ("
                   << mNovel->scenes().count() << ")";
        return false;
    } else if (!index.isValid()){
        qWarning() << "set scene data: Invalid index";
        return false;
    }

    Scene *scene = mNovel->scenes()[row];

    QList<Character *> characters;

    if (role == Qt::DisplayRole || role == Qt::EditRole)
        scene->setHeadline(value.toString());
    else if (scene->plotline() && role == Qt::BackgroundRole){
        qWarning() << "set scene data: BackgroundRole is read-only";
        return false;
    }
    else if (role == HeadlineRole)
        scene->setHeadline(value.toString());
    else if (role == ActionRole)
        scene->setAction(value.toString());
    else if (role == PlotlineRole){
        Plotline *p = mNovel->plotline(QUuid(value.toString()));
        if (!p) qWarning() << "set scene data: Could not find plotline" << value.toInt();
        scene->setPlotline(p);
    }else if (role == CharactersRole){
        for (QJsonValue val : value.toJsonArray())
            characters << mNovel->character(QUuid(val.toString()));
        scene->setCharacters(characters);
    } else if (role == PointsOfViewRole) {
        for (QJsonValue val : value.toJsonArray())
            characters << mNovel->character(QUuid(val.toString()));
        scene->setPointsOfView(characters);
    }
    return true;
}
Example #2
0
void KNConfigure::setData(const QString &key, const QVariant &value)
{
    //Check whether the value is null.
    if(value.isNull())
    {
        //Remove the key from the configure data.
        m_dataObject.remove(key);
        //Complete.
        return;
    }
    //Because the QJsonObject can only insert QJsonValue, and the construct
    //function of QJsonValue only have the following types:
    //   bool, QString, array, double, object.
    //So we have to translate some complex type variant to object.
    switch(value.type())
    {
    //For the basic types(double, float, int, bool, QString), we will save them
    //directly.
    case QVariant::LongLong:
        m_dataObject.insert(key, value.toLongLong());
        break;
    case QVariant::Double:
        m_dataObject.insert(key, value.toDouble());
        break;
    case QVariant::String:
        m_dataObject.insert(key, value.toString());
        break;
    case QVariant::Int:
        m_dataObject.insert(key, value.toInt());
        break;
    case QVariant::Bool:
        m_dataObject.insert(key, value.toBool());
        break;
    //For advanced types(like Font), we have to translate them to a object.
    case QVariant::Font:
    {
        //Generate the font object.
        QFont font=value.value<QFont>();
        QJsonObject fontObject;
        fontObject.insert("Type", QString("Font"));
        fontObject.insert("Family", font.family());
        fontObject.insert("Size", font.pixelSize());
        fontObject.insert("Bold", font.bold());
        fontObject.insert("Italic", font.italic());
        fontObject.insert("Underline", font.underline());
        fontObject.insert("Strikeout", font.strikeOut());
        fontObject.insert("Kerning", font.kerning());
        //Insert the font object.
        m_dataObject.insert(key, fontObject);
        break;
    }
    case QVariant::KeySequence:
    {
        //Generate a key sequence object.
        QKeySequence keySequence=value.value<QKeySequence>();
        //A shortcut object.
        QJsonObject shortcutObject;
        shortcutObject.insert("Type", QString("Shortcut"));
        shortcutObject.insert("Key", keySequence.toString(
                                  QKeySequence::PortableText));
        //Insert the key sequence object.
        m_dataObject.insert(key, shortcutObject);
        break;
    }
    //For the JSON object, it is quite wired that QVariant doesn't enumerate the
    //JSON type here, but it will use the meta type json object here.
    case QMetaType::QJsonObject:
    {
        //Check with the value contains the custom type.
        QJsonObject customObject=value.toJsonObject();
        //Check the custom object type is matched.
        if("CustomObject"==customObject.value("Type").toString())
        {
            //Simply insert the json object to the data.
            m_dataObject.insert(key, customObject);
        }
        break;
    }
    //For the JSON array, directly save the original data.
    case QMetaType::QJsonArray:
    {
        //Simply insert the json array to the data.
        m_dataObject.insert(key, value.toJsonArray());
        break;
    }
    default:
        return;
    }
    //Emit the signal.
    emit valueChanged();
}