diff --git a/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp b/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp index ff621958d1112450e9d6f6d67deac74675fa5ed7..4bd008392d96190584488afd1adec34f443fc32b 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp +++ b/GUI/Views/InstrumentWidgets/InstrumentEditor.cpp @@ -131,8 +131,6 @@ void InstrumentEditor::setItem(InstrumentItem* item) void InstrumentEditor::onInstrumentNameChanged() { - if (m_item) { + if (m_item) m_item->setItemName(m_nameLineEdit->text()); - emit instrumentNameChanged(); - } } diff --git a/GUI/Views/InstrumentWidgets/InstrumentEditor.h b/GUI/Views/InstrumentWidgets/InstrumentEditor.h index 291eb5dd1dd8dda144d5188532a53537d8a6c4cc..c60a0857cbd8019d385bb45b49f1e8e8766ce373 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentEditor.h +++ b/GUI/Views/InstrumentWidgets/InstrumentEditor.h @@ -40,9 +40,6 @@ public: void setItem(InstrumentItem* instrument); -signals: - void instrumentNameChanged(); - private slots: void onInstrumentNameChanged(); diff --git a/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp b/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp index df58d282c718e0ab5bcb62d633618761376b1a3a..c152878cba0cd0bf6d47307f6e2244681744d216 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp +++ b/GUI/Views/InstrumentWidgets/InstrumentListModel.cpp @@ -66,6 +66,15 @@ InstrumentListModel::InstrumentListModel(InstrumentModel* instruments, QObject* m_specularIcon.addPixmap(QPixmap(":/images/specular_instrument_shaded.svg"), QIcon::Normal); m_depthProbeIcon.addPixmap(QPixmap(":/images/depth_instrument.svg"), QIcon::Selected); m_depthProbeIcon.addPixmap(QPixmap(":/images/depth_instrument_shaded.svg"), QIcon::Normal); + + for (InstrumentItem* instrument : m_instruments->instrumentItems()) + attachToInstrument(instrument); +} + +InstrumentListModel::~InstrumentListModel() +{ + for (InstrumentItem* instrument : m_instruments->instrumentItems()) + detachFromInstrument(instrument); } int InstrumentListModel::rowCount(const QModelIndex&) const @@ -129,7 +138,9 @@ QModelIndex InstrumentListModel::addNewDepthProbeInstrument() void InstrumentListModel::removeInstrument(const QModelIndex& index) { beginRemoveRows(QModelIndex(), index.row(), index.row()); - m_instruments->removeInstrument(instrumentForIndex(index)); + InstrumentItem* instrument = instrumentForIndex(index); + detachFromInstrument(instrument); + m_instruments->removeInstrument(instrument); endRemoveRows(); } @@ -149,6 +160,7 @@ QModelIndex InstrumentListModel::copyInstrument(const InstrumentItem* source) beginInsertRows(QModelIndex(), row, row); InstrumentItem* copy = m_instruments->insertCopy(*source); copy->setItemName(copyName); + attachToInstrument(copy); endInsertRows(); return m_instruments->indexOfItem(copy); @@ -166,7 +178,31 @@ template <class Instrument> QModelIndex InstrumentListModel::addNewInstrument() beginInsertRows(QModelIndex(), row, row); Instrument* instrument = m_instruments->insertItem<Instrument>(); instrument->setItemName(name); + attachToInstrument(instrument); endInsertRows(); return m_instruments->indexOfItem(instrument); } + +void InstrumentListModel::attachToInstrument(InstrumentItem* instrument) +{ + instrument->mapper()->setOnPropertyChange + (std::bind(&InstrumentListModel::notifyInstrumentPropertyChange, this, instrument, + std::placeholders::_1), this); +} + +void InstrumentListModel::detachFromInstrument(InstrumentItem* instrument) +{ + instrument->mapper()->unsubscribe(this); +} + +void InstrumentListModel::notifyInstrumentPropertyChange(InstrumentItem* instrument, + const QString& property) +{ + if (SessionItem::isItemNamePropertyName(property)) { + QVector<InstrumentItem*> instruments = m_instruments->instrumentItems(); + int i = instruments.indexOf(instrument); + if (i != -1) + emit dataChanged(index(i,0),index(i,0)); + } +} diff --git a/GUI/Views/InstrumentWidgets/InstrumentListModel.h b/GUI/Views/InstrumentWidgets/InstrumentListModel.h index de0b10658b80a13e1ce929360fcfff1e8f7c7acc..182f381bdf5c73b4dcb9236bace09faf82f45148 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentListModel.h +++ b/GUI/Views/InstrumentWidgets/InstrumentListModel.h @@ -26,6 +26,7 @@ class InstrumentListModel : public QAbstractListModel { public: InstrumentListModel(InstrumentModel* instruments, QObject* parent = nullptr); + ~InstrumentListModel(); virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override; @@ -50,6 +51,10 @@ public: private: template <class Instrument> QModelIndex addNewInstrument(); + void attachToInstrument(InstrumentItem* item); + void detachFromInstrument(InstrumentItem* item); + void notifyInstrumentPropertyChange(InstrumentItem* instrument, const QString& property); + private: InstrumentModel* m_instruments; diff --git a/GUI/Views/InstrumentWidgets/InstrumentListView.cpp b/GUI/Views/InstrumentWidgets/InstrumentListView.cpp index 04545dc1c6c85ceb9ff75cce036bfd83c1e649f5..e9c64295323067c25579e8002b1c99e60d8dc700 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentListView.cpp +++ b/GUI/Views/InstrumentWidgets/InstrumentListView.cpp @@ -146,11 +146,6 @@ QList<QAction*> InstrumentListView::toolbarActions() const m_loadFromLibraryAction}; } -void InstrumentListView::updateSelectedInstrument() -{ - update(); -} - void InstrumentListView::onItemSelectionChanged() { updateActions(); diff --git a/GUI/Views/InstrumentWidgets/InstrumentListView.h b/GUI/Views/InstrumentWidgets/InstrumentListView.h index 3619741f41a84587d986b1fda9bd4f9ef8a3e2b1..917c07163e66c37688ffee426ba59c3ea6915d31 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentListView.h +++ b/GUI/Views/InstrumentWidgets/InstrumentListView.h @@ -41,9 +41,6 @@ public: signals: void instrumentSelected(InstrumentItem* instrument); -public slots: - void updateSelectedInstrument(); - private slots: void onItemSelectionChanged(); void onNewGisas(); diff --git a/GUI/Views/InstrumentWidgets/InstrumentView.cpp b/GUI/Views/InstrumentWidgets/InstrumentView.cpp index f2827c8dbcde053531fec48fd261a26e0e1a3456..ece40599aa1d76f20bd08ae800ec507afba46d43 100644 --- a/GUI/Views/InstrumentWidgets/InstrumentView.cpp +++ b/GUI/Views/InstrumentWidgets/InstrumentView.cpp @@ -44,9 +44,6 @@ InstrumentView::InstrumentView(QWidget* parent, ProjectDocument* document) connect(m_instrumentListView, &InstrumentListView::instrumentSelected, this, &InstrumentView::onInstrumentSelected); - connect(m_instrumentEditor, &InstrumentEditor::instrumentNameChanged, m_instrumentListView, - &InstrumentListView::updateSelectedInstrument); - onInstrumentSelected(nullptr); }