File: kvView.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (253 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download | duplicates (2)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2010 Christian Hohnstaedt.
 *
 * All rights reserved.
 */

#include "lib/func.h"
#include "widgets/kvView.h"
#include <QHeaderView>
#include <QLineEdit>

QWidget *comboDelegate::createEditor(QWidget *parent,
	const QStyleOptionViewItem &,
	const QModelIndex &) const
{
	QComboBox *editor = new QComboBox(parent);
	editor->addItems(keys);
	return editor;
}

void comboDelegate::setEditorData(QWidget *editor,
			const QModelIndex &index) const
{
	QString v = index.model()->data(index, Qt::EditRole).toString();
	QComboBox *c = static_cast<QComboBox*>(editor);
	c->setCurrentIndex(c->findText(v));
}

void comboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
		const QModelIndex &index) const
{
	QComboBox *c = static_cast<QComboBox*>(editor);
	model->setData(index, c->currentText(), Qt::EditRole);
}

QWidget *lineDelegate::createEditor(QWidget *parent,
	const QStyleOptionViewItem &,
	const QModelIndex &) const
{
	return new QLineEdit(parent);
}

void lineDelegate::setEditorData(QWidget *editor,
			const QModelIndex &index) const
{
	QString v, k;

	v = index.model()->data(index, Qt::EditRole).toString();
	QModelIndex key = index.sibling(index.row(), 0);
	QLineEdit *l = static_cast<QLineEdit*>(editor);

	l->setText(v);

	if (key.isValid()) {
		k = key.model()->data(key, Qt::DisplayRole).toString();
		emit setupLineEdit(k, l);
	}
	if (infoLabel)
		infoLabel->setText(k + ": " + l->toolTip());
}

void lineDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
		const QModelIndex &index) const
{
	QLineEdit *l = static_cast<QLineEdit*>(editor);
	model->setData(index, l->text(), Qt::EditRole);
}

kvmodel::kvmodel(const QStringList &heads)
{
	header = heads;
	myCols = heads.size();
}

QStringList kvmodel::getRow(int i)
{
	QStringList sl;
	sl << items[i*myCols] << items[i *myCols +1];
	return sl;
}

void kvmodel::addRow(const QStringList &newrow)
{
	int row = rowCount(QModelIndex());
	beginInsertRows(QModelIndex(), row, row);
	for (int i = 0; i<myCols; i++) {
		if (i >= newrow.size())
			items << QString();
		else
			items << newrow[i].trimmed();
	}
	endInsertRows();
}

QVariant kvmodel::data(const QModelIndex &index, int role) const
{
	int id = index.internalId();
	QString s = items[id];

	switch (role) {
		case Qt::EditRole:
		case Qt::DisplayRole:
			return QVariant(s);
	}
	return QVariant();
}

QVariant kvmodel::headerData(int section, Qt::Orientation orientation,
	int role) const
{
	if (role == Qt::DisplayRole) {
		if (orientation == Qt::Horizontal)
			return QVariant(header[section]);
		if (orientation == Qt::Vertical)
			return QVariant(section);
	}
	return QVariant();
}

bool kvmodel::insertRows(int row, int count, const QModelIndex &)
{
	beginInsertRows(QModelIndex(), row, row+count-1);
	for (int i=0; i< count *myCols; i++) {
		items.insert(row*myCols, QString());
	}
	endInsertRows();
	return true;
}

bool kvmodel::removeRows(int row, int count, const QModelIndex &)
{
	beginRemoveRows(QModelIndex(), row, row+count-1);
	for (int i=0; i< count*myCols; i++) {
		items.removeAt(row*myCols);
	}
	endRemoveRows();
	return true;
}

bool kvmodel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if (index.isValid() && role == Qt::EditRole) {
		items[index.internalId()] = value.toString().trimmed();
		emit dataChanged(index, index);
		return true;
	}
	return false;
}

void kvmodel::moveRow(int oldi, int newi)
{
	QStringList line = items.mid(oldi*myCols, myCols);
	removeRows(oldi, 1);
	insertRows(newi, 1);
	for (int i=0; i<myCols; i++)
		items[newi*myCols +i] = line[i];
}

kvView::kvView(QWidget *parent)
	:QTableView(parent)
{
	QStringList sl;
	sl << tr("Type") << tr("Content");
	initCols(sl);
	setSelectionMode(QAbstractItemView::ExtendedSelection);
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setAlternatingRowColors(true);
	horizontalHeader()->setDefaultSectionSize(200);
	horizontalHeader()->setStretchLastSection(true);
	verticalHeader()->setSectionsMovable(true);
	verticalHeader()->setDefaultSectionSize(24);
	setEditTriggers(QAbstractItemView::AllEditTriggers);

	connect(verticalHeader(), SIGNAL(sectionMoved(int,int,int)),
		this, SLOT(moveRow(int,int,int)));
	infoLabel = NULL;
	initLineDelegate();
}

void kvView::initCols(QStringList &heads)
{
	QAbstractItemModel *m = model();
	setModel(new kvmodel(heads));
	delete m;
}

kvView::~kvView()
{
	delete model();
}

void kvView::initLineDelegate(int col)
{
	lineDelegate *d = new lineDelegate(infoLabel, this);
	setItemDelegateForColumn(col, d);
	connect(static_cast<QItemDelegate*>(d),
	   SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)),
	   this, SLOT(editorExited()));
}

void kvView::setKeys(const QStringList &k, int col)
{
	if (!col)
		keys0 = k;
	comboDelegate *d = new comboDelegate(k, this);
	setItemDelegateForColumn(col, d);
}

void kvView::moveRow(int, int oldi, int newi)
{
	static int moving = 0;

	if (moving)
		return;
	moving = 1;
	verticalHeader()->moveSection(newi, oldi);
	static_cast<kvmodel*>(model())->moveRow(oldi, newi);
	repaint();
	moving = 0;
}

void kvView::addRow(const QStringList &newrow)
{
	int max = MIN(model()->columnCount(QModelIndex()), newrow.size());
	for (int i = 0; i<max; i++) {
		QString key = newrow[i].trimmed();
		static_cast<kvDelegate*>(itemDelegateForColumn(i))->addKey(key);
	}
	static_cast<kvmodel*>(model())->addRow(newrow);
}

void kvView::addKvRow()
{
	QString k;
	if (keys0.count() > 0)
		k = keys0[rowCount() % keys0.count()];
	addRow(QStringList(k));
}

void kvView::deleteCurrentRow()
{
	if (!currentIndex().isValid())
		return;
	model()->removeRows(currentIndex().row(), 1, QModelIndex());
}

void kvView::editorExited()
{
	if (infoLabel)
		infoLabel->clear();
}