/** * @RDE_HEADER@ * GUI Plugin for ParticipantManagement - ParticipantSearchDialog * $Id$ * $HeadURL$ * $Author$ * Created by Patric Karwat, SLSE, 2016-06-13 */ #include "participant_search_dialog.h" #include "ui_participant_search_dialog.h" #include #include namespace ParticipantManagement { int ParticipantSearchDialog::s_preselectedParty = -1; bool ParticipantSearchDialog::s_preselectedAllParticipants = true; ParticipantSearchDialog::ParticipantSearchDialog(QWidget *parent) : LtsWidgets::LtsDialog(parent, LtsWidgets::ButtonCombination::CloseOnly) , m_model(nullptr) , m_equipNo(0) , m_dtc(nullptr) , m_ppmCbxIfc(nullptr) , m_ui(new Ui::ParticipantSearchDialog) , m_logger(getLogger("ParticipantSearchDialog")) { m_ui->setupUi(this); m_ui->leEquipNo->setValidator(new QIntValidator(0, 100000, m_ui->leEquipNo)); setUnchangeable(); m_buttonBox->connectButton(LtsWidgets::DialogButton::Show, this, &ParticipantSearchDialog::onClicked); m_updateObjectIndicator = new QTimer(this); m_updateObjectIndicator->setSingleShot(true); m_updateObjectIndicator->setInterval(100); connect(m_updateObjectIndicator, &QTimer::timeout, this, &ParticipantSearchDialog::updateObjectIndicator); setOnlineHelpFilename(QStringLiteral("Situation-Find_Participant")); } ParticipantSearchDialog::~ParticipantSearchDialog() { if (m_sitMapGuiIfc) m_sitMapGuiIfc->removeObjectIndicator(); delete m_ui; } void ParticipantSearchDialog::retranslate() { translate(m_ui, tr("Participant Search")); m_buttonBox->getButton(LtsWidgets::DialogButton::Show)->setText(tr("Show Participant Info")); } bool ParticipantSearchDialog::init(LtsSession::LtsSession *session, const std::shared_ptr &dtc, const std::shared_ptr &ppmGuiIfc, const std::shared_ptr &ppmOcGuiIfc) { m_dtc = dtc; m_ppmGuiIfc = ppmGuiIfc; m_ocParticipantInfoIfc = ppmOcGuiIfc; if (!m_dtc) return false; if (!session->getForeignGuiInterface(m_ppmCbxIfc)) return false; if (!session->getForeignGuiInterface(m_sitMapGuiIfc)) return false; std::shared_ptr asbIfc; session->getForeignDataInterface(asbIfc); m_ui->cbAll->setChecked(s_preselectedAllParticipants); m_model = new ParticipantEditModel(this, m_dtc, asbIfc); m_ui->tvParticipants->setModel(m_model); connect(m_ui->cbParty, static_cast(&LtsWidgets::LtsComboBox::currentIndexChanged), this, &ParticipantSearchDialog::changeParty); QItemSelectionModel *selectionModel = m_ui->tvParticipants->selectionModel(); connect(selectionModel, &QItemSelectionModel::selectionChanged, this, &ParticipantSearchDialog::slotLookup); connect(m_ui->tvParticipants, &QTreeView::doubleClicked, this, &ParticipantSearchDialog::onDoubleClicked); connect(m_ui->slEquipmentNumber, &QSlider::valueChanged, this, &ParticipantSearchDialog::slotLookup); connect(m_ui->pbLookup, &QPushButton::clicked, this, &ParticipantSearchDialog::slotLookup); connect(m_ui->leEquipNo, &QLineEdit::returnPressed, this, &ParticipantSearchDialog::slotLookup); connect(m_ui->cbAll, &QCheckBox::toggled, this, &ParticipantSearchDialog::changeParty); m_ppmCbxIfc->fillPartyComboBox(m_ui->cbParty, ForeignGuiComboboxInterface::CbxPartyFilter::All); const int idx = m_ui->cbParty->findData(s_preselectedParty); if (idx >= 0) m_ui->cbParty->setCurrentIndex(idx); changeParty(); m_buttonBox->getButton(LtsWidgets::DialogButton::Show)->setEnabled(false); return true; } void ParticipantSearchDialog::slotLookup() { //block all signals here QSignalBlocker blx2(m_ui->slEquipmentNumber); QSignalBlocker blx3(m_ui->leEquipNo); //set sliderposition to current value int newEquipNo = -1; if (qobject_cast(sender())) newEquipNo = changeLabelText(m_ui->slEquipmentNumber->value()); if (qobject_cast(sender()) || qobject_cast(sender())) { //get Number from newEquipNo = m_ui->leEquipNo->text().toInt(); } if (qobject_cast(sender())) { newEquipNo = onSelectionChanged(); //selection cleared (do we allow a deselct?) // if (newEquipNo < 0 ) // { // m_equipNo = newEquipNo; // return; // } } if (newEquipNo == m_equipNo) return; m_equipNo = newEquipNo; //set Label and update lineEdit if (m_equipNo > 0) m_ui->leEquipNo->setText(QString::number(m_equipNo)); changeSliderPosition(m_equipNo); //finally enable show button if found m_buttonBox->getButton(LtsWidgets::DialogButton::Show)->setEnabled(m_participantMap.contains(m_equipNo)); //function for itemselection auto cb = [this](const QModelIndex & idx) { m_ui->tvParticipants->setCurrentIndex(idx); }; auto it = m_participantMap.find(m_equipNo); if (it == m_participantMap.end() && !qobject_cast(sender())) { m_ui->tvParticipants->setCurrentIndex(QModelIndex()); return; } m_model->selectItemByParticipantId(it.value(), cb); // update ObjectIndicator: if (m_updateObjectIndicator) m_updateObjectIndicator->start(); // start is restart } void ParticipantSearchDialog::updateObjectIndicator() { if (!m_sitMapGuiIfc) return; int participantId = m_participantMap.value(m_equipNo, -1); if (participantId >= 0) m_sitMapGuiIfc->objectIndicatorToParticipant(participantId); else m_sitMapGuiIfc->removeObjectIndicator(); } void ParticipantSearchDialog::enterEvent(QEvent *e) { BaseClass::enterEvent(e); updateObjectIndicator(); } void ParticipantSearchDialog::leaveEvent(QEvent *e) { BaseClass::leaveEvent(e); // to early, remove obj at dtor // if (m_sitMapGuiIfc) // m_sitMapGuiIfc->removeObjectIndicator(); } //private int ParticipantSearchDialog::onSelectionChanged() { QModelIndex index = m_ui->tvParticipants->currentIndex(); //finally enable show button m_buttonBox->getButton(LtsWidgets::DialogButton::Show)->setEnabled(true); ParticipantManagement::ParticipantData data; data = m_model->getParticipantData(index); if (!data.equipmentNumber().is_set()) return -1; return data.equipmentNumber().get(); } void ParticipantSearchDialog::onDoubleClicked() { QModelIndex index = m_ui->tvParticipants->currentIndex(); ParticipantManagement::ParticipantData data; data = m_model->getParticipantData(index); if (m_ppmGuiIfc) m_ppmGuiIfc->showParticipantInfoWindow(data.id()); if (m_ocParticipantInfoIfc) m_ocParticipantInfoIfc->showOcParticipantInfoWindow(data.id()); } void ParticipantSearchDialog::clearSelections() { changeSliderPosition(-1); m_ui->leEquipNo->clear(); m_equipNo = 0; m_ui->tvParticipants->setCurrentIndex(QModelIndex()); m_buttonBox->getButton(LtsWidgets::DialogButton::Show)->setEnabled(false); } //Slots void ParticipantSearchDialog::changeParty() { s_preselectedAllParticipants = m_ui->cbAll->isChecked(); if (m_ui->cbAll->isChecked()) { m_ui->cbParty->setEnabled(false); m_ui->tvParticipants->setEnabled(false); m_model->resetModel(); fillMapFromDtc(); } else { m_ui->cbParty->setEnabled(true); m_ui->tvParticipants->setEnabled(true); int partyId = m_ui->cbParty->currentData().toInt(); s_preselectedParty = partyId; m_model->onReinitModel(partyId); m_ui->tvParticipants->expandAll(); fillParticipantMapFromTree(); } m_ui->slEquipmentNumber->setRange(0, m_participantMap.size() - 1); clearSelections(); } void ParticipantSearchDialog::fillMapFromDtc() { m_participantMap.clear(); auto cb = [this](const Participant & data) { if (data.equipmentNumber().is_set()) m_participantMap.insert(data.equipmentNumber().get(), data.id()); }; m_dtc->participants(cb); } int ParticipantSearchDialog::changeLabelText(int value) { if (value < 0 || value >= m_participantMap.size()) return -1; auto it = m_participantMap.begin(); it += value; int newEqNr = it.key(); m_ui->lblEquipmentNumber->setText(QString::number(newEqNr)); m_ui->leEquipNo->setText(QString::number(newEqNr)); return newEqNr; } void ParticipantSearchDialog::changeSliderPosition(int equipNo) { int i = 0; int pos = -1; for (auto it = m_participantMap.cbegin(); it != m_participantMap.cend(); ++it) { if (equipNo == it.key()) { pos = i; break; } i++; } if (pos >= 0) { m_ui->lblEquipmentNumber->setText(QString::number(equipNo)); m_ui->slEquipmentNumber->setSliderPosition(pos); } else { m_ui->slEquipmentNumber->setSliderPosition(-1); m_ui->lblEquipmentNumber->setText(tr("Not found")); } } void ParticipantSearchDialog::fillParticipantMapFromTree() { m_participantMap.clear(); fillParticipantMapFromTree(QModelIndex()); } void ParticipantSearchDialog::fillParticipantMapFromTree(const QModelIndex &parent) { const int rc = m_model->rowCount(parent); for (int row = 0; row < rc; ++row) fillParticipantMapFromTree(m_model->index(row, 0, parent)); const ParticipantManagement::ParticipantData data(m_model->getParticipantData(parent)); if (data.equipmentNumber().is_set()) m_participantMap.insert(data.equipmentNumber().get(), data.id()); } void ParticipantSearchDialog::onClicked() { int participantId = m_participantMap.value(m_equipNo); if (!m_ui->cbAll->isChecked()) onDoubleClicked(); else { //ERROR_LOG(m_logger) << "ParticipantId: " << participantId if (m_ppmGuiIfc) m_ppmGuiIfc->showParticipantInfoWindow(participantId); } } } // namespace ParticipantManagement