From d54f8bc5effe3c008ff39f0e55bf1b71e08c19af Mon Sep 17 00:00:00 2001 From: Arno Date: Thu, 19 Jul 2018 18:21:11 +0200 Subject: Just code shuffle, no functional change Split up all the classes in mappingtreewidget into separate files to make editing easier. --- mappingtreewidget.cpp | 242 +------------------------------------------------- 1 file changed, 2 insertions(+), 240 deletions(-) (limited to 'mappingtreewidget.cpp') diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp index cbd24b1..7a5675a 100644 --- a/mappingtreewidget.cpp +++ b/mappingtreewidget.cpp @@ -26,6 +26,8 @@ #include "mappingtreewidget.h" #include "mappingtreemodel.h" #include "mappingtreeview.h" +#include "mappingtreeproxy.h" +#include "mappinginputdialog.h" #include "smglobals.h" MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){ @@ -325,243 +327,3 @@ const QModelIndex MappingTreeWidget::selected() const{ } return sel.first(); } - - - - -MappingTreeResultView::MappingTreeResultView(QWidget *parent) : SmTreeView(parent) { - setAlternatingRowColors(true); - setPalette(qApp->palette()); -} - -void MappingTreeResultView::keyPressEvent(QKeyEvent *e){ - if(e->key() == Qt::Key_Left && (e->modifiers() & Qt::ControlModifier)){ - clearFocus(); - emit shiftFocus(); - return; - } - if(e->key() == Qt::Key_Left){ - emit removeMapping(); - } - SmTreeView::keyPressEvent(e); -} - -MappingEditWidget::MappingEditWidget(QWidget *parent) : QWidget(parent){ - //the views - mMappingTree = new MappingTreeWidget; - mMappingResult = new MappingTreeResultView; - mResultModel = new MappingTreeResultModel(QStringList() << tr("Name") << tr("MappingId") << tr("ParentId") << tr("MyId"), this); - mMappingResult->setModel(mResultModel); - mMappingResult->setAlternatingRowColors(true); - mMappingResult->setColumnHidden(1, true); - mMappingResult->setColumnHidden(2, true); - mMappingResult->setColumnHidden(3, true); - connect(mMappingTree->mappingTreeView(), &MappingTreeView::addMapping, this, &MappingEditWidget::addMapping); - connect(mMappingTree->mappingTreeView(), &MappingTreeView::clearMappings, this, &MappingEditWidget::clearMapping); - connect(mMappingTree->mappingTreeView(), &MappingTreeView::shiftFocus, this, &MappingEditWidget::shiftFocusResult); - connect(mMappingResult, &MappingTreeResultView::shiftFocus, this, &MappingEditWidget::shiftFocusMappings); - connect(mMappingResult, &MappingTreeResultView::removeMapping, this, &MappingEditWidget::removeMapping); - connect(mMappingResult->selectionModel(), &QItemSelectionModel::currentChanged, this, &MappingEditWidget::resultSelectionChanged); - - //buttons - mAddMapping = new QPushButton(tr(">>")); - connect(mAddMapping, &QPushButton::clicked, this, &MappingEditWidget::addMapping); - mRemoveMapping = new QPushButton(tr("<<")); - connect(mRemoveMapping, &QPushButton::clicked, this, &MappingEditWidget::removeMapping); - mClearMapping = new QPushButton(tr("&Clear")); - connect(mClearMapping, &QPushButton::clicked, this, &MappingEditWidget::clearMapping); - mAddTree = new QPushButton(tr(">>>>")); - connect(mAddTree, &QPushButton::clicked, this, &MappingEditWidget::addTree); - - //layout - QHBoxLayout *mainLayout = new QHBoxLayout; - mainLayout->addWidget(mMappingTree); - QVBoxLayout *buttonLayout = new QVBoxLayout; - buttonLayout->addStretch(); - buttonLayout->addWidget(mAddMapping); - buttonLayout->addWidget(mRemoveMapping); - buttonLayout->addWidget(mClearMapping); - buttonLayout->addWidget(mAddTree); - buttonLayout->addStretch(); - mainLayout->addLayout(buttonLayout); - mainLayout->addWidget(mMappingResult); - setLayout(mainLayout); - mMappingTree->mappingTreeView()->setFocus(); -} - -void MappingEditWidget::addMapping(){ - MappingData selected = mMappingTree->selectedItem(); - mResultModel->addItem(selected); - mMappingResult->expandAll(); - mMappingTree->mappingTreeView()->setFocus(); -} - -void MappingEditWidget::addTree(){ - QList retval = mMappingTree->selectedTree(); - for(const MappingData &md : retval){ - mResultModel->addItem(md); - } - mMappingResult->expandAll(); - mMappingTree->mappingTreeView()->setFocus(); -} - -void MappingEditWidget::removeMapping(){ - QModelIndexList sel = mMappingResult->selectionModel()->selectedRows(); - if(sel.isEmpty()){ - return; - } - QModelIndex firstIdx = sel.first(); - if(firstIdx.isValid()){ - mResultModel->removeItem(firstIdx); - } - mMappingResult->expandAll(); - mMappingTree->mappingTreeView()->setFocus(); -} - -void MappingEditWidget::clearMapping(){ - mResultModel->clearData(); - mMappingTree->mappingTreeView()->setFocus(); -} - -void MappingEditWidget::setMappings(const QList &mappings){ - if(mappings.isEmpty()){ - return; - } - mResultModel->clearData(); - for(const MappingData &d : mappings){ - mResultModel->addItem(d); - } - mMappingResult->expandAll(); -} - -void MappingEditWidget::expandAllResults(){ - mMappingResult->expandAll(); -} - -void MappingEditWidget::shiftFocusResult(){ - mMappingResult->setFocus(); -} - -void MappingEditWidget::shiftFocusMappings(){ - mMappingTree->mappingTreeView()->setFocus(); -} - -void MappingEditWidget::saveMappings(QString where){ - const QList mappingData = model()->mappingData(); - QByteArray saveVal; - QDataStream in(&saveVal, QIODevice::WriteOnly); - for(const MappingData &md : mappingData){ - in << md; - } - QSettings s; - s.setValue(where, saveVal); -} - -void MappingEditWidget::loadMappings(QString from){ - QSettings s; - QByteArray val = s.value(from).toByteArray(); - QDataStream out(&val, QIODevice::ReadOnly); - MappingData md; - while(!out.atEnd()){ - out >> md; - model()->addItem(md); - } - expandAllResults(); -} - -void MappingEditWidget::resultSelectionChanged(const QModelIndex ¤t, const QModelIndex &previous){ - Q_UNUSED(previous) - QStringList path; - QModelIndex c = current; - // gather elements from leaf to root - while(c != QModelIndex()){ - path << c.data().toString(); - c = c.parent(); - } - std::reverse(path.begin(), path.end()); - MappingTreeModel *srcModel = mMappingTree->mappingTreeModel(); - QModelIndex srcIdx = srcModel->rootIndex(); - // now search the source tree starting at the top, - // that's why we reversed the QStringList above - for(const QString &p : path){ - srcIdx = srcModel->find(p, 0, srcIdx); - if(!srcIdx.isValid()){ - return; - } - } - // yes, we have a valid index. Map it to the Proxy... - QModelIndex real = mMappingTree->mappingTreeProxy()->mapFromSource(srcIdx); - // select it and make sure it's visible! - mMappingTree->mappingTreeView()->selectionModel()->select(real, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); - mMappingTree->mappingTreeView()->scrollTo(real, QAbstractItemView::PositionAtCenter); -} - -MappingInputDialog::MappingInputDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){ - mOk = new QPushButton(tr("Ok")); - connect(mOk, &QPushButton::clicked, this, &MappingInputDialog::accept); - mCancel = new QPushButton(tr("Cancel")); - connect(mCancel, &QPushButton::clicked, this, &MappingInputDialog::reject); - mIsRoot = new QCheckBox(tr("Create root item")); - mEditor = new QLineEdit; - - QHBoxLayout *buttonLayout = new QHBoxLayout; - buttonLayout->addStretch(); - buttonLayout->addWidget(mOk); - buttonLayout->addWidget(mCancel); - - QVBoxLayout* inputLayout = new QVBoxLayout; - QLabel *caption = new QLabel(tr("Enter mapping name")); - inputLayout->addWidget(caption); - inputLayout->addWidget(mEditor); - - QHBoxLayout *cbLayout = new QHBoxLayout; - cbLayout->addStretch(); - cbLayout->addWidget(mIsRoot); - - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addLayout(inputLayout); - mainLayout->addLayout(cbLayout); - mainLayout->addLayout(buttonLayout); - setLayout(mainLayout); -} - -QString MappingInputDialog::mappingName() const{ - return mEditor->text(); -} - -bool MappingInputDialog::createRoot() const { - return mIsRoot->checkState() == Qt::Checked; -} - -MappingEditDialog::MappingEditDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){ - mEditWidget = new MappingEditWidget; - mOk = new QPushButton(tr("Ok")); - connect(mOk, &QPushButton::clicked, this, &MappingEditDialog::accept); - mCancel = new QPushButton(tr("Cancel")); - connect(mCancel, &QPushButton::clicked, this, &MappingEditDialog::reject); - - QHBoxLayout *buttonLayout = new QHBoxLayout; - buttonLayout->addStretch(); - buttonLayout->addWidget(mOk); - buttonLayout->addWidget(mCancel); - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addWidget(mEditWidget); - mainLayout->addLayout(buttonLayout); - setLayout(mainLayout); -} - -MappingTreeProxy::MappingTreeProxy(QObject *parent) : QSortFilterProxyModel(parent) {} - -void MappingTreeProxy::setFilter(const QString &filter){ - mFilter = QRegExp(filter); - invalidateFilter(); -} - -bool MappingTreeProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const{ - if(mFilter.isEmpty()){ - return true; - } - QModelIndex nameIdx = sourceModel()->index(sourceRow, MappingTreeModel::Name, sourceParent); - MappingTreeModel *model = qobject_cast(sourceModel()); - return model->matchRecursive(nameIdx, mFilter); -} -- cgit v1.2.3-70-g09d2