File indexing completed on 2024-04-28 04:37:21

0001 /*
0002     SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "problemmodelset.h"
0008 
0009 #include "problemmodel.h"
0010 
0011 namespace KDevelop
0012 {
0013 
0014 class ProblemModelSetPrivate
0015 {
0016 public:
0017     QVector<ModelData> data;
0018 };
0019 
0020 ProblemModelSet::ProblemModelSet(QObject *parent)
0021     : QObject(parent)
0022     , d_ptr(new ProblemModelSetPrivate())
0023 {
0024 }
0025 
0026 ProblemModelSet::~ProblemModelSet() = default;
0027 
0028 void ProblemModelSet::addModel(const QString &id, const QString &name, ProblemModel *model)
0029 {
0030     Q_D(ProblemModelSet);
0031 
0032     ModelData m{id, name, model};
0033 
0034     d->data.push_back(m);
0035 
0036     connect(model, &ProblemModel::problemsChanged, this, &ProblemModelSet::problemsChanged);
0037 
0038     emit added(m);
0039 }
0040 
0041 ProblemModel* ProblemModelSet::findModel(const QString &id) const
0042 {
0043     Q_D(const ProblemModelSet);
0044 
0045     auto it = std::find_if(d->data.constBegin(), d->data.constEnd(), [&](const ModelData& data) {
0046         return (data.id == id);
0047     });
0048 
0049     return (it != d->data.constEnd()) ? it->model : nullptr;
0050 }
0051 
0052 void ProblemModelSet::removeModel(const QString &id)
0053 {
0054     Q_D(ProblemModelSet);
0055 
0056     QVector<ModelData>::iterator itr = d->data.begin();
0057 
0058     while (itr != d->data.end()) {
0059         if(itr->id == id)
0060             break;
0061         ++itr;
0062     }
0063 
0064     if(itr != d->data.end()) {
0065         (*itr).model->disconnect(this);
0066         d->data.erase(itr);
0067         emit removed(id);
0068     }
0069 }
0070 
0071 void ProblemModelSet::showModel(const QString &id)
0072 {
0073     Q_D(ProblemModelSet);
0074 
0075     for (const ModelData &data : qAsConst(d->data)) {
0076         if (data.id == id) {
0077             emit showRequested(data.id);
0078             return;
0079         }
0080     }
0081 }
0082 
0083 QVector<ModelData> ProblemModelSet::models() const
0084 {
0085     Q_D(const ProblemModelSet);
0086 
0087     return d->data;
0088 }
0089 
0090 }
0091 
0092 #include "moc_problemmodelset.cpp"