Filter Sorting Working

This commit is contained in:
igarri
2021-05-12 11:03:02 +01:00
parent 6e15e87e41
commit 1c990b2ef6
5 changed files with 33 additions and 285 deletions
@@ -32,7 +32,7 @@ namespace AzToolsFramework
: QSortFilterProxyModel(parent)
{
m_showColumn.insert(static_cast<int>(AssetBrowserEntry::Column::DisplayName));
m_showColumn.insert(static_cast<int>(AssetBrowserEntry::Column::Path));
//m_showColumn.insert(static_cast<int>(AssetBrowserEntry::Column::Path));
m_collator.setNumericMode(true);
AssetBrowserComponentNotificationBus::Handler::BusConnect();
}
@@ -63,7 +63,6 @@ namespace AzToolsFramework
invalidateFilter();
m_invalidateFilter = false;
}
Q_EMIT entriesUpdated();
}
bool AssetBrowserFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
@@ -53,7 +53,6 @@ namespace AzToolsFramework
Q_SIGNALS:
void filterChanged();
void entriesUpdated();
//////////////////////////////////////////////////////////////////////////
//QSortFilterProxyModel
@@ -7,9 +7,7 @@ AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option")
#include <AssetBrowser/AssetBrowserTableModel.h>
#include <AssetBrowser/AssetBrowserFilterModel.h>
#include <QCollator>
#include <QSharedPointer>
#include <QTimer>
AZ_POP_DISABLE_WARNING
namespace AzToolsFramework
{
@@ -18,18 +16,7 @@ namespace AzToolsFramework
AssetBrowserTableModel::AssetBrowserTableModel(QObject* parent /* = nullptr */)
: QSortFilterProxyModel(parent)
{
sort(0);
setDynamicSortFilter(false);
setRecursiveFilteringEnabled(true);
AssetBrowserComponentNotificationBus::Handler::BusConnect();
}
AssetBrowserTableModel::~AssetBrowserTableModel()
{
AssetBrowserComponentNotificationBus::Handler::BusDisconnect();
}
void AssetBrowserTableModel::OnAssetBrowserComponentReady()
{
//BuildMap(sourceModel());
}
void AssetBrowserTableModel::setSourceModel(QAbstractItemModel* sourceModel)
{
@@ -69,86 +56,68 @@ namespace AzToolsFramework
// no filter present, every entry is not visible
if (!m_filterModel->GetFilter())
{
return false;
return true;
}
return true;
}
QModelIndex AssetBrowserTableModel::index(int row, int column, const QModelIndex& parent) const
{
/*AZ_UNUSED(row);
AZ_UNUSED(column);*/
return parent.isValid() ? QModelIndex() : createIndex(row, column, m_indexMap[row].internalPointer());
}
QVariant AssetBrowserTableModel::data(const QModelIndex& index, int role) const
{
//AZ_UNUSED(role);
auto sourceIndex = mapToSource(index);
if (!sourceIndex.isValid())
return QVariant();
AssetBrowserEntry* entry = GetAssetEntry(sourceIndex); // static_cast<AssetBrowserEntry*>(sourceIndex.internalPointer());
AssetBrowserEntry* entry = GetAssetEntry(sourceIndex);
if (entry == nullptr)
{
AZ_Assert(
false, "ERROR - index internal pointer not pointing to an AssetEntry. Tree provided by the AssetBrowser invalid?");
AZ_Assert(false, "ERROR - index internal pointer not pointing to an AssetEntry. Tree provided by the AssetBrowser invalid?");
return Qt::PartiallyChecked;
}
return sourceIndex.data(role); // return entry->data(index.column());
//return QVariant::fromValue(entry);
//AZ_UNUSED(role);
//if (index.isValid())
//{
// ////if (role == AssetBrowserModel::EntryRole)
// //{
// QModelIndex modelIndex = mapFromSource(index);
// auto assetEntry = static_cast<const AssetBrowserEntry*>(index.internalPointer());
// return QVariant::fromValue(assetEntry);
//}
//return QVariant(); // AzToolsFramework::AssetBrowser::AssetBrowserModel::data(index, role);
return sourceIndex.data(role);
}
int AssetBrowserTableModel::rowCount(const QModelIndex& parent) const
{
return !parent.isValid() ? m_rowMap.size() : 0;
}
QVariant AssetBrowserTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
{
switch (section)
{
case static_cast<int>(AssetBrowserEntry::Column::Name):
return QString("Name");
case static_cast<int>(AssetBrowserEntry::Column::Path):
return QString("Path");
default:
return QString::number(section);
}
}
return QSortFilterProxyModel::headerData(section, orientation, role); // QVariant();
}
int AssetBrowserTableModel::BuildMap(const QAbstractItemModel* model, const QModelIndex& parent, int row)
{
//int rows = model ? model->rowCount(parent) : 0;
//for (int i = 0; i < rows; ++i)
//{
// auto index = model->index(i, 0, parent);
// //if (!model->hasChildren(index))
// //{
// beginInsertRows(parent, row, row);
// m_rowMap[index] = row;
// m_indexMap[row] = index;
// endInsertRows();
// Q_EMIT dataChanged(parent, parent);
// row = row + 1;
// //}
// if (model->hasChildren(index))
// {
// row = BuildMap(model, index, row);
// }
//}
//return row;
int rows = model ? model->rowCount(parent) : 0;
for (int i = 0; i < rows; ++i)
{
auto index = model->index(i, 0, parent);
QModelIndex index = model->index(i, 0, parent);
if (model->hasChildren(index) == false)
{
beginInsertRows(parent, row, row);
m_rowMap[index] = row;
m_indexMap[row] = index;
endInsertRows();
Q_EMIT dataChanged(parent, parent);
Q_EMIT dataChanged(index, index);
row = row + 1;
}
@@ -173,12 +142,12 @@ namespace AzToolsFramework
}
void AssetBrowserTableModel::UpdateMap()
{
//Not properly clears the indexes.
//m_indexMap.clear();
//m_rowMap.clear();
emit layoutAboutToBeChanged();
if (m_indexMap.size() > 0)
{
//beginRemoveRows(m_indexMap.first().parent(), m_indexMap.first().row(), m_indexMap.last().row());
for (const auto& key : m_indexMap.keys())
{
beginRemoveRows(m_indexMap[key], m_indexMap[key].row(), m_indexMap[key].row());
@@ -186,160 +155,11 @@ namespace AzToolsFramework
m_indexMap.remove(key);
endRemoveRows();
}
//endRemoveRows();
}
BuildMap(sourceModel());
sort(0);
}
//---------------------------------------AssetBrowserTableFilterModel--------------------------------------------
//AssetBrowserTableFilterModel::AssetBrowserTableFilterModel(QObject* parent)
// : QSortFilterProxyModel(parent)
//{
// m_showColumn.insert(static_cast<int>(AssetBrowserEntry::Column::DisplayName));
// m_showColumn.insert(static_cast<int>(AssetBrowserEntry::Column::Path));
// AssetBrowserComponentNotificationBus::Handler::BusConnect();
//}
//AssetBrowserTableFilterModel::~AssetBrowserTableFilterModel()
//{
// AssetBrowserComponentNotificationBus::Handler::BusDisconnect();
//}
//void AssetBrowserTableFilterModel::setSourceModel(QAbstractItemModel* sourceModel)
//{
// QSortFilterProxyModel::setSourceModel(sourceModel);
//}
//void AssetBrowserTableFilterModel::SetFilter(FilterConstType filter)
//{
// connect(filter.data(), &AssetBrowserEntryFilter::updatedSignal, this, &AssetBrowserTableFilterModel::filterUpdatedSlot);
// m_filter = filter;
// m_invalidateFilter = true;
// // asset browser entries are not guaranteed to have populated when the filter is set, delay filtering until they are
// bool isAssetBrowserComponentReady = false;
// AssetBrowserComponentRequestBus::BroadcastResult(isAssetBrowserComponentReady, &AssetBrowserComponentRequests::AreEntriesReady);
// if (isAssetBrowserComponentReady)
// {
// OnAssetBrowserComponentReady();
// }
//}
//void AssetBrowserTableFilterModel::FilterUpdatedSlotImmediate()
//{
// auto compFilter = qobject_cast<QSharedPointer<const CompositeFilter>>(m_filter);
// if (compFilter)
// {
// auto& subFilters = compFilter->GetSubFilters();
// auto it = AZStd::find_if(subFilters.begin(), subFilters.end(), [subFilters](FilterConstType filter) -> bool {
// auto assetTypeFilter = qobject_cast<QSharedPointer<const CompositeFilter>>(filter);
// return !assetTypeFilter.isNull();
// });
// if (it != subFilters.end())
// {
// m_assetTypeFilter = qobject_cast<QSharedPointer<const CompositeFilter>>(*it);
// }
// it = AZStd::find_if(subFilters.begin(), subFilters.end(), [subFilters](FilterConstType filter) -> bool {
// auto stringFilter = qobject_cast<QSharedPointer<const StringFilter>>(filter);
// return !stringFilter.isNull();
// });
// if (it != subFilters.end())
// {
// m_stringFilter = qobject_cast<QSharedPointer<const StringFilter>>(*it);
// }
// }
// invalidateFilter();
// Q_EMIT filterChanged();
//}
//void AssetBrowserTableFilterModel::OnAssetBrowserComponentReady()
//{
// if (m_invalidateFilter)
// {
// invalidateFilter();
// m_invalidateFilter = false;
// }
// Q_EMIT entriesUpdated();
//}
//bool AssetBrowserTableFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
//{
// AZ_UNUSED(source_row);
// AZ_UNUSED(source_parent);
// QModelIndex idx = sourceModel()->index(source_row, 0, source_parent);
// if (!idx.isValid())
// {
// return false;
// }
// // no filter present, every entry is visible
// if (!m_filter)
// {
// return true;
// }
// //// the entry is the internal pointer of the index
// //auto entry = static_cast<AssetBrowserEntry*>(idx.internalPointer());
// //if (entry)
// //{
// // // root should return true even if its not displayed in the treeview
// // if (entry && entry->GetEntryType() == AssetBrowserEntry::AssetEntryType::Root)
// // {
// // return true;
// // }
// // return m_filter->Match(entry);
// //}
// return true;
//}
//bool AssetBrowserTableFilterModel::filterAcceptsColumn(int source_column, const QModelIndex&) const
//{
// return m_showColumn.find(source_column) != m_showColumn.end();
//}
//bool AssetBrowserTableFilterModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
//{
// if (source_left.column() == source_right.column())
// {
// QVariant leftData = sourceModel()->data(source_left, AssetBrowserModel::Roles::EntryRole);
// QVariant rightData = sourceModel()->data(source_right, AssetBrowserModel::Roles::EntryRole);
// if (leftData.canConvert<const AssetBrowserEntry*>() && rightData.canConvert<const AssetBrowserEntry*>())
// {
// auto leftEntry = qvariant_cast<const AssetBrowserEntry*>(leftData);
// auto rightEntry = qvariant_cast<const AssetBrowserEntry*>(rightData);
// // folders should always come first
// if (azrtti_istypeof<const FolderAssetBrowserEntry*>(leftEntry) &&
// azrtti_istypeof<const SourceAssetBrowserEntry*>(rightEntry))
// {
// return false;
// }
// if (azrtti_istypeof<const SourceAssetBrowserEntry*>(leftEntry) &&
// azrtti_istypeof<const FolderAssetBrowserEntry*>(rightEntry))
// {
// return true;
// }
// // if both entries are of same type, sort alphabetically
// return m_collator.compare(leftEntry->GetDisplayName(), rightEntry->GetDisplayName()) > 0;
// }
// }
// return QSortFilterProxyModel::lessThan(source_left, source_right);
//}
//void AssetBrowserTableFilterModel::filterUpdatedSlot()
//{
// if (!m_alreadyRecomputingFilters)
// {
// m_alreadyRecomputingFilters = true;
// // de-bounce it, since we may get many filter updates all at once.
// QTimer::singleShot(0, this, [this]() {
// m_alreadyRecomputingFilters = false;
// FilterUpdatedSlotImmediate();
// });
// }
//}
} // namespace AssetBrowser
} // namespace AzToolsFramework
#include "AssetBrowser/moc_AssetBrowserTableModel.cpp"
@@ -1,9 +1,6 @@
#pragma once
#if !defined(Q_MOC_RUN)
#include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
#include <AzToolsFramework/AssetBrowser/Entries/AssetBrowserEntry.h>
#include <AzToolsFramework/AssetBrowser/Search/Filter.h>
#include <AzCore/Asset/AssetCommon.h>
#include <AzCore/std/containers/fixed_unordered_set.h>
#include <AzCore/std/containers/vector.h>
@@ -26,21 +23,15 @@ namespace AzToolsFramework
class AssetBrowserTableModel
: public QSortFilterProxyModel
, public AssetBrowserComponentNotificationBus::Handler
{
Q_OBJECT
public:
AZ_CLASS_ALLOCATOR(AssetBrowserTableModel, AZ::SystemAllocator, 0);
explicit AssetBrowserTableModel(QObject* parent = nullptr);
~AssetBrowserTableModel();
////////////////////////////////////////////////////////////////////
// AssetBrowserComponentNotificationBus
////////////////////////////////////////////////////////////////////
void OnAssetBrowserComponentReady() override;
void setSourceModel(QAbstractItemModel* sourceModel) override;
////////////////////////////////////////////////////////////////////
// QSortFilterProxyModel
void setSourceModel(QAbstractItemModel* sourceModel) override;
QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
@@ -52,7 +43,7 @@ namespace AzToolsFramework
void UpdateMap();
protected:
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
//QVariant headerData(int section, Qt::Orientation orientation, int role /* = Qt::DisplayRole */) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role /* = Qt::DisplayRole */) const override;
////////////////////////////////////////////////////////////////////
private:
@@ -63,52 +54,5 @@ namespace AzToolsFramework
QMap<int, QModelIndex> m_indexMap;
QMap<QModelIndex, int> m_rowMap;
};
//class AssetBrowserTableFilterModel
// : public QSortFilterProxyModel
// , public AssetBrowserComponentNotificationBus::Handler
//{
// Q_OBJECT
//public:
// explicit AssetBrowserTableFilterModel(QObject* parent = nullptr);
// ~AssetBrowserTableFilterModel();
// void setSourceModel(QAbstractItemModel* sourceModel) override;
// // asset type filtering
// void SetFilter(FilterConstType filter);
// void FilterUpdatedSlotImmediate();
// //////////////////////////////////////////////////////////////////////////
// // AssetBrowserComponentNotificationBus
// //////////////////////////////////////////////////////////////////////////
// void OnAssetBrowserComponentReady() override;
//Q_SIGNALS:
// void filterChanged();
// void entriesUpdated();
// //////////////////////////////////////////////////////////////////////////
// // QSortFilterProxyModel
//protected:
// bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
// bool filterAcceptsColumn(int source_column, const QModelIndex& /*source_parent*/) const override;
// bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;
// //////////////////////////////////////////////////////////////////////////
//public Q_SLOTS:
// void filterUpdatedSlot();
//private:
// AZStd::fixed_unordered_set<int, 3, static_cast<int>(AssetBrowserEntry::Column::Count)> m_showColumn;
// bool m_alreadyRecomputingFilters = false;
// // asset source name match filter
// FilterConstType m_filter;
// AZ_PUSH_DISABLE_WARNING(4251, "-Wunknown-warning-option") // 4251: class '...' needs to have dll-interface to be used by clients of class '...'
// QWeakPointer<const StringFilter> m_stringFilter;
// QWeakPointer<const CompositeFilter> m_assetTypeFilter;
// QCollator m_collator; // cache the collator as its somewhat expensive to constantly create and destroy one.
// AZ_POP_DISABLE_WARNING
// bool m_invalidateFilter = false;
//};
} // namespace AssetBrowser
} // namespace AzToolsFramework
@@ -68,7 +68,6 @@ AzAssetBrowserWindow::AzAssetBrowserWindow(QWidget* parent)
, m_ui(new Ui::AzAssetBrowserWindowClass())
, m_filterModel(new AzToolsFramework::AssetBrowser::AssetBrowserFilterModel(parent))
, m_tableModel(new AzToolsFramework::AssetBrowser::AssetBrowserTableModel(parent))
/*, m_tableFilterModel(new AzToolsFramework::AssetBrowser::AssetBrowserTableFilterModel(parent))*/
{
m_ui->setupUi(this);
m_ui->m_searchWidget->Setup(true, true);
@@ -81,21 +80,13 @@ AzAssetBrowserWindow::AzAssetBrowserWindow(QWidget* parent)
m_tableModel->setFilterRole(Qt::DisplayRole);
m_tableModel->setSourceModel(m_filterModel.data());
//m_tableModel->setSourceModel(m_assetBrowserModel);
//m_tableFilterModel->setSourceModel(m_tableModel.data());
//m_tableFilterModel->SetFilter(m_ui->m_searchWidget->GetFilter());
m_ui->m_assetBrowserTreeViewWidget->setModel(m_filterModel.data());
m_ui->m_assetBrowserTreeViewWidget->hideColumn(static_cast<int>(AssetBrowserEntry::Column::Path));
//m_ui->m_assetBrowserTableViewWidget->setModel(m_tableFilterModel.data());
m_ui->m_assetBrowserTableViewWidget->setModel(m_tableModel.data());
m_ui->m_assetBrowserTableViewWidget->setVisible(false);
//connect(m_filterModel.data(), &AssetBrowserFilterModel::entriesUpdated, m_tableModel.data(), &AssetBrowserTableModel::UpdateMap);
connect(m_ui->m_searchWidget->GetFilter().data(), &AssetBrowserEntryFilter::updatedSignal,
m_filterModel.data(), &AssetBrowserFilterModel::filterUpdatedSlot);
connect(m_filterModel.data(), &AssetBrowserFilterModel::filterChanged, this, [this]()
@@ -105,20 +96,15 @@ AzAssetBrowserWindow::AzAssetBrowserWindow(QWidget* parent)
m_ui->m_assetBrowserTreeViewWidget->UpdateAfterFilter(hasFilter, selectFirstFilteredIndex);
});
//connect( m_ui->m_searchWidget->GetFilter().data(), &AssetBrowserEntryFilter::updatedSignal, m_tableFilterModel.data(),
// &AssetBrowserTableFilterModel::filterUpdatedSlot);
//connect(m_tableFilterModel.data(), &AssetBrowserTableFilterModel::filterChanged, this, [this]() {
// const bool hasFilter = !m_ui->m_searchWidget->GetFilterString().isEmpty();
// const bool selectFirstFilteredIndex = false;
// m_ui->m_assetBrowserTableViewWidget->UpdateAfterFilter(hasFilter, selectFirstFilteredIndex);
//});
connect(m_filterModel.data(), &AssetBrowserFilterModel::filterChanged, m_tableModel.data(), &AssetBrowserTableModel::UpdateMap);
connect(m_ui->m_assetBrowserTreeViewWidget, &AssetBrowserTreeView::selectionChangedSignal,
this, &AzAssetBrowserWindow::SelectionChangedSlot);
connect(m_ui->m_assetBrowserTreeViewWidget, &QAbstractItemView::doubleClicked, this, &AzAssetBrowserWindow::DoubleClickedItem);
connect(m_ui->m_assetBrowserTableViewWidget, &QAbstractItemView::doubleClicked, this, &AzAssetBrowserWindow::DoubleClickedItem);
connect(m_ui->m_assetBrowserTreeViewWidget, &AssetBrowserTreeView::ClearStringFilter, m_ui->m_searchWidget, &SearchWidget::ClearStringFilter);
connect(m_ui->m_assetBrowserTreeViewWidget, &AssetBrowserTreeView::ClearTypeFilter, m_ui->m_searchWidget, &SearchWidget::ClearTypeFilter);