From 056841f9b14207121354ed858cd62144ce65df81 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 24 Jun 2016 17:20:18 +0200 Subject: [PATCH] refs #685, settings for readers (value object, traits) --- src/blackcore/registermetadata.cpp | 5 +- src/blackcore/settings/reader.cpp | 78 +++++++++++++++++ src/blackcore/settings/reader.h | 133 +++++++++++++++++++++++++++++ src/blackmisc/propertyindex.h | 1 + 4 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 src/blackcore/settings/reader.cpp create mode 100644 src/blackcore/settings/reader.h diff --git a/src/blackcore/registermetadata.cpp b/src/blackcore/registermetadata.cpp index 175cde116..569fcda55 100644 --- a/src/blackcore/registermetadata.cpp +++ b/src/blackcore/registermetadata.cpp @@ -10,6 +10,7 @@ #include "blackcore/registermetadata.h" #include "blackcore/contextapplication.h" #include "blackcore/db/databasereader.h" +#include "blackcore/settings/reader.h" #include "blackcore/data/globalsetup.h" #include "blackcore/data/updateinfo.h" #include "blackcore/data/vatsimsetup.h" @@ -23,8 +24,6 @@ #include #include - - namespace BlackCore { void registerMetadata() @@ -43,5 +42,7 @@ namespace BlackCore BlackCore::Data::CGlobalSetup::registerMetadata(); BlackCore::Data::CUpdateInfo::registerMetadata(); BlackCore::Data::CVatsimSetup::registerMetadata(); + + BlackCore::Settings::CSettingsReader::registerMetadata(); } } // namespace diff --git a/src/blackcore/settings/reader.cpp b/src/blackcore/settings/reader.cpp new file mode 100644 index 000000000..678f07690 --- /dev/null +++ b/src/blackcore/settings/reader.cpp @@ -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. + */ + +#include "reader.h" + +using namespace BlackMisc; +using namespace BlackMisc::PhysicalQuantities; + +namespace BlackCore +{ + namespace Settings + { + CSettingsReader::CSettingsReader() + { } + + CSettingsReader::CSettingsReader(const CTime &initialTime, const CTime &periodicTime, bool neverUpdate) : + m_initialTime(initialTime), m_periodicTime(periodicTime), m_neverUpdate(neverUpdate) + { } + + QString CSettingsReader::convertToQString(bool i18n) const + { + QString s("CReaderSettings"); + s.append(" ").append(this->m_initialTime.convertToQString(i18n)); + s.append(" ").append(this->m_periodicTime.convertToQString(i18n)); + return s; + } + + const CSettingsReader &CSettingsReader::neverUpdateSettings() + { + static const CSettingsReader s(CTime{ 1.0, CTimeUnit::d()}, CTime{ 1.0, CTimeUnit::d()}, true); + return s; + } + + CVariant CSettingsReader::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexInitialTime: + return this->m_initialTime.propertyByIndex(index.copyFrontRemoved()); + case IndexPeriodicTime: + return this->m_periodicTime.propertyByIndex(index.copyFrontRemoved()); + case IndexNeverUpdate: + return CVariant::fromValue(this->m_neverUpdate); + default: + return CValueObject::propertyByIndex(index); + } + } + + void CSettingsReader::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexInitialTime: + this->m_initialTime.setPropertyByIndex(index.copyFrontRemoved(), variant); + break; + case IndexPeriodicTime: + this->m_periodicTime.setPropertyByIndex(index.copyFrontRemoved(), variant); + break; + case IndexNeverUpdate: + this->m_neverUpdate = variant.toBool(); + break; + default: + CValueObject::setPropertyByIndex(index, variant); + break; + } + } + } // ns +} // ns diff --git a/src/blackcore/settings/reader.h b/src/blackcore/settings/reader.h new file mode 100644 index 000000000..b45521160 --- /dev/null +++ b/src/blackcore/settings/reader.h @@ -0,0 +1,133 @@ +/* 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 BLACKCORE_SETTINGS_READER_H +#define BLACKCORE_SETTINGS_READER_H + +#include "blackcore/blackcoreexport.h" +#include "blackmisc/settingscache.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/pq/time.h" + +namespace BlackCore +{ + namespace Settings + { + /*! + * Settings used with readers + */ + class BLACKCORE_EXPORT CSettingsReader : public BlackMisc::CValueObject + { + public: + //! Properties by index + enum ColumnIndex + { + IndexInitialTime = BlackMisc::CPropertyIndex::GlobalIndexCSettingsReaders, + IndexPeriodicTime, + IndexNeverUpdate + }; + + //! Default constructor. + CSettingsReader(); + + //! Simplified constructor + CSettingsReader(const BlackMisc::PhysicalQuantities::CTime &initialTime, const BlackMisc::PhysicalQuantities::CTime &periodicTime, bool neverUpdate = false); + + //! Get time + const BlackMisc::PhysicalQuantities::CTime &getInitialTime() const { return m_initialTime; } + + //! Set time + void setInitialTime(const BlackMisc::PhysicalQuantities::CTime &time) { m_initialTime = time; } + + //! Get time + const BlackMisc::PhysicalQuantities::CTime &getPeriodicTime() const { return m_periodicTime; } + + //! Set time + void setPeriodicTime(const BlackMisc::PhysicalQuantities::CTime &time) { m_periodicTime = time; } + + //! Never ever update? + bool isNeverUpdate() const { return m_neverUpdate; } + + //! \copydoc BlackMisc::Mixin::Index::propertyByIndex + BlackMisc::CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex + void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const BlackMisc::CVariant &variant); + + //! \copydoc BlackMisc::Mixin::String::toQString + QString convertToQString(bool i18n = false) const; + + //! Settings used when a reader is manually triggered and never updates + static const CSettingsReader &neverUpdateSettings(); + + private: + BlackMisc::PhysicalQuantities::CTime m_initialTime { 30.0, BlackMisc::PhysicalQuantities::CTimeUnit::s()}; + BlackMisc::PhysicalQuantities::CTime m_periodicTime { 30.0, BlackMisc::PhysicalQuantities::CTimeUnit::s()}; + bool m_neverUpdate = false; + + BLACK_METACLASS( + CSettingsReader, + BLACK_METAMEMBER(initialTime), + BLACK_METAMEMBER(periodicTime), + BLACK_METAMEMBER(neverUpdate) + ); + }; + + //! Reader settings + struct SettingsVatsimBookings : public BlackMisc::CSettingTrait + { + //! \copydoc BlackCore::CSettingTrait::key + static const char *key() { return "vatsimreaders/bookings"; } + + //! \copydoc BlackCore::CSettingTrait::defaultValue + static const CSettingsReader &defaultValue() + { + static const CSettingsReader reader {}; + return reader; + } + }; + + //! Reader settings + struct SettingsVatsimDataFile : public BlackMisc::CSettingTrait + { + //! \copydoc BlackCore::CSettingTrait::key + static const char *key() { return "vatsimreaders/datafile"; } + + //! \copydoc BlackCore::CSettingTrait::defaultValue + static const CSettingsReader &defaultValue() + { + static const CSettingsReader reader {}; + return reader; + } + }; + + //! Reader settings + struct SettingsVatsimMetars : public BlackMisc::CSettingTrait + { + //! \copydoc BlackCore::CSettingTrait::key + static const char *key() { return "vatsimreaders/metars"; } + + //! \copydoc BlackCore::CSettingTrait::defaultValue + static const CSettingsReader &defaultValue() + { + static const CSettingsReader reader {}; + return reader; + } + }; + + } // ns +} // ns + +Q_DECLARE_METATYPE(BlackCore::Settings::CSettingsReader) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index d5e539688..75c5a1330 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -108,6 +108,7 @@ namespace BlackMisc GlobalIndexCGuiStateDbOwnModelSetComponent = 14100, GlobalIndexCSettingsDockWidget = 14200, GlobalIndexCSettingsNavigator = 14300, + GlobalIndexCSettingsReaders = 14400, GlobalIndexAbuseMode = 20000 // property index abused as map key or otherwise, to be removed if no longer needed };