refs #640, value object orderable interface (same as ITimestampBased ..)

* interface and list
* added Q_DECLARE_INTERFACE
This commit is contained in:
Klaus Basan
2016-04-18 23:24:39 +02:00
parent 5d39c906e4
commit 5f2110b81a
11 changed files with 392 additions and 11 deletions

View File

@@ -24,6 +24,8 @@ namespace BlackMisc
*/
class BLACKMISC_EXPORT IDatastoreObjectWithIntegerKey : public ITimestampBased
{
Q_INTERFACES(BlackMisc::ITimestampBased)
public:
//! Property index
enum ColumnIndex
@@ -95,6 +97,8 @@ namespace BlackMisc
*/
class BLACKMISC_EXPORT IDatastoreObjectWithStringKey : public ITimestampBased
{
Q_INTERFACES(BlackMisc::ITimestampBased)
public:
//! Property index
enum ColumnIndex
@@ -151,4 +155,7 @@ namespace BlackMisc
} // namespace
Q_DECLARE_INTERFACE(BlackMisc::IDatastoreObjectWithIntegerKey, "org.swift-project.blackmisc.idatastoreobjectwithintegerkey")
Q_DECLARE_INTERFACE(BlackMisc::IDatastoreObjectWithStringKey, "org.swift-project.blackmisc.idatastoreobjectwithstringkey")
#endif // guard

View File

@@ -0,0 +1,87 @@
/* 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 "orderable.h"
#include "blackmisc/comparefunctions.h"
#include "blackmisc/variant.h"
#include <limits>
namespace BlackMisc
{
IOrderable::IOrderable() { }
IOrderable::IOrderable(int order) : m_order(order)
{ }
QString IOrderable::getOrderAsString() const
{
if (this->hasValidOrder()) { return QString::number(this->getOrder()); }
return "-";
}
bool IOrderable::hasValidOrder() const
{
return this->getOrder() >= 0;
}
bool IOrderable::canHandleIndex(const CPropertyIndex &index)
{
if (index.isEmpty()) { return false; }
int i = index.frontCasted<int>();
return (i >= static_cast<int>(IndexOrder)) && (i <= static_cast<int>(IndexOrderString));
}
CVariant IOrderable::propertyByIndex(const CPropertyIndex &index) const
{
if (!index.isEmpty())
{
ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexOrder:
return CVariant::fromValue(this->m_order);
case IndexOrderString:
return CVariant::fromValue(this->getOrderAsString());
default:
break;
}
}
const QString m = QString("Cannot handle index %1").arg(index.toQString());
Q_ASSERT_X(false, Q_FUNC_INFO, m.toLocal8Bit().constData());
return CVariant::fromValue(m);
}
void IOrderable::setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index)
{
if (!index.isEmpty())
{
ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexOrder:
this->setOrder(variant.toInt());
return;
case IndexOrderString:
default:
break;
}
}
const QString m = QString("Cannot handle index %1").arg(index.toQString());
Q_ASSERT_X(false, Q_FUNC_INFO, m.toLocal8Bit().constData());
}
int IOrderable::comparePropertyByIndex(const IOrderable &compareValue, const CPropertyIndex &index) const
{
Q_UNUSED(index);
static const int max = std::numeric_limits<int>::max();
const int o1 = this->hasValidOrder() ? this->getOrder() : max;
const int o2 = compareValue.hasValidOrder() ? compareValue.getOrder() : max;
return Compare::compare(o1, o2);
}
} // namespace

68
src/blackmisc/orderable.h Normal file
View File

@@ -0,0 +1,68 @@
/* 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 BLACKMISC_ORDERABLE_H
#define BLACKMISC_ORDERABLE_H
#include "blackmiscexport.h"
#include "propertyindex.h"
namespace BlackMisc
{
//! Entity with timestamp
class BLACKMISC_EXPORT IOrderable
{
public:
//! Properties by index
enum ColumnIndex
{
IndexOrder = BlackMisc::CPropertyIndex::GlobalIndexIOrderable,
IndexOrderString
};
//! Order
int getOrder() const { return m_order; }
//! Order as string
QString getOrderAsString() const;
//! Set order
void setOrder(int order) { m_order = order; }
//! Valid order set?
bool hasValidOrder() const;
//! Can given index be handled
static bool canHandleIndex(const BlackMisc::CPropertyIndex &index);
protected:
//! Constructor
IOrderable();
//! Constructor
IOrderable(int order);
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index);
//! Compare for index
int comparePropertyByIndex(const IOrderable &compareValue, const CPropertyIndex &index) const;
int m_order = -1; //!< order number
};
} // namespace
Q_DECLARE_INTERFACE(BlackMisc::IOrderable, "org.swift-project.blackmisc.iorderable")
#endif // guard

View File

@@ -0,0 +1,142 @@
/* Copyright (C) 2016
* 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 "blackmisc/orderablelist.h"
#include "blackmisc/simulation/distributorlist.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include <algorithm>
#include <iterator>
namespace BlackMisc
{
template <class OBJ, class CONTAINER>
IOrderableList<OBJ, CONTAINER>::IOrderableList()
{ }
template <class OBJ, class CONTAINER>
const CONTAINER &IOrderableList<OBJ, CONTAINER>::container() const
{
return static_cast<const CONTAINER &>(*this);
}
template <class OBJ, class CONTAINER>
CONTAINER &IOrderableList<OBJ, CONTAINER>::container()
{
return static_cast<CONTAINER &>(*this);
}
template <class OBJ, class CONTAINER>
void IOrderableList<OBJ, CONTAINER>::sortDescendingByOrder()
{
IOrderableList::container().sortAscendingByOrder();
std::reverse(this->container().begin(), this->container().end());
}
template <class OBJ, class CONTAINER>
void IOrderableList<OBJ, CONTAINER>::sortAscendingByOrder()
{
IOrderableList::container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getOrder));
}
template <class OBJ, class CONTAINER>
void IOrderableList<OBJ, CONTAINER>::resetOrder(int offset)
{
int c = offset;
for (OBJ &obj : container())
{
obj.setOrder(c++);
}
}
template <class OBJ, class CONTAINER>
bool IOrderableList<OBJ, CONTAINER>::needsOrder() const
{
for (const OBJ &obj : container())
{
if (!obj.hasValidOrder()) { return true; }
}
return false;
}
template <class OBJ, class CONTAINER>
QList<int> IOrderableList<OBJ, CONTAINER>::orderValues() const
{
QList<int> orders;
for (const OBJ &obj : container())
{
if (!obj.hasValidOrder()) { continue; }
orders.append(obj.getOrder());
}
return orders;
}
template <class OBJ, class CONTAINER>
CONTAINER IOrderableList<OBJ, CONTAINER>::withoutItemsOfSameOrder(const CONTAINER &items) const
{
const QList<int> orders = items.orderValues();
if (orders.isEmpty()) { return this->container(); }
CONTAINER newContainer;
for (const OBJ &obj : container())
{
if (orders.contains(obj.getOrder())) { continue; }
newContainer.push_back(obj);
}
return newContainer;
}
template <class OBJ, class CONTAINER>
void IOrderableList<OBJ, CONTAINER>::removeItemsWithSameOrder(const CONTAINER &items)
{
const QList<int> orders = items.orderValues();
if (orders.isEmpty()) { return; }
CONTAINER newContainer;
for (const OBJ &obj : container())
{
if (orders.contains(obj.getOrder())) { continue; }
newContainer.push_back(obj);
}
this->container() = newContainer;
}
template <class OBJ, class CONTAINER>
void IOrderableList<OBJ, CONTAINER>::moveTo(const CONTAINER &items, int targetOrder)
{
if (items.isEmpty()) { return; }
CONTAINER newContainer(this->withoutItemsOfSameOrder(items)); // this container without items
CONTAINER newItems(items);
const int shift = items.size();
newContainer.sortAscendingByOrder(); // sorted by old order
newContainer.resetOrder();
for (OBJ &obj : newContainer)
{
if (obj.getOrder() < targetOrder) { continue; }
obj.setOrder(obj.getOrder() + shift);
}
newItems.sortAscendingByOrder(); // sort by old order
newItems.resetOrder(targetOrder);
newContainer.push_back(newItems);
this->container() = newContainer;
}
template <class OBJ, class CONTAINER>
void IOrderableList<OBJ, CONTAINER>::freezeOrder()
{
int c = 0;
for (OBJ &obj : container())
{
obj.setOrder(c++);
}
}
template class IOrderableList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
template class IOrderableList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
} // namespace

View File

@@ -0,0 +1,78 @@
/* Copyright (C) 2016
* 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 BLACKMISC_ORDERABLELIST_H
#define BLACKMISC_ORDERABLELIST_H
#include "blackmisc/collection.h"
#include "blackmisc/sequence.h"
namespace BlackMisc
{
//! List of orderable IOrderable objects
template<class OBJ, class CONTAINER>
class IOrderableList
{
public:
//! Sort ascending
void sortAscendingByOrder();
//! Sort descending
void sortDescendingByOrder();
//! Set order member to current order
void resetOrder(int offset = 0);
//! All order values set or missing some?
bool needsOrder() const;
//! All order values IOrderable::order
QList<int> orderValues() const;
//! Items with order will not be included
CONTAINER withoutItemsOfSameOrder(const CONTAINER &items) const;
//! Remove the items based on their order IOrderable::order
void removeItemsWithSameOrder(const CONTAINER &items);
//! Move items to given order
void moveTo(const CONTAINER &items, int targetOrder);
//! Current order of list will be new order values
void freezeOrder();
protected:
//! Constructor
IOrderableList();
//! Container
const CONTAINER &container() const;
//! Container
CONTAINER &container();
};
//! \cond PRIVATE
namespace Simulation
{
class CDistributor;
class CDistributorList;
class CAircraftModel;
class CAircraftModelList;
}
extern template class BLACKMISC_EXPORT_TEMPLATE IOrderableList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
extern template class BLACKMISC_EXPORT_TEMPLATE IOrderableList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
//! \endcond
} //namespace
#endif //guard

View File

@@ -50,9 +50,10 @@ namespace BlackMisc
GlobalIndexCStatusMessage = 200,
GlobalIndexCNameVariantPair = 300,
GlobalIndexITimestampBased = 400,
GlobalIndexCIdentifier = 500,
GlobalIndexCRgbColor = 600,
GlobalIndexCCountry = 700,
GlobalIndexIOrderable = 500,
GlobalIndexCIdentifier = 600,
GlobalIndexCRgbColor = 700,
GlobalIndexCCountry = 800,
GlobalIndexCCallsign = 1000,
GlobalIndexCAircraftSituation = 1100,
GlobalIndexCAtcStation = 1200,

View File

@@ -328,6 +328,7 @@ namespace BlackMisc
CAircraftModel,
BLACK_METAMEMBER(dbKey),
BLACK_METAMEMBER(timestampMSecsSinceEpoch),
BLACK_METAMEMBER(order),
BLACK_METAMEMBER(callsign),
BLACK_METAMEMBER(aircraftIcao),
BLACK_METAMEMBER(livery),

View File

@@ -30,6 +30,9 @@ namespace BlackMisc
public BlackMisc::IDatastoreObjectWithStringKey,
public BlackMisc::IOrderable
{
Q_INTERFACES(BlackMisc::IDatastoreObjectWithStringKey)
Q_INTERFACES(BlackMisc::IOrderable)
public:
//! Property indexes
enum ColumnIndex

View File

@@ -18,7 +18,6 @@
namespace BlackMisc
{
//! Entity with timestamp
class BLACKMISC_EXPORT ITimestampBased
{
@@ -116,7 +115,8 @@ namespace BlackMisc
qint64 m_timestampMSecsSinceEpoch; //!< timestamp value
};
} // namespace
Q_DECLARE_INTERFACE(BlackMisc::ITimestampBased, "org.swift-project.blackmisc.itimestampbased")
#endif // guard

View File

@@ -183,12 +183,9 @@ namespace BlackMisc
template class ITimestampObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
template class ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
template class ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
template class ITimestampObjectList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
template class ITimestampObjectList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
template class ITimestampObjectList<BlackMisc::Network::CTextMessage, BlackMisc::Network::CTextMessageList>;
template class ITimestampObjectList<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>;
template class ITimestampObjectList<BlackMisc::CIdentifier, BlackMisc::CIdentifierList>;
template class ITimestampObjectList<BlackMisc::CCountry, BlackMisc::CCountryList>;

View File

@@ -124,12 +124,9 @@ namespace BlackMisc
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::Network::CTextMessage, BlackMisc::Network::CTextMessageList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::CIdentifier, BlackMisc::CIdentifierList>;
extern template class BLACKMISC_EXPORT_TEMPLATE ITimestampObjectList<BlackMisc::CCountry, BlackMisc::CCountryList>;