summaryrefslogtreecommitdiff
path: root/src/main-view/AppModel.cpp
blob: 45e1956bdad233f118d2263a835c661e93b372c9 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "AppModel.h"

#include <stdlib.h>

#include "../utils/RandomGenerator.h"
#include "../dictionary/Dictionary.h"
#include "../main-view/DictTableModel.h"
#include "../study/WordDrillModel.h"
#include "../study/SpacedRepetitionModel.h"
#include "../dictionary/CardPack.h"

AppModel::AppModel():
    curDictIndex(-1), curCardPackIndex(0)
{
}

AppModel::~AppModel()
{
    while(!dictionaries.isEmpty())
    {
        QPair<Dictionary*, DictTableModel*> pair = dictionaries.takeLast();
        delete pair.second;
        delete pair.first;
    }
}

bool AppModel::openDictionary(const QString& filePath)
{
    Dictionary* dict = new Dictionary( "", false, this );
    bool ok = dict->load(filePath);
    if(ok)
        addDictionary(dict);
    else
    {
        errorMessage = dict->getErrorMessage();
        delete dict;
    }
    return ok;
}

void AppModel::addDictionary( Dictionary* aDict )
{
    DictTableModel* dictModel = new DictTableModel( aDict );
    dictionaries << qMakePair( aDict, dictModel );
    curDictIndex = dictionaries.size() - 1;
}

Dictionary* AppModel::newDictionary()
{
    Dictionary* dict = new Dictionary( "", false, this );
    dict->setDefaultFields();
    addDictionary( dict );
    return dict;
}

void AppModel::fixupCurDictIx()
{
    if( curDictIndex < 0 || curDictIndex >= dictionaries.size() )
        curDictIndex = dictionaries.size()-1;
}

Dictionary* AppModel::curDictionary()
    {
    if( dictionaries.isEmpty() )
        return NULL;
    fixupCurDictIx();
    return dictionaries[curDictIndex].first;
    }

DictTableModel* AppModel::curDictModel()
    {
    if( dictionaries.isEmpty() )
        return NULL;
    fixupCurDictIx();
    return dictionaries[curDictIndex].second;
    }

Dictionary* AppModel::dictionary(int aIndex) const
    {
    if( aIndex < 0 || aIndex >= dictionaries.size() )
        return NULL;
    return dictionaries[aIndex].first;
    }

int AppModel::indexOfDictionary( Dictionary* aDic ) const
    {
    for( int i = 0; i < dictionaries.size(); i++ )
        {
        QPair<Dictionary*, DictTableModel*> pair = dictionaries.at( i );
        if( pair.first == aDic )
            return i;
        }
    return -1;  // Not found
    }

int AppModel::indexOfDictionary( const QString& aFilePath ) const
    {
    for( int i = 0; i < dictionaries.size(); i++ )
        {
        QPair<Dictionary*, DictTableModel*> pair = dictionaries.at( i );
        Q_ASSERT( pair.first );
        if( pair.first->getFilePath() == aFilePath )
            return i;
        }
    return -1;  // Not found
    }

CardPack* AppModel::curCardPack()
    {
    Dictionary* curDic = curDictionary();
    if( curDic )
        return curDic->cardPack( curCardPackIndex );
    else
        return NULL;
    }

bool AppModel::setCurDictionary(int index)
    {
    if( dictionaries.isEmpty() )
        {
        curDictIndex = -1;
        return false;
        }
    if( index < 0 || index >= dictionaries.size())
        {
        curDictIndex = dictionaries.size()-1;
        return false;
        }
    curDictIndex = index;
    return true;
    }

bool AppModel::removeDictionary( int aIndex )
    {
    if( aIndex < 0 || aIndex >= dictionaries.size() )
        return false;
    QPair<Dictionary*, DictTableModel*> pair = dictionaries.takeAt( aIndex );
    delete pair.second;
    delete pair.first;
    return true;
    }

/** Destroys dic model and the dictionary itself.
  */
void AppModel::removeDictModel( QAbstractItemModel* aDictModel )
    {
    int i = 0;
    for( ;i < dictionaries.size(); i++ )
        {
        QPair<Dictionary*, DictTableModel*> pair = dictionaries.at( i );
        if( pair.second == aDictModel )
            {
            dictionaries.removeAt( i );   // Update the size of m_dictionaries before destroying
            delete pair.first;
            delete pair.second;
            break;
            }
        }
    }

IStudyModel* AppModel::createStudyModel(int studyType, int cardPackIndex)
    {
    Dictionary* curDic = curDictionary();
    if(!curDic)
    {
        errorMessage = tr("No dictionary opened.");
        return NULL;
    }
    if(curDic->entriesNum() == 0)
    {
        errorMessage = tr("The current dictionary is empty.");
        return NULL;
    }

    curCardPackIndex = cardPackIndex;
    CardPack* cardPack = curDic->cardPack(curCardPackIndex);
    if(!cardPack || cardPack->cardsNum() == 0)
    {
        errorMessage = tr("The current dictionary is empty.");
        return NULL;
    }

    IStudyModel* studyModel = NULL;
    switch(studyType)
        {
        case WordDrill:
            studyModel = new WordDrillModel(cardPack);
            break;
        case SpacedRepetition:
            studyModel = new SpacedRepetitionModel(cardPack, new RandomGenerator);
            break;
        }
    if(studyModel)
        studyModel->setDictModel( curDictModel() );
    return studyModel;
    }