mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 21:15:33 +08:00
135 lines
5.6 KiB
C++
135 lines
5.6 KiB
C++
/* Copyright (C) 2018
|
|
* swift project Community / Contributors
|
|
*
|
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
|
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
|
|
* or distributed except according to the terms contained in the LICENSE file.
|
|
*/
|
|
|
|
#include "airportcompleter.h"
|
|
#include "ui_airportcompleter.h"
|
|
#include "blackgui/guiapplication.h"
|
|
#include "blackgui/uppercasevalidator.h"
|
|
#include "blackcore/webdataservices.h"
|
|
#include "blackmisc/aviation/airportlist.h"
|
|
#include <QCompleter>
|
|
|
|
using namespace BlackCore;
|
|
using namespace BlackMisc::Aviation;
|
|
|
|
namespace BlackGui
|
|
{
|
|
namespace Components
|
|
{
|
|
CAirportCompleter::CAirportCompleter(QWidget *parent) :
|
|
QFrame(parent),
|
|
ui(new Ui::CAirportCompleter)
|
|
{
|
|
ui->setupUi(this);
|
|
ui->le_Icao->setValidator(new CUpperCaseValidator(ui->le_Icao));
|
|
|
|
connect(sGui->getWebDataServices(), &CWebDataServices::swiftDbAllDataRead, this, &CAirportCompleter::onAirportsChanged);
|
|
connect(ui->le_Icao, &QLineEdit::editingFinished, this, &CAirportCompleter::onIcaoChanged);
|
|
connect(ui->le_Name, &QLineEdit::editingFinished, this, &CAirportCompleter::onNameChanged);
|
|
connect(ui->le_Location, &QLineEdit::editingFinished, this, &CAirportCompleter::onLocationChanged);
|
|
this->onAirportsChanged();
|
|
}
|
|
|
|
CAirportCompleter::~CAirportCompleter()
|
|
{ }
|
|
|
|
void CAirportCompleter::setAirport(const CAirport &airport)
|
|
{
|
|
if (m_current == airport) { return; }
|
|
m_current = airport;
|
|
if (airport.getIcaoAsString() != ui->le_Icao->text()) { ui->le_Icao->setText(airport.getIcaoAsString()); }
|
|
if (airport.getDescriptiveName() != ui->le_Name->text()) { ui->le_Name->setText(airport.getDescriptiveName()); }
|
|
if (airport.getLocation() != ui->le_Location->text()) { ui->le_Location->setText(airport.getLocation()); }
|
|
emit this->changedAirport(m_current);
|
|
}
|
|
|
|
QString CAirportCompleter::getIcaoText() const
|
|
{
|
|
return ui->le_Icao->text().trimmed().toUpper();
|
|
}
|
|
|
|
void CAirportCompleter::setReadOnly(bool readOnly)
|
|
{
|
|
ui->le_Icao->setReadOnly(readOnly);
|
|
ui->le_Location->setReadOnly(readOnly);
|
|
ui->le_Name->setReadOnly(readOnly);
|
|
}
|
|
|
|
void CAirportCompleter::clear()
|
|
{
|
|
m_current = CAirport();
|
|
ui->le_Icao->clear();
|
|
ui->le_Location->clear();
|
|
ui->le_Name->clear();
|
|
}
|
|
|
|
void CAirportCompleter::onIcaoChanged()
|
|
{
|
|
const QString icao(this->getIcaoText());
|
|
if (m_current.getIcaoAsString() == icao) { return; }
|
|
const CAirport airport(sGui->getWebDataServices()->getAirportForIcaoDesignator(icao));
|
|
this->setAirport(airport);
|
|
}
|
|
|
|
void CAirportCompleter::onNameChanged()
|
|
{
|
|
const QString name(ui->le_Name->text());
|
|
if (m_current.getDescriptiveName() == name) { return; }
|
|
const CAirport airport(sGui->getWebDataServices()->getAirportForNameOrLocation(name));
|
|
this->setAirport(airport);
|
|
}
|
|
|
|
void CAirportCompleter::onLocationChanged()
|
|
{
|
|
const QString location(ui->le_Location->text());
|
|
if (m_current.getLocation() == location) { return; }
|
|
const CAirport airport(sGui->getWebDataServices()->getAirportForNameOrLocation(location));
|
|
this->setAirport(airport);
|
|
}
|
|
|
|
void CAirportCompleter::onAirportsChanged()
|
|
{
|
|
if (!sGui && !sGui->hasWebDataServices()) { return; }
|
|
const CAirportList airports = sGui->getWebDataServices()->getAirports();
|
|
ui->le_Icao->setCompleter(new QCompleter(airports.allIcaoCodes(true), ui->le_Icao));
|
|
ui->le_Name->setCompleter(new QCompleter(airports.allDescriptivesNames(true), ui->le_Name));
|
|
ui->le_Location->setCompleter(new QCompleter(airports.allLocationsPlusOptionalDescription(true), ui->le_Location));
|
|
|
|
if (ui->le_Icao->completer()->popup())
|
|
{
|
|
ui->le_Icao->completer()->setCaseSensitivity(Qt::CaseInsensitive);
|
|
ui->le_Icao->completer()->popup()->setObjectName("AirportCompleter.Icao");
|
|
ui->le_Icao->completer()->popup()->setMinimumWidth(75);
|
|
}
|
|
if (ui->le_Name->completer()->popup())
|
|
{
|
|
ui->le_Name->completer()->setCaseSensitivity(Qt::CaseInsensitive);
|
|
ui->le_Name->completer()->popup()->setObjectName("AirportCompleter.Name");
|
|
ui->le_Name->completer()->popup()->setMinimumWidth(150);
|
|
}
|
|
if (ui->le_Location->completer()->popup())
|
|
{
|
|
ui->le_Location->completer()->setCaseSensitivity(Qt::CaseInsensitive);
|
|
ui->le_Location->completer()->popup()->setObjectName("AirportCompleter.Location");
|
|
ui->le_Location->completer()->popup()->setMinimumWidth(150);
|
|
}
|
|
|
|
// turn into airport when it was not possible before
|
|
if (m_current.isNull())
|
|
{
|
|
const QString icao = this->getIcaoText();
|
|
if (CAirportIcaoCode::isValidIcaoDesignator(icao, true))
|
|
{
|
|
const CAirport airport = sGui->getWebDataServices()->getAirportForIcaoDesignator(icao);
|
|
if (!airport.isNull()) { this->setAirport(airport); }
|
|
}
|
|
}
|
|
}
|
|
} // ns
|
|
} // ns
|