mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 13:36:48 +08:00
Ref T219, pilot name form
(used to override server credentials for VATSIM/other servers)
This commit is contained in:
190
src/blackgui/editors/pilotform.cpp
Normal file
190
src/blackgui/editors/pilotform.cpp
Normal file
@@ -0,0 +1,190 @@
|
||||
/* Copyright (C) 2017
|
||||
* 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 and at http://www.swift-project.org/license.html. 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 "pilotform.h"
|
||||
#include "ui_pilotform.h"
|
||||
#include "blackgui/guiapplication.h"
|
||||
#include "blackgui/uppercasevalidator.h"
|
||||
#include "blackcore/webdataservices.h"
|
||||
#include "blackmisc/aviation/aircrafticaocode.h"
|
||||
#include "blackmisc/network/user.h"
|
||||
#include "blackconfig/buildconfig.h"
|
||||
#include <QCompleter>
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
using namespace BlackConfig;
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackMisc::Network;
|
||||
using namespace BlackCore;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Editors
|
||||
{
|
||||
CPilotForm::CPilotForm(QWidget *parent) :
|
||||
CForm(parent),
|
||||
ui(new Ui::CPilotForm)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->lblp_HomeAirport->setToolTips("ok", "wrong");
|
||||
ui->lblp_Id->setToolTips("ok", "wrong");
|
||||
ui->lblp_Password->setToolTips("ok", "wrong");
|
||||
ui->lblp_RealName->setToolTips("ok", "wrong");
|
||||
|
||||
ui->le_HomeAirport->setValidator(new CUpperCaseValidator(this));
|
||||
this->initCompleters(CEntityFlags::AirportEntity);
|
||||
|
||||
connect(ui->tb_UnhidePassword, &QToolButton::clicked, this, &CPilotForm::unhidePassword);
|
||||
connect(ui->le_Id, &QLineEdit::editingFinished, this, &CPilotForm::doValidation);
|
||||
connect(ui->le_HomeAirport, &QLineEdit::editingFinished, this, &CPilotForm::doValidation);
|
||||
connect(ui->le_Password, &QLineEdit::editingFinished, this, &CPilotForm::doValidation);
|
||||
connect(ui->le_RealName, &QLineEdit::editingFinished, this, &CPilotForm::doValidation);
|
||||
|
||||
// web service data
|
||||
Q_ASSERT_X(sGui && sGui->hasWebDataServices(), Q_FUNC_INFO, "Need web data services");
|
||||
connect(sGui->getWebDataServices(), &CWebDataServices::dataRead, this, &CPilotForm::onWebServiceDataRead);
|
||||
}
|
||||
|
||||
CPilotForm::~CPilotForm()
|
||||
{ }
|
||||
|
||||
void CPilotForm::setVatsimValidation(bool vatsim)
|
||||
{
|
||||
if (vatsim == m_vatsim) { return; }
|
||||
m_vatsim = vatsim;
|
||||
ui->le_Id->setValidator(vatsim ? m_vatsimIntValidator.data() : nullptr);
|
||||
}
|
||||
|
||||
CUser CPilotForm::getUser() const
|
||||
{
|
||||
CUser user = CUser(ui->le_Id->text().trimmed(), CUser::beautifyRealName(ui->le_RealName->text()), "", ui->le_Password->text());
|
||||
user.setHomeBase(CAirportIcaoCode(ui->le_HomeAirport->text().trimmed()));
|
||||
return user;
|
||||
}
|
||||
|
||||
bool CPilotForm::setUser(const CUser &user, bool ignoreEmptyUser)
|
||||
{
|
||||
if (ignoreEmptyUser && user.isNull()) { return false; }
|
||||
if (user.hasCredentials())
|
||||
{
|
||||
ui->le_Id->setText(user.getId());
|
||||
ui->le_Password->setText(user.getPassword());
|
||||
ui->le_RealName->setText(user.getRealName());
|
||||
if (user.hasHomeBase())
|
||||
{
|
||||
ui->le_HomeAirport->setText(user.getHomeBase().asString());
|
||||
}
|
||||
}
|
||||
else if (CBuildConfig::isLocalDeveloperDebugBuild())
|
||||
{
|
||||
ui->le_Id->setText("1288459");
|
||||
ui->le_Password->setText("4769");
|
||||
ui->le_HomeAirport->setText("LOWI");
|
||||
ui->le_RealName->setText("Swift Project");
|
||||
}
|
||||
this->validate();
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPilotForm::clear()
|
||||
{
|
||||
ui->le_Id->clear();
|
||||
ui->le_Password->clear();
|
||||
ui->le_HomeAirport->clear();
|
||||
ui->le_RealName->clear();
|
||||
|
||||
ui->lblp_Id->setTicked(false);
|
||||
ui->lblp_HomeAirport->setTicked(false);
|
||||
ui->lblp_Password->setTicked(false);
|
||||
ui->lblp_RealName->setTicked(false);
|
||||
}
|
||||
|
||||
void CPilotForm::setReadOnly(bool readonly)
|
||||
{
|
||||
ui->le_HomeAirport->setReadOnly(readonly);
|
||||
ui->le_Id->setReadOnly(readonly);
|
||||
ui->le_Password->setReadOnly(readonly);
|
||||
ui->le_RealName->setReadOnly(readonly);
|
||||
ui->lblp_Id->setVisible(!readonly);
|
||||
ui->lblp_HomeAirport->setVisible(!readonly);
|
||||
ui->lblp_Password->setVisible(!readonly);
|
||||
ui->lblp_RealName->setVisible(!readonly);
|
||||
}
|
||||
|
||||
CStatusMessageList CPilotForm::validate(bool nested) const
|
||||
{
|
||||
Q_UNUSED(nested);
|
||||
|
||||
CStatusMessageList msgs;
|
||||
const CUser user(this->getUser());
|
||||
const bool validId = m_vatsim ? user.hasValidVatsimId() : user.hasId();
|
||||
ui->lblp_Id->setTicked(validId);
|
||||
if (!validId) { msgs.push_back(CStatusMessage(this).validationError("Invalid id")); }
|
||||
|
||||
const bool validHomeAirport = user.hasValidHomeBase();
|
||||
ui->lblp_HomeAirport->setTicked(validHomeAirport);
|
||||
if (!validHomeAirport) { msgs.push_back(CStatusMessage(this).validationError("Wromg home airport")); }
|
||||
|
||||
const bool validCredentials = user.hasCredentials();
|
||||
ui->lblp_Password->setTicked(validCredentials);
|
||||
if (!validCredentials) { msgs.push_back(CStatusMessage(this).validationError("Invalid credentials")); }
|
||||
|
||||
const bool validRealName = user.hasRealName();
|
||||
ui->lblp_RealName->setTicked(validRealName);
|
||||
if (!validRealName) { msgs.push_back(CStatusMessage(this).validationError("Invalid real name")); }
|
||||
|
||||
return msgs;
|
||||
}
|
||||
|
||||
void CPilotForm::unhidePassword()
|
||||
{
|
||||
static const QLineEdit::EchoMode originalMode = ui->le_Password->echoMode();
|
||||
ui->le_Password->setEchoMode(QLineEdit::Normal);
|
||||
QTimer::singleShot(5000, this, [ = ]
|
||||
{
|
||||
ui->le_Password->setEchoMode(originalMode);
|
||||
});
|
||||
}
|
||||
|
||||
void CPilotForm::initCompleters(CEntityFlags::Entity entity)
|
||||
{
|
||||
// completers where possible
|
||||
if (sGui && sGui->hasWebDataServices())
|
||||
{
|
||||
if (entity.testFlag(CEntityFlags::AirportEntity) && !ui->le_HomeAirport->completer())
|
||||
{
|
||||
// one time init
|
||||
const QStringList airports = sGui->getWebDataServices()->getAirports().allIcaoCodes(true);
|
||||
if (!airports.isEmpty())
|
||||
{
|
||||
QCompleter *completer = new QCompleter(airports, this);
|
||||
QStyledItemDelegate *itemDelegate = new QStyledItemDelegate(completer);
|
||||
completer->popup()->setItemDelegate(itemDelegate);
|
||||
ui->le_HomeAirport->setCompleter(completer);
|
||||
completer->popup()->setObjectName("AirportCompleter");
|
||||
completer->popup()->setMinimumWidth(175);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CPilotForm::onWebServiceDataRead(CEntityFlags::Entity entity, CEntityFlags::ReadState state, int number)
|
||||
{
|
||||
if (state != CEntityFlags::ReadFinished) { return; }
|
||||
Q_UNUSED(number);
|
||||
this->initCompleters(entity);
|
||||
}
|
||||
|
||||
void CPilotForm::doValidation()
|
||||
{
|
||||
this->validate();
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
78
src/blackgui/editors/pilotform.h
Normal file
78
src/blackgui/editors/pilotform.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Copyright (C) 2017
|
||||
* 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 and at http://www.swift-project.org/license.html. 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKGUI_EDITORS_PILOTFORM_H
|
||||
#define BLACKGUI_EDITORS_PILOTFORM_H
|
||||
|
||||
#include "blackgui/editors/form.h"
|
||||
#include "blackgui/blackguiexport.h"
|
||||
#include "blackmisc/network/user.h"
|
||||
#include "blackmisc/network/entityflags.h"
|
||||
#include <QFrame>
|
||||
#include <QScopedPointer>
|
||||
#include <QIntValidator>
|
||||
|
||||
namespace Ui { class CPilotForm; }
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Editors
|
||||
{
|
||||
//! Pilot form (name, homebase)
|
||||
class BLACKGUI_EXPORT CPilotForm : public CForm
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
explicit CPilotForm(QWidget *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
virtual ~CPilotForm();
|
||||
|
||||
//! Set VATSIM validation mode
|
||||
void setVatsimValidation(bool vatsim);
|
||||
|
||||
//! Values as user
|
||||
BlackMisc::Network::CUser getUser() const;
|
||||
|
||||
//! Set user values
|
||||
bool setUser(const BlackMisc::Network::CUser &user, bool ignoreEmptyUser = false);
|
||||
|
||||
//! Clear values
|
||||
void clear();
|
||||
|
||||
//! \name Form class implementations
|
||||
//! @{
|
||||
virtual void setReadOnly(bool readonly) override;
|
||||
virtual BlackMisc::CStatusMessageList validate(bool nested = false) const override;
|
||||
//! @}
|
||||
|
||||
private:
|
||||
//! Unhide password
|
||||
void unhidePassword();
|
||||
|
||||
//! Completers
|
||||
void initCompleters(BlackMisc::Network::CEntityFlags::Entity entity);
|
||||
|
||||
//! Web services read
|
||||
void onWebServiceDataRead(BlackMisc::Network::CEntityFlags::Entity entity, BlackMisc::Network::CEntityFlags::ReadState state, int number);
|
||||
|
||||
//! Call validation
|
||||
void doValidation();
|
||||
|
||||
QScopedPointer<Ui::CPilotForm> ui;
|
||||
QScopedPointer<QIntValidator> m_vatsimIntValidator { new QIntValidator(100000, 9999999, this) };
|
||||
bool m_vatsim = false;
|
||||
};
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
#endif // guard
|
||||
134
src/blackgui/editors/pilotform.ui
Normal file
134
src/blackgui/editors/pilotform.ui
Normal file
@@ -0,0 +1,134 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CPilotForm</class>
|
||||
<widget class="QFrame" name="CPilotForm">
|
||||
<property name="windowTitle">
|
||||
<string>Pilot info</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gl_PilotInfo">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="lbl_Id">
|
||||
<property name="text">
|
||||
<string>Id:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLineEdit" name="le_Id"/>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="BlackGui::CTickLabel" name="lblp_Id">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="3">
|
||||
<widget class="QLabel" name="lbl_Password">
|
||||
<property name="text">
|
||||
<string>Password</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="4">
|
||||
<widget class="QToolButton" name="tb_UnhidePassword">
|
||||
<property name="toolTip">
|
||||
<string>unhide password</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>...</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../blackmisc/blackmisc.qrc">
|
||||
<normaloff>:/diagona/icons/diagona/icons/question-button.png</normaloff>:/diagona/icons/diagona/icons/question-button.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="5">
|
||||
<widget class="QLineEdit" name="le_Password">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::PasswordEchoOnEdit</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="6">
|
||||
<widget class="BlackGui::CTickLabel" name="lblp_Password">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="lbl_RealName">
|
||||
<property name="text">
|
||||
<string>Real name</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="le_RealName">
|
||||
<property name="placeholderText">
|
||||
<string>e.g. Joe Doe</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="BlackGui::CTickLabel" name="lblp_RealName">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<widget class="QLabel" name="lbl_HomeAirport">
|
||||
<property name="toolTip">
|
||||
<string>Home airport, e.g. "KLAX"</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Home</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="4" colspan="2">
|
||||
<widget class="QLineEdit" name="le_HomeAirport">
|
||||
<property name="maxLength">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="placeholderText">
|
||||
<string>e.g. "KLAX"</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="6">
|
||||
<widget class="BlackGui::CTickLabel" name="lblp_HomeAirport">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>BlackGui::CTickLabel</class>
|
||||
<extends>QLabel</extends>
|
||||
<header>blackgui/ticklabel.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources>
|
||||
<include location="../../blackmisc/blackmisc.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user