/** * @RDE_HEADER@ * SituationMap - ConfigureMapUrlsDialog * $Id$ * $HeadURL$ * $Author$ * Created by Norbert Thoden, SLSE, 2018-09-19 */ #include "sitmap_configure_urls.h" #include "ui_sitmap_configure_urls.h" #include "sitmap_configure_url_model.h" #include "sitmap_configure_url_widget.h" #include // MapSettingJson #include "AdapterDds/plugin_provider.h" #include namespace SituationMap { ConfigureMapUrls::ConfigureMapUrls(QWidget *parent) : LtsDialog(parent, LtsWidgets::ButtonCombination::OkApplyCancel) , m_pluginProvider(nullptr) , m_requestHelper(new LtsUtils::RequestHelper(this)) , m_ui(new Ui::Configure_Urls) , m_logger(getLogger("ConfigureMapUrls")) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; m_ui->setupUi(this); ConfigureMapUrlsWidget *w; // clear, we use our own widgets: m_ui->tabWidget->removeTab(0); w = new ConfigureMapUrlsWidget(); w->setModel(new ConfigureMapUrlModel(this)); m_ui->tabWidget->addTab(w, tr("Map")); connect(w, &ConfigureMapUrlsWidget::hasChanges, this, &ConfigureMapUrls::slotChangesMade); w = new ConfigureMapUrlsWidget(); w->setModel(new ConfigureMapUrlModel(this)); m_ui->tabWidget->addTab(w, tr("Scene")); connect(w, &ConfigureMapUrlsWidget::hasChanges, this, &ConfigureMapUrls::slotChangesMade); w = new ConfigureMapUrlsWidget(); w->setModel(new ConfigureMapUrlModel(this)); w->setPlaceholderTextForFile(); m_ui->tabWidget->addTab(w, tr("Elevation")); connect(w, &ConfigureMapUrlsWidget::hasChanges, this, &ConfigureMapUrls::slotChangesMade); w = new ConfigureMapUrlsWidget(); w->setModel(new ConfigureMapUrlModel(this)); w->setPlaceholderTextForFile(); m_ui->tabWidget->addTab(w, tr("SceneObject")); connect(w, &ConfigureMapUrlsWidget::hasChanges, this, &ConfigureMapUrls::slotChangesMade); //NLDEVCTRL-1727 setOnlineHelpFilename(QStringLiteral("Configuration_of_Modells_SLPKs")); // former: Situation-Configure_Map_Urls TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } ConfigureMapUrls::~ConfigureMapUrls() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; delete m_ui; TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } bool ConfigureMapUrls::init(PluginProvider *pluginProvider) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; m_pluginProvider = pluginProvider; if (!m_pluginProvider) { ERROR_LOG(m_logger) << LOGFUNCTION << " no pluginProvider"; return false; } bool bRet = true; m_asmDtc = m_pluginProvider->asmDtc(); if (!m_asmDtc) { ERROR_LOG(m_logger) << LOGFUNCTION << " Can not retrieve AssetManagement::DTC"; bRet = false; } m_asmFtc = m_pluginProvider->asmFtc(); if (!m_asmFtc) { ERROR_LOG(m_logger) << LOGFUNCTION << " Can not retrieve AssetManagement::FTC"; bRet = false; } connect(m_ui->cbTrainingArea, &LtsWidgets::LtsComboBox::currentTextChanged, this, &ConfigureMapUrls::onSelectedTrainingAreaChanged); m_ui->tabWidget->setCurrentIndex(0); fillTrainingAreaCbx(); syncTaToGui(); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; return bRet; } void ConfigureMapUrls::fillTrainingAreaCbx() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; if (!m_asmDtc) { ERROR_LOG(m_logger) << LOGFUNCTION << " no dtc"; return; } { QSignalBlocker blocker(m_ui->cbTrainingArea); // training areas int curIdxTa = m_ui->cbTrainingArea->currentIndex(); m_ui->cbTrainingArea->clear(); auto cb = [this](const AssetManagement::TrainingArea & ta) { m_ui->cbTrainingArea->addItem(QString::fromStdString(ta.name()), QVariant(ta.id())); }; m_asmDtc->trainingAreas(cb); m_ui->cbTrainingArea->model()->sort(0); if (curIdxTa >= 0) m_ui->cbTrainingArea->setCurrentIndex(curIdxTa); } if (m_ui->cbTrainingArea->model()->rowCount() > 0) { const int newTrainingAreaId = m_ui->cbTrainingArea->currentData().toInt(); if (!retrieveTaOfService(newTrainingAreaId)) { ERROR_LOG(m_logger) << LOGFUNCTION << " retrieveTrainingAreaOfService failed"; return; } } // resetChanges(); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } void ConfigureMapUrls::onSelectedTrainingAreaChanged(const QString & /*dummy*/) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; const int newTrainingAreaId = m_ui->cbTrainingArea->currentData().toInt(); if (m_trainingArea.id() == newTrainingAreaId) return; if (hasUnsavedChanges()) { switch (LtsWidgets::LtsMessageDialog::askApplyDiscardCancel(this)) { case LtsWidgets::Button::Apply: apply(); if (!retrieveTaOfService(newTrainingAreaId)) { ERROR_LOG(m_logger) << LOGFUNCTION << " retrieveTrainingAreaOfService failed"; return; } break; case LtsWidgets::Button::Cancel: INFO_LOG(m_logger) << LOGFUNCTION << " set back to m_trainingArea.id() = " << m_trainingArea.id(); m_ui->cbTrainingArea->setCurrentIndex(m_ui->cbTrainingArea->findData(QVariant(m_trainingArea.id()), Qt::UserRole)); break; case LtsWidgets::Button::Discard: if (!retrieveTaOfService(newTrainingAreaId)) { ERROR_LOG(m_logger) << LOGFUNCTION << " retrieveTrainingAreaOfService failed"; return; } break; default: ERROR_LOG(m_logger) << LOGFUNCTION << " got unexpected!!!"; break; } } else { if (!retrieveTaOfService(newTrainingAreaId)) { ERROR_LOG(m_logger) << LOGFUNCTION << " retrieveTrainingAreaOfService failed"; return; } } syncTaToGui(); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } bool ConfigureMapUrls::retrieveTaOfService(int trainingAreaId) { TRACE_LOG(m_logger) << LOGFUNCTION << " START, trainingAreaId=" << trainingAreaId; if (!m_asmDtc) { ERROR_LOG(m_logger) << LOGFUNCTION << " no dtc"; return false; } // clear gui: m_trainingArea = AssetManagement::TrainingArea(); syncTaToGui(); AssetManagement::TrainingArea ta = m_asmDtc->trainingArea(trainingAreaId); if (ta.id() != trainingAreaId) { ERROR_LOG(m_logger) << LOGFUNCTION << " switch to m_trainingArea failed (taId=" << trainingAreaId << ")"; return false; } m_trainingArea = ta; INFO_LOG(m_logger) << LOGFUNCTION << " switch to m_trainingArea " << m_trainingArea.id() << ", ta" << ta; TRACE_LOG(m_logger) << LOGFUNCTION << " END"; return true; } static inline std::vector vecToVec(const QVector &vec) { return LtsUtils::convert>(vec); } static inline QVector vecToVec(const std::vector &vec) { return LtsUtils::convert>(vec); } void basicConfig(ConfigureMapUrlsWidget *tv, bool showTypeMinMax = true) { if (!tv) return; tv->tableView()->resizeColumnsToContents(); tv->tableView()->setColumnWidth(1, 100); tv->tableView()->setColumnWidth(2, 100); tv->tableView()->setColumnWidth(3, 100); tv->tableView()->setColumnWidth(4, 100); if (!showTypeMinMax) { tv->tableView()->hideColumn(ConfigureMapUrlModel::Columns::MapType); tv->tableView()->hideColumn(ConfigureMapUrlModel::Columns::MinScale); tv->tableView()->hideColumn(ConfigureMapUrlModel::Columns::MaxScale); } } void ConfigureMapUrls::syncTaToGui() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; ConfigureMapUrlsWidget *w; w = static_cast(m_ui->tabWidget->widget(0)); if (w && w->model()) { w->model()->setData(vecToVec(m_trainingArea.map2D())); TRACE_LOG(m_logger) << LOGFUNCTION << " model->rowCount()" << w->model()->rowCount(); } basicConfig(w); if (w && w->model()) connect(w->model(), &ConfigureMapUrlModel::dataChanged, this, []() { qCritical() << " Changed!!!"; }); w = static_cast(m_ui->tabWidget->widget(1)); if (w && w->model()) { w->model()->setData(vecToVec(m_trainingArea.scene3D())); TRACE_LOG(m_logger) << LOGFUNCTION << " model->rowCount()" << w->model()->rowCount(); } basicConfig(w); w = static_cast(m_ui->tabWidget->widget(2)); if (w && w->model()) w->model()->setData(vecToVec(m_trainingArea.elevation())); basicConfig(w, false); w = static_cast(m_ui->tabWidget->widget(3)); if (w && w->model()) w->model()->setData(vecToVec(m_trainingArea.sceneObjects())); basicConfig(w, false); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } void ConfigureMapUrls::syncGuiToTa() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; ConfigureMapUrlsWidget *w; w = static_cast(m_ui->tabWidget->widget(0)); if (w && w->model()) m_trainingArea.map2D(vecToVec(w->model()->allData())); w = static_cast(m_ui->tabWidget->widget(1)); if (w && w->model()) m_trainingArea.scene3D(vecToVec(w->model()->allData())); w = static_cast(m_ui->tabWidget->widget(2)); if (w && w->model()) m_trainingArea.elevation(vecToVec(w->model()->allData())); w = static_cast(m_ui->tabWidget->widget(3)); if (w && w->model()) m_trainingArea.sceneObjects(vecToVec(w->model()->allData())); // The model does not know it`s mapDescriptionTypeId. // So set them here: for (auto &item : m_trainingArea.map2D()) item.mapDescriptionTypeId(AssetManagement::MapDescriptionType_def::Map2D); for (auto &item : m_trainingArea.scene3D()) item.mapDescriptionTypeId(AssetManagement::MapDescriptionType_def::Scene3D); for (auto &item : m_trainingArea.elevation()) item.mapDescriptionTypeId(AssetManagement::MapDescriptionType_def::Elevation); for (auto &item : m_trainingArea.sceneObjects()) item.mapDescriptionTypeId(AssetManagement::MapDescriptionType_def::SceneObj); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } void ConfigureMapUrls::retranslate() { translate(m_ui, tr("Configure Map Urls")); // reBuildUi(); } bool ConfigureMapUrls::apply() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; if (!m_asmFtc) { ERROR_LOG(m_logger) << LOGFUNCTION << " Can not retrieve AssetManagement::FTC"; return false; } this->syncGuiToTa(); bool success = m_requestHelper->startSingleRequest([this]() { return m_asmFtc->trainingAreaModifyRequest(m_trainingArea, this); }); TRACE_LOG(m_logger) << LOGFUNCTION << " END, scucess=" << success; return success; } void ConfigureMapUrls::onTrainingAreaModifyReply(const Core::FunctionReplyObject &reply) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; if (reply.error()) { ERROR_LOG(m_logger) << LOGFUNCTION << "Got onTrainingAreaModifyReply error: " << reply(); return; } INFO_LOG(m_logger) << LOGFUNCTION << " Got onTrainingAreaModifyReply with id " << reply.id(); m_requestHelper->requestReply(reply); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } } //namespace SituationMap