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
235
236
|
/*
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 <QSqlDatabase>
#include <QSqlQuery>
#include <QContextMenuEvent>
#include <QMenu>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QAction>
#include <QSortFilterProxyModel>
#include "mappingtableeditor.h"
//widget
MappingTableEditor::MappingTableEditor(const QString &table, QWidget *parent) : SmDialog(parent), mTable(table){
//caption
QString l1String = QString(tr("Edit %1s").arg(mTable));
QLabel *l1 = new QLabel(l1String);
//model + view
QStringList headers = QStringList() << QString("%1s").arg(mTable) << tr("Count");
mModel = new MappingTableEditorModel(table, headers, this);
mView = new MappingTableEditorView;
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
proxy->setSourceModel(mModel);
mView->setModel(proxy);
mView->setRootIsDecorated(false);
mView->setColumnHidden(2, true);
mView->resizeColumnToContents(0);
mView->setSortingEnabled(true);
mView->setAlternatingRowColors(true);
mView->sortByColumn(0, Qt::AscendingOrder);
mView->setSelectionMode(QAbstractItemView::SingleSelection);
//edit
QHBoxLayout *editLayout = new QHBoxLayout;
QLabel *l2 = new QLabel(tr("Item"));
editLayout->addWidget(l2);
mDataEdit = new QLineEdit;
editLayout->addWidget(mDataEdit);
//buttons
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->setAlignment(Qt::AlignRight);
mClose = new QPushButton(tr("Close"));
mClose->setDefault(true);
mRename = new QPushButton(tr("Rename"));
mDelete = new QPushButton(tr("Delete"));
buttonLayout->addWidget(mDelete);
buttonLayout->addWidget(mRename);
buttonLayout->addWidget(mClose);
//mainlayout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(l1);
mainLayout->addWidget(mView);
mainLayout->addLayout(editLayout);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
//actions
mDeleteA = new QAction(tr("Delete"), this);
mView->addAction(mDeleteA);
mEditA = new QAction(tr("Edit..."), this);
mView->addAction(mEditA);
//connections
connect(mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(selectionChanged(QModelIndex,QModelIndex)));
connect(mClose, SIGNAL(clicked()), this, SLOT(accept()));
connect(mDelete, SIGNAL(clicked()), this, SLOT(removeItem()));
connect(mRename, SIGNAL(clicked()), this, SLOT(renameItem()));
connect(mDeleteA, SIGNAL(triggered()), this, SLOT(removeItem()));
connect(mEditA, SIGNAL(triggered()), this, SLOT(editItem()));
}
void MappingTableEditor::selectionChanged(const QModelIndex &cur, const QModelIndex &prev){
Q_UNUSED(prev);
mDataEdit->setText(cur.data(MappingTableEditorModel::NameRole).toString());
}
void MappingTableEditor::removeItem(){
QModelIndexList selected = mView->selectionModel()->selectedRows();
if(selected.isEmpty()){
return;
}
int count = selected.at(0).data(MappingTableEditorModel::CountRole).toInt();
if(count > 0){
QString warning = QString(tr("Really remove %1 %2? There are %3 items associated with it.")).arg(mTable).arg(selected.at(0).data(MappingTableEditorModel::NameRole).toString()).arg(QString::number(count));
int mbRetval = QMessageBox::warning(this, tr("Warning!"), warning, QMessageBox::Yes | QMessageBox::No);
if(mbRetval == QMessageBox::No){
return;
}
}
mModel->removeData(selected.at(0));
}
void MappingTableEditor::renameItem(){
QModelIndexList selected = mView->selectionModel()->selectedRows();
if(selected.isEmpty()){
return;
}
QString newValue = mDataEdit->text().toLower().trimmed();
QModelIndex newIdx = mModel->find(newValue, MappingTableEditorModel::Name);
if(newIdx.isValid()){
QString warning = QString(tr("New value %1 already exists. Merge?")).arg(newValue);
int mbRetval = QMessageBox::warning(this, tr("Warning!"), warning, QMessageBox::Yes | QMessageBox::No);
if(mbRetval == QMessageBox::No){
return;
}
}
mModel->setData(selected.at(0), newValue, Qt::EditRole);
}
void MappingTableEditor::editItem(){
QModelIndexList selected = mView->selectionModel()->selectedRows();
if(selected.isEmpty()){
return;
}
mView->edit(selected.at(0));
}
//view
MappingTableEditorView::MappingTableEditorView(QWidget *parent) : QTreeView(parent) {}
void MappingTableEditorView::contextMenuEvent(QContextMenuEvent *e){
QMenu contextMenu(this);
contextMenu.addActions(actions());
contextMenu.exec(e->globalPos());
}
//model
MappingTableEditorModel::MappingTableEditorModel(const QString &table, const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mTable(table){
mDb = QSqlDatabase::database("treedb");
QString queryString = QString("SELECT t%1name, COUNT(iseriesparts_id), %1s.i%1s_id from %1s, seriesparts_%1map WHERE %1s.i%1s_id = seriesparts_%1map.i%1s_id GROUP BY %1s.t%1name, %1s.i%1s_id ORDER BY t%1name").arg(mTable);
mDataQuery = new QSqlQuery(mDb);
mDataQuery->prepare(queryString);
QString mergeQuery = QString("UPDATE %1s SET %1s_id = %2 WHERE %1s_id = %3");
populate();
}
MappingTableEditorModel::~MappingTableEditorModel(){
delete mDataQuery;
mDb = QSqlDatabase();
}
Qt::ItemFlags MappingTableEditorModel::flags(const QModelIndex &index) const{
if(!index.isValid()){
return 0;
}
Qt::ItemFlags retval = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if(index.column() == 0){
retval |= Qt::ItemIsEditable;
}
return retval;
}
QVariant MappingTableEditorModel::data(const QModelIndex &index, int role) const{
if(!index.isValid()){
return QVariant();
}
SmTreeItem *item = static_cast<SmTreeItem*>(index.internalPointer());
if(role == NameRole){
return item->data(Name);
}
if(role == CountRole){
return item->data(Count);
}
if(role == IdRole){
return item->data(Id);
}
return SmTreeModel::data(index, role);
}
bool MappingTableEditorModel::setData(const QModelIndex &idx, const QVariant &value, int role){
if(!idx.isValid()){
return false;
}
if(role != Qt::EditRole){
return false;
}
bool retval = false;
QModelIndex newValue = find(value);
if(newValue.isValid()){
QSqlQuery mergeQuery(mDb);
QString query = QString("UPDATE %1s SET %1s_id = :newid WHERE %1s_id = :oldid").arg(mTable);
mergeQuery.prepare(query);
mergeQuery.bindValue(":newid", newValue.data(IdRole));
mergeQuery.bindValue(":oldid", idx.data(IdRole));
retval = mergeQuery.exec();
}else{
QSqlQuery renameQuery(mDb);
QString query = QString("UPDATE %1s SET t%1name = :name WHERE i%1s_id = :id").arg(mTable);
renameQuery.prepare(query);
renameQuery.bindValue(":name", value);
renameQuery.bindValue(":id", idx.data(IdRole));
retval = renameQuery.exec();
}
if(retval){
populate();
}
return retval;
}
bool MappingTableEditorModel::removeData(const QModelIndex &idx){
if(!idx.isValid()){
return false;
}
QString removeQuery = QString("DELETE FROM %1s WHERE i%1s_id = %2").arg(mTable).arg(QString::number(idx.data(IdRole).toInt()));
QSqlQuery remove(removeQuery, mDb);
bool retval = remove.exec();
if(retval){
populate();
}
return retval;
}
void MappingTableEditorModel::populate(){
SmTreeItem *rootItem = new SmTreeItem(3);
mDataQuery->exec();
while(mDataQuery->next()){
QList<QVariant> data;
data << mDataQuery->value(0) << mDataQuery->value(1) << mDataQuery->value(2);
SmTreeItem *childItem = new SmTreeItem(data, rootItem);
rootItem->appendChild(childItem);
}
setRoot(rootItem);
}
|