-
Notifications
You must be signed in to change notification settings - Fork 1
/
agenttypedelegate.cpp
52 lines (45 loc) · 1.67 KB
/
agenttypedelegate.cpp
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
/*!
* \file agenttypedelegate.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of agent type delegate
*/
#include <QComboBox>
#include <QDebug>
#include "./agenttypedelegate.h"
AgentTypeDelegate::AgentTypeDelegate(QList<AgentType> * ats, QObject * parent)
: QItemDelegate(parent) {
agentTypes = ats;
}
QWidget *AgentTypeDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const {
QComboBox *editor = new QComboBox(parent);
for (int i = 0; i < agentTypes->count(); i++) {
editor->insertItem(i, agentTypes->at(i).name);
// qDebug() << "AgentTypeDelegate::createEditor "
// << agentTypes->at(i).name;
}
// editor->insertItem(agentTypes->count(), "add...");
return editor;
}
void AgentTypeDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {
QString value = index.data().toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
for (int i = 0; i < comboBox->count(); i++) {
if (comboBox->itemText(i) == value)
comboBox->setCurrentIndex(i);
}
}
void AgentTypeDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model, const QModelIndex &index) const {
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();
model->setData(index, value, Qt::EditRole);
}
void AgentTypeDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const {
editor->setGeometry(option.rect);
}