File: windtablemodel.cpp

package info (click to toggle)
boats 202008-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,572 kB
  • sloc: cpp: 11,129; xml: 10; makefile: 9
file content (152 lines) | stat: -rw-r--r-- 4,332 bytes parent folder | download
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
//
// C++ Implementation: windtablemodel
//
// Description:
//
//
// Author: Thibaut GRIDEL <tgridel@free.fr>
//
// Copyright (c) 2011-2015 Thibaut GRIDEL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//
#include <iostream>

#include "windtablemodel.h"

#include "commontypes.h"
#include "situationmodel.h"
#include "windmodel.h"

extern int debugLevel;

WindTableModel::WindTableModel(WindModel *wind, QObject *parent)
        : QAbstractTableModel(parent),
        m_wind(wind) {
    if (debugLevel & 1 << MODEL) std::cout << "new windtable " << this << std::endl;
}

WindTableModel::~WindTableModel() {
    if (debugLevel & 1 << MODEL) std::cout << "end windtable " << this << std::endl;
}

void WindTableModel::setWind(WindModel *wind)  {
    if (m_wind != wind) {
        beginResetModel();
        m_wind = wind;
        endResetModel();
    }
}

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

    if (index.row() >= m_wind->size() || index.row() < 0)
        return QVariant();

    if (role == Qt::DisplayRole) {
        switch (index.column()) {
            case WIND_INDEX:
                return index.row()+1;
                break;
            case WIND_DIRECTION:
                return m_wind->windAt(index.row());
                break;
            default:
                return QVariant();
                break;
        }
    } else if (role == Qt::CheckStateRole) {
        switch (index.column()) {
            case WIND_DELETE:
                return false;
                break;
            default:
                return QVariant();
                break;
        }
    }
    return QVariant();
}

QVariant WindTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
            case WIND_INDEX:
                return tr("Index");

            case WIND_DIRECTION:
                return tr("Direction");

            case WIND_DELETE:
                return tr("Delete");

            default:
                return QVariant();
        }
    }
    return QVariant();
}

Qt::ItemFlags WindTableModel::flags(const QModelIndex &index) const {
    Q_UNUSED(index);
    return Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsUserCheckable;
}

bool WindTableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    Q_UNUSED(role);
    if (!index.isValid())
        return false;

    if (index.row() > m_wind->size() || index.row() < 0)
        return false;

    switch (index.column()) {
        case WIND_DIRECTION:
            if (value.canConvert<qreal>()) {
                qreal newValue = value.value<qreal>();
                if (debugLevel & 1 << MODEL) std::cout << "setting wind " << newValue;
                if (index.row() == m_wind->size()) {
                    m_wind->situation()->addWind(newValue);
                } else {
                    if (newValue != m_wind->windAt(index.row())) {
                        m_wind->situation()->setWind(index.row(), newValue);
                    }
                }
                return true;
            }
            break;
        case WIND_DELETE:
            if (debugLevel & 1 << MODEL) std::cout << "deleting index " << index.row();
            if (index.row() < m_wind->size()) {
                m_wind->situation()->deleteWind(index.row());
            }
            return true;
            break;
        default:
            return false;
            break;
    }
    return false;
}

void WindTableModel::updateWind() {
    beginResetModel();
    endResetModel();
}