From 949407c1a71ff3641a6ac1ee791e46a0e3c4bd46 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 6 Mar 2017 02:32:29 +0100 Subject: [PATCH] refs #899, list model for callsign based models (callsign is unique key) --- .../models/listmodelcallsignobjects.cpp | 59 +++++++++++++++ .../models/listmodelcallsignobjects.h | 74 +++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/blackgui/models/listmodelcallsignobjects.cpp create mode 100644 src/blackgui/models/listmodelcallsignobjects.h diff --git a/src/blackgui/models/listmodelcallsignobjects.cpp b/src/blackgui/models/listmodelcallsignobjects.cpp new file mode 100644 index 000000000..446378e45 --- /dev/null +++ b/src/blackgui/models/listmodelcallsignobjects.cpp @@ -0,0 +1,59 @@ +/* Copyright (C) 2017 + * 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 "listmodelcallsignobjects.h" +#include "allmodelcontainers.h" +#include "blackmisc/db/datastoreobjectlist.h" +#include "blackmisc/orderable.h" + +#include +#include +#include + +using namespace BlackMisc; +using namespace BlackMisc::Aviation; + +namespace BlackGui +{ + namespace Models + { + template + CListModelCallsignObjects::CListModelCallsignObjects(const QString &translationContext, QObject *parent) : + CListModelBase(translationContext, parent) + { } + + template + QVariant CListModelCallsignObjects::data(const QModelIndex &index, int role) const + { + if (role != Qt::BackgroundRole) { return CListModelBase::data(index, role); } + if (isHighlightedIndex(index)) { return QBrush(m_highlightColor); } + return CListModelBase::data(index, role); + } + + template + BlackMisc::Aviation::CCallsign CListModelCallsignObjects::callsignForIndex(const QModelIndex &index) const + { + if (!index.isValid()) { return CCallsign(); } + return this->at(index).getCallsign(); + } + + template + bool CListModelCallsignObjects::isHighlightedIndex(const QModelIndex &index) const + { + if (!index.isValid()) { return false; } + if (m_highlightCallsigns.isEmpty()) { return false; } + return m_highlightCallsigns.contains(callsignForIndex(index)); + } + + // see here for the reason of thess forward instantiations + // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl + template class CListModelCallsignObjects; + template class CListModelCallsignObjects; + } // namespace +} // namespace diff --git a/src/blackgui/models/listmodelcallsignobjects.h b/src/blackgui/models/listmodelcallsignobjects.h new file mode 100644 index 000000000..b5286b6b3 --- /dev/null +++ b/src/blackgui/models/listmodelcallsignobjects.h @@ -0,0 +1,74 @@ +/* Copyright (C) 2017 + * 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_LISTMODELCALLSIGNOBJECTS_H +#define BLACKGUI_MODELS_LISTMODELCALLSIGNOBJECTS_H + +#include "blackgui/models/listmodelbase.h" +#include "blackmisc/aviation/callsignset.h" + +#include +#include +#include +#include +#include + +class QModelIndex; +class QObject; + +namespace BlackGui +{ + namespace Models + { + //! List model for callsign based objects (callsign is unique key) + template class CListModelCallsignObjects : + public CListModelBase + { + public: + //! Destructor + virtual ~CListModelCallsignObjects() {} + + //! Keys to be highlighted + void setHighlightedCallsigns(const BlackMisc::Aviation::CCallsignSet &callsigns) { m_highlightCallsigns = callsigns; } + + //! Clear the highlighted callsign + void clearHighlightedCallsigns() { m_highlightCallsigns.clear(); } + + //! \copydoc BlackGui::Models::CListModelBaseNonTemplate::clearHighlighting + virtual void clearHighlighting() override + { + this->clearHighlightedCallsigns(); + CListModelBase::clearHighlighting(); + } + + //! Set color for highlighting + void setHighlightColor(QColor color) { m_highlightColor = color; } + + //! Get data for index and role + virtual QVariant data(const QModelIndex &index, int role) const override; + + //! Callsign for given index + BlackMisc::Aviation::CCallsign callsignForIndex(const QModelIndex &index) const; + + //! Highlight index? + bool isHighlightedIndex(const QModelIndex &index) const; + + protected: + //! Constructor + CListModelCallsignObjects(const QString &translationContext, QObject *parent = nullptr); + + private: + BlackMisc::Aviation::CCallsignSet m_highlightCallsigns; //!< callsigns to be highlighted + QColor m_highlightColor = Qt::green; + }; + } // namespace +} // namespace +#endif // guard