From 34774bd005188f9beb82c0f900dfd6bead7b7a11 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 16 Dec 2013 00:20:18 +0000 Subject: [PATCH] blackgui library with list model classes for our lists refs #81 --- client.pro | 3 + src/blackgui/aircraftlistmodel.cpp | 46 +++++++ src/blackgui/aircraftlistmodel.h | 41 +++++++ src/blackgui/atcstationlistmodel.cpp | 39 ++++++ src/blackgui/atcstationlistmodel.h | 41 +++++++ src/blackgui/blackgui.pro | 30 +++++ src/blackgui/columns.cpp | 111 +++++++++++++++++ src/blackgui/columns.h | 113 ++++++++++++++++++ src/blackgui/listmodelbase.cpp | 146 +++++++++++++++++++++++ src/blackgui/listmodelbase.h | 171 +++++++++++++++++++++++++++ src/blackgui/serverlistmodel.cpp | 61 ++++++++++ src/blackgui/serverlistmodel.h | 79 +++++++++++++ 12 files changed, 881 insertions(+) create mode 100644 src/blackgui/aircraftlistmodel.cpp create mode 100644 src/blackgui/aircraftlistmodel.h create mode 100644 src/blackgui/atcstationlistmodel.cpp create mode 100644 src/blackgui/atcstationlistmodel.h create mode 100644 src/blackgui/blackgui.pro create mode 100644 src/blackgui/columns.cpp create mode 100644 src/blackgui/columns.h create mode 100644 src/blackgui/listmodelbase.cpp create mode 100644 src/blackgui/listmodelbase.h create mode 100644 src/blackgui/serverlistmodel.cpp create mode 100644 src/blackgui/serverlistmodel.h diff --git a/client.pro b/client.pro index 3d0c14079..1c72388c1 100644 --- a/client.pro +++ b/client.pro @@ -6,6 +6,7 @@ include (externals.pri) WITH_BLACKMISC = ON WITH_BLACKCORE = ON +WITH_BLACKGUI = ON WITH_SAMPLES = ON #WITH_DRIVER_FSX = ON @@ -23,6 +24,8 @@ equals(WITH_BLACKCORE, ON) { SUBDIRS += src/blackcore } +equals(WITH_BLACKGUI, ON) { + SUBDIRS += src/blackgui } equals(WITH_DRIVER_FSX, ON) { diff --git a/src/blackgui/aircraftlistmodel.cpp b/src/blackgui/aircraftlistmodel.cpp new file mode 100644 index 000000000..ff9a1bcdf --- /dev/null +++ b/src/blackgui/aircraftlistmodel.cpp @@ -0,0 +1,46 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "aircraftlistmodel.h" +#include "blackmisc/avaircraftsituation.h" +#include "blackmisc/blackmiscfreefunctions.h" + +using namespace BlackMisc::Aviation; + +namespace BlackGui +{ + /* + * Constructor + */ + CAircraftListModel::CAircraftListModel(QObject *parent) : CListModelBase("ViewAircraftList", parent) + { + this->m_columns.addColumn(CAircraft::IndexCallsign, "callsign"); + this->m_columns.addColumn(CAircraft::IndexPilotRealname, "pilotrealname"); + this->m_columns.addColumn(CAircraft::IndexDistance, "distance", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAircraft::IndexFrequencyCom1, "frequency", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAircraftIcao::IndexAsString, "model"); + this->m_columns.addColumn(CAircraft::IndexTansponderFormatted, "transponder"); + this->m_columns.addColumn(CAircraftSituation::IndexPositionLatitude, "latitude", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAircraftSituation::IndexPositionLongitude, "longitude", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAircraftSituation::IndexAltitude, "altitude", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAircraftSituation::IndexGroundspeed, "groundspeed", Qt::AlignRight | Qt::AlignVCenter); + + // default sort order + this->setSortColumnByPropertyIndex(CAircraft::IndexDistance); + this->m_sortOrder = Qt::AscendingOrder; + + // force strings for translation in resource files + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "callsign"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "pilotrealname"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "latitude"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "longitude"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "altitude"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "distance"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "height"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "transponder"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "groundspeed"); + (void)QT_TRANSLATE_NOOP("ViewAircraftList", "model"); + } +} diff --git a/src/blackgui/aircraftlistmodel.h b/src/blackgui/aircraftlistmodel.h new file mode 100644 index 000000000..8e7981547 --- /dev/null +++ b/src/blackgui/aircraftlistmodel.h @@ -0,0 +1,41 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/*! + \file +*/ + +#ifndef BLACKGUI_AIRCRAFTLISTMODEL_H +#define BLACKGUI_AIRCRAFTLISTMODEL_H + +#include "blackmisc/avaircraftlist.h" +#include "blackgui/listmodelbase.h" +#include +#include + +namespace BlackGui +{ + /*! + * \brief Aircraft list model + */ + class CAircraftListModel : public CListModelBase + { + + public: + + /*! + * \brief Constructor + * \param parent + */ + explicit CAircraftListModel(QObject *parent = nullptr); + + /*! + * \brief Destructor + */ + virtual ~CAircraftListModel() {} + + }; +} +#endif // guard diff --git a/src/blackgui/atcstationlistmodel.cpp b/src/blackgui/atcstationlistmodel.cpp new file mode 100644 index 000000000..9d24cc793 --- /dev/null +++ b/src/blackgui/atcstationlistmodel.cpp @@ -0,0 +1,39 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "atcstationlistmodel.h" +#include "blackmisc/blackmiscfreefunctions.h" + +using namespace BlackMisc::Aviation; + +namespace BlackGui +{ + /* + * Constructor + */ + CAtcListModel::CAtcListModel(QObject *parent) : CListModelBase("ViewAtcList", parent) + { + this->m_columns.addColumn(CAtcStation::IndexCallsignAsStringAsSet, "callsign"); + this->m_columns.addColumn(CAtcStation::IndexDistance, "distance", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAtcStation::IndexFrequency, "frequency", Qt::AlignRight | Qt::AlignVCenter); + this->m_columns.addColumn(CAtcStation::IndexControllerRealname, "controllername"); + this->m_columns.addColumn(CAtcStation::IndexIsOnline, "online"); + this->m_columns.addColumn(CAtcStation::IndexBookedFrom, "bookedfrom"); + this->m_columns.addColumn(CAtcStation::IndexBookedUntil, "bookeduntil"); + + // default sort order + this->setSortColumnByPropertyIndex(CAtcStation::IndexDistance); + this->m_sortOrder = Qt::AscendingOrder; + + // force strings for translation in resource files + (void)QT_TRANSLATE_NOOP("ViewAtcList", "callsign"); + (void)QT_TRANSLATE_NOOP("ViewAtcList", "distance"); + (void)QT_TRANSLATE_NOOP("ViewAtcList", "frequency"); + (void)QT_TRANSLATE_NOOP("ViewAtcList", "controllername"); + (void)QT_TRANSLATE_NOOP("ViewAtcList", "online"); + (void)QT_TRANSLATE_NOOP("ViewAtcList", "bookedfrom"); + (void)QT_TRANSLATE_NOOP("ViewAtcList", "bookeduntil"); + } +} diff --git a/src/blackgui/atcstationlistmodel.h b/src/blackgui/atcstationlistmodel.h new file mode 100644 index 000000000..c63c222bf --- /dev/null +++ b/src/blackgui/atcstationlistmodel.h @@ -0,0 +1,41 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/*! + \file +*/ + +#ifndef BLACKGUI_ATCLISTMODEL_H +#define BLACKGUI_ATCLISTMODEL_H + +#include "blackmisc/avatcstationlist.h" +#include "blackgui/listmodelbase.h" +#include +#include + +namespace BlackGui +{ + /*! + * \brief ATC list model + */ + class CAtcListModel : public CListModelBase + { + + public: + + /*! + * \brief Constructor + * \param parent + */ + explicit CAtcListModel(QObject *parent = nullptr); + + /*! + * \brief Destructor + */ + virtual ~CAtcListModel() {} + + }; +} +#endif // guard diff --git a/src/blackgui/blackgui.pro b/src/blackgui/blackgui.pro new file mode 100644 index 000000000..0ececfe0e --- /dev/null +++ b/src/blackgui/blackgui.pro @@ -0,0 +1,30 @@ +QT += network dbus gui widgets + +TARGET = blackgui +TEMPLATE = lib +CONFIG += staticlib c++11 + +INCLUDEPATH += .. +DEPENDPATH += . .. + +# PRECOMPILED_HEADER = stdpch.h +precompile_header:!isEmpty(PRECOMPILED_HEADER) { + DEFINES += USING_PCH +} + +DEFINES += LOG_IN_FILE +# RESOURCES += blackgui.qrc + +# lupdate (from cmd, e.g. lupdate blackmisc.pro) +# CODECFORTR = UTF-8 +# TRANSLATIONS += translations/blackgui_i18n_de.ts \ +# translations/blackgui_i18n_fr.ts \ +# translations/blackgui_i18n_en.ts + +win32:!win32-g++*: PRE_TARGETDEPS += ../../lib/blackmisc.lib +else: PRE_TARGETDEPS += ../../lib/libblackmisc.a + +HEADERS += *.h +SOURCES += *.cpp +DESTDIR = ../../lib +OTHER_FILES += diff --git a/src/blackgui/columns.cpp b/src/blackgui/columns.cpp new file mode 100644 index 000000000..34238925c --- /dev/null +++ b/src/blackgui/columns.cpp @@ -0,0 +1,111 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "columns.h" +#include + +namespace BlackGui +{ + +/* + * Header + */ +CColumns::CColumns(const QString &translationContext, QObject *parent) : + QObject(parent), m_translationContext(translationContext) +{ + // void +} + +/* + * Add column name + */ +void CColumns::addColumn(int propertyIndex, const QString &name, int alignment) +{ + this->m_headerNames.append(name); + this->m_propertyIndexes.append(propertyIndex); + this->m_alignments.append(alignment); +} + +/* + * Property index to name + */ +QString CColumns::propertyIndexToName(int propertyIndex) const +{ + int column = this->propertyIndexToColumn(propertyIndex); + return this->m_headerNames.at(column); +} + +/* + * Index to name + */ +QString CColumns::columnToName(int column) const +{ + Q_ASSERT(column >= 0 && column < this->m_headerNames.size()); + return this->m_headerNames.at(column); +} + +/* + * Get property index + */ +int CColumns::columnToPropertyIndex(int column) const +{ + Q_ASSERT(column >= 0 && column < this->m_propertyIndexes.size()); + return this->m_propertyIndexes.at(column); +} + +/* + * Property index to column + */ +int CColumns::propertyIndexToColumn(int propertyIndex) const +{ + return this->m_propertyIndexes.indexOf(propertyIndex); +} + +/* + * Name to property index + */ +int CColumns::nameToPropertyIndex(const QString &name) const +{ + int column = this->m_headerNames.indexOf(name); + if (column < 0) return -1; + return this->m_propertyIndexes.at(column); +} + +/* + * Size + */ +int CColumns::size() const +{ + return this->m_headerNames.size(); +} + +/* + * Alignmet + */ +bool CColumns::hasAlignment(const QModelIndex &index) const +{ + if (index.column() < 0 || index.column() >= this->m_alignments.size()) return false; + return this->m_alignments.at(index.column()) >= 0; +} + +/* + * Aligmnet as QVariant + */ +QVariant CColumns::aligmentAsQVariant(const QModelIndex &index) const +{ + if (index.column() < 0 || index.column() >= this->m_alignments.size()) return QVariant(); + if (!this->hasAlignment(index)) return QVariant(Qt::AlignVCenter | Qt::AlignLeft); // default + return QVariant(this->m_alignments.at(index.column())); +} + +/* + * Context + */ +const char *CColumns::getTranslationContext() const +{ + return this->m_translationContext.toUtf8().constData(); +} + +} // namespace BlackGui diff --git a/src/blackgui/columns.h b/src/blackgui/columns.h new file mode 100644 index 000000000..80e3f1810 --- /dev/null +++ b/src/blackgui/columns.h @@ -0,0 +1,113 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/*! + \file +*/ + +#ifndef BLACKGUI_COLUMNS_H +#define BLACKGUI_COLUMNS_H + +#include "blackmisc/valueobject.h" // for qHash overload, include before Qt stuff due GCC issue +#include +#include + +namespace BlackGui +{ + + /*! + * \brief Header data for a table + */ + class CColumns : public QObject + { + public: + /*! + * \brief Columns constructors + * \param translationContext + * \param parent + */ + CColumns(const QString &translationContext, QObject *parent = nullptr); + + /*! + * \brief Add a column name + * \param propertyIndex + * \param name + * \param alignment + */ + void addColumn(int propertyIndex, const QString &name, int alignment = -1); + + /*! + * \brief Property index to name + * \param propertyIndex + */ + QString propertyIndexToName(int propertyIndex) const; + + /*! + * \brief Column index to name + * \param column + */ + QString columnToName(int column) const; + + /*! + * \brief Column to property index + * \param index + */ + int columnToPropertyIndex(int column) const; + + /*! + * \brief Property index to column + * \param propertyIndex + * \return + */ + int propertyIndexToColumn(int propertyIndex) const; + + /*! + * \brief Column index to property index + * \param index + */ + int indexToPropertyIndex(int index) const; + + /*! + * \brief Column index to name + * \param index + */ + int nameToPropertyIndex(const QString &name) const; + + /*! + * \brief Size (number of columns) + * \return + */ + int size() const; + + /*! + * \brief Alignment for this column? + * \param index + * \return + */ + bool hasAlignment(const QModelIndex &index) const; + + /*! + * \brief Aligment as QVariant + * \param index + * \return + */ + QVariant aligmentAsQVariant(const QModelIndex &index) const; + + /*! + * \brief Translation context + * \return + */ + const char *getTranslationContext() const; + + private: + QString m_translationContext; + QList m_headerNames; + QList m_propertyIndexes; // column to property index + QList m_alignments; + }; + +} // namespace BlackGui + +#endif // guard diff --git a/src/blackgui/listmodelbase.cpp b/src/blackgui/listmodelbase.cpp new file mode 100644 index 000000000..15264b7d5 --- /dev/null +++ b/src/blackgui/listmodelbase.cpp @@ -0,0 +1,146 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "listmodelbase.h" +#include "blackmisc/avatcstationlist.h" +#include "blackmisc/avaircraftlist.h" +#include "blackmisc/nwserverlist.h" +#include "blackmisc/blackmiscfreefunctions.h" + +namespace BlackGui +{ + + /* + * Column count + */ + template int CListModelBase::columnCount(const QModelIndex & /** modelIndex **/) const + { + int c = this->m_columns.size(); + return c; + } + + /* + * Row count + */ + template int CListModelBase::rowCount(const QModelIndex & /** parent */) const + { + return this->m_list.size(); + } + + /* + * Column to property index + */ + template int CListModelBase::columnToPropertyIndex(int column) const + { + return this->m_columns.columnToPropertyIndex(column); + } + + /* + * Header data + */ + template QVariant CListModelBase::headerData(int section, Qt::Orientation orientation, int role) const + { + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) + { + if (section < 0 || section >= this->m_columns.size()) return QVariant(); + QString col = this->m_columns.columnToName(section); + col = QCoreApplication::translate(this->m_columns.getTranslationContext(), col.toUtf8().constData()); + return QVariant(col); + } + return QVariant(); + } + + /* + * Data + */ + template QVariant CListModelBase::data(const QModelIndex &index, int role) const + { + // checks + if (index.row() < 0 || index.row() >= this->m_list.size() || + index.column() < 0 || index.column() >= this->columnCount(index)) + { + return QVariant(); + } + + if (role == Qt::DisplayRole) + { + ObjectType obj = this->m_list[index.row()]; + int propertyIndex = this->columnToPropertyIndex(index.column()); + QString propertyString = obj.propertyByIndexAsString(propertyIndex, true); + return QVariant::fromValue(propertyString); + } + else if (role == Qt::TextAlignmentRole) + { + return this->m_columns.aligmentAsQVariant(index); + } + return QVariant(); + } + + /* + * Update + */ + template int CListModelBase::update(const ListType &list) + { + ListType copyList = (list.size() > 1 && this->hasValidSortColumn() ? + this->sortListByColumn(list, this->m_sortedColumn, this->m_sortOrder) : + list); + this->beginResetModel(); + this->m_list.clear(); + foreach(ObjectType object, copyList) + { + this->m_list.push_back(object); + } + this->endResetModel(); + return this->m_list.size(); + } + + /* + * Sort + */ + template void CListModelBase::sort(int column, Qt::SortOrder order) + { + this->m_sortedColumn = column; + this->m_sortOrder = order; + if (this->m_list.size() < 2) return; // nothing to do + + // sort the values + this->update( + this->sortListByColumn(this->m_list, column, order) + ); + } + + /* + * Sort list + */ + template ListType CListModelBase::sortListByColumn(const ListType &list, int column, Qt::SortOrder order) + { + if (list.size() < 2) return list; // nothing to do + int propertyIndex = this->m_columns.columnToPropertyIndex(column); + Q_ASSERT(propertyIndex >= 0); + if (propertyIndex < 0) return list; + + // sort the values + return list.sorted + ([ = ](const ObjectType & a, const ObjectType & b) -> bool + { + QVariant aQv = a.propertyByIndex(propertyIndex); + QVariant bQv = b.propertyByIndex(propertyIndex); + int compare = (order == Qt::AscendingOrder) ? + BlackMisc::compareQVariants(aQv, bQv) : + BlackMisc::compareQVariants(bQv, aQv); + return compare < 0; + } + ); + + } + + // see here for the reason of thess forward instantiations + // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html + template class CListModelBase; + template class CListModelBase; + template class CListModelBase; + + +} // namespace diff --git a/src/blackgui/listmodelbase.h b/src/blackgui/listmodelbase.h new file mode 100644 index 000000000..ba2d36325 --- /dev/null +++ b/src/blackgui/listmodelbase.h @@ -0,0 +1,171 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/*! + \file +*/ + +#ifndef BLACKGUI_LISTMODELBASE_H +#define BLACKGUI_LISTMODELBASE_H + +#include "blackgui/columns.h" +#include + +namespace BlackGui +{ + /*! + * \brief List model + */ + template class CListModelBase : public QAbstractListModel + { + + protected: + ListType m_list; + CColumns m_columns; + int m_sortedColumn; + Qt::SortOrder m_sortOrder; + + /*! + * \brief Constructor + * \param translationContext + * \param parent + */ + CListModelBase(const QString &translationContext, QObject *parent = nullptr) + : QAbstractListModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder) + { + // void + } + + /*! + * \brief Sort list by given order + * \param list + * \param column + * \param order + * \return + */ + ListType sortListByColumn(const ListType &list, int column, Qt::SortOrder order); + + public: + + /*! + * \brief Destructor + */ + virtual ~CListModelBase() {} + + /*! + * \brief Column count + * \return + */ + virtual int columnCount(const QModelIndex &modelIndex) const; + + /*! + * \brief Header data + * \param section + * \param orientation + * \param role + * \return + */ + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; + + /*! + * \brief Column to property index + * \param column + */ + virtual int columnToPropertyIndex(int column) const; + + /*! + * \brief Set column for sort + * \param column + */ + virtual void setSortColumn(int column) + { + this->m_sortedColumn = column; + } + + /*! + * \brief Set column for sort + * \param + */ + virtual void setSortColumnByPropertyIndex(int propertyIndex) + { + this->m_sortedColumn = this->m_columns.propertyIndexToColumn(propertyIndex); + } + + /*! + * \brief Get sort column + * \return + */ + virtual int getSortColumn() const + { + return this->m_sortedColumn; + } + + /*! + * \brief Has valid sort column + * \return + */ + virtual bool hasValidSortColumn() const + { + return this->m_sortedColumn >= 0 && this->m_sortedColumn < this->m_columns.size(); + } + + /*! + * \brief Get sort column + * \return + */ + virtual Qt::SortOrder getSortOrder() const + { + return this->m_sortOrder; + } + + /*! + * \brief data + * \param index + * \param role + * \return + */ + virtual QVariant data(const QModelIndex &index, int role) const; + + /*! + * \brief Row count + * \return + */ + virtual int rowCount(const QModelIndex &index = QModelIndex()) const; + + /*! + * \brief Update + * \param list + * \return + */ + virtual int update(const ListType &list); + + /*! + * \brief Object at row position + * \param index + * \return + */ + virtual const ObjectType &at(const QModelIndex &index) const + { + if (index.row() < 0 || index.row() >= this->m_list.size()) + { + const static ObjectType def; + return def; + } + else + { + return this->m_list[index.row()]; + } + } + + /*! + * \brief sort + * \param column + * \param order + */ + virtual void sort(int column, Qt::SortOrder order); + + }; +} +#endif // guard diff --git a/src/blackgui/serverlistmodel.cpp b/src/blackgui/serverlistmodel.cpp new file mode 100644 index 000000000..fe06b4b0e --- /dev/null +++ b/src/blackgui/serverlistmodel.cpp @@ -0,0 +1,61 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "serverlistmodel.h" +#include "blackmisc/blackmiscfreefunctions.h" +#include +#include + +using namespace BlackMisc::Network; + +namespace BlackGui +{ + /* + * Constructor + */ + CServerListModel::CServerListModel(QObject *parent) : + CListModelBase("ViewServerList", parent) + { + this->m_columns.addColumn(CServer::IndexName, "name"); + this->m_columns.addColumn(CServer::IndexDescription, "description"); + this->m_columns.addColumn(CServer::IndexAddress, "address"); + this->m_columns.addColumn(CServer::IndexPort, "port"); + this->m_columns.addColumn(CServer::IndexUserRealname, "username"); + this->m_columns.addColumn(CServer::IndexUserId, "userid"); + + // force strings for translation in resource files + (void)QT_TRANSLATE_NOOP("ViewServerList", "name"); + (void)QT_TRANSLATE_NOOP("ViewServerList", "description"); + (void)QT_TRANSLATE_NOOP("ViewServerList", "address"); + (void)QT_TRANSLATE_NOOP("ViewServerList", "port"); + (void)QT_TRANSLATE_NOOP("ViewServerList", "username"); + (void)QT_TRANSLATE_NOOP("ViewServerList", "userid"); + } + + /* + * Special functions + */ + QVariant CServerListModel::data(const QModelIndex &index, int role) const + { + if (role == Qt::BackgroundRole) + { + if (!this->hasSelectedServer()) return QVariant(); + CServer currentRow = this->at(index); + if (currentRow == this->getSelectedServer()) + { + QBrush background(Qt::green); + return background; + } + else + { + return QVariant(); + } + } + else + { + return CListModelBase::data(index, role); + } + } +} diff --git a/src/blackgui/serverlistmodel.h b/src/blackgui/serverlistmodel.h new file mode 100644 index 000000000..cbba32c31 --- /dev/null +++ b/src/blackgui/serverlistmodel.h @@ -0,0 +1,79 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/*! + \file +*/ + +#ifndef BLACKGUI_SERVERLISTMODEL_H +#define BLACKGUI_SERVERLISTMODEL_H + +#include "blackmisc/nwserverlist.h" +#include "blackgui/listmodelbase.h" +#include +#include + +namespace BlackGui +{ + /*! + * \brief Server list model + */ + class CServerListModel : public CListModelBase + { + + private: + BlackMisc::Network::CServer m_selectedServer; + + public: + + /*! + * \brief Constructor + * \param parent + */ + explicit CServerListModel(QObject *parent = nullptr); + + /*! + * \brief Destructor + */ + virtual ~CServerListModel() {} + + /*! + * \brief Has selected server? + * \return + */ + bool hasSelectedServer() const + { + return this->m_selectedServer.isValidForLogin(); + } + + /*! + * \brief Get selected server + * \return + */ + const BlackMisc::Network::CServer &getSelectedServer() const + { + return this->m_selectedServer; + } + + /*! + * \brief SetSelectedServer + * \param selectedServer + */ + void setSelectedServer(const BlackMisc::Network::CServer &selectedServer) + { + this->m_selectedServer = selectedServer; + } + + /*! + * \brief data + * \param index + * \param role + * \return + */ + virtual QVariant data(const QModelIndex &index, int role) const; + + }; +} +#endif // guard