/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */ #include #include #include #include #include "mappingeditwidget.h" #include "mappingtreewidget.h" #include "mappingtreeresultview.h" #include "mappingtreeresultmodel.h" #include "mappingtreeview.h" #include "mappingtreeproxy.h" #include "mappingdata.h" 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); }