KPublicTransport

abstractquerymodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "abstractquerymodel.h"
8 #include "abstractquerymodel_p.h"
9 #include "assetrepository_p.h"
10 #include "datatypes/attributionutil_p.h"
11 
12 #include <KPublicTransport/Attribution>
13 #include <KPublicTransport/Manager>
14 #include <KPublicTransport/Reply>
15 
16 #include <QDebug>
17 #include <QTimer>
18 
19 using namespace KPublicTransport;
20 
21 AbstractQueryModelPrivate::~AbstractQueryModelPrivate() = default;
22 
23 void AbstractQueryModelPrivate::setLoading(bool l)
24 {
25  if (m_loading == l) {
26  return;
27  }
28  m_loading = l;
29  Q_EMIT q_ptr->loadingChanged();
30 }
31 
32 void AbstractQueryModelPrivate::setErrorMessage(const QString &msg)
33 {
34  if (m_errorMessage == msg) {
35  return;
36  }
37  m_errorMessage = msg;
38  Q_EMIT q_ptr->errorMessageChanged();
39 }
40 
41 void AbstractQueryModelPrivate::monitorReply(Reply *reply)
42 {
43  m_reply = reply;
44  QObject::connect(reply, &Reply::finished, q_ptr, [this, reply]() {
45  setLoading(false);
46  reply->deleteLater();
47  m_reply = nullptr;
48  if (reply->error() == KPublicTransport::Reply::NoError) {
49  AttributionUtil::merge(m_attributions, std::move(reply->takeAttributions()));
50  Q_EMIT q_ptr->attributionsChanged();
51  } else {
52  setErrorMessage(reply->errorString());
53  }
54  });
55 }
56 
57 void AbstractQueryModelPrivate::query()
58 {
59  if (m_pendingQuery || !m_manager) {
60  return;
61  }
62 
63  m_pendingQuery = true;
64  QTimer::singleShot(0, q_ptr, [this]() {
65  m_pendingQuery = false;
66  q_ptr->clear();
67  doQuery();
68  });
69 }
70 
71 
72 AbstractQueryModel::AbstractQueryModel(AbstractQueryModelPrivate* dd, QObject* parent)
73  : QAbstractListModel(parent)
74  , d_ptr(dd)
75 {
76  d_ptr->q_ptr = this;
77 
78  connect(AssetRepository::instance(), &AssetRepository::downloadFinished, this, [this]() {
79  const auto rows = rowCount();
80  if (rows > 0) {
81  Q_EMIT dataChanged(index(0, 0), index(rows - 1, 0));
82  }
83  });
84 }
85 
86 AbstractQueryModel::~AbstractQueryModel() = default;
87 
88 Manager* AbstractQueryModel::manager() const
89 {
90  return d_ptr->m_manager;
91 }
92 
93 void AbstractQueryModel::setManager(Manager *mgr)
94 {
95  if (d_ptr->m_manager == mgr) {
96  return;
97  }
98 
99  d_ptr->m_manager = mgr;
100  Q_EMIT managerChanged();
101  d_ptr->query();
102 }
103 
104 bool AbstractQueryModel::isLoading() const
105 {
106  return d_ptr->m_loading;
107 }
108 
109 QString AbstractQueryModel::errorMessage() const
110 {
111  return d_ptr->m_errorMessage;
112 }
113 
114 const std::vector<Attribution>& AbstractQueryModel::attributions() const
115 {
116  return d_ptr->m_attributions;
117 }
118 
119 QVariantList AbstractQueryModel::attributionsVariant() const
120 {
121  QVariantList l;
122  l.reserve(d_ptr->m_attributions.size());
123  std::transform(d_ptr->m_attributions.begin(), d_ptr->m_attributions.end(), std::back_inserter(l), [](const auto &attr) { return QVariant::fromValue(attr); });
124  return l;
125 }
126 
128 {
129  d_ptr->setLoading(false);
130  delete d_ptr->m_reply;
131  d_ptr->m_reply = nullptr;
132 }
133 
135 {
136  cancel();
137  if (rowCount() > 0) {
138  beginResetModel();
139  d_ptr->doClearResults();
140  endResetModel();
141  }
142 
143  if (!d_ptr->m_attributions.empty()) {
144  d_ptr->m_attributions.clear();
145  Q_EMIT attributionsChanged();
146  }
147 
148  d_ptr->setErrorMessage({});
149 }
150 
151 #include "moc_abstractquerymodel.moc"
virtual int rowCount(const QModelIndex &parent) const const =0
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
void finished()
Emitted whenever the corresponding search has been completed.
const std::vector< Attribution > & attributions() const
The attribution information for the current model content.
void deleteLater()
Error error() const
Error code.
Definition: reply.cpp:46
KCRASH_EXPORT void setErrorMessage(const QString &message)
Q_INVOKABLE void cancel()
Cancel ongoing query operations, but keep the results that are already there.
QVariant fromValue(const T &value)
Nothing went wrong.
Definition: reply.h:32
QString errorString() const
Textual error message.
Definition: reply.cpp:54
std::vector< Attribution > && takeAttributions()
Returns the attributions for the provided data for moving them elsewhere.
Definition: reply.cpp:89
Q_INVOKABLE void clear()
Cancel any ongoing query operations, and clear the results.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
Entry point for starting public transport queries.
Definition: manager.h:41
Query response base class.
Definition: reply.h:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.