objectbrowser.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "objectbrowser.h"
  11. #include "../lib/mapObjects/CObjectClassesHandler.h"
  12. ObjectBrowser::ObjectBrowser(QObject *parent)
  13. : QSortFilterProxyModel{parent}, terrain(Terrain::ANY_TERRAIN)
  14. {
  15. }
  16. bool ObjectBrowser::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
  17. {
  18. bool result = QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
  19. QModelIndex currentIndex = sourceModel()->index(source_row, 0, source_parent);
  20. int childCount = currentIndex.model()->rowCount(currentIndex);
  21. if(childCount)
  22. return false;
  23. auto item = dynamic_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(currentIndex);
  24. if(!item)
  25. return result;
  26. if(!filterAcceptsRowText(source_row, source_parent))
  27. return false;
  28. if(terrain == Terrain::ANY_TERRAIN)
  29. return result;
  30. auto data = item->data().toJsonObject();
  31. if(data.empty())
  32. return result;
  33. auto objIdJson = data["id"];
  34. if(objIdJson == QJsonValue::Undefined)
  35. return result;
  36. auto objId = data["id"].toInt();
  37. auto objSubId = data["subid"].toInt();
  38. auto templateId = data["template"].toInt();
  39. auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
  40. auto templ = factory->getTemplates()[templateId];
  41. result = result & templ->canBePlacedAt(terrain);
  42. //if we are here, just text filter will be applied
  43. return result;
  44. }
  45. bool ObjectBrowser::filterAcceptsRowText(int source_row, const QModelIndex &source_parent) const
  46. {
  47. if(source_parent.isValid())
  48. {
  49. if(filterAcceptsRowText(source_parent.row(), source_parent.parent()))
  50. return true;
  51. }
  52. QModelIndex index = sourceModel()->index(source_row, 0 ,source_parent);
  53. if(!index.isValid())
  54. return false;
  55. auto item = dynamic_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(index);
  56. if(!item)
  57. return false;
  58. return (filter.isNull() || filter.isEmpty() || item->text().contains(filter, Qt::CaseInsensitive));
  59. }