diff --git a/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.cpp b/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.cpp index 400872780976caee678a45eab0bc6025b8d8c329..4931c1476a14004e57832160301f3a40e94e32fd 100644 --- a/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.cpp +++ b/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.cpp @@ -18,17 +18,18 @@ #include <QItemSelectionModel> #include <QMenu> #include <QModelIndex> +#include <QVariant> +#include <QDebug> InstrumentViewActions::InstrumentViewActions(QWidget* parent) : QObject(parent) - , m_addInstrumentAction(nullptr) + , m_addInstrumentMenu(nullptr) , m_removeInstrumentAction(nullptr) , m_cloneInstrumentAction(nullptr) , m_model(nullptr) , m_selectionModel(nullptr) { - m_addInstrumentAction = new QAction(QIcon(":/images/toolbar16dark_newitem.svg"), - "Add new instrument", this); + initAddInstrumentMenu(); m_removeInstrumentAction = new QAction(QIcon(":/images/toolbar16dark_recycle.svg"), "Remove this instrument", this); @@ -36,25 +37,54 @@ InstrumentViewActions::InstrumentViewActions(QWidget* parent) m_cloneInstrumentAction = new QAction(QIcon(":/images/toolbar16dark_cloneitem.svg"), "Clone this instrument", this); - connect(m_addInstrumentAction, SIGNAL(triggered()), this, SLOT(onAddInstrument())); - connect(m_removeInstrumentAction, SIGNAL(triggered()), this, SLOT(onRemoveInstrument())); - connect(m_cloneInstrumentAction, SIGNAL(triggered()), this, SLOT(onCloneInstrument())); + connect(m_removeInstrumentAction, &QAction::triggered, + this, &InstrumentViewActions::onRemoveInstrument); + connect(m_cloneInstrumentAction, &QAction::triggered, + this, &InstrumentViewActions::onCloneInstrument); } -void InstrumentViewActions::setModel(SessionModel* model) { m_model = model; } +void InstrumentViewActions::setModel(SessionModel* model) +{ + m_model = model; +} void InstrumentViewActions::setSelectionModel(QItemSelectionModel* selectionModel) { m_selectionModel = selectionModel; } +//! Returns menu to create one of available instrument types. + +QMenu* InstrumentViewActions::addInstrumentMenu() +{ + return m_addInstrumentMenu; +} + +//! Adds instrument of certain type. Type of instrument is extracted from sender internal data. + void InstrumentViewActions::onAddInstrument() { - SessionItem* instrument = m_model->insertNewItem(Constants::InstrumentType); - instrument->setItemName(suggestInstrumentName("Default GISAS")); - updateSelection(); + auto action = qobject_cast<QAction *>(sender()); + Q_ASSERT(action); + Q_ASSERT(action->data().canConvert(QVariant::String)); + + QString instrumentType = action->data().toString(); + + if (instrumentType == Constants::InstrumentType) { + SessionItem* instrument = m_model->insertNewItem(instrumentType); + instrument->setItemName(suggestInstrumentName("Default GISAS")); + updateSelection(); + } else { + qInfo() << "InstrumentViewActions::onAddInstrument() -> Not supported instrument type" + << instrumentType; + } + + // Setting default action to the just triggered action + m_addInstrumentMenu->setDefaultAction(action); } +//! Removes currently selected instrument. + void InstrumentViewActions::onRemoveInstrument() { QModelIndex currentIndex = m_selectionModel->currentIndex(); @@ -65,6 +95,8 @@ void InstrumentViewActions::onRemoveInstrument() updateSelection(); } +//! Clones currently selected instrument. + void InstrumentViewActions::onCloneInstrument() { QModelIndex currentIndex = m_selectionModel->currentIndex(); @@ -84,18 +116,15 @@ void InstrumentViewActions::onContextMenuRequest(const QPoint& point, setAllActionsEnabled(indexAtPoint.isValid()); - m_addInstrumentAction->setEnabled(true); - menu.addAction(m_cloneInstrumentAction); menu.addAction(m_removeInstrumentAction); menu.addSeparator(); - menu.addAction(m_addInstrumentAction); + menu.addMenu(m_addInstrumentMenu); menu.exec(point); } void InstrumentViewActions::setAllActionsEnabled(bool value) { - m_addInstrumentAction->setEnabled(value); m_removeInstrumentAction->setEnabled(value); m_cloneInstrumentAction->setEnabled(value); } @@ -144,3 +173,24 @@ QMap<QString, int> InstrumentViewActions::mapOfNames() return result; } + +//! Constructs menu to add instruments of various types. The type of instrument +//! is encoded in QAction internal data. + +void InstrumentViewActions::initAddInstrumentMenu() +{ + m_addInstrumentMenu = new QMenu("Add new instrument"); + + auto action = m_addInstrumentMenu->addAction("Default GISAS"); + action->setData(QVariant::fromValue(Constants::InstrumentType)); + connect(action, &QAction::triggered, this, &InstrumentViewActions::onAddInstrument); + m_addInstrumentMenu->setDefaultAction(action); + + action = m_addInstrumentMenu->addAction("Default OffSpec"); + action->setData(QVariant::fromValue(QStringLiteral("NotImplementedOffSpecType"))); + connect(action, &QAction::triggered, this, &InstrumentViewActions::onAddInstrument); + + action = m_addInstrumentMenu->addAction("Default Specular"); + action->setData(QVariant::fromValue(QStringLiteral("NotImplementedSpecType"))); + connect(action, &QAction::triggered, this, &InstrumentViewActions::onAddInstrument); +} diff --git a/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.h b/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.h index 41f8d8ba4c3c9f8f7d54972060350f4cd9fecb35..fc2aa4f78cd8477add93f93823f5820ec204f261 100644 --- a/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.h +++ b/GUI/coregui/Views/InstrumentWidgets/InstrumentViewActions.h @@ -23,6 +23,7 @@ class QAction; class SessionModel; class QItemSelectionModel; class QModelIndex; +class QMenu; //! Collection of actions to add/remove/clone instrument. @@ -31,11 +32,13 @@ class BA_CORE_API_ InstrumentViewActions : public QObject Q_OBJECT public: - explicit InstrumentViewActions(QWidget* parent = 0); + explicit InstrumentViewActions(QWidget* parent = nullptr); void setModel(SessionModel* model); void setSelectionModel(QItemSelectionModel* selectionModel); + QMenu* addInstrumentMenu(); + public slots: void onAddInstrument(); void onRemoveInstrument(); @@ -47,8 +50,9 @@ private: void updateSelection(); QString suggestInstrumentName(const QString& currentName); QMap<QString, int> mapOfNames(); + void initAddInstrumentMenu(); - QAction* m_addInstrumentAction; + QMenu* m_addInstrumentMenu; QAction* m_removeInstrumentAction; QAction* m_cloneInstrumentAction; SessionModel* m_model;