Ref T472, utility functions for category/category list

This commit is contained in:
Klaus Basan
2019-02-03 21:27:53 +01:00
committed by Mat Sutcliffe
parent 716cd67a2c
commit b85222e1f5
4 changed files with 124 additions and 15 deletions

View File

@@ -79,14 +79,31 @@ namespace BlackMisc
void CAircraftCategory::setLevel(int l1, int l2, int l3)
{
m_level[0] = l1;
m_level[1] = l2;
m_level[2] = l3;
m_l1 = l1;
m_l2 = l2;
m_l3 = l3;
}
bool CAircraftCategory::isFirstLevel() const
{
return (m_l3 == 0 && m_l2 == 0 && m_l1 > 0);
}
int CAircraftCategory::getDepth() const
{
if (this->isFirstLevel()) { return 1; }
if (m_l3 == 0 && m_l2 > 0 && m_l1 > 0) { return 2; }
return 3;
}
QString CAircraftCategory::getLevelString() const
{
return QStringLiteral("%1.%2.%3").arg(m_level[0]).arg(m_level[1]).arg(m_level[2]);
return QStringLiteral("%1.%2.%3").arg(m_l1).arg(m_l2).arg(m_l3);
}
QString CAircraftCategory::getLevelAndName() const
{
return QStringLiteral("%1 %2").arg(this->getLevelString(), this->getName());
}
bool CAircraftCategory::matchesPath(const QString &path, Qt::CaseSensitivity cs)
@@ -132,9 +149,10 @@ namespace BlackMisc
switch (i)
{
case IndexName: return m_name.compare(compareValue.getName(), Qt::CaseInsensitive);
case IndexPath: return m_description.compare(compareValue.getPath(), Qt::CaseInsensitive);
case IndexDescription: return m_path.compare(compareValue.getDescription(), Qt::CaseInsensitive);
case IndexPath: return m_path.compare(compareValue.getPath(), Qt::CaseInsensitive);
case IndexDescription: return m_description.compare(compareValue.getDescription(), Qt::CaseInsensitive);
case IndexAssignable: return Compare::compare(this->isAssignable(), compareValue.isAssignable());
case IndexLevelString: return this->compareByLevel(compareValue);
default: return CValueObject::comparePropertyByIndex(index, *this);
}
Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison");
@@ -156,5 +174,16 @@ namespace BlackMisc
cat.setKeyVersionTimestampFromDatabaseJson(json, prefix);
return cat;
}
int CAircraftCategory::compareByLevel(const CAircraftCategory &other) const
{
// any faster or better way to compare can be used here
int c = Compare::compare(m_l1, other.m_l1);
if (c != 0) { return c; }
c = Compare::compare(m_l2, other.m_l2);
if (c != 0) { return c; }
c = Compare::compare(m_l3, other.m_l3);
return c;
}
} // namespace
} // namespace

View File

@@ -38,7 +38,7 @@ namespace BlackMisc
//! Properties by index
enum ColumnIndex
{
IndexName = BlackMisc::CPropertyIndex::GlobalIndexCAircraftCategory,
IndexName = CPropertyIndex::GlobalIndexCAircraftCategory,
IndexDescription,
IndexLevelString,
IndexPath,
@@ -78,9 +78,21 @@ namespace BlackMisc
//! Level
void setLevel(int l1, int l2, int l3);
//! First level
int getFirstLevel() const { return m_l1; }
//! First level
bool isFirstLevel() const;
//! Depth 1, 2, 3
int getDepth() const;
//! Level string
QString getLevelString() const;
//! Level and name
QString getLevelAndName() const;
//! Matching path?
bool matchesPath(const QString &path, Qt::CaseSensitivity cs);
@@ -108,6 +120,9 @@ namespace BlackMisc
//! Null ICAO?
bool isNull() const;
//! Level compare
int compareByLevel(const CAircraftCategory &other) const;
//! NULL object
static const CAircraftCategory &null();
@@ -119,13 +134,17 @@ namespace BlackMisc
QString m_description; //!< description
QString m_path; //!< path
bool m_assignable = true; //!< can assign to category?
int m_level[3] = { 0, 0, 0};
int m_l1 = 0;
int m_l2 = 0;
int m_l3 = 0;
BLACK_METACLASS(
CAircraftCategory,
BLACK_METAMEMBER(dbKey),
BLACK_METAMEMBER(timestampMSecsSinceEpoch),
BLACK_METAMEMBER(name),
// BLACK_METAMEMBER(level),
BLACK_METAMEMBER(l1),
BLACK_METAMEMBER(l2),
BLACK_METAMEMBER(l3),
BLACK_METAMEMBER(description),
BLACK_METAMEMBER(path),
BLACK_METAMEMBER(assignable)

View File

@@ -33,20 +33,66 @@ namespace BlackMisc
});
}
void CAircraftCategoryList::sortByRank()
void CAircraftCategoryList::sortByPath()
{
this->sortBy(&CAircraftCategory::getPath);
}
void CAircraftCategoryList::sortByLevel()
{
this->sort([](const CAircraftCategory & a, const CAircraftCategory & b)
{
const int c = a.compareByLevel(b);
return c < 0;
});
}
QSet<QString> CAircraftCategoryList::getLevelStrings() const
{
QSet<QString> levels;
for (const CAircraftCategory &category : *this)
{
levels.insert(category.getLevelString());
}
return levels;
}
QList<int> CAircraftCategoryList::getFirstLevels() const
{
QSet<int> levels;
for (const CAircraftCategory &category : *this)
{
levels.insert(category.getFirstLevel());
}
QList<int> ll = levels.toList();
std::sort(ll.begin(), ll.end());
return ll;
}
CAircraftCategoryList CAircraftCategoryList::findByFirstLevel(int level) const
{
CAircraftCategoryList categories;
for (const CAircraftCategory &category : *this)
{
if (category.getFirstLevel() != level) { continue; }
}
return categories;
}
CAircraftCategoryList CAircraftCategoryList::findFirstLevels() const
{
return this->findBy(&CAircraftCategory::isFirstLevel, true);
}
CAircraftCategoryList CAircraftCategoryList::fromDatabaseJson(const QJsonArray &array)
{
CAircraftCategoryList codes;
CAircraftCategoryList categories;
for (const QJsonValue &value : array)
{
const CAircraftCategory category(CAircraftCategory::fromDatabaseJson(value.toObject()));
codes.push_back(category);
categories.push_back(category);
}
return codes;
return categories;
}
} // namespace

View File

@@ -21,7 +21,7 @@
#include <QJsonArray>
#include <QMetaType>
#include <QStringList>
#include <QSet>
#include <tuple>
namespace BlackMisc
@@ -47,7 +47,22 @@ namespace BlackMisc
CAircraftCategoryList findByName(const QString &name, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
//! Sort by path
void sortByRank();
void sortByPath();
//! Sort by level
void sortByLevel();
//! Get all level strings
QSet<QString> getLevelStrings() const;
//! All levels sorted
QList<int> getFirstLevels() const;
//! Find by first level
CAircraftCategoryList findByFirstLevel(int level) const;
//! Find first levels
CAircraftCategoryList findFirstLevels() const;
//! From our database JSON format
static CAircraftCategoryList fromDatabaseJson(const QJsonArray &array);