File indexing completed on 2024-04-28 08:33:02

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KPublicTransport/Location>
0008 #include <KPublicTransport/LocationQueryModel>
0009 #include <KPublicTransport/Manager>
0010 
0011 #include <QApplication>
0012 #include <QCompleter>
0013 #include <QDebug>
0014 #include <QLabel>
0015 #include <QLineEdit>
0016 #include <QVBoxLayout>
0017 
0018 using namespace KPublicTransport;
0019 
0020 int main(int argc, char **argv)
0021 {
0022     QCoreApplication::setApplicationName(QStringLiteral("locationcompleter"));
0023     QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0024     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0025 
0026     QApplication app(argc, argv);
0027 
0028     Manager ptMgr;
0029 
0030     QWidget window;
0031     auto topLayout = new QVBoxLayout;
0032     window.setLayout(topLayout);
0033     auto lineEdit = new QLineEdit;
0034     topLayout->addWidget(lineEdit);
0035     auto label = new QLabel;
0036     topLayout->addWidget(label);
0037 
0038     auto completer = new QCompleter;
0039     auto model = new LocationQueryModel(completer);
0040     model->setManager(&ptMgr);
0041     model->setQueryDelay(250);
0042     completer->setModel(model);
0043     completer->setCaseSensitivity(Qt::CaseInsensitive);
0044     completer->setFilterMode(Qt::MatchContains);
0045     completer->setCompletionRole(Qt::DisplayRole);
0046     lineEdit->setCompleter(completer);
0047     QObject::connect(lineEdit, &QLineEdit::textEdited, model, [model](const auto &text) {
0048         LocationRequest req;
0049         req.setName(text);
0050         model->setRequest(req);
0051     });
0052     // TODO this seems very wrong...
0053     QObject::connect(model, &QAbstractItemModel::rowsInserted, completer, [completer]() { completer->complete(); });
0054     QObject::connect(completer, QOverload<const QModelIndex&>::of(&QCompleter::activated), label, [label](const QModelIndex &idx) {
0055         const auto loc = idx.data(LocationQueryModel::LocationRole).value<Location>();
0056         QString s = QLatin1String("Name: ") + loc.name()
0057             + QLatin1String("\nLat: ") + QString::number(loc.latitude()) + QLatin1String(" Lon: ") + QString::number(loc.longitude());
0058         label->setText(s);
0059     });
0060 
0061     window.show();
0062     return app.exec();
0063 }