Files
o3de/Code/Framework/AzToolsFramework/AzToolsFramework/AssetBrowser/AssetBrowserTableModel.cpp
T
igarri b0ddd93824 Code cleanup and review changes
Signed-off-by: igarri <igarri@amazon.com>
2021-07-02 11:35:49 +01:00

160 lines
5.6 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AssetBrowser/AssetBrowserFilterModel.h>
#include <AssetBrowser/AssetBrowserTableModel.h>
#include <AzToolsFramework/AssetBrowser/Entries/AssetBrowserEntry.h>
namespace AzToolsFramework
{
namespace AssetBrowser
{
AssetBrowserTableModel::AssetBrowserTableModel(QObject* parent /* = nullptr */)
: QSortFilterProxyModel(parent)
, m_numberOfItemsDisplayed(200)
{
setDynamicSortFilter(false);
}
void AssetBrowserTableModel::setSourceModel(QAbstractItemModel* sourceModel)
{
m_filterModel = qobject_cast<AssetBrowserFilterModel*>(sourceModel);
AZ_Assert(
m_filterModel,
"Error in AssetBrowserTableModel initialization, class expects source model to be an AssetBrowserFilterModel.");
QSortFilterProxyModel::setSourceModel(sourceModel);
}
QModelIndex AssetBrowserTableModel::mapToSource(const QModelIndex& proxyIndex) const
{
Q_ASSERT(!proxyIndex.isValid() || proxyIndex.model() == this);
if (!proxyIndex.isValid())
{
return QModelIndex();
}
return m_indexMap[proxyIndex.row()];
}
QVariant AssetBrowserTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
{
return tr(AssetBrowserEntry::m_columnNames[section]);
}
return QSortFilterProxyModel::headerData(section, orientation, role);
}
QVariant AssetBrowserTableModel::data(const QModelIndex& index, int role) const
{
auto sourceIndex = mapToSource(index);
if (!sourceIndex.isValid())
{
return QVariant();
}
AssetBrowserEntry* entry = GetAssetEntry(sourceIndex);
if (entry == nullptr)
{
AZ_Assert(false, "AssetBrowserTableModel - QModelIndex does not reference an AssetEntry. Source model is not valid.");
return QVariant();
}
return sourceIndex.data(role);
}
QModelIndex AssetBrowserTableModel::parent([[maybe_unused]] const QModelIndex& child) const
{
return QModelIndex();
}
QModelIndex AssetBrowserTableModel::sibling(
[[maybe_unused]] int row, [[maybe_unused]] int column, [[maybe_unused]] const QModelIndex& idx) const
{
return QModelIndex();
}
QModelIndex AssetBrowserTableModel::index(int row, int column, const QModelIndex& parent) const
{
return parent.isValid() ? QModelIndex() : createIndex(row, column, m_indexMap[row].internalPointer());
}
int AssetBrowserTableModel::rowCount(const QModelIndex& parent) const
{
return !parent.isValid() ? m_indexMap.size() : 0;
}
int AssetBrowserTableModel::BuildTableModelMap(
const QAbstractItemModel* model, const QModelIndex& parent /*= QModelIndex()*/, int row /*= 0*/)
{
static int displayedItemsCounter = 0;
int rows = model ? model->rowCount(parent) : 0;
if (parent == QModelIndex())
{
displayedItemsCounter = 0;
}
for (int i = 0; i < rows; ++i)
{
if (displayedItemsCounter < m_numberOfItemsDisplayed)
{
QModelIndex index = model->index(i, 0, parent);
AssetBrowserEntry* entry = GetAssetEntry(m_filterModel->mapToSource(index));
// We only wanna see the source assets.
if (entry->GetEntryType() == AssetBrowserEntry::AssetEntryType::Source)
{
beginInsertRows(parent, row, row);
m_indexMap[row] = index;
endInsertRows();
Q_EMIT dataChanged(index, index);
++row;
++displayedItemsCounter;
}
if (model->hasChildren(index))
{
row = BuildTableModelMap(model, index, row);
}
}
break;
}
return row;
}
AssetBrowserEntry* AssetBrowserTableModel::GetAssetEntry(QModelIndex index) const
{
if (index.isValid())
{
return static_cast<AssetBrowserEntry*>(index.internalPointer());
}
else
{
AZ_Error("AssetBrowser", false, "Invalid Source Index provided to GetAssetEntry.");
return nullptr;
}
}
void AssetBrowserTableModel::UpdateTableModelMaps()
{
emit layoutAboutToBeChanged();
if (!m_indexMap.isEmpty())
{
beginRemoveRows(m_indexMap.first(), m_indexMap.first().row(), m_indexMap.last().row());
m_indexMap.clear();
endRemoveRows();
}
AzToolsFramework::EditorSettingsAPIBus::BroadcastResult(
m_numberOfItemsDisplayed, &AzToolsFramework::EditorSettingsAPIBus::Handler::GetMaxNumberOfItemsShownInSearchView);
BuildTableModelMap(sourceModel());
emit layoutChanged();
}
} // namespace AssetBrowser
} // namespace AzToolsFramework
#include "AssetBrowser/moc_AssetBrowserTableModel.cpp"