summaryrefslogtreecommitdiff
path: root/src/study/IStudyWindow.cpp
blob: f91893622ae11b68ea2d876d2fb411df40248424 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "IStudyWindow.h"

#include <QCloseEvent>
#include <QMessageBox>

#include "IStudyModel.h"
#include "CardSideView.h"
#include "CardEditDialog.h"
#include "../dictionary/Dictionary.h"
#include "../dictionary/CardPack.h"
#include "../main-view/MainWindow.h"

const int IStudyWindow::AnsButtonPage = 0;
const int IStudyWindow::AnsLabelPage = 1;
const int IStudyWindow::CardPage = 0;
const int IStudyWindow::MessagePage = 1;

IStudyWindow::IStudyWindow(IStudyModel* aModel , QString aStudyName, QWidget *aParent):
    m_model( aModel ), curCard( NULL ), state(StateAnswerHidden), m_studyName( aStudyName ),
    m_parentWidget( aParent ), m_cardEditDialog( NULL )
{
    setMinimumWidth(MinWidth);
    setMaximumWidth(MaxWidth);
    setAttribute( Qt::WA_DeleteOnClose );
    connect( m_model, SIGNAL(nextCardSelected()), SLOT(showNextCard()) );
    connect( m_model, SIGNAL(curCardUpdated()), SLOT(updateCurCard()) );
}

IStudyWindow::~IStudyWindow()
{
    delete m_model;
}

void IStudyWindow::OnDictionaryRemoved()
{
    close();
}

void IStudyWindow::createUI()
{
    centralStackedLt = new QStackedLayout;
    centralStackedLt->addWidget(createWrapper(createCardView()));
    centralStackedLt->addWidget(createWrapper(createMessageLayout()));
    centralStackedLt->setCurrentIndex(CardPage);
    connect(centralStackedLt, SIGNAL(currentChanged(int)), SLOT(updateToolBarVisibility(int)));

    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addLayout(createUpperPanel());
    mainLayout->addLayout(centralStackedLt);
    mainLayout->addLayout(createLowerPanel());
    setLayout(mainLayout);

    ReadSettings();
}

QWidget* IStudyWindow::createWrapper(QBoxLayout* layout)
{
    QWidget* widget = new QWidget;
    widget->setLayout(layout);
    return widget;
}

QVBoxLayout* IStudyWindow::createMessageLayout()
{
    messageLabel = createMessageLabel();

    QVBoxLayout* lt = new QVBoxLayout;
    lt->setContentsMargins(QMargins());
    lt->addStretch();
    lt->addWidget(messageLabel);
    lt->addStretch();
    lt->addWidget(createClosePackButton(), 0, Qt::AlignHCenter);
    lt->addStretch();
    return lt;
}

QLabel* IStudyWindow::createMessageLabel()
{
    QLabel* label = new QLabel;
    label->setAutoFillBackground(true);
    label->setPalette(QPalette("#fafafa"));
    label->setWordWrap(true);
    label->setFrameStyle(QFrame::StyledPanel);
    label->setMinimumHeight(200);
    return label;
}

QPushButton* IStudyWindow::createClosePackButton()
{
    QPushButton* button = new QPushButton(tr("Close this pack"));
    button->setFixedSize(BigButtonWidth, BigButtonHeight);
    connect(button, SIGNAL(clicked()), SLOT(close()));
    return button;
}

QHBoxLayout* IStudyWindow::createUpperPanel()
{
    editCardBtn = createEditCardButton();
    deleteCardBtn = createDeleteCardButton();

    QHBoxLayout* upperPanelLt = new QHBoxLayout;
    upperPanelLt->addWidget(new QLabel(m_model->getCardPack()->id()));
    upperPanelLt->addWidget(deleteCardBtn);
    upperPanelLt->addWidget(editCardBtn);

    return upperPanelLt;
}

QToolButton* IStudyWindow::createEditCardButton()
{
    QToolButton* button = new QToolButton( this );
    button->setIcon( QIcon(":/images/pencil.png") );
    button->setIconSize( QSize(ToolBarIconSize, ToolBarIconSize) );
    button->setShortcut( tr("E", "Shortcut for 'Edit card' button") );
    button->setToolTip( tr("Edit card")+" ("+button->shortcut().toString()+")" );
    button->setFixedSize( ToolBarButtonSize, ToolBarButtonSize );
    connect( button, SIGNAL(clicked()), SLOT(openCardEditDialog()) );
    return button;
}

QToolButton* IStudyWindow::createDeleteCardButton()
{
    QToolButton* button = new QToolButton( this );
    button->setIcon( QIcon(":/images/red-cross.png") );
    button->setIconSize( QSize(ToolBarIconSize, ToolBarIconSize) );
    button->setShortcut( tr("D", "Shortcut for 'Delete card' button") );
    button->setToolTip( tr("Delete card")+" ("+button->shortcut().toString()+")" );
    button->setFixedSize( ToolBarButtonSize, ToolBarButtonSize );
    connect( button, SIGNAL(clicked()), SLOT(deleteCard()) );
    return button;
}

QVBoxLayout* IStudyWindow::createCardView()
{
    questionLabel = new CardSideView( CardSideView::QstMode );
    questionLabel->setPack( m_model->getCardPack() );

    answerStackedLt = new QStackedLayout;
    answerStackedLt->addWidget(createWrapper(createAnswerButtonLayout()));
    answerStackedLt->addWidget(createWrapper(createAnswerLayout()));
    answerStackedLt->setCurrentIndex(AnsButtonPage);

    QVBoxLayout* cardViewLt = new QVBoxLayout;
    cardViewLt->addWidget(questionLabel, 1);
    cardViewLt->addLayout(answerStackedLt, 1);

    return cardViewLt;
}

QBoxLayout* IStudyWindow::createAnswerButtonLayout()
{
    answerBtn = createAnswerButton();

    QBoxLayout* lt = new QVBoxLayout;
    lt->addStretch();
    if(getAnswerEdit())
        lt->addWidget(getAnswerEdit(), 0, Qt::AlignCenter);
    lt->addWidget(answerBtn, 0, Qt::AlignCenter);
    lt->addStretch();
    return lt;
}

QPushButton* IStudyWindow::createAnswerButton()
{
    QPushButton* button = new QPushButton(QIcon(":/images/info.png"), tr("Show answer"));
    button->setFixedSize(BigButtonWidth, BigButtonHeight);
    button->setShortcut(Qt::Key_Return);
    button->setToolTip(tr("Show answer") + ShortcutToStr(button) );
    connect(button, SIGNAL(clicked()), SLOT(showAnswer()));
    return button;
}

QBoxLayout* IStudyWindow::createAnswerLayout()
{
    answerLabel = new CardSideView( CardSideView::AnsMode );
    answerLabel->setPack( m_model->getCardPack() );

    QBoxLayout* lt = new QVBoxLayout;
    lt->setContentsMargins(QMargins());
    if(getUserAnswerLabel())
        lt->addWidget(getUserAnswerLabel(), 1);
    lt->addWidget(answerLabel, 1);
    return lt;
}

const DictTableView* IStudyWindow::cardEditView() const
    {
    if( m_cardEditDialog )
        return m_cardEditDialog->cardEditView();
    else
        return NULL;
    }

void IStudyWindow::openCardEditDialog()
    {
    if( m_cardEditDialog )  // already open
        return;
    Card* curCard = m_model->getCurCard();
    Q_ASSERT( curCard );
    MainWindow* mainWindow = dynamic_cast<MainWindow*>( m_parentWidget );
    if( !mainWindow )
        return;
    m_cardEditDialog = new CardEditDialog( curCard, mainWindow, this );
    m_cardEditDialog->exec();    
    delete m_cardEditDialog;
    m_cardEditDialog = NULL;
    Dictionary* dict = static_cast<Dictionary*>(m_model->getCardPack()->dictionary());
    dict->save();
    setWindowTitle(m_studyName + " - " + dict->shortName());
    }

void IStudyWindow::deleteCard()
    {
    QString question = m_model->getCurCard()->getQuestion();
    QMessageBox::StandardButton pressedButton;
    pressedButton = QMessageBox::question( this, tr("Delete card?"), tr("Delete card \"%1\"?").arg( question ),
        QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
    if( pressedButton == QMessageBox::Yes )
        static_cast<Dictionary*>(m_model->getCardPack()->dictionary())->
            removeRecord( question );
    }

void IStudyWindow::showNextCard()
{
    Q_ASSERT( m_model );
    if( curCard )
        disconnect( curCard, 0, this, 0 );
    curCard = m_model->getCurCard();
    if( curCard )
        {
        connect( curCard, SIGNAL(answersChanged()), SLOT(updateCurCard()) );
        connect( curCard, SIGNAL(destroyed()), SLOT(invalidateCurCard()) );
        }
    setWindowTitle( m_studyName + " - " +
        static_cast<Dictionary*>(m_model->getCardPack()->dictionary())->shortName() );
    setStateForNextCard();
    processState();
}

void IStudyWindow::setStateForNextCard()
    {
    if( curCard )
        state = StateAnswerHidden;
    else
        state = StateNoCards;
    }

void IStudyWindow::showAnswer()
    {
    answerStackedLt->setCurrentIndex( AnsLabelPage );
    if( state == StateAnswerHidden )
        state = StateAnswerVisible;
    else
        return;
    processState();
    }

void IStudyWindow::updateCurCard()
{
    if( curCard )
        disconnect( curCard, 0, this, 0 );
    curCard = m_model->getCurCard();
    if( curCard )
        {
        connect( curCard, SIGNAL(answersChanged()), SLOT(updateCurCard()) );
        connect( curCard, SIGNAL(destroyed()), SLOT(invalidateCurCard()) );
        }
    setWindowTitle( m_studyName + " - " +
        static_cast<Dictionary*>(m_model->getCardPack()->dictionary())->shortName() );
    int origState = state;
    if( state == StateAnswerHidden || state == StateAnswerVisible )
        {
        state = StateAnswerHidden;
        processState();
        }
    if( origState == StateAnswerVisible )
        {
        state = StateAnswerVisible;
        processState();
        }
}

QString IStudyWindow::ShortcutToStr( QAbstractButton* aButton )
{
    return " (" + aButton->shortcut().toString( QKeySequence::NativeText ) + ")";
}

void IStudyWindow::updateToolBarVisibility(int index)
{
    bool visible = (index == CardPage);
    deleteCardBtn->setVisible(visible);
    editCardBtn->setVisible(visible);
}