From 372ac2707fea95b31d34953a9be6e34cde28e089 Mon Sep 17 00:00:00 2001 From: Alex Montgomery Date: Fri, 9 Jul 2021 19:37:57 -0700 Subject: [PATCH] fix outliner sorting using incorrect types for comparisons (#2052) * fix outliner sorting using incorrect types for comparisons, fixes for [LY-122258] and [LYN-3666] Signed-off-by: Alex Montgomery * fix annoying but necessary Qt type cast Signed-off-by: Alex Montgomery --- .../Outliner/OutlinerSortFilterProxyModel.cpp | 19 ++++++++++++++++++- .../EntityOutlinerSortFilterProxyModel.cpp | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerSortFilterProxyModel.cpp b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerSortFilterProxyModel.cpp index 660ea137cf..89cd361fb6 100644 --- a/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerSortFilterProxyModel.cpp +++ b/Code/Editor/Plugins/ComponentEntityEditorPlugin/UI/Outliner/OutlinerSortFilterProxyModel.cpp @@ -32,7 +32,24 @@ bool OutlinerSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelI bool OutlinerSortFilterProxyModel::lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const { - return sourceModel()->data(leftIndex).toString() < sourceModel()->data(rightIndex).toString(); + if (leftIndex.isValid() && rightIndex.isValid()) + { + QVariant leftData = sourceModel()->data(leftIndex); + QVariant rightData = sourceModel()->data(rightIndex); + + // make sure to compare the correct data types for sorting the current column + AZ_Assert(leftData.type() == rightData.type(), "OutlinerSortFilterProxyModel::lessThan types do not agree!"); + if (static_cast(leftData.type()) == QMetaType::QString) + { + return leftData.toString() < rightData.toString(); + } + else if (static_cast(leftData.type()) == QMetaType::ULongLong) + { + return leftData.toULongLong() < rightData.toULongLong(); + } + AZ_Error("Editor", false, "Error! Unhandled type \"%s\" in OutlinerSortFilterProxyModel::lessThan", leftData.typeName()); + } + return false; } void OutlinerSortFilterProxyModel::sort(int /*column*/, Qt::SortOrder /*order*/) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerSortFilterProxyModel.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerSortFilterProxyModel.cpp index 73139b5a67..90274bc734 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerSortFilterProxyModel.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UI/Outliner/EntityOutlinerSortFilterProxyModel.cpp @@ -34,7 +34,24 @@ namespace AzToolsFramework bool EntityOutlinerSortFilterProxyModel::lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const { - return sourceModel()->data(leftIndex).toString() < sourceModel()->data(rightIndex).toString(); + if (leftIndex.isValid() && rightIndex.isValid()) + { + QVariant leftData = sourceModel()->data(leftIndex); + QVariant rightData = sourceModel()->data(rightIndex); + + // make sure to compare the correct data types for sorting the current column + AZ_Assert(leftData.type() == rightData.type(), "EntityOutlinerSortFilterProxyModel::lessThan types do not agree!"); + if (static_cast(leftData.type()) == QMetaType::QString) + { + return leftData.toString() < rightData.toString(); + } + else if (static_cast(leftData.type()) == QMetaType::ULongLong) + { + return leftData.toULongLong() < rightData.toULongLong(); + } + AZ_Error("Editor", false, "Error! Unhandled type \"%s\" in EntityOutlinerSortFilterProxyModel::lessThan", leftData.typeName()); + } + return false; } void EntityOutlinerSortFilterProxyModel::sort(int /*column*/, Qt::SortOrder /*order*/)