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
|
/*
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 <QIcon>
#include <QTreeView>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QCompleter>
#include <QGroupBox>
#include <QSettings>
#include "mappingtablewidget.h"
#include "mappingtablemodel.h"
#include "smglobals.h"
MappingTableWidget::MappingTableWidget(const QString &table, QWidget *parent) : QWidget(parent), mTable(table), mCurrentId(-1){
//the view
mView = new QTreeView;
mView->setHeaderHidden(true);
mModel = new MappingTableItemModel(this);
mModel->setHeaderData(0, Qt::Horizontal, mTable);
mView->setModel(mModel);
mView->setAlternatingRowColors(true);
//mapping model
mMappingModel = static_cast<MappingTableModel*>(SmGlobals::instance()->model(mTable));
//editor
QHBoxLayout *itemEditLayout = new QHBoxLayout;
QString l1Text = QString(tr("&Edit %1")).arg(mTable);
QLabel *l1 = new QLabel(l1Text);
mItemEdit = new QLineEdit;
QCompleter *completer = new QCompleter(this);
completer->setModel(mMappingModel);
mItemEdit->setCompleter(completer);
l1->setBuddy(mItemEdit);
itemEditLayout->addWidget(l1);
itemEditLayout->addWidget(mItemEdit);
//buttons
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
mAddItem = new QPushButton(tr("&Add item"));
mRemoveItem = new QPushButton(tr("Remove item"));
buttonLayout->addWidget(mAddItem);
buttonLayout->addWidget(mRemoveItem);
connect(mAddItem, SIGNAL(clicked()), this, SLOT(addItem()));
connect(mRemoveItem, SIGNAL(clicked()), this, SLOT(removeItem()));
//layout
QVBoxLayout *groupBoxLayout = new QVBoxLayout;
groupBoxLayout->addWidget(mView);
groupBoxLayout->addLayout(itemEditLayout);
groupBoxLayout->addLayout(buttonLayout);
QString gbCaption = QString(tr("Edit %1")).arg(mTable);
QGroupBox *gb = new QGroupBox(gbCaption);
gb->setLayout(groupBoxLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(gb);
setLayout(mainLayout);
setContentsMargins(0, 0, 0, 0);
}
void MappingTableWidget::setStringList(const QStringList &list){
mModel->setStringList(list);
}
void MappingTableWidget::setEditEnabled(bool enabled){
mAddItem->setEnabled(enabled);
mRemoveItem->setEnabled(enabled);
mItemEdit->setEnabled(enabled);
}
void MappingTableWidget::addItem(){
QString value = mItemEdit->text().toLower().trimmed();
if(mModel->stringList().contains(value)){
return;
}
if(!mMappingModel->contains(value)){
mMappingModel->addItem(value);
}
int row = mModel->lowerBound(value);
if(mModel->insertRows(row, 1)){
QModelIndex idx = mModel->index(row, 0);
mModel->setData(idx, value);
QModelIndex mapIndex = mMappingModel->find(value, 0, QModelIndex());
if(mapIndex.isValid()){
int id = mapIndex.data(MappingTableModel::ItemIdRole).toInt();
mMappingModel->addMapping(mCurrentId, id);
}
}
mItemEdit->clear();
}
void MappingTableWidget::removeItem(){
QModelIndexList selected = mView->selectionModel()->selectedRows();
if(selected.isEmpty()){
return;
}
foreach(QPersistentModelIndex i, selected){
QString item = i.data().toString();
mModel->removeRows(i.row(), 1);
QModelIndex itemIdx = mMappingModel->find(item);
if(itemIdx.isValid()){
mMappingModel->removeMapping(mCurrentId, itemIdx.data(MappingTableModel::ItemIdRole).toInt());
}
}
}
MappingTableItemModel::MappingTableItemModel(QObject *parent) : QStringListModel(parent){
QSettings s;
QString iconName = s.value("ui/folderIcon").toString();
mDecorationIcon = QIcon(SmGlobals::instance()->icons().value(iconName));
}
QVariant MappingTableItemModel::data(const QModelIndex &index, int role) const{
if(role == Qt::DecorationRole){
return decorationIcon();
}
return QStringListModel::data(index, role);
}
Qt::ItemFlags MappingTableItemModel::flags(const QModelIndex &index) const{
Q_UNUSED(index);
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
int MappingTableItemModel::lowerBound(const QString &value) const{
int retval = 0;
for(int i = 0; i < stringList().count(); ++i){
if(stringList().at(i) < value){
++retval;
}else{
break;
}
}
return retval;
}
QModelIndex MappingTableItemModel::find(const QString &value) const{
int row = stringList().indexOf(value);
if(row == -1){
return QModelIndex();
}
return index(row, 0);
}
|