From bb16d5d069368b404a87bafdd614b71749ac389b Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 25 Mar 2016 03:27:15 +0100 Subject: [PATCH] refs #618, allow to display icon when DB key is set --- src/blackmisc/datastore.cpp | 34 ++++++++++++++++++++++++---------- src/blackmisc/datastore.h | 12 ++++++++++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/blackmisc/datastore.cpp b/src/blackmisc/datastore.cpp index 1c1022a70..62da269ab 100644 --- a/src/blackmisc/datastore.cpp +++ b/src/blackmisc/datastore.cpp @@ -34,6 +34,13 @@ namespace BlackMisc this->m_dbKey = k; } + const CIcon &IDatastoreObjectWithIntegerKey::toDatabaseIcon() const + { + static const CIcon empty; + if (this->hasValidDbKey()) { return CIconList::iconByIndex(CIcons::StandardIconDatabaseKey16); } + return empty; + } + int IDatastoreObjectWithIntegerKey::stringToDbKey(const QString &candidate) { if (candidate.isEmpty()) { return invalidDbKey(); } @@ -69,8 +76,8 @@ namespace BlackMisc switch (i) { case IndexDbIntegerKey: return CVariant::from(this->m_dbKey); - default: - break; + case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon()); + default: break; } return CVariant(); } @@ -100,10 +107,9 @@ namespace BlackMisc ColumnIndex i = index.frontCasted(); switch (i) { - case IndexDbIntegerKey: - return Compare::compare(this->m_dbKey, compareValue.getDbKey()); - default: - break; + case IndexDbIntegerKey: return Compare::compare(this->m_dbKey, compareValue.getDbKey()); + case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey()); + default: break; } Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed"); return 0; @@ -113,7 +119,7 @@ namespace BlackMisc { if (ITimestampBased::canHandleIndex(index)) { return true;} int i = index.frontCasted(); - return (i >= static_cast(IndexDbIntegerKey)) && (i <= static_cast(IndexDbIntegerKey)); + return (i >= static_cast(IndexDbIntegerKey)) && (i <= static_cast(IndexDatabaseIcon)); } QJsonValue IDatastoreObjectWithStringKey::getDbKeyAsJsonValue() const @@ -123,6 +129,13 @@ namespace BlackMisc return null; } + const CIcon &IDatastoreObjectWithStringKey::toDatabaseIcon() const + { + static const CIcon empty; + if (this->hasValidDbKey()) { return CIconList::iconByIndex(CIcons::StandardIconDatabaseKey16); } + return empty; + } + void IDatastoreObjectWithStringKey::setKeyAndTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix) { QString dbKey = json.value(prefix + "id").toString(); @@ -143,6 +156,7 @@ namespace BlackMisc switch (i) { case IndexDbStringKey: return CVariant::from(this->m_dbKey); + case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon()); default: break; } @@ -174,8 +188,8 @@ namespace BlackMisc ColumnIndex i = index.frontCasted(); switch (i) { - case IndexDbStringKey: - return this->m_dbKey.compare(compareValue.getDbKey()); + case IndexDbStringKey: return this->m_dbKey.compare(compareValue.getDbKey()); + case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey()); default: break; } @@ -188,7 +202,7 @@ namespace BlackMisc if (index.isEmpty()) { return false; } if (ITimestampBased::canHandleIndex(index)) { return true;} int i = index.frontCasted(); - return (i >= static_cast(IndexDbStringKey)) && (i <= static_cast(IndexDbStringKey)); + return (i >= static_cast(IndexDbStringKey)) && (i <= static_cast(IndexDatabaseIcon)); } } // namespace diff --git a/src/blackmisc/datastore.h b/src/blackmisc/datastore.h index 39051873f..5ab878287 100644 --- a/src/blackmisc/datastore.h +++ b/src/blackmisc/datastore.h @@ -28,7 +28,8 @@ namespace BlackMisc //! Property index enum ColumnIndex { - IndexDbIntegerKey = CPropertyIndex::GlobalIndexIDatastoreInteger + IndexDbIntegerKey = CPropertyIndex::GlobalIndexIDatastoreInteger, + IndexDatabaseIcon }; //! Get DB key. @@ -52,6 +53,9 @@ namespace BlackMisc //! Has valid DB key bool hasValidDbKey() const { return m_dbKey >= 0; } + //! Database icon if this has valid key, otherwise empty + const CIcon &toDatabaseIcon() const; + //! Invalid key static int invalidDbKey() { return -1; } @@ -95,7 +99,8 @@ namespace BlackMisc //! Property index enum ColumnIndex { - IndexDbStringKey = CPropertyIndex::GlobalIndexIDatastoreString + IndexDbStringKey = CPropertyIndex::GlobalIndexIDatastoreString, + IndexDatabaseIcon }; //! Get DB key. @@ -110,6 +115,9 @@ namespace BlackMisc //! Has valid DB key bool hasValidDbKey() const { return !m_dbKey.isEmpty(); } + //! Database icon if this has valid key, otherwise empty + const CIcon &toDatabaseIcon() const; + //! Invalid key static QString invalidDbKey() { return ""; }