objectbrowser.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * objectbrowser.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "objectbrowser.h"
  12. #include "../lib/mapObjects/CObjectClassesHandler.h"
  13. ObjectBrowser::ObjectBrowser(QObject *parent)
  14. : QSortFilterProxyModel{parent}, terrain(Terrain::ANY_TERRAIN)
  15. {
  16. }
  17. bool ObjectBrowser::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
  18. {
  19. bool result = QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
  20. QModelIndex currentIndex = sourceModel()->index(source_row, 0, source_parent);
  21. int childCount = currentIndex.model()->rowCount(currentIndex);
  22. if(childCount)
  23. return false;
  24. auto item = dynamic_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(currentIndex);
  25. if(!item)
  26. return result;
  27. if(!filterAcceptsRowText(source_row, source_parent))
  28. return false;
  29. if(terrain == Terrain::ANY_TERRAIN)
  30. return result;
  31. auto data = item->data().toJsonObject();
  32. if(data.empty())
  33. return result;
  34. auto objIdJson = data["id"];
  35. if(objIdJson == QJsonValue::Undefined)
  36. return result;
  37. auto objId = data["id"].toInt();
  38. auto objSubId = data["subid"].toInt();
  39. auto templateId = data["template"].toInt();
  40. auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
  41. auto templ = factory->getTemplates()[templateId];
  42. result = result && templ->canBePlacedAt(terrain);
  43. //if we are here, just text filter will be applied
  44. return result;
  45. }
  46. bool ObjectBrowser::filterAcceptsRowText(int source_row, const QModelIndex &source_parent) const
  47. {
  48. if(source_parent.isValid())
  49. {
  50. if(filterAcceptsRowText(source_parent.row(), source_parent.parent()))
  51. return true;
  52. }
  53. QModelIndex index = sourceModel()->index(source_row, 0 ,source_parent);
  54. if(!index.isValid())
  55. return false;
  56. auto item = dynamic_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(index);
  57. if(!item)
  58. return false;
  59. return (filter.isNull() || filter.isEmpty() || item->text().contains(filter, Qt::CaseInsensitive));
  60. }