Found during T215, rectify CIdentifier issues

* null/anonymous identifier
* CIdentifier can be linked with QObject and
* CIdentifier reflects name changes of QObject (UI components change name after ctor call)
This commit is contained in:
Klaus Basan
2018-01-02 19:43:16 +01:00
parent 2fab47a5d3
commit 26b5a6ce28
14 changed files with 99 additions and 68 deletions

View File

@@ -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
{

View File

@@ -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)

View File

@@ -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<void(const BlackMisc::Weather::CWeatherGrid &)> &callback)
const CSlot<void(const CWeatherGrid &)> &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

View File

@@ -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; }

View File

@@ -60,7 +60,7 @@ namespace BlackGui
QScopedPointer<Ui::CFlightPlanComponent> 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<BlackMisc::Simulation::Data::TLastModel> m_lastAircraftModel { this }; //!< recently used aircraft model
BlackMisc::CDataReadOnly<BlackMisc::Network::Data::TLastServer> 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();

View File

@@ -96,7 +96,7 @@ namespace BlackGui
void disableButtonBriefly();
QScopedPointer<Ui::CMainKeypadAreaComponent> ui;
BlackMisc::CIdentifier m_identifier { QStringLiteral("KEYPADAREACOMPONENT") };
BlackMisc::CIdentifier m_identifier { "MainKeypadAreaComponent", this };
};
} // namespace
} // namespace

View File

@@ -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

View File

@@ -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

View File

@@ -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<CTextMessageTextEdit *>();
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

View File

@@ -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::CTextMessageComponent> ui;
BlackMisc::CIdentifier m_identifier;
BlackMisc::CSettingReadOnly<BlackGui::Settings::TextMessageSettings> m_messageSettings { this };
BlackMisc::CIdentifier m_identifier { "TextMessageComponent", this };
BlackMisc::CSettingReadOnly<Settings::TextMessageSettings> 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);

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -22,6 +22,7 @@
#include <QByteArray>
#include <QMetaType>
#include <QString>
#include <QObject>
#include <QUuid>
#include <QtGlobal>
@@ -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;

View File

@@ -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);