summaryrefslogtreecommitdiffstats
path: root/mappingeditwidget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mappingeditwidget.cpp')
-rw-r--r--mappingeditwidget.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/mappingeditwidget.cpp b/mappingeditwidget.cpp
new file mode 100644
index 0000000..9ae13d5
--- /dev/null
+++ b/mappingeditwidget.cpp
@@ -0,0 +1,167 @@
+/*
+ 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 <QPushButton>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
+#include <QSettings>
+
+#include "mappingeditwidget.h"
+#include "mappingtreewidget.h"
+#include "mappingtreeresultview.h"
+#include "mappingtreeview.h"
+#include "mappingtreeproxy.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<MappingData> 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<MappingData> &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> 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 &current, 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);
+}