summaryrefslogtreecommitdiff
path: root/src/study/CardEditDialog.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/CardEditDialog.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/study/CardEditDialog.cpp')
-rw-r--r--src/study/CardEditDialog.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/study/CardEditDialog.cpp b/src/study/CardEditDialog.cpp
new file mode 100644
index 0000000..5dc47a3
--- /dev/null
+++ b/src/study/CardEditDialog.cpp
@@ -0,0 +1,119 @@
+#include "CardEditDialog.h"
+#include "IStudyWindow.h"
+#include "IStudyModel.h"
+#include "../dictionary/Card.h"
+#include "../dictionary/CardPack.h"
+#include "../dictionary/Dictionary.h"
+#include "../main-view/DictTableModel.h"
+#include "../main-view/DictTableView.h"
+#include "../main-view/CardFilterModel.h"
+#include "../main-view/MainWindow.h"
+
+#include <QSettings>
+#include <QCloseEvent>
+#include <QPushButton>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QHeaderView>
+#include <QVariant>
+
+CardEditDialog::CardEditDialog(Card* aCurCard, MainWindow* aMainWindow, IStudyWindow* aStudyWindow ):
+ QDialog( aStudyWindow ), m_cardEditModel( NULL ), m_cardEditView( NULL ), m_mainWindow( aMainWindow )
+ {
+ DictTableModel* tableModel = aStudyWindow->studyModel()->getDictModel();
+ if( !tableModel )
+ return;
+
+ m_cardEditModel = new CardFilterModel( tableModel );
+ m_cardEditModel->setSourceModel( tableModel );
+
+ const CardPack* cardPack = static_cast<const CardPack*>(aCurCard->getCardPack());
+ Q_ASSERT(cardPack);
+ m_dictionary = static_cast<const Dictionary*>(cardPack->dictionary());
+ Q_ASSERT( m_dictionary );
+ foreach( const DicRecord* record, aCurCard->getSourceRecords() )
+ {
+ int row = m_dictionary->indexOfRecord( const_cast<DicRecord*>( record ) );
+ if( row > -1 )
+ m_cardEditModel->addFilterRow( row );
+ }
+
+ m_cardEditView = new DictTableView( m_cardEditModel, this );
+ m_cardEditView->setContextMenuPolicy( Qt::ActionsContextMenu );
+ m_cardEditView->verticalHeader()->setContextMenuPolicy( Qt::ActionsContextMenu );
+ if( m_mainWindow )
+ {
+ m_cardEditView->addActions(m_mainWindow->getContextMenuActions());
+ m_cardEditView->verticalHeader()->addActions(m_mainWindow->getContextMenuActions());
+ connect( m_cardEditView->selectionModel(),
+ SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
+ m_mainWindow, SLOT(updateSelectionActions()) );
+ }
+
+ QPushButton* dicWindowBtn = new QPushButton(tr("Go to dictionary window"));
+ connect( dicWindowBtn, SIGNAL(clicked()), SLOT(goToDictionaryWindow()));
+ QPushButton* closeBtn = new QPushButton(tr("Close"));
+ connect( closeBtn, SIGNAL(clicked()), SLOT(close()));
+
+ QVBoxLayout* editLt = new QVBoxLayout;
+ editLt->addWidget( m_cardEditView );
+ QHBoxLayout* buttonsLt = new QHBoxLayout;
+ buttonsLt->addStretch( 1 );
+ buttonsLt->addWidget( dicWindowBtn );
+ buttonsLt->addWidget( closeBtn );
+ editLt->addLayout( buttonsLt );
+
+ setLayout( editLt );
+ setWindowTitle( QString(tr("Edit card: ", "In title of card edit view")) + aCurCard->getName() );
+ setWindowModality( Qt::WindowModal );
+ closeBtn->setFocus();
+ m_cardEditView->setCurrentIndex( m_cardEditModel->index(0, 0) );
+
+ QSettings settings;
+ QVariant posVar = settings.value("cardeditview-pos");
+ if( !posVar.isNull() )
+ move( posVar.toPoint() );
+ resize( settings.value("cardeditview-size", QSize(CardEditViewWidth, CardEditViewHeight)).toSize() );
+ }
+
+CardEditDialog::~CardEditDialog()
+ {
+ delete m_cardEditView;
+ delete m_cardEditModel;
+ }
+
+void CardEditDialog::closeEvent( QCloseEvent* event )
+ {
+ QSettings settings;
+ settings.setValue("cardeditview-pos", pos());
+ settings.setValue("cardeditview-size", size());
+ event->accept();
+ }
+
+bool CardEditDialog::event( QEvent* event )
+ {
+ if( event->type() == QEvent::WindowActivate || event->type() == QEvent::WindowDeactivate )
+ {
+ m_mainWindow->updateSelectionActions();
+ return true;
+ }
+ return QDialog::event( event );
+ }
+
+const DictTableView* CardEditDialog::cardEditView() const
+ {
+ if( isActiveWindow() )
+ return m_cardEditView;
+ else
+ return NULL;
+ }
+
+void CardEditDialog::goToDictionaryWindow()
+ {
+ close();
+ QModelIndex curProxyIndex = m_cardEditView->currentIndex();
+ QModelIndex curSourceIndex = m_cardEditModel->mapToSource( curProxyIndex );
+ if( !m_dictionary )
+ return;
+ m_mainWindow->goToDictionaryRecord( m_dictionary, curSourceIndex.row() );
+ }