refs #568, specialized model class for models from DB

* support for keys
* renamed to listmodeldbobjects (unified with views)
* key type as template parameter
* adjusted derived classes
This commit is contained in:
Klaus Basan
2016-01-08 00:21:58 +01:00
parent 6770114f2c
commit f607b72821
17 changed files with 131 additions and 124 deletions

View File

@@ -19,7 +19,7 @@ namespace BlackGui
namespace Models
{
CAircraftIcaoCodeListModel::CAircraftIcaoCodeListModel(QObject *parent) :
CListModelBase("AircraftIcaoListModel", parent)
CListModelDbObjects("AircraftIcaoListModel", parent)
{
this->m_columns.addColumn(CColumn::standardString("id", CAircraftIcaoCode::IndexDbIntegerKey, CDefaultFormatter::alignRightVCenter()));
this->m_columns.addColumn(CColumn::standardValueObject("ICAO", CAircraftIcaoCode::IndexAircraftDesignator));

View File

@@ -15,7 +15,7 @@
#include "blackgui/blackguiexport.h"
#include <QAbstractItemModel>
#include "blackmisc/aviation/aircrafticaocodelist.h"
#include "blackgui/models/listmodelbase.h"
#include "blackgui/models/listmodeldbobjects.h"
namespace BlackGui
{
@@ -23,7 +23,7 @@ namespace BlackGui
{
//! Airport list model
class BLACKGUI_EXPORT CAircraftIcaoCodeListModel :
public CListModelBase<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList, true>
public CListModelDbObjects<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList, int, true>
{
public:
//! Constructor

View File

@@ -21,7 +21,7 @@ namespace BlackGui
namespace Models
{
CAircraftModelListModel::CAircraftModelListModel(AircraftModelMode mode, QObject *parent) :
CModelsWithDbKeysBase("CAircraftModelListModel", parent)
CListModelDbObjects("CAircraftModelListModel", parent)
{
this->setAircraftModelMode(mode);
@@ -142,31 +142,16 @@ namespace BlackGui
QVariant CAircraftModelListModel::data(const QModelIndex &index, int role) const
{
if (role != Qt::BackgroundRole) { return CListModelBase::data(index, role); }
bool db = highlightDbData();
if (role != Qt::BackgroundRole) { return CListModelDbObjects::data(index, role); }
bool ms = highlightGivenModelStrings() && !m_highlightStrings.isEmpty();
if (!db && !ms) { return CListModelBase::data(index, role); }
if (!ms) { return CListModelDbObjects::data(index, role); }
CAircraftModel model(this->at(index));
// highlight stashed first
if (ms)
if (m_highlightStrings.contains(model.getModelString(), Qt::CaseInsensitive))
{
if (m_highlightStrings.contains(model.getModelString(), Qt::CaseInsensitive))
{
static const QBrush b(Qt::yellow);
return b;
}
return QVariant();
}
// highlight DB models
if (db)
{
if (model.hasValidDbKey())
{
static const QBrush b(Qt::green);
return b;
}
static const QBrush b(Qt::yellow);
return b;
}
return QVariant();
}

View File

@@ -13,7 +13,7 @@
#define BLACKGUI_AIRCRAFTMODELLISTMODEL_H
#include "blackgui/blackguiexport.h"
#include "blackgui/models/modelswithdbkey.h"
#include "blackgui/models/listmodeldbobjects.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include <QStringList>
#include <QAbstractItemModel>
@@ -24,7 +24,7 @@ namespace BlackGui
{
//! Aircraft model list model
class BLACKGUI_EXPORT CAircraftModelListModel :
public CModelsWithDbKeysBase<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList, int, true>
public CListModelDbObjects<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList, int, true>
{
public:
//! How to display
@@ -51,12 +51,6 @@ namespace BlackGui
//! Mode
AircraftModelMode getModelMode() const { return m_mode; }
//! Highlight the DB models
bool highlightDbData() const { return m_highlightDbData; }
//! Highlight the DB models
void setHighlightDbData(bool highlightDbData) { m_highlightDbData = highlightDbData; }
//! Highlight models
bool highlightGivenModelStrings() const { return m_highlightModelStrings; }
@@ -72,13 +66,11 @@ namespace BlackGui
//! Replace models with same model string, or just add
void replaceOrAddByModelString(const BlackMisc::Simulation::CAircraftModelList &models);
protected:
//! \copydoc QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const override;
private:
AircraftModelMode m_mode = NotSet; //!< current mode
bool m_highlightDbData = false; //!< highlight if DB data entry (valid key)
bool m_highlightModelStrings = false; //!< highlight in in model strings
QStringList m_highlightStrings; //!< model strings to highlight
};

View File

@@ -20,7 +20,7 @@ namespace BlackGui
namespace Models
{
CAirlineIcaoCodeListModel::CAirlineIcaoCodeListModel(QObject *parent) :
CListModelBase("AircraftIcaoListModel", parent)
CListModelDbObjects("AircraftIcaoListModel", parent)
{
this->m_columns.addColumn(CColumn::standardString("id", CAirlineIcaoCode::IndexDbIntegerKey, CDefaultFormatter::alignRightVCenter()));
this->m_columns.addColumn(CColumn::standardValueObject("ICAO", CAirlineIcaoCode::IndexAirlineDesignator));

View File

@@ -15,14 +15,15 @@
#include "blackgui/blackguiexport.h"
#include <QAbstractItemModel>
#include "blackmisc/aviation/airlineicaocodelist.h"
#include "blackgui/models/listmodelbase.h"
#include "blackgui/models/listmodeldbobjects.h"
namespace BlackGui
{
namespace Models
{
//! Airport list model
class BLACKGUI_EXPORT CAirlineIcaoCodeListModel : public CListModelBase<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList, true>
class BLACKGUI_EXPORT CAirlineIcaoCodeListModel :
public CListModelDbObjects<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList, int, true>
{
public:
//! Constructor

View File

@@ -18,7 +18,7 @@ namespace BlackGui
namespace Models
{
CCountryListModel::CCountryListModel(QObject *parent) :
CListModelBase("CountryListModel", parent)
CListModelDbObjects("CountryListModel", parent)
{
CColumn col("country", CCountry::IndexIcon);
col.setSortPropertyIndex(CCountry::IndexIsoCode);

View File

@@ -14,7 +14,7 @@
#include "blackgui/blackguiexport.h"
#include "blackmisc/countrylist.h"
#include "blackgui/models/listmodelbase.h"
#include "blackgui/models/listmodeldbobjects.h"
#include <QAbstractItemModel>
namespace BlackGui
@@ -23,7 +23,7 @@ namespace BlackGui
{
//! Country list model
class BLACKGUI_EXPORT CCountryListModel :
public CListModelBase<BlackMisc::CCountry, BlackMisc::CCountryList, true>
public CListModelDbObjects<BlackMisc::CCountry, BlackMisc::CCountryList, QString, true>
{
public:
//! Constructor

View File

@@ -19,7 +19,7 @@ namespace BlackGui
namespace Models
{
CDistributorListModel::CDistributorListModel(QObject *parent) :
CListModelBase("ModelDistributorList", parent)
CListModelDbObjects("ModelDistributorList", parent)
{
this->m_columns.addColumn(CColumn::standardString("key", CDistributor::IndexDbStringKey));
this->m_columns.addColumn(CColumn::standardString("description", CDistributor::IndexDescription));

View File

@@ -14,7 +14,7 @@
#include "blackgui/blackguiexport.h"
#include "blackmisc/simulation/distributorlist.h"
#include "blackgui/models/listmodelbase.h"
#include "blackgui/models/listmodeldbobjects.h"
#include <QAbstractItemModel>
namespace BlackGui
@@ -23,10 +23,9 @@ namespace BlackGui
{
//! Distributor list model
class BLACKGUI_EXPORT CDistributorListModel :
public CListModelBase<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList, true>
public CListModelDbObjects<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList, QString, true>
{
public:
//! Constructor
explicit CDistributorListModel(QObject *parent = nullptr);

View File

@@ -188,7 +188,7 @@ namespace BlackGui
int CListModelBase<ObjectType, ContainerType, UseCompare>::rowCount(const QModelIndex &parentIndex) const
{
Q_UNUSED(parentIndex);
return this->getContainerOrFilteredContainer().size();
return this->containerOrFilteredContainer().size();
}
template <typename ObjectType, typename ContainerType, bool UseCompare>
@@ -209,7 +209,7 @@ namespace BlackGui
if (!formatter) { return QVariant(); }
//! Formatted data
ObjectType obj = this->getContainerOrFilteredContainer()[index.row()];
ObjectType obj = this->containerOrFilteredContainer()[index.row()];
BlackMisc::CPropertyIndex propertyIndex = this->columnToPropertyIndex(index.column());
return formatter->data(role, obj.propertyByIndex(propertyIndex)).getQVariant();
}
@@ -398,7 +398,7 @@ namespace BlackGui
}
else
{
return this->getContainerOrFilteredContainer()[index.row()];
return this->containerOrFilteredContainer()[index.row()];
}
}
@@ -478,13 +478,16 @@ namespace BlackGui
}
template <typename ObjectType, typename ContainerType, bool UseCompare>
const ContainerType &CListModelBase<ObjectType, ContainerType, UseCompare>::getContainerOrFilteredContainer() const
const ContainerType &CListModelBase<ObjectType, ContainerType, UseCompare>::containerOrFilteredContainer() const
{
if (!this->hasFilter())
if (this->hasFilter())
{
return this->m_containerFiltered;
}
else
{
return this->m_container;
}
return m_containerFiltered;
}
template <typename ObjectType, typename ContainerType, bool UseCompare>
@@ -503,7 +506,7 @@ namespace BlackGui
template <typename ObjectType, typename ContainerType, bool UseCompare>
void CListModelBase<ObjectType, ContainerType, UseCompare>::emitRowCountChanged()
{
int n = this->getContainerOrFilteredContainer().size();
int n = this->containerOrFilteredContainer().size();
emit this->rowCountChanged(n, this->hasFilter());
emit this->changed();
}

View File

@@ -153,6 +153,9 @@ namespace BlackGui
//! Used container data
const ContainerType &container() const;
//! Full container or cached filtered container as approproiate
const ContainerType &containerOrFilteredContainer() const;
//! \copydoc QStandardItemModel::data()
virtual QVariant data(const QModelIndex &index, int role) const override;
@@ -168,6 +171,7 @@ namespace BlackGui
virtual int rowCount(const QModelIndex &parentIndex = QModelIndex()) const override;
//! Update by new container
//! \return int size after update
//! \remarks a sorting is performed only if a valid sort column is set
virtual int update(const ContainerType &container, bool sort = true);
@@ -247,24 +251,21 @@ namespace BlackGui
void takeFilterOwnership(std::unique_ptr<IModelFilter<ContainerType> > &filter);
protected:
std::unique_ptr<IModelFilter<ContainerType> > m_filter; //!< Used filter
//! \copydoc CListModelBaseNonTemplate::CListModelBaseNonTemplate
CListModelBase(const QString &translationContext, QObject *parent = nullptr);
//! \copydoc CModelBaseNonTemplate::performUpdateContainer
virtual int performUpdateContainer(const BlackMisc::CVariant &variant, bool sort) override;
//! Full container or cached filtered container as approproiate
const ContainerType &getContainerOrFilteredContainer() const;
//! Update filtered container
void updateFilteredContainer();
//! Row count changed
void emitRowCountChanged();
ContainerType m_container; //!< used container
ContainerType m_containerFiltered; //!< cache for filtered container data
std::unique_ptr<IModelFilter<ContainerType> > m_filter; //!< Used filter
ContainerType m_container; //!< used container
ContainerType m_containerFiltered; //!< cache for filtered container data
};
namespace Private

View File

@@ -0,0 +1,70 @@
/* Copyright (C) 2053
* 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 "listmodeldbobjects.h"
#include "allmodelcontainers.h"
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
namespace BlackGui
{
namespace Models
{
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
CListModelDbObjects<ObjectType, ContainerType, KeyType, UseCompare>::CListModelDbObjects(const QString &translationContext, QObject *parent) :
CListModelBase<ObjectType, ContainerType, UseCompare>(translationContext, parent)
{
// void
}
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
QVariant CListModelDbObjects<ObjectType, ContainerType, KeyType, UseCompare>::data(const QModelIndex &index, int role) const
{
if (role != Qt::BackgroundRole) { return CListModelBase<ObjectType, ContainerType, UseCompare>::data(index, role); }
if (isHighlightIndex(index)) { return QBrush(m_highlightColor); }
if (!highlightDbData()) { return CListModelBase<ObjectType, ContainerType, UseCompare>::data(index, role); }
ObjectType obj(this->at(index));
// highlight DB models
if (obj.hasValidDbKey())
{
static const QBrush b(Qt::green);
return b;
}
return QVariant();
}
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
KeyType CListModelDbObjects<ObjectType, ContainerType, KeyType, UseCompare>::dbKeyForIndex(const QModelIndex &index) const
{
if (!index.isValid()) { return ObjectType::invalidDbKey(); }
return this->at(index).getDbKey();
}
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
bool CListModelDbObjects<ObjectType, ContainerType, KeyType, UseCompare>::isHighlightIndex(const QModelIndex &index) const
{
if (!index.isValid()) { return false; }
if (m_highlightKeys.isEmpty()) { return false; }
return m_highlightKeys.contains(dbKeyForIndex(index));
}
// see here for the reason of thess forward instantiations
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
template class CListModelDbObjects<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList, int, true>;
template class CListModelDbObjects<BlackMisc::CCountry, BlackMisc::CCountryList, QString, true>;
template class CListModelDbObjects<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList, int, true>;
template class CListModelDbObjects<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList, int, true>;
template class CListModelDbObjects<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList, int, true>;
template class CListModelDbObjects<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList, QString, true>;
} // namespace
} // namespace

View File

@@ -9,25 +9,32 @@
//! \file
#ifndef BLACKGUI_MODELSWITHDBKEY_H
#define BLACKGUI_MODELSWITHDBKEY_H
#ifndef BLACKGUI_LISTMODELDBOBJECTS_H
#define BLACKGUI_LISTMODELDBOBJECTS_H
#include "listmodelbase.h"
#include "blackgui/blackguiexport.h"
namespace BlackGui
{
namespace Models
{
//! Base class for models with DB keys
template <typename ObjectType, typename ContainerType, typename KeyType = int, bool UseCompare = false>
class CModelsWithDbKeysBase : public CListModelBase<ObjectType, ContainerType, UseCompare>
//! List model for DB objects
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare = false> class CListModelDbObjects :
public CListModelBase<ObjectType, ContainerType, UseCompare>
{
public:
//! Destructor
virtual ~CModelsWithDbKeysBase() {}
virtual ~CListModelDbObjects() {}
//! Highlight the DB models
bool highlightDbData() const { return m_highlightDbData; }
//! Highlight the DB models
void setHighlightDbData(bool highlightDbData) { m_highlightDbData = highlightDbData; }
//! Keys to be highlighted
void setHighlightDbKeys(const QList<KeyType> &keys) { m_highlightIntKeys = keys; }
void setHighlightDbKeys(const QList<KeyType> &keys) { m_highlightKeys = keys; }
//! Set color for highlighting
void setHighlightColor(QColor color) { m_highlightColor = color; }
@@ -42,13 +49,15 @@ namespace BlackGui
bool isHighlightIndex(const QModelIndex &index) const;
protected:
//! \copydoc CListModelBase::CListModelBase
CModelsWithDbKeysBase(const QString &translationContext, QObject *parent = nullptr);
//! \copydoc CListModelDbObjectsNonTemplate::CListModelDbObjectsNonTemplate
CListModelDbObjects(const QString &translationContext, QObject *parent = nullptr);
private:
QList<KeyType> m_highlightIntKeys; //!< keys to be highlighted
bool m_highlightDbData = false; //!< highlight if DB data entry (valid key)
QList<KeyType> m_highlightKeys; //!< keys to be highlighted
QColor m_highlightColor = Qt::green;
};
} // namespace
} // namespace
#endif // guard

View File

@@ -21,7 +21,7 @@ namespace BlackGui
namespace Models
{
CLiveryListModel::CLiveryListModel(QObject *parent) :
CListModelBase("ModelLiveryList", parent)
CListModelDbObjects("ModelLiveryList", parent)
{
this->m_columns.addColumn(CColumn::standardString("id", CLivery::IndexDbIntegerKey, CDefaultFormatter::alignRightVCenter()));
this->m_columns.addColumn(CColumn::standardString("code", CLivery::IndexCombinedCode));

View File

@@ -14,7 +14,7 @@
#include "blackgui/blackguiexport.h"
#include "blackmisc/aviation/liverylist.h"
#include "blackgui/models/listmodelbase.h"
#include "blackgui/models/listmodeldbobjects.h"
#include <QAbstractItemModel>
namespace BlackGui
@@ -23,10 +23,9 @@ namespace BlackGui
{
//! Distributor list model
class BLACKGUI_EXPORT CLiveryListModel :
public CListModelBase<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList, true>
public CListModelDbObjects<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList, int, true>
{
public:
//! Constructor
explicit CLiveryListModel(QObject *parent = nullptr);

View File

@@ -1,52 +0,0 @@
/* Copyright (C) 2015
* 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 "modelswithdbkey.h"
#include "allmodelcontainers.h"
namespace BlackGui
{
namespace Models
{
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
CModelsWithDbKeysBase<ObjectType, ContainerType, KeyType, UseCompare>::CModelsWithDbKeysBase(const QString &translationContext, QObject *parent) :
CListModelBase<ObjectType, ContainerType, UseCompare>(translationContext, parent)
{ }
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
QVariant CModelsWithDbKeysBase<ObjectType, ContainerType, KeyType, UseCompare>::data(const QModelIndex &index, int role) const
{
if (role == Qt::BackgroundRole)
{
if (isHighlightIndex(index)) { return QBrush(m_highlightColor); }
}
return CListModelBase<ObjectType, ContainerType, UseCompare>::data(index, role);
}
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
KeyType CModelsWithDbKeysBase<ObjectType, ContainerType, KeyType, UseCompare>::dbKeyForIndex(const QModelIndex &index) const
{
if (!index.isValid()) { return ObjectType::invalidDbKey(); }
return this->at(index).getDbKey();
}
template <typename ObjectType, typename ContainerType, typename KeyType, bool UseCompare>
bool CModelsWithDbKeysBase<ObjectType, ContainerType, KeyType, UseCompare>::isHighlightIndex(const QModelIndex &index) const
{
if (!index.isValid()) { return false; }
if (m_highlightIntKeys.isEmpty()) { return false; }
return m_highlightIntKeys.contains(dbKeyForIndex(index));
}
// see here for the reason of thess forward instantiations
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
template class CModelsWithDbKeysBase<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList, int, true>;
} // namespace
} // namespace