2022-10-12 23:51:55 +02:00
|
|
|
/*
|
|
|
|
* objectbrowser.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-10-16 12:22:34 +02:00
|
|
|
#include "StdInc.h"
|
2022-09-18 01:23:17 +02:00
|
|
|
#include "objectbrowser.h"
|
|
|
|
#include "../lib/mapObjects/CObjectClassesHandler.h"
|
|
|
|
|
2022-12-04 04:45:39 +02:00
|
|
|
ObjectBrowserProxyModel::ObjectBrowserProxyModel(QObject *parent)
|
2022-10-08 21:54:45 +02:00
|
|
|
: QSortFilterProxyModel{parent}, terrain(Terrain::ANY_TERRAIN)
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:45:39 +02:00
|
|
|
bool ObjectBrowserProxyModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
|
|
|
bool result = QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
|
|
|
|
|
|
|
|
QModelIndex currentIndex = sourceModel()->index(source_row, 0, source_parent);
|
|
|
|
int childCount = currentIndex.model()->rowCount(currentIndex);
|
|
|
|
if(childCount)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto item = dynamic_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(currentIndex);
|
|
|
|
if(!item)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(!filterAcceptsRowText(source_row, source_parent))
|
|
|
|
return false;
|
|
|
|
|
2022-10-08 21:54:45 +02:00
|
|
|
if(terrain == Terrain::ANY_TERRAIN)
|
2022-09-18 01:23:17 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
auto data = item->data().toJsonObject();
|
|
|
|
if(data.empty())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
auto objIdJson = data["id"];
|
|
|
|
if(objIdJson == QJsonValue::Undefined)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
auto objId = data["id"].toInt();
|
|
|
|
auto objSubId = data["subid"].toInt();
|
|
|
|
auto templateId = data["template"].toInt();
|
|
|
|
|
|
|
|
auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
|
|
|
|
auto templ = factory->getTemplates()[templateId];
|
|
|
|
|
2022-11-15 02:20:55 +02:00
|
|
|
result = result && templ->canBePlacedAt(terrain);
|
2022-09-18 01:23:17 +02:00
|
|
|
|
2022-10-12 23:40:52 +02:00
|
|
|
//if we are here, just text filter will be applied
|
2022-09-18 01:23:17 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:45:39 +02:00
|
|
|
bool ObjectBrowserProxyModel::filterAcceptsRowText(int source_row, const QModelIndex &source_parent) const
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
|
|
|
if(source_parent.isValid())
|
|
|
|
{
|
|
|
|
if(filterAcceptsRowText(source_parent.row(), source_parent.parent()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex index = sourceModel()->index(source_row, 0 ,source_parent);
|
|
|
|
if(!index.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto item = dynamic_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(index);
|
|
|
|
if(!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (filter.isNull() || filter.isEmpty() || item->text().contains(filter, Qt::CaseInsensitive));
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:45:39 +02:00
|
|
|
Qt::ItemFlags ObjectBrowserProxyModel::flags(const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
|
|
|
|
|
|
|
|
if (index.isValid())
|
|
|
|
return Qt::ItemIsDragEnabled | defaultFlags;
|
|
|
|
|
|
|
|
return defaultFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ObjectBrowserProxyModel::mimeTypes() const
|
|
|
|
{
|
|
|
|
QStringList types;
|
|
|
|
types << "application/vcmi.object";
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData * ObjectBrowserProxyModel::mimeData(const QModelIndexList & indexes) const
|
|
|
|
{
|
|
|
|
assert(indexes.size() == 1);
|
|
|
|
|
|
|
|
auto * standardModel = qobject_cast<QStandardItemModel*>(sourceModel());
|
|
|
|
assert(standardModel);
|
|
|
|
|
|
|
|
QModelIndex index = indexes.front();
|
|
|
|
QByteArray encodedData;
|
|
|
|
|
|
|
|
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
|
|
|
|
2022-12-04 12:51:01 +02:00
|
|
|
if(!index.isValid())
|
|
|
|
return nullptr;
|
|
|
|
|
2022-12-04 18:48:21 +02:00
|
|
|
auto text = standardModel->itemFromIndex(mapToSource(index))->data();
|
2022-12-04 12:51:01 +02:00
|
|
|
stream << text;
|
2022-12-04 04:45:39 +02:00
|
|
|
|
2022-12-04 12:51:01 +02:00
|
|
|
QMimeData * mimeData = new QMimeData;
|
2022-12-04 04:45:39 +02:00
|
|
|
mimeData->setData("application/vcmi.object", encodedData);
|
|
|
|
return mimeData;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectBrowser::ObjectBrowser(QWidget * parent):
|
|
|
|
QTreeView(parent)
|
|
|
|
{
|
2022-12-04 12:51:01 +02:00
|
|
|
setDropIndicatorShown(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectBrowser::startDrag(Qt::DropActions supportedActions)
|
|
|
|
{
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
auto indexes = selectedIndexes();
|
|
|
|
if(indexes.isEmpty())
|
|
|
|
return;
|
2022-12-04 04:45:39 +02:00
|
|
|
|
2022-12-04 12:51:01 +02:00
|
|
|
QMimeData * mimeData = model()->mimeData(indexes);
|
|
|
|
if(!mimeData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
|
|
|
|
Qt::DropAction dropAction = drag->exec();
|
2022-12-04 04:45:39 +02:00
|
|
|
}
|