From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- src/study/CardEditDialog.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/study/CardEditDialog.cpp (limited to 'src/study/CardEditDialog.cpp') 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 +#include +#include +#include +#include +#include +#include + +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(aCurCard->getCardPack()); + Q_ASSERT(cardPack); + m_dictionary = static_cast(cardPack->dictionary()); + Q_ASSERT( m_dictionary ); + foreach( const DicRecord* record, aCurCard->getSourceRecords() ) + { + int row = m_dictionary->indexOfRecord( const_cast( 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() ); + } -- cgit