summaryrefslogtreecommitdiff
path: root/src/study/WordDrillModel.cpp
blob: 403535a6650141dd64edcc156ae7abc3231631bf (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
#include "WordDrillModel.h"
#include "../dictionary/Card.h"
#include "../dictionary/CardPack.h"
#include "StudySettings.h"

#include <stdlib.h>
#include <time.h>

WordDrillModel::WordDrillModel( CardPack* aCardPack ):
    IStudyModel( aCardPack ), m_historyCurPackStart(0)
    {
    curCardNum = NoCardIndex;
    srand( time(NULL) );
    connect( aCardPack, SIGNAL(cardsGenerated()), SLOT(updateStudyState()) );

    generateFreshPack();
    pickNextCard();
    }

/** Get current card.
  * If the current card doesn't exist any more, find the next valid card.
  */

Card* WordDrillModel::getCurCard() const
    {
    if( m_cardHistory.isEmpty() || curCardNum < 0 )   // No cards in the pack
        return NULL;
    if( curCardNum >= m_cardHistory.size() )
        return NULL;
    QString cardName = m_cardHistory.at(curCardNum);
    return cardPack->getCard( cardName );
    }

void WordDrillModel::updateStudyState()
    {
    generateFreshPack();
    cleanHistoryFromRemovedCards();
    if( getCurCard() )
        emit curCardUpdated();
    else
        pickNextCard();
    }

void WordDrillModel::generateFreshPack()
    {
    m_freshPack = cardPack->getCardQuestions();
    // Remove already reviewed cards
    if( m_historyCurPackStart >= m_cardHistory.size() )
        return;
    foreach( QString reviewedCard, m_cardHistory.mid( m_historyCurPackStart ) )
        m_freshPack.removeAll( reviewedCard );
    }

void WordDrillModel::cleanHistoryFromRemovedCards()
    {
    if( m_cardHistory.isEmpty() )
        return;
    bool cardsRemoved = false;
    QMutableStringListIterator it( m_cardHistory );
    int i = 0;
    while( it.hasNext() )
        {
        QString cardName = it.next();
        if( !cardPack->containsQuestion( cardName ) )
            {
            it.remove();
            cardsRemoved = true;
            if( i < curCardNum )
                curCardNum--;
            }
        i++;
        }
    if( cardsRemoved )
        {
        if( curCardNum >= m_cardHistory.size() )
            curCardNum = m_cardHistory.size() - 1;
        emit nextCardSelected();
        }
    }


/** Picks a random card. Removes the selected card from the fresh pack and adds it to the history.
 * Thus, the new card is the last entry in the history.
 * This function guarantees that the new card's question will be different from the previous one, unless there is no choice.
 *
 * Updates #m_curCardNum.
 */
void WordDrillModel::pickNextCard()
    {
    QString selectedCardName;
    const Card* selectedCard = NULL;
    do
        {
        if( m_freshPack.isEmpty() )  // No fresh cards
            {
            m_historyCurPackStart = m_cardHistory.size(); // Refers beyond the history pack
            generateFreshPack();
            if( m_freshPack.isEmpty() )  // Still no any cards available - no useful cards in the dictionary or it's empty
                {
                curCardNum = NoCardIndex;
                emit nextCardSelected();
                return;
                }
            }
        if( m_freshPack.size() == 1 )    // Only 1 fresh card, no choice
            selectedCardName = m_freshPack.takeFirst();
        else
            {
            int selectedCardNum;
            if( StudySettings::inst()->showRandomly )
                selectedCardNum = rand() % m_freshPack.size();
            else
                selectedCardNum = 0;
            if( !m_cardHistory.isEmpty() )
                while( m_freshPack[ selectedCardNum ] == m_cardHistory.last() ) // The new question must be different from the current one
                    {
                    selectedCardNum++;
                    selectedCardNum %= m_freshPack.size();
                    }
            selectedCardName = m_freshPack.takeAt( selectedCardNum );
            }
        selectedCard = cardPack->getCard( selectedCardName );
        }
    while( !selectedCard );
    m_cardHistory << selectedCardName;
    curCardNum = m_cardHistory.size() - 1;
    emit nextCardSelected();
    }

/** Go back along the history line.
 * @return true, if the transition was successful
 */
bool WordDrillModel::goBack()
    {
    if( !canGoBack() )
        return false;
    curCardNum--;
    emit nextCardSelected();
    return true;
    }

/** Go forward along the history line.
 * @return true, if the transition was successful
 */
bool WordDrillModel::goForward()
    {
    if( !canGoForward() )
        return false;
    curCardNum++;
    emit nextCardSelected();
    return true;
    }

bool WordDrillModel::canGoBack()
    {
    return curCardNum > 0;
    }

bool WordDrillModel::canGoForward()
    {
    return curCardNum < m_cardHistory.size() - 1;
    }