summaryrefslogtreecommitdiffstats
path: root/mappingeditwidget.cpp
blob: 6e1a60f148a0a430f19d998b8d322830f9fee817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
  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 "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
    QPushButton *addMappingB = new QPushButton(tr(">>"));
    addMappingB->setFocusPolicy(Qt::ClickFocus);
    connect(addMappingB, &QPushButton::clicked, this, &MappingEditWidget::addMapping);
    QPushButton *removeMappingB = new QPushButton(tr("<<"));
    removeMappingB->setFocusPolicy(Qt::ClickFocus);
    connect(removeMappingB, &QPushButton::clicked, this, &MappingEditWidget::removeMapping);
    QPushButton *clearMappingB = new QPushButton(tr("&Clear"));
    clearMappingB->setFocusPolicy(Qt::ClickFocus);
    connect(clearMappingB, &QPushButton::clicked, this, &MappingEditWidget::clearMapping);
    QPushButton *addTreeB = new QPushButton(tr(">>>>"));
    addTreeB->setFocusPolicy(Qt::ClickFocus);
    connect(addTreeB, &QPushButton::clicked, this, &MappingEditWidget::addTree);
    QPushButton *copyActorB = new QPushButton(tr("Copy"));
    copyActorB->setFocusPolicy(Qt::ClickFocus);
    connect(copyActorB, &QPushButton::clicked, this, &MappingEditWidget::copyActor);

    //layout
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(mMappingTree);
    QVBoxLayout *buttonLayout = new QVBoxLayout;
    buttonLayout->addStretch();
    buttonLayout->addWidget(addMappingB);
    buttonLayout->addWidget(removeMappingB);
    buttonLayout->addWidget(clearMappingB);
    buttonLayout->addWidget(addTreeB);
    buttonLayout->addWidget(copyActorB);
    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()){
        // removing an item from the model inevitably calls resultSelctionChanged
        // check and clear the flag there!
        blockResultChange(true);
        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)
    if(mBlockResultChange){
        blockResultChange(false);
        return;
    }
    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()->clear();
    mMappingTree->mappingTreeView()->selectionModel()->select(real, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
    mMappingTree->mappingTreeView()->selectionModel()->setCurrentIndex(real, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
    mMappingTree->mappingTreeView()->scrollTo(real, QAbstractItemView::PositionAtCenter);
}

void MappingEditWidget::copyActor(){
    MappingData selected = mMappingTree->selectedItem();
    QList<MappingData> data = mMappingTree->mappingTreeModel()->siblingMappingDataFromId(selected.mappingId);
    setMappings(data);
}