diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
commit | d24f813f3f2a05c112e803e4256b53535895fc98 (patch) | |
tree | 601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/dic-options/FieldsPage.cpp |
Diffstat (limited to 'src/dic-options/FieldsPage.cpp')
-rw-r--r-- | src/dic-options/FieldsPage.cpp | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/dic-options/FieldsPage.cpp b/src/dic-options/FieldsPage.cpp new file mode 100644 index 0000000..ea9a741 --- /dev/null +++ b/src/dic-options/FieldsPage.cpp @@ -0,0 +1,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 ); + } |