Ref T442, moved tree model into own class

* columns can be used as with list model
* own class/file
This commit is contained in:
Klaus Basan
2018-12-04 19:38:18 +01:00
parent 4dae61c171
commit 45e34001a8
5 changed files with 179 additions and 60 deletions

View File

@@ -16,6 +16,7 @@
#include "blackgui/infoarea.h"
#include "blackgui/uppercasevalidator.h"
#include "blackgui/models/atcstationlistmodel.h"
#include "blackgui/models/atcstationtreemodel.h"
#include "blackgui/views/atcstationview.h"
#include "blackgui/views/viewbase.h"
#include "blackmisc/aviation/atcstationlist.h"
@@ -377,7 +378,7 @@ namespace BlackGui
//! \fixme EXPERIMENTAL CODE: change model so we can directly use hierarchies
QAbstractItemModel *old = (ui->tvp_AtcStationsOnlineTree->model());
ui->tvp_AtcStationsOnlineTree->setModel(
ui->tvp_AtcStationsOnline->derivedModel()->toAtcGroupModel()
ui->tvp_AtcStationsOnline->derivedModel()->toAtcTreeModel()
);
if (old) { old->deleteLater(); } // delete old model replaced by current model
if (!ui->tvp_AtcStationsOnlineTree->model()) { return; }

View File

@@ -8,6 +8,7 @@
*/
#include "blackgui/models/atcstationlistmodel.h"
#include "blackgui/models/atcstationtreemodel.h"
#include "blackgui/models/columnformatters.h"
#include "blackgui/models/columns.h"
#include "blackmisc/audio/voiceroom.h"
@@ -24,8 +25,6 @@
#include <QList>
#include <QMap>
#include <QModelIndex>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QString>
#include <Qt>
#include <QtDebug>
@@ -109,66 +108,17 @@ namespace BlackGui
}
}
QStandardItemModel *CAtcStationListModel::toAtcGroupModel() const
CAtcStationTreeModel *CAtcStationListModel::toAtcTreeModel() const
{
QStandardItemModel *model = new QStandardItemModel();
if (this->isEmpty()) { return model; }
model->setColumnCount(4);
const CAtcStationList atcStations = this->container().sortedByAtcSuffixSortOrderAndDistance();
const QStringList types = atcStations.getSuffixes();
for (const QString &type : types)
{
// ownership of QStandardItem is taken by model
QStandardItem *typeFolderFirstColumn = new QStandardItem(CCallsign::atcSuffixToIcon(type).toQIcon(), type);
typeFolderFirstColumn->setEditable(false);
QList<QStandardItem *> typeFolderRow { typeFolderFirstColumn };
model->invisibleRootItem()->appendRow(typeFolderRow);
CAtcStationList stations = this->container().findBySuffix(type);
for (const CAtcStation &station : stations)
{
QList<QStandardItem *> stationRow;
switch (m_stationMode)
{
case StationsOnline:
stationRow = QList<QStandardItem *>
{
new QStandardItem(station.getCallsign().toQString()),
new QStandardItem(station.getFrequency().valueRoundedWithUnit(CFrequencyUnit::MHz(), 2, true)),
new QStandardItem(station.getControllerRealName()),
new QStandardItem(station.getRelativeDistance().valueRoundedWithUnit(CLengthUnit::NM(), 1, true))
};
break;
case StationsBooked:
stationRow = QList<QStandardItem *>
{
new QStandardItem(station.getCallsign().toQString()),
new QStandardItem(station.getControllerRealName()),
new QStandardItem(station.getBookedFromUtc().toString(CDateTimeFormatter::formatYmdhm())),
new QStandardItem(station.getBookedUntilUtc().toString(CDateTimeFormatter::formatYmdhm()))
};
break;
case NotSet:
default:
Q_ASSERT(false);
break;
}
// make not editable
for (QStandardItem *si : stationRow)
{
si->setEditable(false);
}
// add all items
typeFolderFirstColumn->appendRow(stationRow);
}
}
return model;
CAtcStationTreeModel *tm = new CAtcStationTreeModel(QObject::parent());
tm->setColumns(m_columns);
tm->updateContainer(this->container());
return tm;
}
void CAtcStationListModel::changedAtcStationConnectionStatus(const CAtcStation &station, bool added)
{
if (station.getCallsign().isEmpty()) return;
if (station.getCallsign().isEmpty()) { return; }
if (added)
{
bool c = m_container.contains(&CAtcStation::getCallsign, station.getCallsign());

View File

@@ -12,10 +12,10 @@
#ifndef BLACKGUI_MODELS_ATCLISTMODEL_H
#define BLACKGUI_MODELS_ATCLISTMODEL_H
#include "blackgui/blackguiexport.h"
#include "blackgui/models/listmodelcallsignobjects.h"
#include "blackmisc/aviation/atcstation.h"
#include "blackmisc/aviation/atcstationlist.h"
#include "blackgui/blackguiexport.h"
#include <QObject>
@@ -23,6 +23,8 @@ namespace BlackGui
{
namespace Models
{
class CAtcStationTreeModel;
//! ATC list model
class BLACKGUI_EXPORT CAtcStationListModel : public CListModelCallsignObjects<BlackMisc::Aviation::CAtcStation, BlackMisc::Aviation::CAtcStationList, true>
{
@@ -48,7 +50,7 @@ namespace BlackGui
AtcStationMode getStationMode() const { return this->m_stationMode; }
//! A group by type (TWR, APP, ...) model
QStandardItemModel *toAtcGroupModel() const;
CAtcStationTreeModel *toAtcTreeModel() const;
public slots:
//! Used to quickly update single station (better response for the user)

View File

@@ -0,0 +1,108 @@
/* 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 "blackgui/models/atcstationtreemodel.h"
#include "blackgui/models/columnformatters.h"
#include "blackgui/models/columns.h"
#include "blackmisc/audio/voiceroom.h"
#include "blackmisc/aviation/callsign.h"
#include "blackmisc/compare.h"
#include "blackmisc/icon.h"
#include "blackmisc/network/user.h"
#include "blackmisc/pq/frequency.h"
#include "blackmisc/pq/length.h"
#include "blackmisc/pq/units.h"
#include "blackmisc/variant.h"
#include "blackmisc/propertyindex.h"
#include <QDateTime>
#include <QList>
#include <QMap>
#include <QModelIndex>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QString>
#include <Qt>
#include <QtDebug>
#include <QtGlobal>
using namespace BlackMisc;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Audio;
using namespace BlackMisc::Aviation;
using namespace BlackMisc::Network;
namespace BlackGui
{
namespace Models
{
CAtcStationTreeModel::CAtcStationTreeModel(QObject *parent) : QStandardItemModel(parent)
{
// force strings for translation in resource files
(void)QT_TRANSLATE_NOOP("ModelAtcList", "callsign");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "distance");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "frequency");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "controllername");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "online");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "bookedfrom");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "bookeduntil");
(void)QT_TRANSLATE_NOOP("ModelAtcList", "voiceroomurl");
}
void CAtcStationTreeModel::updateContainer(const CAtcStationList &stations)
{
m_stations = stations.sortedByAtcSuffixSortOrderAndDistance();
m_stationsBySuffix = m_stations.splitPerSuffix();
m_suffixes = m_stations.getSuffixes();
this->clear();
int visibleColumns = 0;
for (const QString &suffix : m_suffixes)
{
// ownership of QStandardItem is taken by model
QStandardItem *typeFolderFirstColumn = new QStandardItem(CCallsign::atcSuffixToIcon(suffix).toQIcon(), suffix);
typeFolderFirstColumn->setEditable(false);
this->invisibleRootItem()->appendRow(typeFolderFirstColumn);
for (const CAtcStation &station : m_stationsBySuffix[suffix])
{
int colCount = 0;
QList<QStandardItem *> stationRow;
for (const CColumn &column : m_columns.columns())
{
const CPropertyIndex i(column.getPropertyIndex());
const CVariant v(station.propertyByIndex(i));
QStandardItem *si = nullptr;
if (column.getFormatter()->supportsRole(Qt::DecorationRole))
{
const QIcon icon = column.getFormatter()->decorationRole(v).toPixmap();
si = new QStandardItem(icon, QStringLiteral(""));
}
else if (column.getFormatter()->supportsRole(Qt::DisplayRole))
{
const CVariant f = column.getFormatter()->displayRole(v);
si = new QStandardItem(f.toQString(true));
}
if (!si) { continue; }
colCount++;
si->setEditable(false); // make not editable
stationRow.push_back(si);
}
// add all items
if (stationRow.isEmpty()) { continue; }
typeFolderFirstColumn->appendRow(stationRow);
visibleColumns = qMax(visibleColumns, colCount);
}
}
this->setColumnCount(visibleColumns);
}
} // namespace
} // namespace

View File

@@ -0,0 +1,58 @@
/* 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.
*/
//! \file
#ifndef BLACKGUI_MODELS_ATCTREEMODEL_H
#define BLACKGUI_MODELS_ATCTREEMODEL_H
#include "columns.h"
#include "blackmisc/aviation/atcstationlist.h"
#include "blackgui/blackguiexport.h"
#include <QStandardItemModel>
#include <QHash>
namespace BlackGui
{
namespace Models
{
//! ATC list model
class BLACKGUI_EXPORT CAtcStationTreeModel : public QStandardItemModel
{
Q_OBJECT
public:
//! Constructor
explicit CAtcStationTreeModel(QObject *parent = nullptr);
//! Destructor
virtual ~CAtcStationTreeModel() override {}
//! Set station mode
void setColumns(const CColumns &columns) { m_columns.setColumns(columns); }
//! Update container
void updateContainer(const BlackMisc::Aviation::CAtcStationList &stations);
//! Get container
const BlackMisc::Aviation::CAtcStationList &container() const { return m_stations; }
//! Used to quickly update single station (better response for the user)
void changedAtcStationConnectionStatus(const BlackMisc::Aviation::CAtcStation &station, bool added);
private:
CColumns m_columns { "CAtcStationTreeModel" };
BlackMisc::Aviation::CAtcStationList m_stations;
QHash<QString, BlackMisc::Aviation::CAtcStationList> m_stationsBySuffix;
QStringList m_suffixes;
};
} // ns
} // ns
#endif // guard