diff --git a/src/blackcore/airspacemonitor.cpp b/src/blackcore/airspacemonitor.cpp index 8299faab4..7b3f40ee3 100644 --- a/src/blackcore/airspacemonitor.cpp +++ b/src/blackcore/airspacemonitor.cpp @@ -1077,8 +1077,8 @@ namespace BlackCore model.updateMissingParts(aircraft.getModel()); // Use anonymous as originator here, since the remote aircraft provider is ourselves and the call to updateAircraftModel() would // return without doing anything. - this->updateAircraftModel(callsign, model, CIdentifier::anonymous()); - this->updateAircraftNetworkModel(callsign, model, CIdentifier::anonymous()); + this->updateAircraftModel(callsign, model, CIdentifier::null()); + this->updateAircraftNetworkModel(callsign, model, CIdentifier::null()); } else { diff --git a/src/blackcore/context/contextapplicationproxy.cpp b/src/blackcore/context/contextapplicationproxy.cpp index 4d9b16a3b..63af3c0f2 100644 --- a/src/blackcore/context/contextapplicationproxy.cpp +++ b/src/blackcore/context/contextapplicationproxy.cpp @@ -128,7 +128,7 @@ namespace BlackCore void CContextApplicationProxy::synchronizeLocalSettings() { // note this proxy method does not call synchronizeLocalSettings in core - CSettingsCache::instance()->changeValuesFromRemote(this->getAllSettings(), CIdentifier::anonymous()); + CSettingsCache::instance()->changeValuesFromRemote(this->getAllSettings(), CIdentifier::null()); } BlackMisc::CStatusMessage CContextApplicationProxy::saveSettings(const QString &keyPrefix) diff --git a/src/blackcore/weathermanager.cpp b/src/blackcore/weathermanager.cpp index e2e3891a2..20ff1c750 100644 --- a/src/blackcore/weathermanager.cpp +++ b/src/blackcore/weathermanager.cpp @@ -39,7 +39,7 @@ namespace BlackCore // todo: send weather grid to drivers from here } - void CWeatherManager::requestWeatherGrid(const CWeatherGrid &weatherGrid, const BlackMisc::CIdentifier &identifier) + void CWeatherManager::requestWeatherGrid(const CWeatherGrid &weatherGrid, const CIdentifier &identifier) { WeatherRequest request { identifier, weatherGrid, {} }; m_pendingRequests.append(request); @@ -48,7 +48,7 @@ namespace BlackCore } void CWeatherManager::requestWeatherGrid(const CWeatherGrid &weatherGrid, - const BlackMisc::CSlot &callback) + const CSlot &callback) { if (m_isWeatherClear) { @@ -56,7 +56,7 @@ namespace BlackCore return; } - WeatherRequest weatherRequest { CIdentifier::anonymous(), weatherGrid, callback }; + WeatherRequest weatherRequest { CIdentifier::null(), weatherGrid, callback }; m_pendingRequests.append(weatherRequest); // Serialize the requests, since plugins can handle only one at a time diff --git a/src/blackgui/components/flightplancomponent.cpp b/src/blackgui/components/flightplancomponent.cpp index e97b0b4c5..bf8d74760 100644 --- a/src/blackgui/components/flightplancomponent.cpp +++ b/src/blackgui/components/flightplancomponent.cpp @@ -778,12 +778,6 @@ namespace BlackGui } } - const CIdentifier &CFlightPlanComponent::flightPlanIdentifier() - { - static const CIdentifier i(QStringLiteral("FLIGHTPLANCOMPONENT")); - return i; - } - void CFlightPlanComponent::initCompleters() { if (!sGui || !sGui->hasWebDataServices()) { return; } diff --git a/src/blackgui/components/flightplancomponent.h b/src/blackgui/components/flightplancomponent.h index cb84b5671..17b053aa6 100644 --- a/src/blackgui/components/flightplancomponent.h +++ b/src/blackgui/components/flightplancomponent.h @@ -60,7 +60,7 @@ namespace BlackGui QScopedPointer ui; BlackMisc::Aviation::CFlightPlan m_flightPlan; //!< My flight plan BlackMisc::Simulation::CAircraftModel m_model; //!< currently used model - BlackMisc::CIdentifier m_identifier; //!< Flightplan identifier + BlackMisc::CIdentifier m_identifier { "FlightPlanComponent", this }; //!< Flightplan identifier BlackMisc::CDataReadOnly m_lastAircraftModel { this }; //!< recently used aircraft model BlackMisc::CDataReadOnly m_lastServer { this }; //!< recently used server (VATSIM, other) @@ -76,7 +76,7 @@ namespace BlackGui static const QString &defaultTime() { static const QString t("00:00"); return t; } //! Identifier - static const BlackMisc::CIdentifier &flightPlanIdentifier(); + const BlackMisc::CIdentifier &flightPlanIdentifier() const { return m_identifier; } //! Prefill with own aircraft data void prefillWithOwnAircraftData(); diff --git a/src/blackgui/components/mainkeypadareacomponent.h b/src/blackgui/components/mainkeypadareacomponent.h index cd55a7623..3db4ec1e3 100644 --- a/src/blackgui/components/mainkeypadareacomponent.h +++ b/src/blackgui/components/mainkeypadareacomponent.h @@ -96,7 +96,7 @@ namespace BlackGui void disableButtonBriefly(); QScopedPointer ui; - BlackMisc::CIdentifier m_identifier { QStringLiteral("KEYPADAREACOMPONENT") }; + BlackMisc::CIdentifier m_identifier { "MainKeypadAreaComponent", this }; }; } // namespace } // namespace diff --git a/src/blackgui/components/mappingcomponent.cpp b/src/blackgui/components/mappingcomponent.cpp index e3a8829b6..93534c118 100644 --- a/src/blackgui/components/mappingcomponent.cpp +++ b/src/blackgui/components/mappingcomponent.cpp @@ -374,15 +374,6 @@ namespace BlackGui } } - CIdentifier CMappingComponent::mappingIdentifier() - { - if (m_identifier.getName().isEmpty()) - { - m_identifier = CIdentifier(QStringLiteral("MAPPINGCOMPONENT")); - } - return m_identifier; - } - void CMappingComponent::updateRenderedAircraftView(bool forceUpdate) { m_updateTimer.start(); // restart diff --git a/src/blackgui/components/mappingcomponent.h b/src/blackgui/components/mappingcomponent.h index 068f60856..1f554d1db 100644 --- a/src/blackgui/components/mappingcomponent.h +++ b/src/blackgui/components/mappingcomponent.h @@ -137,7 +137,7 @@ namespace BlackGui void connectionStatusChanged(BlackCore::INetwork::ConnectionStatus from, BlackCore::INetwork::ConnectionStatus to); //! Identifier for data send from this component - BlackMisc::CIdentifier mappingIdentifier(); + const BlackMisc::CIdentifier &mappingIdentifier() const { return m_identifier; } //! Update simulated aircraft view void updateRenderedAircraftView(bool forceUpdate = false); @@ -151,7 +151,7 @@ namespace BlackGui QTimer m_updateTimer { this }; BlackMisc::CTokenBucket m_bucket { 3, BlackMisc::PhysicalQuantities::CTime(5.0, BlackMisc::PhysicalQuantities::CTimeUnit::s()), 1}; Views::CCheckBoxDelegate *m_currentMappingsViewDelegate = nullptr; //! checkbox in view - BlackMisc::CIdentifier m_identifier; + BlackMisc::CIdentifier m_identifier { "MappingComponent", this }; }; } // namespace } // namespace diff --git a/src/blackgui/components/textmessagecomponent.cpp b/src/blackgui/components/textmessagecomponent.cpp index 7b9787cf8..83887ea1f 100644 --- a/src/blackgui/components/textmessagecomponent.cpp +++ b/src/blackgui/components/textmessagecomponent.cpp @@ -92,14 +92,10 @@ namespace BlackGui { switch (tab) { - case TextMessagesAll: - return ui->tb_TextMessagesAll; - case TextMessagesCom1: - return ui->tb_TextMessagesCOM1; - case TextMessagesCom2: - return ui->tb_TextMessagesCOM2; - case TextMessagesUnicom: - return ui->tb_TextMessagesUnicom; + case TextMessagesAll: return ui->tb_TextMessagesAll; + case TextMessagesCom1: return ui->tb_TextMessagesCOM1; + case TextMessagesCom2: return ui->tb_TextMessagesCOM2; + case TextMessagesUnicom: return ui->tb_TextMessagesUnicom; default: Q_ASSERT_X(false, Q_FUNC_INFO, "Wrong index"); break; @@ -120,7 +116,7 @@ namespace BlackGui { if (messages.isEmpty()) return; const CSimulatedAircraft ownAircraft(this->getOwnAircraft()); - const CTextMessageSettings msgSettings(this->m_messageSettings.getThreadLocal()); + const CTextMessageSettings msgSettings(m_messageSettings.getThreadLocal()); for (const CTextMessage &message : messages) { @@ -193,14 +189,6 @@ namespace BlackGui } } - CIdentifier CTextMessageComponent::componentIdentifier() - { - if (m_identifier.getName().isEmpty()) - m_identifier = CIdentifier(QStringLiteral("TEXTMESSAGECOMPONENT")); - - return m_identifier; - } - void CTextMessageComponent::onChangedAircraftCockpit() { this->showCurrentFrequenciesFromCockpit(); @@ -214,7 +202,7 @@ namespace BlackGui if (textMessage.isPrivateMessage()) { // private message - CCallsign cs = textMessage.getSenderCallsign(); + const CCallsign cs = textMessage.getSenderCallsign(); if (cs.isEmpty()) return false; QWidget *tab = this->findTextMessageTabByName(cs.getStringAsSet()); if (!tab) { return false; } @@ -292,12 +280,12 @@ namespace BlackGui if (!textMessage.isPrivateMessage()) { return; } const CCallsign cs = textMessage.wasSent() ? textMessage.getRecipientCallsign() : textMessage.getSenderCallsign(); if (cs.isEmpty()) { return; } - QWidget *tab = this->findTextMessageTabByCallsign(cs); + const QWidget *tab = this->findTextMessageTabByCallsign(cs); if (!tab) { tab = this->addNewTextMessageTab(cs); } Q_ASSERT_X(tab, Q_FUNC_INFO, "Missing tab"); CTextMessageTextEdit *textEdit = tab->findChild(); BLACK_VERIFY_X(textEdit, Q_FUNC_INFO, "Missing text edit"); - if (!textEdit) { return; } // do not crash, though this situation could not happen + if (!textEdit) { return; } // do not crash, though this situation should not happen textEdit->insertTextMessage(textMessage); // sound diff --git a/src/blackgui/components/textmessagecomponent.h b/src/blackgui/components/textmessagecomponent.h index d61563977..27a7be995 100644 --- a/src/blackgui/components/textmessagecomponent.h +++ b/src/blackgui/components/textmessagecomponent.h @@ -56,7 +56,7 @@ namespace BlackGui virtual ~CTextMessageComponent(); //! \copydoc CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea - virtual bool setParentDockWidgetInfoArea(BlackGui::CDockWidgetInfoArea *parentDockableWidget) override; + virtual bool setParentDockWidgetInfoArea(CDockWidgetInfoArea *parentDockableWidget) override; signals: //! Message to be displayed in info window @@ -80,8 +80,8 @@ namespace BlackGui private: QScopedPointer ui; - BlackMisc::CIdentifier m_identifier; - BlackMisc::CSettingReadOnly m_messageSettings { this }; + BlackMisc::CIdentifier m_identifier { "TextMessageComponent", this }; + BlackMisc::CSettingReadOnly m_messageSettings { this }; //! Enum to widget QWidget *getTabWidget(Tab tab) const; @@ -124,7 +124,7 @@ namespace BlackGui QString textMessageToCommand(const QString &enteredLine); //! Identifier - BlackMisc::CIdentifier componentIdentifier(); + const BlackMisc::CIdentifier &componentIdentifier() const { return m_identifier; } //! Handle a text message entered void handleEnteredTextMessage(const QString &textMessage); diff --git a/src/blackmisc/datacache.cpp b/src/blackmisc/datacache.cpp index 48ba0c669..237747cae 100644 --- a/src/blackmisc/datacache.cpp +++ b/src/blackmisc/datacache.cpp @@ -316,7 +316,7 @@ namespace BlackMisc if (! m_deferredChanges.isEmpty()) { m_deferredChanges.setSaved(); - emit valuesLoadedFromStore(m_deferredChanges, CIdentifier::anonymous()); + emit valuesLoadedFromStore(m_deferredChanges, CIdentifier::null()); deliverPromises(m_cache->m_revision.loadedValuePromises()); m_deferredChanges.clear(); } diff --git a/src/blackmisc/identifier.cpp b/src/blackmisc/identifier.cpp index 28cc0d36e..9d277cae9 100644 --- a/src/blackmisc/identifier.cpp +++ b/src/blackmisc/identifier.cpp @@ -32,6 +32,16 @@ namespace BlackMisc m_processId(QCoreApplication::applicationPid()) { } + CIdentifier::CIdentifier(const QString &name, QObject *object) : CIdentifier(name) + { + if (object) + { + // append object name + this->linkWithQObjectName(object); + this->appendName(object->objectName()); + } + } + CIdentifier::CIdentifier(const QString &name, const QString &machineId, const QString &machineName, const QString &processName, qint64 processId) : ITimestampBased(QDateTime::currentMSecsSinceEpoch()), @@ -41,7 +51,13 @@ namespace BlackMisc const CIdentifier &CIdentifier::anonymous() { - static const CIdentifier id("anonymous", "", "", "", 0); + static const CIdentifier id(""); + return id; + } + + const CIdentifier &CIdentifier::null() + { + static const CIdentifier id("", "", "", "", 0); return id; } @@ -67,6 +83,18 @@ namespace BlackMisc return toUuid().toString(); } + void CIdentifier::appendName(const QString &name) + { + if (m_name.endsWith(name)) { return; } + m_name += QStringLiteral(":") + name; + } + + void CIdentifier::linkWithQObjectName(QObject *object) + { + if (!object) { return; } + QObject::connect(object, &QObject::objectNameChanged, object, [ = ](const QString & name) { this->appendName(name); }); + } + QByteArray CIdentifier::getMachineId() const { return QByteArray::fromBase64(m_machineIdBase64.toLocal8Bit()); @@ -97,6 +125,11 @@ namespace BlackMisc return &anonymous() == this || anonymous() == *this; } + bool CIdentifier::isNull() const + { + return &null() == this || null() == *this; + } + QString CIdentifier::convertToQString(bool i18n) const { Q_UNUSED(i18n); diff --git a/src/blackmisc/identifier.h b/src/blackmisc/identifier.h index 96e0bda5a..ec15e9118 100644 --- a/src/blackmisc/identifier.h +++ b/src/blackmisc/identifier.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -50,11 +51,22 @@ namespace BlackMisc }; //! Constructor. - CIdentifier(const QString &name = QString()); + //! \private use CIdentifier::anonymous() if you need an identifier without name and CIdentifier::null() for an empty identifier + CIdentifier() : CIdentifier("") {} - //! Returns an anonymous identifier. + //! Constructor. + CIdentifier(const QString &name); + + //! Constructor using object's name + CIdentifier(const QString &name, QObject *object); + + //! Returns an anonymous identifier, which is a valid identifier without name + //! \remark same as CIdentifier() static const CIdentifier &anonymous(); + //! Null (empty) identifier + static const CIdentifier &null(); + //! Returns a fake identifier. static const CIdentifier &fake(); @@ -70,6 +82,16 @@ namespace BlackMisc //! Has name bool hasName() const { return !m_name.isEmpty(); } + //! Set the name + void setName(const QString &name) { m_name = name; } + + //! Set name or append name + //! \remark append makes sense if an object name changes after some time (like for UI components when the whole UI is setup) + void appendName(const QString &name); + + //! Reflect changes of QObject:: + void linkWithQObjectName(QObject *object); + //! Get machine id QByteArray getMachineId() const; @@ -100,6 +122,9 @@ namespace BlackMisc //! Check if it is anonymous identifier bool isAnonymous() const; + //! Null identifier (no name, ids etc) + bool isNull() const; + //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; diff --git a/tests/blackmisc/testinput.cpp b/tests/blackmisc/testinput.cpp index fa0ceb73a..a2827bb7d 100644 --- a/tests/blackmisc/testinput.cpp +++ b/tests/blackmisc/testinput.cpp @@ -80,12 +80,12 @@ namespace BlackMiscTest CHotkeyCombination comb1; comb1.addKeyboardKey(Key_ControlLeft); comb1.addKeyboardKey(Key_D); - CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + CActionHotkey actionHotkey1 (CIdentifier::anonymous(), comb1, "action"); CHotkeyCombination comb2; comb2.addKeyboardKey(Key_ControlLeft); comb2.addKeyboardKey(Key_D); - CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + CActionHotkey actionHotkey2 (CIdentifier::anonymous(), comb2, "action"); QVERIFY2(actionHotkey1 == actionHotkey2, "Objects should equal"); } @@ -94,24 +94,24 @@ namespace BlackMiscTest CHotkeyCombination comb; comb.addKeyboardKey(Key_ControlLeft); comb.addKeyboardKey(Key_D); - CActionHotkey actionHotkey1 (CIdentifier(), comb, "action"); + CActionHotkey actionHotkey1 (CIdentifier::anonymous(), comb, "action"); CHotkeyCombination comb2; comb2.addKeyboardKey(Key_ControlLeft); comb2.addKeyboardKey(Key_C); - CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + CActionHotkey actionHotkey2 (CIdentifier::anonymous(), comb2, "action"); QVERIFY2(actionHotkey1 != actionHotkey2, "Objects should not be equal"); CHotkeyCombination comb3; comb3.addKeyboardKey(Key_ShiftLeft); comb3.addKeyboardKey(Key_D); - CActionHotkey actionHotkey3 (CIdentifier(), comb2, "action"); + CActionHotkey actionHotkey3 (CIdentifier::anonymous(), comb2, "action"); QVERIFY2(actionHotkey1 != actionHotkey3, "Objects should not be equal"); CHotkeyCombination comb4; comb4.addKeyboardKey(Key_ControlLeft); comb4.addKeyboardKey(Key_D); - CActionHotkey actionHotkey4 (CIdentifier(), comb2, "other_action"); + CActionHotkey actionHotkey4 (CIdentifier::anonymous(), comb2, "other_action"); QVERIFY2(actionHotkey1 != actionHotkey4, "Objects should not be equal"); } @@ -120,14 +120,14 @@ namespace BlackMiscTest CHotkeyCombination comb1; comb1.addKeyboardKey(Key_ControlLeft); comb1.addKeyboardKey(Key_C); - CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + CActionHotkey actionHotkey1 (CIdentifier::anonymous(), comb1, "action"); CActionHotkeyList list1; list1.push_back(actionHotkey1); CHotkeyCombination comb2; comb2.addKeyboardKey(Key_ControlLeft); comb2.addKeyboardKey(Key_D); - CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + CActionHotkey actionHotkey2 (CIdentifier::anonymous(), comb2, "action"); CActionHotkeyList list2; list2.push_back(actionHotkey2); QVERIFY2(CVariant::fromValue(list1) != CVariant::fromValue(list2), "List should not be equal"); @@ -138,16 +138,16 @@ namespace BlackMiscTest CHotkeyCombination comb1; comb1.addKeyboardKey(Key_ControlLeft); comb1.addKeyboardKey(Key_D); - CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + CActionHotkey actionHotkey1 (CIdentifier::anonymous(), comb1, "action"); CHotkeyCombination comb2; comb2.addKeyboardKey(Key_ControlLeft); - CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + CActionHotkey actionHotkey2 (CIdentifier::anonymous(), comb2, "action"); CHotkeyCombination comb3; comb3.addKeyboardKey(Key_ShiftLeft); comb3.addKeyboardKey(Key_D); - CActionHotkey actionHotkey3 (CIdentifier(), comb3, "action"); + CActionHotkey actionHotkey3 (CIdentifier::anonymous(), comb3, "action"); CActionHotkeyList list1; list1.push_back(actionHotkey1); @@ -160,17 +160,17 @@ namespace BlackMiscTest { CHotkeyCombination comb1; comb1.addKeyboardKey(Key_ControlLeft); - CActionHotkey actionHotkey1 (CIdentifier(), comb1, "action"); + CActionHotkey actionHotkey1 (CIdentifier::anonymous(), comb1, "action"); CHotkeyCombination comb2; comb2.addKeyboardKey(Key_ControlLeft); comb2.addKeyboardKey(Key_D); - CActionHotkey actionHotkey2 (CIdentifier(), comb2, "action"); + CActionHotkey actionHotkey2 (CIdentifier::anonymous(), comb2, "action"); CHotkeyCombination comb3; comb3.addKeyboardKey(Key_ShiftLeft); comb3.addKeyboardKey(Key_D); - CActionHotkey actionHotkey3 (CIdentifier(), comb3, "action"); + CActionHotkey actionHotkey3 (CIdentifier::anonymous(), comb3, "action"); CActionHotkeyList list1; list1.push_back(actionHotkey1);