/** * @RDE_HEADER@ * ProjectDataManagement - PdmComboBox * $Id$ * $HeadURL$ * $Author$ * Created by Patric Karwat, SLSE, 2016-09-12 */ #include "pdm_combobox.h" #include #include #include #include #include #include #include #include namespace ProjectDataManagement { namespace PdmComboBox { static QLatin1String emptyEntryComboBox = QLatin1String("--"); void ChargeTypeFilterProxyModel::setPropertyFilter(const QString &property, const QVariant &value) { if (m_property == property && m_value == value) return; m_property = property; m_value = value; invalidate(); } void ChargeTypeFilterProxyModel::setSourceModel(QAbstractItemModel *model) { m_model = qobject_cast(model); QSortFilterProxyModel::setSourceModel(model); } bool ChargeTypeFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { if (!m_model || source_parent != QModelIndex()) return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); if (m_property.isEmpty() || m_value.isNull()) return true; QStandardItem *item = m_model->item(source_row); QJsonObject obj = item->data(Qt::UserRole + 2).toJsonObject(); if (obj.isEmpty()) { QJsonDocument doc(QJsonDocument::fromJson(item->data(Qt::UserRole + 1).toByteArray())); if (doc.isObject()) obj = doc.object(); else obj = QJsonObject(); if (obj.isEmpty()) obj[QStringLiteral("empty")] = 1; item->setData(obj, Qt::UserRole + 2); } return obj.value(m_property) == m_value; } void fillGearClass(QComboBox *cbx, GearClass gearClass, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); ChargeTypeFilterProxyModel *fpm = new ChargeTypeFilterProxyModel(cbx); fpm->setSourceModel(itemModel); cbx->setModel(fpm); // ULEIS codes 3000+ not actually omitted, but there are no (yet) defined. const auto charges = dtc->chargesForGearClass(gearClass); for (const ChargeType &charge : charges) { QStandardItem *item = new QStandardItem(dtc->trChargeTypeShortName(charge.id(), true)); item->setToolTip(dtc->trChargeTypeName(charge.id(), true)); item->setData(charge.id(), CBUserRoles::Id); item->setData(QByteArray::fromStdString(charge.properties()), CBUserRoles::Properties); itemModel->appendRow(item); } } void fillIgniterType(QComboBox *cbx, int chargeTypeId, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); cbx->setModel(itemModel); QStandardItem *itemEmpty = new QStandardItem(emptyEntryComboBox); itemEmpty->setData(-1, CBUserRoles::Id); itemModel->appendRow(itemEmpty); const auto igniters = dtc->ignitersForChargeType(chargeTypeId); for (const IgniterType &igniter : igniters) { QStandardItem *item = new QStandardItem(dtc->trIgniterTypeShortName(igniter.id())); item->setToolTip(dtc->trIgniterTypeName(igniter.id())); item->setData(igniter.id(), CBUserRoles::Id); item->setData(QByteArray::fromStdString(igniter.properties()), CBUserRoles::Properties); itemModel->appendRow(item); } itemModel->sort(0, Qt::AscendingOrder); // Choose Option if only one option beside the empty entry exists if (cbx->count() == 2) cbx->setCurrentIndex(1); } void fillIgniterFunction(QComboBox *cbx, int chargeTypeId, int igniterTypeId, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); cbx->setModel(itemModel); QStandardItem *itemEmpty = new QStandardItem(emptyEntryComboBox); itemEmpty->setData(-1, CBUserRoles::Id); itemModel->appendRow(itemEmpty); const auto chargeTypeIgTypeIgFunMap = dtc->igniterFunctionsForChargeTypeIgniterType(chargeTypeId, igniterTypeId); for (const ChargeTypeIgniterTypeIgniterFunction &chargeTypeIgTypeIgFun : chargeTypeIgTypeIgFunMap) { QStandardItem *item = new QStandardItem(dtc->trIgniterFunctionShortName(chargeTypeIgTypeIgFun.igniterFunctionId())); item->setToolTip(dtc->trIgniterFunctionName(chargeTypeIgTypeIgFun.igniterFunctionId())); item->setData(chargeTypeIgTypeIgFun.igniterFunctionId(), CBUserRoles::Id); item->setData(QByteArray::fromStdString(chargeTypeIgTypeIgFun.properties()), CBUserRoles::Properties); itemModel->appendRow(item); } itemModel->sort(0, Qt::AscendingOrder); // Choose Option if only one option beside the empty entry exists if (cbx->count() == 2) cbx->setCurrentIndex(1); } void fillLoadType(QComboBox *cbx, int chargeTypeId, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); cbx->setModel(itemModel); QStandardItem *itemEmpty = new QStandardItem(emptyEntryComboBox); itemEmpty->setData(-1, CBUserRoles::Id); itemModel->appendRow(itemEmpty); const auto loads = dtc->loadsForChargeType(chargeTypeId); for (const LoadType &load : loads) { QStandardItem *item = new QStandardItem(dtc->trLoadTypeShortName(load.id())); item->setToolTip(dtc->trLoadTypeName(load.id())); item->setData(load.id(), CBUserRoles::Id); itemModel->appendRow(item); } itemModel->sort(0, Qt::AscendingOrder); // Choose Option if only one option beside the empty entry exists if (cbx->count() == 2) cbx->setCurrentIndex(1); } void fillTargetType(QComboBox *cbx, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); cbx->setModel(itemModel); QStandardItem *itemEmpty = new QStandardItem(emptyEntryComboBox); itemEmpty->setData(-1, CBUserRoles::Id); itemModel->appendRow(itemEmpty); const auto targeTypes = dtc->targetTypes(); for (const TargetType &targetType : targeTypes) { QStandardItem *item = new QStandardItem(dtc->trTargetTypeName(targetType.id())); item->setToolTip(dtc->trTargetTypeName(targetType.id())); item->setData(targetType.id(), CBUserRoles::Id); itemModel->appendRow(item); } itemModel->sort(0, Qt::AscendingOrder); } void fillTargetSubType(QComboBox *cbx, int targetTypeId, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); cbx->setModel(itemModel); const auto targetSubTypeVec = dtc->targetSubTypesForTargetType(targetTypeId); QStandardItem *itemEmpty = new QStandardItem(emptyEntryComboBox); itemEmpty->setData(-1, TargetSubTypeRole::Id); itemModel->appendRow(itemEmpty); for (const TargetSubType &targetSubType : targetSubTypeVec) { QStandardItem *item = new QStandardItem(dtc->trTargetSubTypeName(targetSubType.id())); item->setToolTip(dtc->trTargetSubTypeName(targetSubType.id())); item->setData(targetSubType.id(), TargetSubTypeRole::Id); item->setData(QString::fromStdString(targetSubType.abbreviation()), TargetSubTypeRole::Abbreviation); itemModel->appendRow(item); } itemModel->sort(0, Qt::AscendingOrder); } void fillFirePurpose(QComboBox *cbx, const std::shared_ptr &dtc) { QStandardItemModel *itemModel = new QStandardItemModel(cbx); cbx->setModel(itemModel); QStandardItem *itemEmpty = new QStandardItem(emptyEntryComboBox); itemEmpty->setData(-1, CBUserRoles::Id); itemModel->appendRow(itemEmpty); const auto &firePurposes = dtc->firePurposes(); for (const FirePurpose &firePurpose : firePurposes) { QStandardItem *item = new QStandardItem(dtc->trFirePurposeName(firePurpose.id())); item->setToolTip(dtc->trFirePurposeName(firePurpose.id())); item->setData(firePurpose.id(), CBUserRoles::Id); item->setData(QByteArray::fromStdString(firePurpose.properties()), CBUserRoles::Properties); itemModel->appendRow(item); } itemModel->sort(0, Qt::AscendingOrder); } bool fill(QComboBox *cbx, PdmComboBoxType type) { if (!cbx) return false; const QVariant curData = cbx->currentData(); QSignalBlocker block(cbx); cbx->clear(); switch (type) { case PdmComboBoxType::CbLanguage: { const QString languages = LtsUtils::ProjectSettings::getInstance()->value(QStringLiteral("pdm"), QStringLiteral("EquipmentLanguages")).toString(); const QVector slLang = languages.splitRef(QLatin1Char(',')); QSet alreadySeen; alreadySeen += LanguageConfiguration::NoChange; cbx->addItem(toString(LanguageConfiguration::NoChange), LanguageConfiguration::NoChange); for (const QStringRef &r : slLang) { const int langId = r.toInt(); if (alreadySeen.contains(langId)) continue; alreadySeen += langId; cbx->addItem(toString(static_cast(langId)), static_cast(langId)); } break; } case PdmComboBoxType::CbOperationMode: { cbx->addItem(toString(OperationMode::NoChange), OperationMode::NoChange); cbx->addItem(toString(OperationMode::Normal), OperationMode::Normal); cbx->addItem(toString(OperationMode::BeStill), OperationMode::BeStill); break; } case PdmComboBoxType::CbModeHelmet: case PdmComboBoxType::CbModeVest: { cbx->addItem(toString(WphMode::NoChange), WphMode::NoChange); cbx->addItem(toString(WphMode::Without), WphMode::Without); cbx->addItem(toString(WphMode::With), WphMode::With); break; } case PdmComboBoxType::CbPersonalProtection: { cbx->addItem(toString(PersonalProtection::NoChange), PersonalProtection::NoChange); cbx->addItem(toString(PersonalProtection::Without), PersonalProtection::Without); cbx->addItem(toString(PersonalProtection::Suit), PersonalProtection::Suit); cbx->addItem(toString(PersonalProtection::SuitAndGloves), PersonalProtection::SuitAndGloves); break; } case PdmComboBoxType::CbProtectionLevel: { cbx->addItem(toString(ProtectionLevel::NoChange), ProtectionLevel::NoChange); cbx->addItem(toString(ProtectionLevel::IdzBs), ProtectionLevel::IdzBs); cbx->addItem(toString(ProtectionLevel::IdzEs), ProtectionLevel::IdzEs); cbx->addItem(toString(ProtectionLevel::Ksk), ProtectionLevel::Ksk); cbx->addItem(toString(ProtectionLevel::SoldierWithBodyArmor), ProtectionLevel::SoldierWithBodyArmor); cbx->addItem(toString(ProtectionLevel::SoldierActor), ProtectionLevel::SoldierActor); break; } case PdmComboBoxType::CbHandgrenadeSearchMode: { cbx->addItem(toString(HandgrenadeSearchMode::NoChange), HandgrenadeSearchMode::NoChange); cbx->addItem(toString(HandgrenadeSearchMode::On), HandgrenadeSearchMode::On); cbx->addItem(toString(HandgrenadeSearchMode::Off), HandgrenadeSearchMode::Off); break; } case PdmComboBoxType::CbInternationalMode: { cbx->addItem(toString(InternationalMode::NoChange), InternationalMode::NoChange); cbx->addItem(toString(InternationalMode::On), InternationalMode::On); cbx->addItem(toString(InternationalMode::Off), InternationalMode::Off); break; } case PdmComboBoxType::CbABCProtectionMode: { cbx->addItem(toString(ABCProtectionMode::NoChange), ABCProtectionMode::NoChange); cbx->addItem(toString(ABCProtectionMode::On), ABCProtectionMode::On); cbx->addItem(toString(ABCProtectionMode::Off), ABCProtectionMode::Off); break; } case PdmComboBoxType::CbUnkown: break; } // end switch if (curData.isValid() && (cbx->findData(curData) != -1)) cbx->setCurrentIndex(cbx->findData(curData)); else if (cbx->count()) cbx->setCurrentIndex(0); cbx->model()->sort(0, Qt::AscendingOrder); return true; } bool fill(QComboBox *cbx, GearClass gearClass, const std::shared_ptr &dtc) { if (!cbx) return false; const QVariant curData = cbx->currentData(); QSignalBlocker block(cbx); cbx->clear(); fillGearClass(cbx, gearClass, dtc); if (curData.isValid() && (cbx->findData(curData) != -1)) cbx->setCurrentIndex(cbx->findData(curData)); else if (cbx->count()) cbx->setCurrentIndex(0); return true; } bool setChargeTypePropertyFilter(QComboBox *cbx, const QString &property, const QVariant &value) { if (!cbx) return false; ChargeTypeFilterProxyModel *fpm = qobject_cast(cbx->model()); if (!fpm) return false; fpm->setPropertyFilter(property, value); return true; } } // namespace PdmComboBox } // namespace ProjectDataManagement