summaryrefslogtreecommitdiff
path: root/src/study/CardEditDialog.cpp
blob: 5dc47a354d51dcb607893dc594576f2d4c1f7ef5 (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
#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() );
    }