/** * @RDE_HEADER@ * Restock Charges - Restocks Charges of a distinct weapon selectively. Or their defaults. Or both. * $Id$ * $HeadURL$ * $Author$ * Created by Iuliana Ojog - 2022-03-17 */ #include "participant_gear_restock.h" #include "ui_participant_gear_restock.h" #include "participant_gear_restock_charge.h" #include #include #include namespace ParticipantManagement { ParticipantGearRestock::ParticipantGearRestock(int gearId, int gearTypeId, const Participant &participant, DialogMode dialogMode, QWidget *parent) : LtsWidgets::LtsDialog(parent, LtsWidgets::ButtonCombination::OkApplyCancel) , m_requestHelper(new LtsUtils::RequestHelper(this)) , m_gearId(gearId) , m_gearTypeId(gearTypeId) , m_participant(participant) , m_dialogMode(dialogMode) , m_ui(new Ui::ParticipantGearRestock) , m_logger(getLogger("ParticipantGearRestock")) { m_ui->setupUi(this); // Defaultmäßig restocken... m_ui->cbRestockNow->setChecked(true); m_ui->cbRestockNow->setVisible(dialogMode == DM_SetRestockDefaults); } ParticipantGearRestock::~ParticipantGearRestock() { delete m_ui; } void ParticipantGearRestock::retranslate() { QString eqNoString; if (m_participant.equipmentNumber().is_set()) eqNoString = QString::number(m_participant.equipmentNumber().get()); else eqNoString = tr("[Participant Id: %1]").arg(m_participant.id()); if (m_dialogMode == DM_Restock) translate(m_ui, tr("Set %1's Restock Defaults of Participant %2").arg(m_pdmDtc->trGearTypeName(m_gearTypeId), eqNoString)); // translate(m_ui, tr("Restock %1 of Participant %2").arg(m_pdmDtc->trGearTypeName(m_gearTypeId), eqNoString)); // else } bool ParticipantGearRestock::init(LtsSession::LtsSession *session, bool showAllOptions) { if (!session) return false; m_session = session; if (!session->getDtc(m_dtc)) return false; if (!session->getFtc(m_ftc)) return false; if (!session->getDtc(m_pdmDtc)) return false; if (!m_pdmDtc->canRestock(m_gearTypeId) && m_dialogMode == DialogMode::DM_Restock) { WARNING_LOG(m_logger) << "Not possible to restock gearTypeId = " << m_gearTypeId; return false; } if (!m_pdmDtc->canSetDefaults(m_gearTypeId) && m_dialogMode == DialogMode::DM_SetRestockDefaults) { WARNING_LOG(m_logger) << "Not possible to set restock defaults gearTypeId = " << m_gearTypeId; return false; } m_max = m_pdmDtc->maxCharges(m_gearTypeId); m_ui->lblMaxCharges->setText(QString::number(m_max)); QHBoxLayout * hLayoutMaxCharge = new QHBoxLayout(); QLabel *lblMax = new QLabel(); QLabel *lblMaxCharges = new QLabel(); lblMaxCharges->setTextFormat(Qt::TextFormat::RichText); lblMaxCharges->setText(QStringLiteral("") + QString::number(m_max) + QStringLiteral("")); lblMax->setText(QStringLiteral("Max:")); lblMax->setVisible(m_max > 0); lblMaxCharges->setVisible(m_max > 0); // m_ui->lblMax->setVisible(m_max > 0); // m_ui->lblMaxCharges->setVisible(m_max > 0); m_ui->lblMax->setVisible(false); m_ui->lblMaxCharges->setVisible(false); QVBoxLayout* vLayout = new QVBoxLayout(m_ui->scrollArea); vLayout->setMargin(0); m_ui->scrollArea->setLayout(vLayout); for (const ProjectDataManagement::GearTypeChargeType &gearType2ChargeType : m_pdmDtc->gearTypeChargeTypes()) { if (gearType2ChargeType.gearTypeId() != m_gearTypeId) continue; int chargeTypeId = gearType2ChargeType.chargeTypeId(); int maxCharge = m_pdmDtc->maxCharge(m_gearTypeId, chargeTypeId); DEBUG_LOG(m_logger) << "maxCharge = " << maxCharge; ParticipantGearRestockCharge* pgrc = new ParticipantGearRestockCharge(m_ui->scrollArea, showAllOptions); pgrc->init(chargeTypeId, m_pdmDtc->trChargeTypeShortName(chargeTypeId, true), m_pdmDtc->factoryDefault(m_gearTypeId, chargeTypeId), maxCharge); connect(pgrc, &ParticipantGearRestockCharge::valueChanged, this, &ParticipantGearRestock::validate); vLayout->addWidget(pgrc); } hLayoutMaxCharge->addWidget(lblMax); hLayoutMaxCharge->addWidget(lblMaxCharges); hLayoutMaxCharge->setMargin(8); hLayoutMaxCharge->setSpacing(0); vLayout->addLayout(hLayoutMaxCharge); validate(0); setUnchangeable(true); connect(this, &ParticipantGearRestock::signalChangesReset, this, [this]() { m_buttonBox->getButton(LtsWidgets::DialogButton::Ok)->setEnabled(true); m_buttonBox->getButton(LtsWidgets::DialogButton::Apply)->setEnabled(true); }); m_buttonBox->getButton(LtsWidgets::DialogButton::Ok)->setEnabled(true); m_buttonBox->getButton(LtsWidgets::DialogButton::Apply)->setEnabled(true); return true; } bool ParticipantGearRestock::apply() { SelectiveReammunitionRequest req; req.id(m_participant.id()); req.gearId(m_gearId); for (ParticipantGearRestockCharge* pgrc : m_ui->scrollArea->findChildren()) { Reammunition ra; ra.chargeTypeId(pgrc->chargeTypeId()); ra.tracerRatio(0); ra.newStock((m_dialogMode == DM_Restock || m_ui->cbRestockNow->isChecked()) ? pgrc->value() : 0xfffe); ra.newDefaultStock(m_dialogMode == DM_SetRestockDefaults ? pgrc->value() : 0xfffe); req.reammunition().push_back(ra); } return m_requestHelper->startSingleRequest([req, this]() { return m_ftc->selectiveReammunitionRequest(req, this); }); } void ParticipantGearRestock::validate(int newVal) { Q_UNUSED(newVal); if (m_max <= 0) return; int total = 0; auto items = m_ui->scrollArea->findChildren(); for (ParticipantGearRestockCharge* pgrc : items) total += pgrc->value(); for (ParticipantGearRestockCharge* pgrc : items) pgrc->setMaxValue(m_max + pgrc->value() - total); } }