diff --git a/GUI/coregui/Models/ItemFactory.cpp b/GUI/coregui/Models/ItemFactory.cpp index 6988a86cdc6045cb9ba631f1b01a1e72f0939e4b..211fd6c471896b40d60462fd96fea185b868e298 100644 --- a/GUI/coregui/Models/ItemFactory.cpp +++ b/GUI/coregui/Models/ItemFactory.cpp @@ -18,9 +18,16 @@ #include "LayerItem.h" #include "ParticleDecorationItem.h" +QList<QString> ItemFactory::m_all_item_names = QList<QString>() + << QString("MultiLayer") + << QString("Layer") + << QString("ParticleDecoration"); ParameterizedItem *ItemFactory::createItem(const QString &model_name) { + if (!m_all_item_names.contains(model_name)) { + return 0; + } if (model_name==QString("MultiLayer")) { return new MultiLayerItem(); } diff --git a/GUI/coregui/Models/ItemFactory.h b/GUI/coregui/Models/ItemFactory.h index 1a7f8f50ed7c40184ce49e76f27a4030781cf8eb..ce4316bbac7a914e0990cba3e2cccd4a953dea7e 100644 --- a/GUI/coregui/Models/ItemFactory.h +++ b/GUI/coregui/Models/ItemFactory.h @@ -22,7 +22,12 @@ class ItemFactory { public: static ParameterizedItem *createItem(const QString &model_name); + static QList<QString> getAllItemNames() { + return m_all_item_names; + } + private: + static QList<QString> m_all_item_names; ItemFactory() {} ~ItemFactory() {} }; diff --git a/GUI/coregui/Views/SampleView.cpp b/GUI/coregui/Views/SampleView.cpp index 5dd348be528a6e51f607aacd1f980bb5e68bb057..4c216f7c34dbc9b75928d05b66de5c831c93f10d 100644 --- a/GUI/coregui/Views/SampleView.cpp +++ b/GUI/coregui/Views/SampleView.cpp @@ -4,12 +4,11 @@ #include "SampleToolBar.h" #include "MaterialBrowser.h" #include "GUIHelpers.h" +#include <ItemFactory.h> #include <QDockWidget> #include <QAbstractItemView> -//#include "widgetbox.h" -//#include "QToolBar" #include <QToolBar> #include <QAction> #include <QToolButton> @@ -101,13 +100,13 @@ void SampleView::initSubWindows() void SampleView::createActions() { - addAct = new QAction(tr("Add"), this); - addAct->setStatusTip(tr("Add a new object")); - connect(addAct, SIGNAL(triggered()), this, SLOT(addItem())); + m_add_item_mapper = new QSignalMapper(this); + connect(m_add_item_mapper, SIGNAL(mapped(const QString &)), + this, SLOT(addItem(const QString &))); - delAct = new QAction(tr("Delete"), this); - delAct->setStatusTip(tr("Delete current object")); - connect(delAct, SIGNAL(triggered()), this, SLOT(deleteItem())); + m_delete_item_action = new QAction(tr("Delete"), this); + m_delete_item_action->setStatusTip(tr("Delete current object")); + connect(m_delete_item_action, SIGNAL(triggered()), this, SLOT(deleteItem())); } void SampleView::resetToDefaultLayout() @@ -133,10 +132,15 @@ void SampleView::resetToDefaultLayout() setTrackingEnabled(true); } -void SampleView::addItem() +void SampleView::addItem(const QString &item_name) { + QModelIndex currentIndex = getTreeView()->currentIndex(); + getSessionModel()->insertNewItem(item_name, currentIndex); + setDirty(); + updateUi(); } + void SampleView::deleteItem() { QModelIndex currentIndex = getTreeView()->currentIndex(); @@ -162,12 +166,40 @@ void SampleView::deleteItem() void SampleView::showContextMenu(const QPoint &pnt) { - QList<QAction *> actions; - if (getTreeView()->indexAt(pnt).isValid()) { - actions.append(delAct); + QMenu menu; + QMenu add_menu(QString("Add")); + QList<QString> addItemNames; + QModelIndex parent_index = getTreeView()->indexAt(pnt); + getTreeView()->setCurrentIndex(parent_index); + if (!parent_index.isValid()) { + addItemNames = ItemFactory::getAllItemNames(); + } else { + QStandardItem *parent = getSessionModel()->itemFromIndex(parent_index); + ParameterizedItem *parent_par = static_cast<ParameterizedItem *>(parent); + addItemNames = parent_par->getAcceptableChildren(); + } + if (addItemNames.size() > 0) { + foreach (QString item_name, addItemNames) { + QAction *add_action = 0; + if (m_add_action_map.contains(item_name)) { + add_action = m_add_action_map[item_name]; + } + else { + add_action = new QAction(item_name, this); + m_add_action_map[item_name] = add_action; + connect(add_action, SIGNAL(triggered()), + m_add_item_mapper, SLOT(map())); + m_add_item_mapper->setMapping(add_action, item_name); + } + add_menu.addAction(add_action); + } + menu.addMenu(&add_menu); + } + if (parent_index.isValid()) { + menu.addAction(m_delete_item_action); } - if (actions.count() > 0) { - QMenu::exec(actions, getTreeView()->mapToGlobal(pnt)); + if (!menu.isEmpty()) { + menu.exec(getTreeView()->mapToGlobal(pnt)); } } diff --git a/GUI/coregui/Views/SampleView.h b/GUI/coregui/Views/SampleView.h index 70500a309d9b962e1e9d4034056baebbba49cccf..ba2c7b2d5d65b78de2e8fdfc2e854135ccf66d4d 100644 --- a/GUI/coregui/Views/SampleView.h +++ b/GUI/coregui/Views/SampleView.h @@ -7,6 +7,7 @@ #include <QDockWidget> #include <QTreeView> #include <QAction> +#include <QSignalMapper> #include "SessionModel.h" @@ -35,10 +36,9 @@ public: SampleView(QWidget *parent = 0); virtual ~SampleView(); - public slots: void resetToDefaultLayout(); - void addItem(); + void addItem(const QString &item_name); void deleteItem(); // void materialEditorCall(); @@ -51,6 +51,7 @@ private: void initSubWindows(); void createActions(); void connectSignals(); + void clearSignalMapper(); SessionModel *getSessionModel(); QTreeView *getTreeView(); @@ -61,8 +62,9 @@ private: QWidget *m_subWindows[NumberOfSubWindows]; QDockWidget *m_dockWidgets[NumberOfSubWindows]; - QAction *addAct; - QAction *delAct; + QSignalMapper *m_add_item_mapper; + QMap<QString, QAction *> m_add_action_map; + QAction *m_delete_item_action; SessionModel *m_session; QTreeView *m_tree_view;