Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Common/LtsUtils/LtsUtilsGui/LtsUtilsGuiDds/request_helper.cpp
T
torstenandCopilot 7a6be7474e Qt6 migration: Fix BUILD_RTI=OFF build errors in TrainingAreaBase
- training_area_telegrams.cpp: Fixed QJsonObject initializer syntax (Qt6 requires {{}} not ({}))
- training_area_telegrams.cpp: Fixed fromJson() methods to access m_* members directly
- training_area_telegrams.cpp: Fixed brace mismatches in multiple toJson() methods
- training_area_telegrams.h: Changed Header struct to public members for consistency
- tx_mixin.cpp: Fixed getEquipmentNumber() template for direct member access
- lts_coordinate.cpp: Added missing #include <iomanip>
- MIGRATION_STATUS.md: Added migration progress tracking document

C++17 constraint enforced due to RTI Connext DDS 7.7.0 incompatibility with C++20+

Build status: BUILD_RTI=OFF successful (0 errors, warnings only)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-23 20:49:23 +02:00

260 lines
6.8 KiB
C++

#include "request_helper.h"
#ifdef REQUEST_HELPER_WITH_GUI
#include <LtsWidgets/lts_global_wait_dialog.h>
#include <LtsWidgets/lts_message_dialog.h>
#include <QtWidgets/QWidget>
#endif
#include <QEventLoop>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QTimer>
#include <memory>
#define DEBUG
namespace LtsUtils
{
bool RequestHelper::s_debug = false;
RequestHelper::RequestHelper(QObject* parent, bool quiet,
const std::function<void(void)>& success, const std::function<void(void)>& failure)
: QObject(parent), m_failed(false), m_repliedId(-1), m_quiet(quiet), m_blockingCall(true), m_successFn(success),
m_failureFn(failure)
{
connect(this, &RequestHelper::signalSuccess, this, m_successFn, Qt::QueuedConnection);
connect(this, &RequestHelper::signalFailed, this, m_failureFn, Qt::QueuedConnection);
if (s_debug)
{
QTimer* t = new QTimer(this);
t->setInterval(2000);
t->setSingleShot(true);
connect(this, &RequestHelper::signalNextRequest, t, static_cast<void (QTimer::*)(void)>(&QTimer::start));
connect(t, &QTimer::timeout, this, &RequestHelper::slotNextRequest, Qt::QueuedConnection);
}
else
{
connect(this, &RequestHelper::signalNextRequest, this, &RequestHelper::slotNextRequest, Qt::QueuedConnection
);
}
}
void RequestHelper::addRequest(const std::function<int64_t()>& requestFunction,
const std::function<void(const FunctionReplyType& baseReply)>& baseReplyHandler)
{
m_requestFunctions.append(RequestFunctionContainer(requestFunction, baseReplyHandler));
}
bool RequestHelper::requestReply(const FunctionReplyType& baseReply)
{
#ifdef BUILD_RTI
// Bei BUILD_RTI=ON: baseReply ist Core::FunctionReply, cast von dds::core::Reference
// Die Implementation erwartet Core::FunctionReplyObject
const Core::FunctionReplyObject fro(baseReply.id(), baseReply.errorString(), baseReply.errorParams());
#else
// Stub-Version: nur id und error string
const Core::FunctionReplyObject fro(baseReply.id(), baseReply.str().c_str());
#endif
return _requestReply(fro, &baseReply);
}
bool RequestHelper::requestReply(const Core::FunctionReplyObject& replyObject)
{
return _requestReply(replyObject, nullptr);
}
bool RequestHelper::_requestReply(const Core::FunctionReplyObject& fro, const FunctionReplyType* baseReply)
{
m_errorString = fro.translated();
#ifdef REQUEST_HELPER_WITH_GUI
if (fro.error())
{
if (!m_quiet)
{
if (m_blockingCall)
{
LtsWidgets::LtsMessageDialog::showMessage(qobject_cast<QWidget*>(parent()), m_errorString,
tr("Request failed"));
}
else
{
new LtsWidgets::LtsMessageDialogNew(qobject_cast<QWidget*>(parent()), m_errorString,
tr("Request failed"));
}
}
}
#endif
m_repliedId = fro.id();
if (m_singleRequestCustomFunctionReply != nullptr)
{
if (baseReply)
m_singleRequestCustomFunctionReply(*baseReply);
else
#ifdef BUILD_RTI
m_singleRequestCustomFunctionReply(
Core::FunctionReply(fro.id(), fro.str(), fro.args(), fro.additionalInfo()));
#else
// Stub-Version: FunctionReply mit passendem Konstruktor
m_singleRequestCustomFunctionReply(
Core::FunctionReply(fro.id(), fro.str(), fro.args(), fro.additionalInfo()));
#endif
}
if (fro.error())
{
failure();
return false;
}
emit signalNextRequest();
return true;
}
bool RequestHelper::startSingleRequest(
const std::function<int64_t()>& requestFunction,
const std::function<void(const Core::FunctionReply& baseReply)>& baseReplyHandler)
{
addRequest(requestFunction, baseReplyHandler);
return startRequests();
}
void RequestHelper::failure()
{
m_failed = true;
emit signalFailed();
done();
}
void RequestHelper::done()
{
#ifdef REQUEST_HELPER_WITH_GUI
if (!m_quiet)
LtsWidgets::LtsGlobalWaitDialog::getInstance()->done();
#endif
m_requestFunctions.clear();
m_singleRequestCustomFunctionReply = nullptr;
if (m_blockingCall)
emit signalQuitLoop();
m_blockingCall = true;
if (!m_blockingCall)
deleteLater();
}
void RequestHelper::slotNextRequest()
{
if (m_requestFunctions.isEmpty())
{
if (!m_blockingCall)
emit signalSuccess();
done();
return;
}
const RequestFunctionContainer rfc = m_requestFunctions.takeFirst();
m_singleRequestCustomFunctionReply = rfc.second;
int64_t result = rfc.first();
if (result == 0)
{
#ifdef REQUEST_HELPER_WITH_GUI
if (!m_quiet)
{
if (m_blockingCall)
{
LtsWidgets::LtsMessageDialog::showMessage(qobject_cast<QWidget*>(parent()), tr("Request failed"));
}
else
{
connect(new LtsWidgets::LtsMessageDialogNew(qobject_cast<QWidget*>(parent()), tr("Request failed")),
&LtsWidgets::LtsMessageDialogNew::destroyed, this, [this]() { failure(); });
return;
}
}
#endif
failure();
}
}
void RequestHelper::startSingleBackgroundRequest(
const std::function<int64_t()>& requestFunction,
const std::function<void(const FunctionReplyType&)>& baseReplyHandler)
{
m_blockingCall = false;
startSingleRequest(requestFunction, baseReplyHandler);
}
void RequestHelper::startBackgroundRequests()
{
m_blockingCall = false;
startRequests();
}
void RequestHelper::setCustomWaitMessage(const QString& customWaitMessage)
{
m_customWaitMessage = (customWaitMessage + QStringLiteral(" ..."));
}
bool RequestHelper::startRequests()
{
if (m_requestFunctions.isEmpty())
{
#ifdef LTS_DEBUG
qWarning("RequestHelper: started request helper without requests!");
#endif
return true;
}
m_failed = false;
#ifdef REQUEST_HELPER_WITH_GUI
if (!m_quiet)
LtsWidgets::LtsGlobalWaitDialog::getInstance()->wait(m_customWaitMessage);
m_customWaitMessage.clear();
#endif
m_errorString.clear();
m_repliedId = -1;
if (s_debug)
{
QTimer* t = new QTimer(this);
t->setInterval(2000);
connect(t, &QTimer::timeout, this, &RequestHelper::slotNextRequest, Qt::QueuedConnection);
connect(t, &QTimer::timeout, t, &QTimer::deleteLater);
t->start();
}
else
{
slotNextRequest();
if (m_blockingCall)
{
QEventLoop loop;
connect(this, &RequestHelper::signalQuitLoop, &loop, &QEventLoop::quit, Qt::QueuedConnection);
loop.exec();
}
}
return (!m_failed);
}
} // namespace LtsUtils