From 8e84f632b0bd59237cb9e26bc389515415ad17eb Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 6 Mar 2017 02:33:04 +0100 Subject: [PATCH] refs #899, specialized view for callsign based views --- src/blackgui/views/viewcallsignobjects.cpp | 110 +++++++++++++++++++++ src/blackgui/views/viewcallsignobjects.h | 60 +++++++++++ 2 files changed, 170 insertions(+) create mode 100644 src/blackgui/views/viewcallsignobjects.cpp create mode 100644 src/blackgui/views/viewcallsignobjects.h diff --git a/src/blackgui/views/viewcallsignobjects.cpp b/src/blackgui/views/viewcallsignobjects.cpp new file mode 100644 index 000000000..8c219832e --- /dev/null +++ b/src/blackgui/views/viewcallsignobjects.cpp @@ -0,0 +1,110 @@ +/* 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 "blackgui/views/viewcallsignobjects.h" +#include "blackgui/models/atcstationlistmodel.h" +#include "blackgui/models/simulatedaircraftlistmodel.h" +#include "blackmisc/icons.h" + +#include +#include +#include +#include +#include +#include + +using namespace BlackMisc; +using namespace BlackMisc::Aviation; +using namespace BlackGui; +using namespace BlackGui::Models; + +namespace BlackGui +{ + namespace Views + { + template + CViewWithCallsignObjects::CViewWithCallsignObjects(QWidget *parent) : + CViewBase(parent) + { + // void + } + + template + void CViewWithCallsignObjects::selectCallsign(const CCallsign &callsign) + { + const CCallsignSet cs({callsign}); + this->selectCallsigns(cs); + } + + template + void CViewWithCallsignObjects::selectCallsigns(const CCallsignSet &callsigns) + { + if (callsigns.isEmpty()) { return; } + this->clearSelection(); + int r = -1; + QSet rows; + for (const ObjectType &obj : CViewBase::containerOrFilteredContainer()) + { + r++; + if (callsigns.contains(obj.getCallsign())) + { + rows.insert(r); + } + } + this->selectRows(rows); + } + + template + CCallsignSet CViewWithCallsignObjects::selectedCallsigns() const + { + if (!this->hasSelection()) { return CCallsignSet(); } + const ContainerType selected(this->selectedObjects()); + return selected.getCallsigns(); + } + + template + int CViewWithCallsignObjects::removeCallsigns(const CCallsignSet &callsigns) + { + if (callsigns.isEmpty()) { return 0; } + if (this->isEmpty()) { return 0; } + + ContainerType newObjects(this->container()); + int delta = newObjects.removeByCallsigns(callsigns); + if (delta > 0) + { + this->updateContainerMaybeAsync(newObjects); + } + return delta; + } + + template + int CViewWithCallsignObjects::replaceOrAddObjectsByCallsign(const ContainerType &container) + { + if (container.isEmpty()) { return 0; } + ContainerType copy(this->container()); + int c = copy.replaceOrAddObjectsByCallsign(container); + if (c == 0) { return 0; } + this->updateContainerMaybeAsync(copy); + return c; + } + + template + void CViewWithCallsignObjects::selectObjects(const ContainerType &selectedObjects) + { + if (!selectedObjects.isEmpty()) + { + this->selectCallsigns(selectedObjects.getCallsigns()); + } + } + + template class CViewWithCallsignObjects; + template class CViewWithCallsignObjects; + + } // namespace +} // namespace diff --git a/src/blackgui/views/viewcallsignobjects.h b/src/blackgui/views/viewcallsignobjects.h new file mode 100644 index 000000000..f5d299269 --- /dev/null +++ b/src/blackgui/views/viewcallsignobjects.h @@ -0,0 +1,60 @@ +/* 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_VIEWS_VIEWCALLSIGNOBJECTS_H +#define BLACKGUI_VIEWS_VIEWCALLSIGNOBJECTS_H + +#include "viewbase.h" +#include +#include +#include +#include + +class QAction; +class QIntValidator; +class QLineEdit; +class QWidget; + +namespace BlackGui +{ + namespace Menus { class CMenuActions; } + namespace Views + { + //! Base class for views with DB objects + template class CViewWithCallsignObjects : + public CViewBase + { + public: + //! Select callsign + void selectCallsign(const BlackMisc::Aviation::CCallsign &callsign); + + //! Select given callsigns + void selectCallsigns(const BlackMisc::Aviation::CCallsignSet &callsigns); + + //! Get selected callsigns + BlackMisc::Aviation::CCallsignSet selectedCallsigns() const; + + //! Remove callsigns + int removeCallsigns(const BlackMisc::Aviation::CCallsignSet &callsigns); + + //! Update or insert data (based on callsign) + int replaceOrAddObjectsByCallsign(const ContainerType &container); + + //! Reselect by callsigns + virtual void selectObjects(const ContainerType &selectedObjects) override; + + protected: + //! Constructor + explicit CViewWithCallsignObjects(QWidget *parent = nullptr); + }; + } // namespace +} // namespace +#endif // guard