mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 08:36:52 +08:00
Ref T384, ATC button component and integration in text message component
This commit is contained in:
133
src/blackgui/components/atcbuttoncomponent.cpp
Normal file
133
src/blackgui/components/atcbuttoncomponent.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/* 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 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 "atcbuttoncomponent.h"
|
||||
#include "ui_atcbuttoncomponent.h"
|
||||
|
||||
#include "blackgui/guiapplication.h"
|
||||
#include "blackgui/guiutility.h"
|
||||
#include "blackmisc/aviation/atcstationlist.h"
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <QPushButton>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackCore;
|
||||
using namespace BlackCore::Context;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
namespace Components
|
||||
{
|
||||
CAtcButtonComponent::CAtcButtonComponent(QWidget *parent) :
|
||||
QFrame(parent),
|
||||
ui(new Ui::CAtcButtonComponent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
if (sGui && sGui->getIContextNetwork())
|
||||
{
|
||||
connect(sGui->getIContextNetwork(), &IContextNetwork::changedAtcStationsOnlineDigest, this, &CAtcButtonComponent::onChangedAtcStations);
|
||||
connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this, &CAtcButtonComponent::onConnectionStatusChanged);
|
||||
}
|
||||
|
||||
this->setVisible(false); // will be changed when ATC stations are reported
|
||||
}
|
||||
|
||||
CAtcButtonComponent::~CAtcButtonComponent()
|
||||
{ }
|
||||
|
||||
void CAtcButtonComponent::updateStations()
|
||||
{
|
||||
this->setVisible(false);
|
||||
this->setMinimumHeight(0);
|
||||
|
||||
if (!sGui || !sGui->getIContextNetwork()) { return; }
|
||||
if (m_maxNumber < 1) { return; }
|
||||
const int max = qMin(m_maxNumber, m_rows * m_cols);
|
||||
CAtcStationList stations = sGui->getIContextNetwork()->getClosestAtcStationsOnline(max);
|
||||
if (stations.isEmpty()) { return; }
|
||||
|
||||
CGuiUtility::deleteLayout(this->layout(), true);
|
||||
QGridLayout *layout = new QGridLayout(this);
|
||||
|
||||
layout->setObjectName("gl_CAtcButtonComponent");
|
||||
layout->setSpacing(4);
|
||||
layout->setMargin(0);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
int row = 0;
|
||||
int col = 0;
|
||||
int added = 0;
|
||||
for (const CAtcStation &station : as_const(stations))
|
||||
{
|
||||
if (m_ignoreNonAtc)
|
||||
{
|
||||
// strict check, only "real ATC stations", no supervisors etc
|
||||
if (!station.getCallsign().hasAtcSuffix()) { continue; }
|
||||
}
|
||||
|
||||
QPushButton *button = new QPushButton(this);
|
||||
button->setText(station.getCallsignAsString());
|
||||
button->setIcon(station.toPixmap());
|
||||
QObject::connect(button, &QPushButton::released, this, &CAtcButtonComponent::onButtonClicked);
|
||||
const CVariant atcv = CVariant::fromValue(station);
|
||||
layout->addWidget(button, row, col++);
|
||||
button->show();
|
||||
button->setProperty("atc", atcv.getQVariant());
|
||||
added++;
|
||||
|
||||
if (col >= m_cols)
|
||||
{
|
||||
if (row == m_rows) { break; }
|
||||
row++;
|
||||
col = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (added > 0)
|
||||
{
|
||||
this->setVisible(true);
|
||||
this->setMinimumHeight(row * 25);
|
||||
}
|
||||
}
|
||||
|
||||
void CAtcButtonComponent::setRowsColumns(int rows, int cols, bool setMaxElements)
|
||||
{
|
||||
m_rows = rows;
|
||||
m_cols = cols;
|
||||
if (setMaxElements) { m_maxNumber = rows * cols; }
|
||||
}
|
||||
|
||||
void CAtcButtonComponent::onChangedAtcStations()
|
||||
{
|
||||
if (!m_backgroundUpdates) { return; }
|
||||
this->updateStations();
|
||||
}
|
||||
|
||||
void CAtcButtonComponent::onConnectionStatusChanged(INetwork::ConnectionStatus from, INetwork::ConnectionStatus to)
|
||||
{
|
||||
Q_UNUSED(from);
|
||||
if (INetwork::isDisconnectedStatus(to))
|
||||
{
|
||||
this->setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
void CAtcButtonComponent::onButtonClicked()
|
||||
{
|
||||
QPushButton *button = qobject_cast<QPushButton *>(QObject::sender());
|
||||
if (!button) { return; }
|
||||
const CVariant v(button->property("atc"));
|
||||
if (!v.isValid() || !v.canConvert<CAtcStation>()) { return; }
|
||||
const CAtcStation station = v.value<CAtcStation>();
|
||||
emit this->requestAtcStation(station);
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
Reference in New Issue
Block a user