aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/numeric_value_chooser_delegate.cpp
blob: d4a10cf8f80027b7e629fea5d08169de8884ad15 (plain)
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
/* numeric_value_chooser_delegate.cpp
 * Delegate to select a numeric value for a treeview entry
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+*/

#include "config.h"

#include <ui/qt/models/numeric_value_chooser_delegate.h>

#include <QStyledItemDelegate>
#include <QSpinBox>

NumericValueChooserDelegate::NumericValueChooserDelegate(int min, int max, QObject *parent)
    : QStyledItemDelegate(parent)
{
    _min = min;
    _max = max;
    _default = min;
}

NumericValueChooserDelegate::~NumericValueChooserDelegate()
{
}

void NumericValueChooserDelegate::setMinMaxRange(int min, int max)
{
    _min = qMin(min, max);
    _max = qMax(min, max);
    /* ensure, that the default value is within the new min<->max */
    _default = qMin(_max, qMax(_min, _default));
    _defReturn = QVariant::fromValue(_default);
}

void NumericValueChooserDelegate::setDefaultValue(int defValue, QVariant defaultReturn)
{
    /* ensure, that the new default value is within min<->max */
    _default = qMin(_max, qMax(_min, defValue));
    _defReturn = defaultReturn;
}

QWidget* NumericValueChooserDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!index.isValid()) {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }

    QSpinBox * editor = new QSpinBox(parent);
    editor->setMinimum(_min);
    editor->setMaximum(_max);
    editor->setWrapping(true);

    connect(editor, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged(int)));

    return editor;
}

void NumericValueChooserDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if ( index.isValid() )
    {
        bool canConvert = false;
        int val = index.data().toInt(&canConvert);
        if ( ! canConvert )
            val = _default;

        QSpinBox * spinBox = qobject_cast<QSpinBox *>(editor);
        spinBox->setValue(val);
    }
    else
        QStyledItemDelegate::setEditorData(editor, index);
}

void NumericValueChooserDelegate::setModelData(QWidget *editor, QAbstractItemModel * model, const QModelIndex &index) const
{
    if ( index.isValid() ) {
        QSpinBox * spinBox = qobject_cast<QSpinBox *>(editor);
        model->setData(index, _default == spinBox->value() ? _defReturn : QVariant::fromValue(spinBox->value()));
    } else {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

void NumericValueChooserDelegate::onValueChanged(int)
{
    QSpinBox * spinBox = qobject_cast<QSpinBox *>(sender());
    emit commitData(spinBox);
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */