summaryrefslogtreecommitdiff
path: root/src/dic-options/PacksPage.cpp
blob: e78bbf0ced4038a1575012bb2942b999045c39c8 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "PacksPage.h"
#include "FieldsListModel.h"
#include "PacksListModel.h"
#include "PackFieldsListModel.h"
#include "PackFieldsView.h"
#include "UnusedFieldsListModel.h"
#include "../dictionary/Dictionary.h"
#include "../study/CardSideView.h"
#include "../dictionary/CardPack.h"
#include "../field-styles/FieldStyleFactory.h"

#include <QLabel>
#include <QPushButton>
#include <QToolButton>
#include <QStringListModel>

PacksPage::PacksPage( DictionaryOptionsDialog* aParent ):
    QWidget( aParent ), m_parent( aParent )
    {
    createPacksList();
    createPackFieldsList();
    createUnusedFieldsList();
    createPackPreview();

    QGridLayout* mainLt = new QGridLayout;
    mainLt->addLayout( m_packsListLt, 0, 0, 2, 1 );
    mainLt->addLayout( m_fieldsListLt, 0, 1 );
    mainLt->addLayout( m_unusedFieldsListLt, 1, 1 );
    mainLt->addLayout( m_previewLt, 0, 2, 2, 1 );
    setLayout( mainLt );

    // Select first pack
    m_packsListView->selectionModel()->setCurrentIndex( m_packFieldsListModel->index(0, 0),
        QItemSelectionModel::Select );
    }

PacksPage::~PacksPage()
    {
    }

void PacksPage::createPacksList()
    {
    m_packsListModel = new PacksListModel( m_parent );
    m_packsListView = new PackFieldsView( this );
    m_packsListView->setModel( m_packsListModel );
    m_packsListView->setDropIndicatorShown(true);
    m_packsListView->setMinimumWidth( 200 );

    QPushButton* addPackBtn = new QPushButton( QPixmap(":/images/add.png"), tr("Add") );
    QPushButton* removePackBtn = new QPushButton( QPixmap(":/images/delete.png"), tr("Remove") );
    QHBoxLayout* packBtnLt = new QHBoxLayout;
    packBtnLt->addWidget( addPackBtn );
    packBtnLt->addWidget( removePackBtn );

    connect( addPackBtn, SIGNAL(clicked()), this, SLOT(addPack()) );
    connect( removePackBtn, SIGNAL(clicked()), this, SLOT(removePacks()) );

    QToolButton* packMoveUpBtn = new QToolButton;
    packMoveUpBtn->setObjectName("pack-up");
    packMoveUpBtn->setIcon(QIcon(":/images/1uparrow.png"));
    packMoveUpBtn->setToolTip(tr("Move pack up"));
    QToolButton* packMoveDownBtn = new QToolButton;
    packMoveDownBtn->setObjectName("pack-down");
    packMoveDownBtn->setIcon(QIcon(":/images/1downarrow.png"));
    packMoveDownBtn->setToolTip(tr("Move pack down"));
    QVBoxLayout* packMoveBtnsLt = new QVBoxLayout;
    packMoveBtnsLt->addWidget( packMoveUpBtn );
    packMoveBtnsLt->addWidget( packMoveDownBtn );
    packMoveBtnsLt->addStretch( 1 );

    connect( packMoveUpBtn, SIGNAL(clicked()), this, SLOT(moveItemsUpDown()) );
    connect( packMoveDownBtn, SIGNAL(clicked()), this, SLOT(moveItemsUpDown()) );

    m_packsListLt = new QGridLayout;
    m_packsListLt->addWidget( new QLabel("<b>"+tr("Card packs")+"</b>"), 0, 0, 1, 2 );
    m_packsListLt->addWidget( m_packsListView, 1, 0 );
    m_packsListLt->addLayout( packMoveBtnsLt, 1, 1 );
    m_packsListLt->addLayout( packBtnLt, 2, 0, 1, 2 );
    }

void PacksPage::createPackFieldsList()
    {
    m_packFieldsListModel = new PackFieldsListModel( m_parent );
    m_fieldsListView = new PackFieldsView( this );
    m_fieldsListView->setModel( m_packFieldsListModel );
    m_fieldsListView->setDropIndicatorShown(true);

    connect( m_packsListView->selectionModel(),
        SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
        m_packFieldsListModel, SLOT(changeParentRow(const QModelIndex&)) );
    connect( m_packsListView->selectionModel(),
        SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
        SLOT(updateUsesExactAnswer(const QModelIndex&)) );
    connect( m_packFieldsListModel,
        SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
        m_packsListView, SLOT(updateCurrent()) );
    connect( m_packFieldsListModel,
        SIGNAL(rowsInserted(const QModelIndex&, int, int)),
        m_packsListView, SLOT(updateCurrent()) );

    QToolButton* fieldMoveUpBtn = new QToolButton;
    fieldMoveUpBtn->setObjectName("field-up");
    fieldMoveUpBtn->setIcon(QIcon(":/images/1uparrow.png"));
    fieldMoveUpBtn->setToolTip(tr("Move field up"));
    QToolButton* fieldMoveDownBtn = new QToolButton;
    fieldMoveDownBtn->setObjectName("field-down");
    fieldMoveDownBtn->setIcon(QIcon(":/images/1downarrow.png"));
    fieldMoveDownBtn->setToolTip(tr("Move field down"));
    QVBoxLayout* fieldBtnsLt = new QVBoxLayout;
    fieldBtnsLt->addWidget( fieldMoveUpBtn );
    fieldBtnsLt->addWidget( fieldMoveDownBtn );
    fieldBtnsLt->addStretch( 1 );

    connect( fieldMoveUpBtn, SIGNAL(clicked()), this, SLOT(moveItemsUpDown()) );
    connect( fieldMoveDownBtn, SIGNAL(clicked()), this, SLOT(moveItemsUpDown()) );

    m_fieldsListLt = new QGridLayout;
    m_fieldsListLt->addWidget( new QLabel("<b>"+tr("Pack fields")+"</b>"), 0, 0, 1, 2 );
    m_fieldsListLt->addWidget( m_fieldsListView, 1, 0 );
    m_fieldsListLt->addLayout( fieldBtnsLt, 1, 1 );

    }

void PacksPage::createUnusedFieldsList()
    {
    m_unusedFieldsListModel = new UnusedFieldsListModel( m_parent );
    m_unusedFieldsListView = new PackFieldsView( this );
    m_unusedFieldsListView->setModel( m_unusedFieldsListModel );

    connect( m_packsListView->selectionModel(),
        SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
        m_unusedFieldsListModel, SLOT(changeParentRow(const QModelIndex&)) );

    connect( m_packFieldsListModel,
        SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
        m_unusedFieldsListModel, SLOT(updateUnusedFields()) );
    connect( m_packFieldsListModel,
        SIGNAL(rowsInserted(const QModelIndex&, int, int)),
        m_unusedFieldsListModel, SLOT(updateUnusedFields()) );
    connect( m_parent->m_fieldsListModel,
        SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
        m_unusedFieldsListModel, SLOT(updateUnusedFields()) );
    connect( m_parent->m_fieldsListModel,
        SIGNAL(rowsInserted(const QModelIndex&, int, int)),
        m_unusedFieldsListModel, SLOT(updateUnusedFields()) );
    connect( m_parent->m_fieldsListModel,
        SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
        m_unusedFieldsListModel, SLOT(updateUnusedFields()) );

    QToolButton* fieldRemoveBtn = new QToolButton;
    fieldRemoveBtn->setIcon(QIcon(":/images/down.png"));
    fieldRemoveBtn->setToolTip(tr("Remove field from pack"));
    QToolButton* fieldAddBtn = new QToolButton;
    fieldAddBtn->setIcon(QIcon(":/images/up.png"));
    fieldAddBtn->setToolTip(tr("Add field to pack"));
    QHBoxLayout* usedFieldsBtnsLt = new QHBoxLayout;
    usedFieldsBtnsLt->addWidget( fieldRemoveBtn );
    usedFieldsBtnsLt->addWidget( fieldAddBtn );

    connect( fieldRemoveBtn, SIGNAL(clicked()), this, SLOT(removeFields()) );
    connect( fieldAddBtn, SIGNAL(clicked()), this, SLOT(addFields()) );

    usesExactAnswerBox = new QCheckBox(tr("Uses exact answer"));
    connect(usesExactAnswerBox,
        SIGNAL(stateChanged(int)), SLOT(updatePackUsesExactAnswer(int)));

    m_unusedFieldsListLt = new QGridLayout;
    m_unusedFieldsListLt->addLayout( usedFieldsBtnsLt, 0, 0  );
    m_unusedFieldsListLt->addWidget( new QLabel("<b>"+tr("Unused fields")+"</b>"), 0, 1 );
    m_unusedFieldsListLt->addWidget( m_unusedFieldsListView, 1, 0, 1, 2 );
    m_unusedFieldsListLt->addWidget(usesExactAnswerBox, 2, 0, 1, 2);
    }

void PacksPage::createPackPreview()
    {
    m_qstPreview = new CardSideView( CardSideView::QstMode );
    m_qstPreview->setMinimumSize( 200, 70 );
    m_ansPreview = new CardSideView( CardSideView::AnsMode );
    m_ansPreview->setMinimumSize( 200, 70 );
    m_previewLt = new QVBoxLayout;
    m_previewLt->addWidget( new QLabel( "<b>"+tr("Preview")+"</b>") );
    m_previewLt->addWidget( m_qstPreview, 1 );
    m_previewLt->addWidget( m_ansPreview, 1 );

    // From: the packs list
    connect( m_packsListView->selectionModel(),
        SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
        SLOT(updatePreviewForPack()) );

    // From: the pack fields list
    connect( m_packFieldsListModel,
        SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
        SLOT(updatePreviewForPack()) );
    connect( m_packFieldsListModel,
        SIGNAL(rowsInserted(const QModelIndex&, int, int)),
        SLOT(updatePreviewForPack()) );

    // From: the dictionary field list at the "Fields" page
    connect( m_parent->m_fieldsListModel,
        SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
        SLOT(updatePreviewForPack()) );
    connect( m_parent->m_fieldsListModel,
        SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
        SLOT(updatePreviewForPack()) );
    }

void PacksPage::updatePreviewForPack()
    {
    int selectedPack = m_packsListView->selectionModel()->currentIndex().row();
    if( selectedPack > -1 ) // If any selection
        m_curPack = selectedPack;
    CardPack* pack = m_parent->m_dict.cardPack( m_curPack );
    if( !pack )
        return;

    const Field* qstField = pack->getQuestionField();
    if( !qstField )
        return;
    QStringList ansList;
    foreach( const Field* field, pack->getAnswerFields() )
        ansList << field->name();
    m_qstPreview->setPack( pack );
    m_qstPreview->setQuestion( qstField->name() );
    m_ansPreview->setPack( pack );
    m_ansPreview->setQstAnsr( qstField->name(), ansList );
    }

void PacksPage::moveItemsUpDown()
    {
    PackFieldsView* view;
    if( sender()->objectName().contains("pack") )
        view = m_packsListView;
    else if( sender()->objectName().contains("field") )
        view = m_fieldsListView;
    else
        return;
    DraggableListModel* model = view->model();

    QModelIndexList selectedIndexes = view->selectionModel()->selectedIndexes();
    QList<QPersistentModelIndex> pSrcIndexes;
    foreach (QModelIndex idx, selectedIndexes)
        if( idx.isValid() )
            pSrcIndexes << QPersistentModelIndex( idx );
    int rowsNum = pSrcIndexes.size();
    if( rowsNum == 0 )
        return;
    qSort( pSrcIndexes );

    int beginRow;
    if( sender()->objectName().contains("up") )
        beginRow = pSrcIndexes[0].row()-1;
    else if( sender()->objectName().contains("down") )
        beginRow = pSrcIndexes[rowsNum-1].row()+2;
    else
        return;
    if( rowsNum == 1 && (beginRow < 0 || beginRow > model->rowCount()) )
        return;
    if( beginRow < 0 )
            beginRow = 0;
    if( beginRow > model->rowCount() )
            beginRow = model->rowCount();

    QMimeData* mime = model->mimeData( selectedIndexes );
    model->dropMimeData( mime, Qt::MoveAction, beginRow, 0, QModelIndex() );
    foreach (QModelIndex index, pSrcIndexes)
        model->removeRow( index.row() );
    }

void PacksPage::removeFields()
    {
    if( m_packFieldsListModel->rowCount() <= 1 )
        return;
    QModelIndexList selectedIndexes = m_fieldsListView->selectionModel()->selectedIndexes();
    if( selectedIndexes.isEmpty() )
        return;
    QList<QPersistentModelIndex> pSrcIndexes;
    foreach (QModelIndex idx, selectedIndexes)
        if( idx.isValid() )
            pSrcIndexes << QPersistentModelIndex( idx );
    foreach (QModelIndex index, pSrcIndexes)
        m_fieldsListView->model()->removeRow( index.row() );
    }

void PacksPage::addFields()
    {
    QModelIndexList selectedIndexes = m_unusedFieldsListView->selectionModel()->selectedIndexes();
    if( selectedIndexes.isEmpty() )
        return;
    int dstRow = m_fieldsListView->currentIndex().row()+1;
    QMimeData* mime = m_unusedFieldsListModel->mimeData( selectedIndexes );
    m_packFieldsListModel->dropMimeData( mime, Qt::MoveAction, dstRow, 0, QModelIndex() );
    }


void PacksPage::addPack()
    {
    QModelIndexList selectedIndexes = m_packsListView->selectionModel()->selectedRows();
    int row;
    if( selectedIndexes.size() > 0 )
        {
        qSort(selectedIndexes);
        row = selectedIndexes[selectedIndexes.size()-1].row() + 1;
        }
    else
        row = 0;
    m_packsListModel->insertRow( row, QModelIndex() );
    }

void PacksPage::removePacks()
    {
    QModelIndexList selectedIndexes = m_packsListView->selectionModel()->selectedRows();
    QList<QPersistentModelIndex> pIndexes;
    foreach (QModelIndex idx, selectedIndexes)
        if( idx.isValid() )
            pIndexes << QPersistentModelIndex( idx );
    if( pIndexes.size() == 0 )
        return;
    foreach( QModelIndex idx, pIndexes )
        m_packsListModel->removePack( idx.row() );
    }

void PacksPage::renamePack()
    {
    QModelIndex idx = m_packsListView->currentIndex();
    if( !idx.isValid() )
        return;
    m_packsListView->edit( idx );
    }

void PacksPage::updateUsesExactAnswer(const QModelIndex& index)
{
    int selectedPack = index.row();
    if( selectedPack > -1 ) // If any selection
        m_curPack = selectedPack;
    CardPack* pack = m_parent->m_dict.cardPack( m_curPack );
    if( !pack )
        return;
    usesExactAnswerBox->setChecked(pack->getUsesExactAnswer());
}

void PacksPage::updatePackUsesExactAnswer(int state)
{
    CardPack* pack = m_parent->m_dict.cardPack( m_curPack );
    pack->setUsesExactAnswer(state == Qt::Checked);
}