/* 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" #include "blackmisc/db/datastoreobjectlist.h" #include "blackmisc/orderable.h" #include #include #include using namespace BlackMisc; using namespace BlackMisc::Aviation; using namespace BlackMisc::Db; namespace BlackGui { namespace Models { template CListModelDbObjects::CListModelDbObjects(const QString &translationContext, QObject *parent) : CListModelBase(translationContext, parent) { constexpr bool hasIntegerKey = std::is_base_of::value && std::is_same::value; constexpr bool hasStringKey = std::is_base_of::value && std::is_base_of::value; static_assert(hasIntegerKey || hasStringKey, "ObjectType needs to implement IDatastoreObjectWithXXXXKey and have appropriate KeyType"); } template QVariant CListModelDbObjects::data(const QModelIndex &index, int role) const { if (role != Qt::BackgroundRole) { return CListModelBase::data(index, role); } if (isHighlightIndex(index)) { return QBrush(m_highlightColor); } return CListModelBase::data(index, role); } template KeyType CListModelDbObjects::dbKeyForIndex(const QModelIndex &index) const { if (!index.isValid()) { return ObjectType::invalidDbKey(); } return this->at(index).getDbKey(); } template bool CListModelDbObjects::isHighlightIndex(const QModelIndex &index) const { if (!index.isValid()) { return false; } if (m_highlightKeys.isEmpty()) { return false; } return m_highlightKeys.contains(dbKeyForIndex(index)); } template COrderableListModelDbObjects::COrderableListModelDbObjects(const QString &translationContext, QObject *parent) : CListModelDbObjects(translationContext, parent) { } template void COrderableListModelDbObjects::moveItems(const ContainerType &items, int position) { if (items.isEmpty()) { return; } ContainerType container(this->container()); int order = 0; if (position >= 0 && position < container.size()) { order = container[position].getOrder(); } this->setSortColumnToOrder(); container.moveTo(items, order); // update container this->updateContainerMaybeAsync(container); } template void COrderableListModelDbObjects::setSortColumnToOrder() { // force sorted by order, otherwise display looks confusing this->setSorting(IOrderable::IndexOrder); } template int COrderableListModelDbObjects::update(const ContainerType &container, bool sort) { if (container.needsOrder()) { ContainerType orderable(container); orderable.resetOrder(); return CListModelDbObjects::update(orderable, sort); } return CListModelDbObjects::update(container, sort); } // see here for the reason of thess forward instantiations // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl template class CListModelDbObjects; template class CListModelDbObjects; template class CListModelDbObjects; template class CListModelDbObjects; template class CListModelDbObjects; template class CListModelDbObjects; template class COrderableListModelDbObjects; template class COrderableListModelDbObjects; } // namespace } // namespace