summaryrefslogtreecommitdiff
path: root/src/dic-options/FieldsPage.cpp
blob: ea9a741f7ffdb251424905b87987a267695bfaa4 (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
#include "FieldsPage.h"
#include "../dictionary/Dictionary.h"
#include "../dictionary/CardPack.h"
#include "FieldsListModel.h"
#include "FieldsPreviewModel.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QToolButton>
#include <QGroupBox>

FieldsPage::FieldsPage( DictionaryOptionsDialog* aParent ):
    QWidget( aParent ), m_parent( aParent )
    {
    createFieldsList();
    createFieldsPreview();

    QHBoxLayout* mainLt = new QHBoxLayout;
    mainLt->addLayout( m_fieldsLt );
    mainLt->addWidget( m_previewGroup );
    setLayout( mainLt );
    }

void FieldsPage::createFieldsList()
    {
    QGroupBox* fieldsGroup = new QGroupBox( tr("Fields") );

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

    connect( moveUpBtn, SIGNAL(clicked()), this, SLOT(moveItemsUpDown()) );
    connect( moveDownBtn, SIGNAL(clicked()), this, SLOT(moveItemsUpDown()) );

    m_fieldsListView = new FieldsView;
    m_fieldsListView->setModel( m_parent->m_fieldsListModel );
    QHBoxLayout* listHLt = new QHBoxLayout;
    listHLt->addWidget( m_fieldsListView );
    listHLt->addLayout( upDownLt );
    fieldsGroup->setLayout( listHLt );    

    QPushButton* addBtn = new QPushButton( QPixmap(":/images/add.png"), tr("Add") );
    addBtn->setToolTip(tr("Add new field"));
    QPushButton* removeBtn = new QPushButton( QPixmap(":/images/delete.png"), tr("Remove") );
    removeBtn->setToolTip(tr("Remove field(s)"));
    QPushButton* editBtn = new QPushButton( QPixmap(":/images/edit.png"), tr("Rename") );
    editBtn->setToolTip(tr("Rename field"));
    QHBoxLayout* btnLt = new QHBoxLayout;
    btnLt->addWidget( addBtn );
    btnLt->addWidget( removeBtn );
    btnLt->addWidget( editBtn );

    connect( addBtn, SIGNAL(clicked()), this, SLOT(addRow()) );
    connect( removeBtn, SIGNAL(clicked()), this, SLOT(removeRows()) );
    connect( editBtn, SIGNAL(clicked()), this, SLOT(editField()) );

    m_fieldsLt = new QVBoxLayout;
    m_fieldsLt->addWidget( fieldsGroup );
    m_fieldsLt->addLayout( btnLt );
    }

void FieldsPage::createFieldsPreview()
    {
    FieldsPreviewModel* fieldsPreviewModel = new FieldsPreviewModel( m_parent );
    m_fieldsPreview = new QListView;
    m_fieldsPreview->setModel( fieldsPreviewModel );

    connect( m_parent->m_fieldsListModel,
            SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
            m_fieldsPreview, SLOT(reset()) );
    connect( m_parent->m_fieldsListModel,
            SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
            m_fieldsPreview, SLOT(reset()) );
    connect( m_parent->m_fieldsListModel,
            SIGNAL(rowsInserted(const QModelIndex&, int, int)),
            m_fieldsPreview, SLOT(reset()) );

    QHBoxLayout* previewLt = new QHBoxLayout;
    previewLt->addWidget( m_fieldsPreview );
    m_previewGroup = new QGroupBox(tr("Preview"));
    m_previewGroup->setLayout( previewLt );
    }

void FieldsPage::moveItemsUpDown()
    {
    int direction;
    if( sender()->objectName() == "up" )
        direction = -1;
    else
        direction = 1;
    QModelIndexList selectedIndexes = m_fieldsListView->selectionModel()->selectedIndexes();
    m_fieldsListView->model()->moveIndexesUpDown( selectedIndexes,  direction );
    }

void FieldsPage::addRow()
    {
    QModelIndexList selectedIndexes = m_fieldsListView->selectionModel()->selectedRows();
    int row;
    if( selectedIndexes.size() > 0 )
        {
        qSort(selectedIndexes);
        row = selectedIndexes[selectedIndexes.size()-1].row() + 1;
        }
    else
        row = 0;
    m_fieldsListView->model()->insertRow( row, QModelIndex() );
    }

void FieldsPage::removeRows()
    {
    QModelIndexList selectedIndexes = m_fieldsListView->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_fieldsListView->model()->removeField( idx.row() );
    }

void FieldsPage::editField()
    {
    QModelIndex idx = m_fieldsListView->currentIndex();
    if( !idx.isValid() )
        return;
    m_fieldsListView->edit( idx );
    }