summaryrefslogtreecommitdiff
path: root/src/dictionary/DicRecord.cpp
blob: 91cebcab805fbcb2d224cbc03f04b0c39d99057e (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
#include "DicRecord.h"
#include "Field.h"
#include "ICardPack.h"

DicRecord::DicRecord()
    {
    }

DicRecord::DicRecord( const DicRecord& aOther ):
    QObject(0)
    {
    fields = aOther.fields;
    m_id04 = aOther.m_id04;
    }

void DicRecord::setField( QString aField, QString aValue )
    {
    fields[aField] = aValue;
    emit valueChanged( aField );
    }

void DicRecord::renameField( const QString aOldFieldName, const QString aNewFieldName )
    {
    if( !fields.keys().contains( aOldFieldName ) )
        return;
    QString value = fields.value( aOldFieldName );
    fields.remove( aOldFieldName );
    fields[ aNewFieldName ] = value;
}

bool DicRecord::isValid(const QString& qstFieldName) const
{
    return !fields.value(qstFieldName).isEmpty() &&
        hasNonEmptyAnswerField( qstFieldName );
}

bool DicRecord::hasNonEmptyAnswerField(const QString& qstFieldName) const
{
    foreach(QString name, fields.keys())
        if(name != qstFieldName && !fields.value(name).isEmpty())
            return true;
    return false;
}

QStringList DicRecord::getFieldElements(const QString& fieldName) const
{
    QString value = fields.value(fieldName);
    QStringList elements = value.split(ICardPack::SynonymDelimiter, QString::SkipEmptyParts);
    QStringList trimmedElements;
    foreach(QString element, elements)
        trimmedElements << element.trimmed();
    return trimmedElements;
}

QString DicRecord::getPreviewQuestionForPack(ICardPack* pack) const
{
    if(!pack)
        return QString();
    QString fieldName = pack->getQuestionField()->name();
    QString questionFieldValue = getFieldValue(fieldName);
    return questionFieldValue.split(ICardPack::SynonymDelimiter).first().trimmed();
}