mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 10:15:38 +08:00
refs #525, specialized model class for DB entities and improved funtions in view base class
* will allow to highlight entities * nested custom menus * moved displayAutomatically() menus in view base class * fixed which menus are displayed for aircraft models * also changed to QStandardItem model to see if this is causing any trouble (the real bigger changes will follow in #530)
This commit is contained in:
@@ -21,7 +21,7 @@ namespace BlackGui
|
||||
namespace Models
|
||||
{
|
||||
CAircraftModelListModel::CAircraftModelListModel(AircraftModelMode mode, QObject *parent) :
|
||||
CListModelBase("CAircraftModelListModel", parent)
|
||||
CModelsWithDbKeysBase("CAircraftModelListModel", parent)
|
||||
{
|
||||
this->setAircraftModelMode(mode);
|
||||
|
||||
@@ -40,6 +40,7 @@ namespace BlackGui
|
||||
{
|
||||
case NotSet:
|
||||
case OwnSimulatorModel:
|
||||
case StashModel:
|
||||
this->m_columns.addColumn(CColumn::standardString("model", { CAircraftModel::IndexModelString}));
|
||||
this->m_columns.addColumn(CColumn::standardString("description", { CAircraftModel::IndexDescription}));
|
||||
this->m_columns.addColumn(CColumn::standardString("name", { CAircraftModel::IndexName}));
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#include "blackgui/blackguiexport.h"
|
||||
#include "blackmisc/simulation/aircraftmodellist.h"
|
||||
#include "blackgui/models/listmodelbase.h"
|
||||
#include "blackgui/models/modelswithdbkey.h"
|
||||
#include <QAbstractItemModel>
|
||||
|
||||
namespace BlackGui
|
||||
@@ -23,7 +23,7 @@ namespace BlackGui
|
||||
{
|
||||
//! Aircraft model list model
|
||||
class BLACKGUI_EXPORT CAircraftModelListModel :
|
||||
public CListModelBase<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList, true>
|
||||
public CModelsWithDbKeysBase<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList, int, true>
|
||||
{
|
||||
public:
|
||||
//! How to display
|
||||
@@ -34,7 +34,8 @@ namespace BlackGui
|
||||
OwnSimulatorModel, ///< model existing with my sim
|
||||
MappedModel, ///< Model based on mapping operation
|
||||
Database, ///< Database entry
|
||||
VPilotRuleModel ///< vPilot rule turned into model
|
||||
VPilotRuleModel, ///< vPilot rule turned into model
|
||||
StashModel ///< stashed models
|
||||
};
|
||||
|
||||
//! Constructor
|
||||
@@ -55,6 +56,12 @@ namespace BlackGui
|
||||
//! Highlight the DB models
|
||||
void setHighlightDbData(bool highlightDbData) { m_highlightDbData = highlightDbData; }
|
||||
|
||||
//! Highlight stashed models
|
||||
bool highlightStashedModels() const { return m_highlightStashedData; }
|
||||
|
||||
//! Highlight stashed models
|
||||
void setHighlightStashedModels(bool highlightStashedModels) { m_highlightStashedData = highlightStashedModels; }
|
||||
|
||||
protected:
|
||||
//! \copydoc QAbstractItemModel::data
|
||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||
@@ -62,6 +69,7 @@ namespace BlackGui
|
||||
private:
|
||||
AircraftModelMode m_mode = NotSet; //!< current mode
|
||||
bool m_highlightDbData = false;
|
||||
bool m_highlightStashedData = false;
|
||||
};
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace BlackGui
|
||||
QModelIndex CListModelBaseNonTemplate::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return QAbstractItemModel::createIndex(row, column);
|
||||
return QStandardItemModel::createIndex(row, column);
|
||||
}
|
||||
|
||||
QModelIndex CListModelBaseNonTemplate::parent(const QModelIndex &child) const
|
||||
@@ -105,7 +105,7 @@ namespace BlackGui
|
||||
|
||||
Qt::ItemFlags CListModelBaseNonTemplate::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags f = QAbstractItemModel::flags(index);
|
||||
Qt::ItemFlags f = QStandardItemModel::flags(index);
|
||||
if (!index.isValid()) { return f; }
|
||||
bool editable = this->m_columns.isEditable(index);
|
||||
f = editable ? (f | Qt::ItemIsEditable) : (f ^ Qt::ItemIsEditable);
|
||||
@@ -132,7 +132,7 @@ namespace BlackGui
|
||||
|
||||
Qt::DropActions CListModelBaseNonTemplate::supportedDropActions() const
|
||||
{
|
||||
return QAbstractItemModel::supportedDropActions();
|
||||
return QStandardItemModel::supportedDropActions();
|
||||
}
|
||||
|
||||
QStringList CListModelBaseNonTemplate::mimeTypes() const
|
||||
@@ -157,12 +157,17 @@ namespace BlackGui
|
||||
}
|
||||
|
||||
CListModelBaseNonTemplate::CListModelBaseNonTemplate(const QString &translationContext, QObject *parent)
|
||||
: QAbstractItemModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder)
|
||||
: QStandardItemModel(parent), m_columns(translationContext), m_sortedColumn(-1), m_sortOrder(Qt::AscendingOrder)
|
||||
{
|
||||
// non unique default name, set translation context as default
|
||||
this->setObjectName(translationContext);
|
||||
}
|
||||
|
||||
template <typename ObjectType, typename ContainerType, bool UseCompare>
|
||||
CListModelBase<ObjectType, ContainerType, UseCompare>::CListModelBase(const QString &translationContext, QObject *parent)
|
||||
: CListModelBaseNonTemplate(translationContext, parent)
|
||||
{ }
|
||||
|
||||
template <typename ObjectType, typename ContainerType, bool UseCompare>
|
||||
int CListModelBase<ObjectType, ContainerType, UseCompare>::rowCount(const QModelIndex &parentIndex) const
|
||||
{
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include "blackgui/models/columns.h"
|
||||
#include "blackgui/models/modelfilter.h"
|
||||
#include "blackmisc/worker.h"
|
||||
#include <QAbstractItemModel>
|
||||
#include <QStandardItemModel>
|
||||
#include <QThread>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
@@ -27,7 +27,7 @@ namespace BlackGui
|
||||
namespace Models
|
||||
{
|
||||
//! Non templated base class, allows Q_OBJECT and signals to be used
|
||||
class BLACKGUI_EXPORT CListModelBaseNonTemplate : public QAbstractItemModel
|
||||
class BLACKGUI_EXPORT CListModelBaseNonTemplate : public QStandardItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -38,16 +38,16 @@ namespace BlackGui
|
||||
//! Destructor
|
||||
virtual ~CListModelBaseNonTemplate() {}
|
||||
|
||||
//! \copydoc QAbstractItemModel::columnCount()
|
||||
//! \copydoc QStandardItemModel::columnCount()
|
||||
virtual int columnCount(const QModelIndex &modelIndex = QModelIndex()) const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::headerData()
|
||||
//! \copydoc QStandardItemModel::headerData()
|
||||
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::headerData()
|
||||
//! \copydoc QStandardItemModel::headerData()
|
||||
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::parent()
|
||||
//! \copydoc QStandardItemModel::parent()
|
||||
virtual QModelIndex parent(const QModelIndex &child) const override;
|
||||
|
||||
//! Column to property index
|
||||
@@ -78,16 +78,16 @@ namespace BlackGui
|
||||
//! Translation context
|
||||
virtual const QString &getTranslationContext() const;
|
||||
|
||||
//! \copydoc QAbstractItemModel::flags
|
||||
//! \copydoc QStandardItemModel::flags
|
||||
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::supportedDragActions
|
||||
//! \copydoc QStandardItemModel::supportedDragActions
|
||||
virtual Qt::DropActions supportedDragActions() const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::supportedDropActions
|
||||
//! \copydoc QStandardItemModel::supportedDropActions
|
||||
virtual Qt::DropActions supportedDropActions() const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::supportedDropActions
|
||||
//! \copydoc QStandardItemModel::supportedDropActions
|
||||
virtual QStringList mimeTypes() const override;
|
||||
|
||||
//! Mark as about to be destroyed, normally marked from view
|
||||
@@ -140,14 +140,14 @@ namespace BlackGui
|
||||
//! Used container data
|
||||
const ContainerType &container() const;
|
||||
|
||||
//! \copydoc QAbstractItemModel::data()
|
||||
//! \copydoc QStandardItemModel::data()
|
||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::setData()
|
||||
//! \copydoc QStandardItemModel::setData()
|
||||
//! \sa CListModelBaseNonTemplate::flags
|
||||
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
|
||||
//! \copydoc QAbstractItemModel::rowCount()
|
||||
//! \copydoc QStandardItemModel::rowCount()
|
||||
virtual int rowCount(const QModelIndex &parentIndex = QModelIndex()) const override;
|
||||
|
||||
//! Update by new container
|
||||
@@ -172,7 +172,7 @@ namespace BlackGui
|
||||
//! Sort by given sort order \sa getSortColumn() \sa getSortOrder()
|
||||
void sort();
|
||||
|
||||
//! \copydoc QAbstractItemModel::sort()
|
||||
//! \copydoc QStandardItemModel::sort()
|
||||
virtual void sort(int column, Qt::SortOrder order) override;
|
||||
|
||||
//! Truncate to given number
|
||||
@@ -211,7 +211,7 @@ namespace BlackGui
|
||||
//! Empty?
|
||||
virtual bool isEmpty() const;
|
||||
|
||||
//! \copydoc QAbstractItemModel::mimeData
|
||||
//! \copydoc QStandardItemModel::mimeData
|
||||
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override;
|
||||
|
||||
//! Filter available
|
||||
@@ -227,9 +227,7 @@ namespace BlackGui
|
||||
std::unique_ptr<IModelFilter<ContainerType> > m_filter; //!< Used filter
|
||||
|
||||
//! \copydoc CListModelBaseNonTemplate::CListModelBaseNonTemplate
|
||||
CListModelBase(const QString &translationContext, QObject *parent = nullptr)
|
||||
: CListModelBaseNonTemplate(translationContext, parent)
|
||||
{ }
|
||||
CListModelBase(const QString &translationContext, QObject *parent = nullptr);
|
||||
|
||||
//! \copydoc CModelBaseNonTemplate::performUpdateContainer
|
||||
virtual int performUpdateContainer(const BlackMisc::CVariant &variant, bool sort) override;
|
||||
|
||||
52
src/blackgui/models/modelswithdbkey.cpp
Normal file
52
src/blackgui/models/modelswithdbkey.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
/* 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
|
||||
54
src/blackgui/models/modelswithdbkey.h
Normal file
54
src/blackgui/models/modelswithdbkey.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKGUI_MODELSWITHDBKEY_H
|
||||
#define BLACKGUI_MODELSWITHDBKEY_H
|
||||
|
||||
#include "listmodelbase.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>
|
||||
{
|
||||
public:
|
||||
//! Destructor
|
||||
virtual ~CModelsWithDbKeysBase() {}
|
||||
|
||||
//! Keys to be highlighted
|
||||
void setHighlightDbKeys(const QList<KeyType> &keys) { m_highlightIntKeys = keys; }
|
||||
|
||||
//! Set color for highlighting
|
||||
void setHighlightColor(QColor color) { m_highlightColor = color; }
|
||||
|
||||
//! \copydoc CListModelBase::data
|
||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
//! DB key for given index
|
||||
KeyType dbKeyForIndex(const QModelIndex &index) const;
|
||||
|
||||
//! Highlight index
|
||||
bool isHighlightIndex(const QModelIndex &index) const;
|
||||
|
||||
protected:
|
||||
//! \copydoc CListModelBase::CListModelBase
|
||||
CModelsWithDbKeysBase(const QString &translationContext, QObject *parent = nullptr);
|
||||
|
||||
private:
|
||||
QList<KeyType> m_highlightIntKeys; //!< keys to be highlighted
|
||||
QColor m_highlightColor = Qt::green;
|
||||
};
|
||||
} // namespace
|
||||
} // namespace
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user