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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
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 <QSortFilterProxyModel>
#include <QComboBox>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QStringListModel>
#include <QMessageBox>
#include <QSettings>
#include <QHideEvent>
#include <QAction>
#include <QMenu>
#include <algorithm>
#include "mappingtreewidget.h"
#include "mappingtreemodel.h"
#include "smglobals.h"
MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){
//setup model
mModel = static_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree"));
mProxy = new QSortFilterProxyModel(this);
mProxy->setSourceModel(mModel);
mTypesModel = new QStringListModel(this);
mTypesModel->setStringList(mModel->mappingTypeNames());
//setup gui
mTree = new MappingTreeView;
mTree->setModel(mProxy);
mTree->setColumnHidden(1, true);
mTree->setColumnHidden(2, true);
mTree->setAlternatingRowColors(true);
mTree->expandAll();
connect(mModel, SIGNAL(needExpansion()), mTree, SLOT(expandAll()));
connect(mTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged()));
mTypeBox = new QComboBox;
mTypeBox->setModel(mTypesModel);
connect(mTypeBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(typeChanged(QString)));
mAddType = new QPushButton(tr("Add &type"));
connect(mAddType, SIGNAL(clicked()), this, SLOT(addType()));
mDeleteType = new QPushButton(tr("Delete type"));
connect(mDeleteType, SIGNAL(clicked()), this, SLOT(deleteType()));
mEdit = new QLineEdit;
mAddChild = new QPushButton(tr("&Add"));
connect(mAddChild, SIGNAL(clicked()), this, SLOT(addChild()));
mDeleteChild = new QPushButton(tr("Delete"));
connect(mDeleteChild, SIGNAL(clicked()), this, SLOT(deleteChild()));
QHBoxLayout *typesButtonLayout = new QHBoxLayout;
typesButtonLayout->addWidget(mDeleteType);
typesButtonLayout->addWidget(mAddType);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(mDeleteChild);
buttonLayout->addWidget(mAddChild);
//select type and populate model
QSettings s;
QString lastType = s.value("ui/mappingtreetype").toString();
if(lastType.isEmpty()){
if(!mTypesModel->stringList().isEmpty()){
lastType = mTypesModel->stringList().at(0);
}
}
int typeId = mModel->mappingTypeIdFromName(lastType);
if(typeId != -1){
mModel->setType(typeId);
mTypeBox->setCurrentIndex(mTypeBox->findText(lastType));
typeChanged(lastType);
}
//setup actions
mDeleteChildA = new QAction(tr("Delete"), this);
connect(mDeleteChildA, SIGNAL(triggered()), this, SLOT(deleteChild()));
mTree->addAction(mDeleteChildA);
mEditChildA = new QAction(tr("Edit..."), this);
connect(mEditChildA, SIGNAL(triggered()), this, SLOT(editChild()));
mTree->addAction(mEditChildA);
//widget layout and tab order
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mTypeBox);
mainLayout->addLayout(typesButtonLayout);
mainLayout->addWidget(mTree);
mainLayout->addWidget(mEdit);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
mEdit->setFocus();
setTabOrder(mEdit, mAddChild);
}
MappingData MappingTreeWidget::selectedItem() const {
MappingData retval;
QModelIndex sel = selected();
if(!sel.isValid()){
retval.id = -1;
}else{
QModelIndex real = mProxy->mapToSource(sel);
if(mModel->childCount(real)){
retval.id = -1;
}else{
retval.id = sel.data(MappingTreeModel::IdRole).toInt();
}
retval.name = sel.data(MappingTreeModel::NameRole).toString();
QStringList p;
QModelIndex parent = sel;
while(parent.isValid()){
p << parent.data().toString();
parent = parent.parent();
}
std::reverse(p.begin(), p.end());
retval.path = p;
}
return retval;
}
void MappingTreeWidget::addChild(){
QModelIndex sel = selected();
if(!sel.isValid()){
QMessageBox::critical(this, tr("Error"), tr("No parent item selected!"));
return;
}
QString value = mEdit->text().toLower().trimmed();
if(value.isEmpty()){
return;
}
QModelIndex real = mProxy->mapToSource(sel);
mModel->addChild(value, real);
}
void MappingTreeWidget::addType(){
QString typeName = QInputDialog::getText(this, tr("Enter type name"), tr("Name")).toLower().trimmed();
if(typeName.isEmpty()){
return;
}
if(mModel->addMappingType(typeName)){
mTypesModel->setStringList(mModel->mappingTypeNames());
int idxOf = mTypeBox->findText(typeName);
if(idxOf != -1){
mTypeBox->setCurrentIndex(idxOf);
}
}
}
void MappingTreeWidget::deleteChild(){
QModelIndex sel = selected();
if(!sel.isValid()){
QMessageBox::critical(this, tr("Error"), tr("No item selected!"));
return;
}
QModelIndex real = mProxy->mapToSource(sel);
if(mModel->rowCount(real) > 0){
QString msg = QString(tr("Cannot delete item %1, because it has %2 children!")).arg(real.data().toString()).arg(QString::number(mModel->rowCount(real)));
QMessageBox::critical(this, tr("Error"), msg);
return;
}
mModel->deleteChild(real);
}
void MappingTreeWidget::deleteType(){
QString curText = mTypeBox->currentText();
if(curText.isEmpty()){
return;
}
QString msg = QString(tr("Really delete %1?")).arg(curText);
int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if(retval == QMessageBox::Yes){
int typeId = mModel->mappingTypeIdFromName(curText);
if(typeId != -1){
if(mModel->deleteMappingType(typeId)){
mTypesModel->setStringList(mModel->mappingTypeNames());
if(mTypesModel->stringList().count() > 0){
mTypeBox->setCurrentIndex(0);
}
}
}
}
}
void MappingTreeWidget::hideEvent(QHideEvent *event){
QString type = mTypeBox->currentText();
if(!type.isEmpty()){
QSettings s;
s.setValue("ui/mappingtreetype", type);
}
event->accept();
}
void MappingTreeWidget::typeChanged(QString type){
int typeId = mModel->mappingTypeIdFromName(type);
if(typeId != -1){
mModel->setType(typeId);
mModel->populate();
mTree->expandAll();
}
}
void MappingTreeWidget::editChild(){
QModelIndex sel = selected();
if(sel.isValid()){
mTree->edit(sel);
}
}
void MappingTreeWidget::selectionChanged(){
QModelIndex sel = selected();
QModelIndex real = mProxy->mapToSource(sel);
emit mappingChanged(real.data(MappingTreeModel::IdRole).toInt());
}
const QModelIndex MappingTreeWidget::selected() const{
QModelIndexList sel = mTree->selectionModel()->selectedRows();
if(sel.isEmpty()){
return QModelIndex();
}
return sel.first();
}
MappingTreeView::MappingTreeView(QWidget *parent) : QTreeView(parent) {}
void MappingTreeView::contextMenuEvent(QContextMenuEvent *e){
QMenu ctxMenu(this);
ctxMenu.addActions(actions());
ctxMenu.exec(e->globalPos());
}
|