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

WordDrillWindow::WordDrillWindow( WordDrillModel* aModel, QWidget* aParent ):
    IStudyWindow(aModel, tr("Word drill"), aParent)
{
    studyType = AppModel::WordDrill;
    createUI(); // Can't call from parent constructor
    setWindowIcon( QIcon(":/images/word-drill.png") );
    showNextCard();
}

WordDrillWindow::~WordDrillWindow()
{
    WriteSettings();
}

QVBoxLayout* WordDrillWindow::createLowerPanel()
{
    QHBoxLayout* cardNumLayout = new QHBoxLayout;
    iCardNumLabel = new QLabel;
    iCardNumLabel->setAlignment( Qt::AlignVCenter );
    iCardNumLabel->setToolTip(tr("Current card / All cards"));
    iProgressBar = new QProgressBar;
    iProgressBar->setTextVisible( false );
    iProgressBar->setMaximumHeight( iProgressBar->sizeHint().height()/2 );
    iProgressBar->setToolTip(tr("Progress of reviewing cards"));
    iShowAnswersCB = new QCheckBox(tr("Show answers"));
    iShowAnswersCB->setShortcut( tr("S", "Shortcut for 'Show answers' checkbox") );
    iShowAnswersCB->setChecked( true );
    connect( iShowAnswersCB, SIGNAL(stateChanged(int)), this, SLOT(ToggleAnswer()) );

    cardNumLayout->addWidget( iCardNumLabel );
    cardNumLayout->addWidget( iProgressBar );
    cardNumLayout->addWidget( iShowAnswersCB );

    iBackBtn = new QPushButton( QIcon(":/images/back.png"), tr("Back"), this);
    iBackBtn->setShortcut( Qt::Key_Left );
    iBackBtn->setToolTip( tr("Go back in history") + ShortcutToStr( iBackBtn ) );
    connect( iBackBtn, SIGNAL(clicked()), qobject_cast<WordDrillModel*>(m_model), SLOT(goBack()) );

    iForwardBtn = new QPushButton( QIcon(":/images/forward.png"), tr("Forward"), this);
    iForwardBtn->setShortcut( Qt::Key_Right );
    iForwardBtn->setToolTip( tr("Go forward in history") + ShortcutToStr( iForwardBtn ) );
    connect( iForwardBtn, SIGNAL(clicked()), qobject_cast<WordDrillModel*>(m_model), SLOT(goForward()) );

    iNextBtn = new QPushButton( QIcon(":/images/next.png"), tr("Next"), this);
    iNextBtn->setShortcut( Qt::Key_Return );
    iNextBtn->setToolTip( tr("Show next card (Enter)") );
    connect( iNextBtn, SIGNAL(clicked()), qobject_cast<WordDrillModel*>(m_model), SLOT(pickNextCard()) );

    QHBoxLayout* controlLt = new QHBoxLayout;
    controlLt->addWidget( iBackBtn );
    controlLt->addWidget( iForwardBtn );
    controlLt->addWidget( iNextBtn );

    QVBoxLayout* lowerPanelLt = new QVBoxLayout;
    lowerPanelLt->addLayout( cardNumLayout );
    if( bigScreen() )
        lowerPanelLt->addLayout( controlLt );

    return lowerPanelLt;
}

void WordDrillWindow::setStateForNextCard()
{
    if( m_model->getCurCard() )
        state = iShowAnswersCB->isChecked()? StateAnswerVisible: StateAnswerHidden;
    else
        state = StateNoCards;
 }

void WordDrillWindow::processState()
{
if( state == StateAnswerHidden || state == StateAnswerVisible )
    {
    centralStackedLt->setCurrentIndex( CardPage );
    DisplayCardNum();
    UpdateButtons();
    }
switch( state )
    {
    case StateAnswerHidden:
        questionLabel->setQuestion( m_model->getCurCard()->getQuestion() );
        answerStackedLt->setCurrentIndex( AnsButtonPage );
        answerBtn->setFocus();
        break;
    case StateAnswerVisible:
        {
        Card* card = m_model->getCurCard();
        if( iShowAnswersCB->isChecked() )   // StateAnswerHidden was skipped
            questionLabel->setQuestion( card->getQuestion() );
        answerStackedLt->setCurrentIndex( AnsLabelPage );
        answerLabel->setQstAnsr( card->getQuestion(), card->getAnswers() );
        iNextBtn->setFocus();
        break;
        }
    case StateNoCards:
        centralStackedLt->setCurrentIndex(MessagePage);
        messageLabel->setText(
                QString("<div align=\"center\" style=\"font-size: 22pt; font-weight: bold\">") +
                tr("No cards available") + "</div>");
        break;
    }
}

void WordDrillWindow::DisplayCardNum()
{
int curCardIndex = qobject_cast<WordDrillModel*>(m_model)->getCurCardNum();
int curCardNum = curCardIndex + 1;
int historySize = qobject_cast<WordDrillModel*>(m_model)->historySize();
int packSize = m_model->getCardPack()->cardsNum();

int curCardNumInPack = curCardIndex % packSize + 1;
int passNum;
if( packSize > 0 )
    passNum = curCardIndex / packSize + 1;
else
    passNum = 1;
QString cardNumStr = QString("%1/%2").arg( curCardNumInPack ).arg( packSize );
QString passIconStr = "<img src=\":/images/passes.png\"/>";
if( passNum > 1 )
    cardNumStr += " " + passIconStr + QString::number( passNum );
if( curCardNum < historySize && packSize > 0 )
    {
    int lastCardIndex = historySize - 1;
    int lastCardNumInPack = lastCardIndex % packSize + 1;
    cardNumStr += " (" + QString::number( lastCardNumInPack );
    int sessionPassNum = lastCardIndex / packSize + 1;
    if( sessionPassNum > 1 )
        cardNumStr += " " + passIconStr + QString::number( sessionPassNum );
    cardNumStr += ")";
    }

iCardNumLabel->setText( cardNumStr );
iProgressBar->setMaximum( packSize );
iProgressBar->setValue( curCardNumInPack );
}

void WordDrillWindow::UpdateButtons()
{
iBackBtn->setEnabled( qobject_cast<WordDrillModel*>(m_model)->canGoBack() );
iForwardBtn->setEnabled( qobject_cast<WordDrillModel*>(m_model)->canGoForward() );
}

void WordDrillWindow::ToggleAnswer()
{
if( !m_model->getCurCard() )
    return;
answerStackedLt->setCurrentIndex( iShowAnswersCB->isChecked()? AnsLabelPage: AnsButtonPage );
switch( state )
    {
    case StateAnswerHidden:
        state = StateAnswerVisible;
        break;
    case StateAnswerVisible:
        state = iShowAnswersCB->isChecked()? StateAnswerVisible: StateAnswerHidden;
        break;
    default:
        return;
    }
processState();
}

void WordDrillWindow::ReadSettings()
{
QSettings settings;
move( settings.value("worddrill-pos", QPoint(PosX, PosY)).toPoint() );
resize( settings.value("worddrill-size", QSize(Width, Height)).toSize() );
}

void WordDrillWindow::WriteSettings()
{
QSettings settings;
settings.setValue("worddrill-pos", pos());
settings.setValue("worddrill-size", size());
}