File: SetWithModel.h

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (190 lines) | stat: -rw-r--r-- 6,058 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Type/SetWithModel.h
//! @brief     Defines and implements templated class SetWithModel.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifdef SWIG
#error no need to expose this header to Swig
#endif // SWIG
#ifndef BORNAGAIN_GUI_MODEL_TYPE_SETWITHMODEL_H
#define BORNAGAIN_GUI_MODEL_TYPE_SETWITHMODEL_H

#include "Base/Type/OwningVector.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Type/ModelForSet.h"
#include <QObject>
#include <QStringList>
#include <iostream>
#include <vector>

//! Non-templated base class for SetWithModel.
//!
//! Needed to support Qt signals (the Q_OBJECT macro does not work in templated classes).
//! The virtual functions setCurrentIndex(i) and model() are used in class SetView.

class AbstractSetModel : public QObject {
    Q_OBJECT
public:
    explicit AbstractSetModel();
    virtual ~AbstractSetModel();

    virtual size_t currentIndex() const = 0;
    virtual void setCurrentIndex(size_t i) = 0;
    virtual QAbstractListModel* model() = 0;

signals:
    void setChanged() const;
    void indexSet() const;
};

//! A set of NamedItem%s that has a current item and a QListModel.

template <typename T> class SetWithModel : public AbstractSetModel {
public:
    SetWithModel()
        : m_qmodel(std::make_unique<ModelForSet<T>>(this))
    {
    }
    virtual ~SetWithModel() = default;

    void clear()
    {
        m_qmodel->beginResetModel();
        m_vec.clear();
        m_idx = -1;
        m_qmodel->endResetModel();
        emit AbstractSetModel::setChanged();
    }
    void delete_current()
    {
        ASSERT(m_idx != size_t(-1));
        m_qmodel->beginRemoveRows({}, m_idx, m_idx);
        m_vec.delete_at(m_idx);
        update_current();
        m_qmodel->endRemoveRows();
        emit AbstractSetModel::setChanged();
    }
    void add_item(T* t)
    {
        m_qmodel->beginInsertRows({}, size(), size());
        t->renumber(itemNames());
        m_vec.push_back(t);
        m_idx = m_vec.size() - 1;
        m_qmodel->endInsertRows();
        emit AbstractSetModel::setChanged();
    }
    void add_items(const std::vector<T*>& v)
    {
        if (v.empty())
            return;
        m_qmodel->beginInsertRows({}, size(), size() + v.size() - 1);
        for (T* t : v) {
            t->renumber(itemNames());
            m_vec.push_back(t);
        }
        m_idx = m_vec.size() - 1;
        m_qmodel->endInsertRows();
        emit AbstractSetModel::setChanged();
    }
    void move_current_to(size_t i)
    {
        if (i >= size())
            return;
        QModelIndex parent = m_qmodel->index(m_idx).parent();
        m_qmodel->beginMoveRows(parent, m_idx, m_idx, parent, (i <= m_idx) ? i : i + 1);
        m_vec.insert_at(i, m_vec.release_at(m_idx));
        m_idx = i;
        m_qmodel->endMoveRows();
        emit AbstractSetModel::setChanged();
    }

    void setCurrentIndex(size_t i) override
    {
        if (!(i < m_vec.size() || i == size_t(-1))) {
            std::cerr << "setCurrentIndex i=" << i << " vs vec#=" << m_vec.size() << std::endl;
            ASSERT_NEVER;
        }
        m_qmodel->beginResetModel();
        if (i != m_idx)
            m_idx = i;
        m_qmodel->endResetModel();
        emit indexSet();
        emit AbstractSetModel::setChanged();
    }

    void setCurrentName(const QString& s)
    {
        if (s != currentItem()->name()) {
            currentItem()->setName(s);
            currentDataChanged();
        }
    }
    void setCurrentDescription(const QString& s)
    {
        if (s != currentItem()->description()) {
            currentItem()->setDescription(s);
            currentDataChanged();
        }
    }

    size_t currentIndex() const override { return m_idx; }
    const T* currentItem() const { return m_idx < m_vec.size() ? m_vec.at(m_idx) : (T*)nullptr; }
    T* currentItem() { return m_idx < m_vec.size() ? m_vec.at(m_idx) : (T*)nullptr; }

    const T* at(size_t i) const { return m_vec.at(i); }
    T* at(size_t i) { return m_vec.at(i); }
    const T* back() const { return m_vec.back(); }
    T* back() { return m_vec.back(); }

    size_t size() const { return m_vec.size(); }
    bool empty() const { return m_vec.empty(); }
    int index_of(const T* t) const { return m_vec.index_of(t); }
    const T* at_or(size_t i, T* defolt) const { return m_vec.at_or(i, defolt); }

    QStringList itemNames() const
    {
        QStringList result;
        for (const auto* t : m_vec)
            result << t->name();
        return result;
    }

    using Iterator = typename std::vector<T*>::iterator; // "typename" can be dropped under C++20
    using ConstIterator = typename std::vector<T*>::const_iterator;

    ConstIterator begin() const { return m_vec.begin(); }
    ConstIterator end() const { return m_vec.end(); }
    Iterator begin() { return m_vec.begin(); }
    Iterator end() { return m_vec.end(); }

    QAbstractListModel* model() override { return m_qmodel.get(); }

private:
    //! To be called when m_idx may have fallen out of range.
    void update_current()
    {
        if (m_idx == m_vec.size())
            m_idx = m_vec.size() - 1;
    }
    //! To be called when name or description of the current item have changed.
    void currentDataChanged()
    {
        QModelIndex qi = m_qmodel->index(m_idx, 0);
        m_qmodel->dataChanged(qi, qi, QVector<int>({Qt::DisplayRole, Qt::EditRole}));
    }

    OwningVector<T> m_vec;
    size_t m_idx = -1; //!< current index, or -1 for empty set
    std::unique_ptr<ModelForSet<T>> m_qmodel;
};

#endif // BORNAGAIN_GUI_MODEL_TYPE_SETWITHMODEL_H