#include "FieldsPage.h" #include "../dictionary/Dictionary.h" #include "../dictionary/CardPack.h" #include "FieldsListModel.h" #include "FieldsPreviewModel.h" #include #include #include #include #include #include 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 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 ); }