ResultFilterProxyModel.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "ResultFilterProxyModel.hpp"
  2. #include "ResultListModel.hpp"
  3. #include <QString>
  4. ResultFilterProxyModel::ResultFilterProxyModel(QObject *parent)
  5. : QSortFilterProxyModel(parent) {}
  6. void ResultFilterProxyModel::setSearchText(const QString &searchText) {
  7. if (m_searchText != searchText) {
  8. m_searchText = searchText;
  9. updateVisibleGroups();
  10. invalidateFilter();
  11. }
  12. }
  13. void ResultFilterProxyModel::updateVisibleGroups() {
  14. m_visibleGroupIds.clear();
  15. if (m_searchText.isEmpty()) {
  16. return; // Fast path: all visible when no search text
  17. }
  18. // Determine which groups have at least one image matching the search text
  19. ResultListModel *source = qobject_cast<ResultListModel *>(sourceModel());
  20. if (!source)
  21. return;
  22. int rowCount = source->rowCount(QModelIndex());
  23. for (int row = 0; row < rowCount; ++row) {
  24. const auto &item = source->getItem(row);
  25. if (item.type == ResultListItem::ImageRow) {
  26. if (m_visibleGroupIds.find(item.groupId) != m_visibleGroupIds.end()) {
  27. continue; // Group already marked as visible
  28. }
  29. for (const auto &img : item.images) {
  30. QString pathStr = QString::fromStdString(img.path);
  31. if (pathStr.contains(m_searchText, Qt::CaseInsensitive)) {
  32. m_visibleGroupIds.insert(item.groupId);
  33. break; // One match is enough for the entire group
  34. }
  35. }
  36. }
  37. }
  38. }
  39. bool ResultFilterProxyModel::filterAcceptsRow(
  40. int source_row, const QModelIndex &source_parent) const {
  41. if (m_searchText.isEmpty()) {
  42. return true; // Show all if no filter
  43. }
  44. ResultListModel *source = qobject_cast<ResultListModel *>(sourceModel());
  45. if (!source)
  46. return true;
  47. const auto &item = source->getItem(source_row);
  48. return m_visibleGroupIds.find(item.groupId) != m_visibleGroupIds.end();
  49. }