123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <SceneAPI/SceneCore/Containers/Scene.h>
- #include <SceneAPI/SceneCore/Containers/SceneGraph.h>
- #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
- #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
- #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidget.h>
- #include <SceneAPI/SceneUI/RowWidgets/NodeListSelectionWidget.h>
- namespace AZ
- {
- namespace SceneAPI
- {
- namespace UI
- {
- AZ_CLASS_ALLOCATOR_IMPL(NodeListSelectionWidget, SystemAllocator);
- NodeListSelectionWidget::NodeListSelectionWidget(QWidget* parent)
- : QComboBox(parent)
- , m_classTypeId(Uuid::CreateNull())
- , m_exactClassTypeMatch(false)
- , m_hasDirtyList(true)
- , m_useShortNames(false)
- , m_excludeEndPoints(false)
- , m_defaultToDisabled(false)
- {
- connect(this, &QComboBox::currentTextChanged, this, &NodeListSelectionWidget::OnTextChange);
- }
- void NodeListSelectionWidget::SetCurrentSelection(const AZStd::string& selection)
- {
- m_currentSelection = selection;
- if (!m_hasDirtyList)
- {
- SetSelection();
- }
- }
- AZStd::string NodeListSelectionWidget::GetCurrentSelection() const
- {
- return currentText().toStdString().c_str();
- }
- void NodeListSelectionWidget::AddDisabledOption(const AZStd::string& option)
- {
- m_disabledOption = option;
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::AddDisabledOption(AZStd::string&& option)
- {
- m_disabledOption = AZStd::move(option);
- m_hasDirtyList = true;
- }
- const AZStd::string& NodeListSelectionWidget::GetDisabledOption() const
- {
- return m_disabledOption;
- }
- void NodeListSelectionWidget::UseShortNames(bool use)
- {
- m_useShortNames = use;
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::ExcludeEndPoints(bool exclude)
- {
- m_excludeEndPoints = exclude;
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::DefaultToDisabled(bool value)
- {
- m_defaultToDisabled = value;
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::SetClassTypeId(const Uuid& classTypeId)
- {
- m_classTypeId = classTypeId;
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::ClearClassTypeId()
- {
- m_classTypeId = Uuid::CreateNull();
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::UseExactClassTypeMatch(bool exactMatch)
- {
- m_exactClassTypeMatch = exactMatch;
- m_hasDirtyList = true;
- }
- void NodeListSelectionWidget::OnTextChange(const QString& text)
- {
- if (!m_hasDirtyList)
- {
- m_currentSelection = text.toStdString().c_str();
- emit valueChanged(m_currentSelection);
- }
- }
- void NodeListSelectionWidget::showEvent(QShowEvent* event)
- {
- if (m_hasDirtyList)
- {
- clear();
- ManifestWidget* mainWidget = ManifestWidget::FindRoot(this);
- AZ_Assert(mainWidget, "NodeListSelectionWidget is not an (in)direct child of the ManifestWidget.");
- if (!mainWidget)
- {
- return;
- }
- const Containers::SceneGraph& graph = mainWidget->GetScene()->GetGraph();
- BuildList(graph);
- AddDisabledOption();
- SetSelection();
- setEnabled(count() > 1);
-
- m_hasDirtyList = false;
- }
- QComboBox::showEvent(event);
- }
- void NodeListSelectionWidget::BuildList(const Containers::SceneGraph& graph)
- {
- QStringList entries;
- auto view = Containers::Views::MakePairView(graph.GetNameStorage(), graph.GetContentStorage());
- for (auto it = view.begin(); it != view.end(); ++it)
- {
- if (!it->second || it->first.GetPathLength() == 0)
- {
- continue;
- }
- if (!IsCorrectType(*it->second))
- {
- continue;
- }
- if (m_excludeEndPoints)
- {
- Containers::SceneGraph::NodeIndex index = graph.ConvertToNodeIndex(it.GetFirstIterator());
- if (graph.IsNodeEndPoint(index))
- {
- continue;
- }
- }
- AddEntry(entries, it->first);
- }
- if (!entries.empty())
- {
- entries.removeDuplicates();
- addItems(entries);
- }
- }
- bool NodeListSelectionWidget::IsCorrectType(const DataTypes::IGraphObject& object) const
- {
- if (m_classTypeId.IsNull())
- {
- return true;
- }
- if (m_exactClassTypeMatch)
- {
- return object.RTTI_GetType() == m_classTypeId;
- }
- else
- {
- return object.RTTI_IsTypeOf(m_classTypeId);
- }
- }
- void NodeListSelectionWidget::AddEntry(QStringList& comboListEntries, const Containers::SceneGraph::Name& name)
- {
- if (m_useShortNames)
- {
- const char* shortName = name.GetName();
- comboListEntries.append(QString(shortName));
- }
- else
- {
- const char* pathName = name.GetPath();
- comboListEntries.append(QString(pathName));
- }
- }
- void NodeListSelectionWidget::SetSelection()
- {
- QString entryName = m_currentSelection.c_str();
- int index = findText(entryName);
- if (m_disabledOption.empty())
- {
- if (index >= 0)
- {
- setCurrentIndex(index);
- }
- else
- {
- if (!isEditable())
- {
- // If not freeform editable, and no disabled option available to set to first entry.
- setCurrentIndex(0);
- }
- else
- {
- setEditText(entryName);
- }
- }
- }
- else
- {
- // Check against index 1 as an empty string will return the separator.
- if (index >= 0 && index != 1)
- {
- setCurrentIndex(index);
- }
- else if (!m_defaultToDisabled && count() >= 2)
- {
- // Pick third option as the first is the default followed
- // by the separator.
- setCurrentIndex(2);
- }
- else if (!isEditable())
- {
- // If not editable and no match was found, set to first entry.
- setCurrentIndex(0);
- }
- else
- {
- setEditText(entryName);
- }
- }
- }
- void NodeListSelectionWidget::AddDisabledOption()
- {
- if (!m_disabledOption.empty())
- {
- insertItem(0, m_disabledOption.c_str());
- // Only add a separator if the disabled option isn't the only entry.
- if (count() > 1)
- {
- insertSeparator(1);
- }
- }
- }
- } // UI
- } // SceneAPI
- } // AZ
- #include <RowWidgets/moc_NodeListSelectionWidget.cpp>
|