aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/info_proxy_model.cpp
blob: 197cc9e0d881ec502896b5e80c24ba856e3231bd (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
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
/* info_proxy_model.cpp
 * Proxy model for displaying an info text at the end of any QAbstractListModel
 *
 * 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/info_proxy_model.h>

#include <QFont>

InfoProxyModel::InfoProxyModel(QObject * parent)
 : QIdentityProxyModel(parent),
 column_(-1)
{
}

InfoProxyModel::~InfoProxyModel()
{
    infos_.clear();
}

void InfoProxyModel::appendInfo(QString info)
{
    if ( ! infos_.contains(info) )
        infos_ << info;
}

void InfoProxyModel::clearInfos()
{
    infos_.clear();
}

int InfoProxyModel::rowCount(const QModelIndex &parent) const
{
    return sourceModel()->rowCount(parent) + infos_.count();
}

QVariant InfoProxyModel::data (const QModelIndex &index, int role) const
{
    if ( ! index.isValid() )
        return QVariant();

    if ( index.row() < sourceModel()->rowCount() )
        return sourceModel()->data(mapToSource(index), role);

    int ifIdx = index.row() - sourceModel()->rowCount();
    if ( index.column() != column_ || ifIdx < 0 || ifIdx >= infos_.count() )
        return QVariant();

    switch ( role )
    {
    case Qt::DisplayRole:
        return infos_.at(ifIdx);
        break;
    case Qt::FontRole:
        QFont font = QIdentityProxyModel::data(index, Qt::FontRole).value<QFont>();
        font.setItalic(true);
        return font;
    }

    return QIdentityProxyModel::data(index, role);
}

Qt::ItemFlags InfoProxyModel::flags(const QModelIndex &index) const
{
    if ( index.row() < sourceModel()->rowCount() )
        return sourceModel()->flags(mapToSource(index));

    return 0;
}

QModelIndex InfoProxyModel::index(int row, int column, const QModelIndex &parent) const
{
    if ( row >= sourceModel()->rowCount() && row < rowCount() )
        return createIndex(row, column);

    return QIdentityProxyModel::index(row, column, parent);
}

QModelIndex InfoProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
    if ( ! proxyIndex.isValid() )
        return QModelIndex();

    if ( proxyIndex.row() >= sourceModel()->rowCount() )
        return QModelIndex();

    return QIdentityProxyModel::mapToSource(proxyIndex);
}

QModelIndex InfoProxyModel::mapFromSource(const QModelIndex &fromIndex) const
{
    return QIdentityProxyModel::mapFromSource(fromIndex);
}

void InfoProxyModel::setColumn(int column)
{
    int old_column = column_;
    column_ = column;

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    QVector<int> roles;
    roles << Qt::DisplayRole;
#endif

    if (old_column >= 0) {
        //Notify old column has changed
        emit dataChanged(index(0, old_column), index(rowCount(), old_column)
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
                         , roles
#endif
        );
    }

    if (column_ >= 0) {
        //Notify new column has changed
        emit dataChanged(index(0, column_), index(rowCount(), column_)
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
                         , roles
#endif
        );
    }
}

/*
 * 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:
 */