comboboxitemdelegate.cpp 1.69 KB
Newer Older
Hugo Hörnquist's avatar
Hugo Hörnquist committed
1
2
3
4
5
6
7
#include "comboboxitemdelegate.h"

#include <QComboBox>
#include <QtCore>

#include <QTableView>

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
8
static QString get_index_string(int);
Hugo Hörnquist's avatar
Hugo Hörnquist committed
9

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
10
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
Hugo Hörnquist's avatar
Hugo Hörnquist committed
11

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
12
// TODO should the destructor actually do anything
Hugo Hörnquist's avatar
Hugo Hörnquist committed
13
14
ComboBoxItemDelegate::~ComboBoxItemDelegate() {}

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
15
// initialises initial data
16
QWidget* ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const
Hugo Hörnquist's avatar
Hugo Hörnquist committed
17
18
19
{
    QComboBox* cb = new QComboBox(parent);
    for (int i = 0; i < 3; i++)
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
20
        cb->addItem(get_index_string(i));
Hugo Hörnquist's avatar
Hugo Hörnquist committed
21
22
23
    return cb;
}

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
24
// Sets data when clicked
Hugo Hörnquist's avatar
Hugo Hörnquist committed
25
26
27
void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (QComboBox* cb = qobject_cast<QComboBox*>(editor)) {
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
28
29
        cb->setCurrentIndex(index.data().toInt());
    } else
Hugo Hörnquist's avatar
Hugo Hörnquist committed
30
31
32
        QStyledItemDelegate::setEditorData(editor, index);
}

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
33
// sets return data
Hugo Hörnquist's avatar
Hugo Hörnquist committed
34
35
36
37
38
39
40
41
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
        model->setData(index, cb->currentIndex(), Qt::EditRole);
    else
        QStyledItemDelegate::setModelData(editor, model, index);
}

Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
42
// sets what's displayed when not in edit mode
43
QString ComboBoxItemDelegate::displayText(const QVariant &value, const QLocale& /*locale*/) const
Hugo Hörnquist's avatar
Hugo Hörnquist committed
44
{
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
45
46
47
48
49
50
    return get_index_string(value.toInt());
}

static QString get_index_string(int index) {
    switch (index) {
        case 0:
51
            return "Till salu";
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
52
        case 1:
53
            return "Ej till salu";
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
54
        case 2:
55
            return "Dold";
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
56
        default:
57
            return "ERROR";
Hugo Hörnquist's avatar
work    
Hugo Hörnquist committed
58
    }
Hugo Hörnquist's avatar
Hugo Hörnquist committed
59
}