summaryrefslogtreecommitdiff
path: root/src/study/IStudyWindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/study/IStudyWindow.cpp')
-rw-r--r--src/study/IStudyWindow.cpp293
1 files changed, 293 insertions, 0 deletions
diff --git a/src/study/IStudyWindow.cpp b/src/study/IStudyWindow.cpp
new file mode 100644
index 0000000..f918936
--- /dev/null
+++ b/src/study/IStudyWindow.cpp
@@ -0,0 +1,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);
+}