summaryrefslogtreecommitdiff
path: root/src/study/WordDrillWindow.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/study/WordDrillWindow.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/study/WordDrillWindow.cpp')
-rw-r--r--src/study/WordDrillWindow.cpp180
1 files changed, 180 insertions, 0 deletions
diff --git a/src/study/WordDrillWindow.cpp b/src/study/WordDrillWindow.cpp
new file mode 100644
index 0000000..dd13bd1
--- /dev/null
+++ b/src/study/WordDrillWindow.cpp
@@ -0,0 +1,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());
+}