1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00
vcmi/mapeditor/objectbrowser.cpp
Alexander Wilms 1b85abb508 Use auto instead of redundant type in initializations using new
grep -r --include \*.h --include \*.cpp "=" * | grep -v "auto\|int\|char\|bool\|float|\double\|for\|if\|googletest\|fuzzylite\|size_t\|using\|return\|{\|}\|= \"\|= tr(\|virtual\|void" | grep -Po ".*[^ ]+ [^ ]+ [^ ]*[ ]*=.*;" | grep -v "float\|nullptr" | grep "new" | grep -v "AI/FuzzyLite" | grep \( | grep "= new" > redundant_types.txt

import re

with open("redundant_types.txt") as f:
    for line in f:
        line = line.strip()
        path = line.split(":", 1)[0]
        original_code = line.split(":")[1].strip()
        if "new " in original_code:

            cpp_type = original_code.split(" ")[0]
            if original_code.count(cpp_type) == 2:
                print()
                print(path)
                print(original_code)
                new_code = "auto "+" ".join(original_code.split(" ")[1:])
                print(new_code)

                with open(path, "r") as f:
                    filedata = f.read()

                filedata = filedata.replace(original_code, new_code)

                with open(path, "w") as f:
                    f.write(filedata)
2024-01-16 21:40:53 +00:00

135 lines
3.5 KiB
C++

/*
* 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
*
*/
#include "StdInc.h"
#include "objectbrowser.h"
#include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
#include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
#include "../lib/mapObjects/ObjectTemplate.h"
#include "../lib/VCMI_Lib.h"
ObjectBrowserProxyModel::ObjectBrowserProxyModel(QObject *parent)
: QSortFilterProxyModel{parent}, terrain(ETerrainId::ANY_TERRAIN)
{
}
bool ObjectBrowserProxyModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
{
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;
if(terrain == ETerrainId::ANY_TERRAIN)
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];
result = result && templ->canBePlacedAt(terrain);
//if we are here, just text filter will be applied
return result;
}
bool ObjectBrowserProxyModel::filterAcceptsRowText(int source_row, const QModelIndex &source_parent) const
{
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;
auto data = item->data().toJsonObject();
return (filter.isNull() || filter.isEmpty()
|| item->text().contains(filter, Qt::CaseInsensitive)
|| data["typeName"].toString().contains(filter, Qt::CaseInsensitive));
}
Qt::ItemFlags ObjectBrowserProxyModel::flags(const QModelIndex & index) const
{
Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index);
if (index.isValid())
return Qt::ItemIsDragEnabled | defaultFlags;
return defaultFlags;
}
QMimeData * ObjectBrowserProxyModel::mimeData(const QModelIndexList & indexes) const
{
assert(indexes.size() == 1);
auto * standardModel = qobject_cast<QStandardItemModel*>(sourceModel());
assert(standardModel);
QModelIndex index = indexes.front();
if(!index.isValid())
return nullptr;
QMimeData * mimeData = new QMimeData;
mimeData->setImageData(standardModel->itemFromIndex(mapToSource(index))->data());
return mimeData;
}
ObjectBrowser::ObjectBrowser(QWidget * parent):
QTreeView(parent)
{
setDropIndicatorShown(false);
}
void ObjectBrowser::startDrag(Qt::DropActions supportedActions)
{
logGlobal->info("Drag'n'drop: Start dragging object from ObjectBrowser");
auto indexes = selectedIndexes();
if(indexes.isEmpty())
return;
QMimeData * mimeData = model()->mimeData(indexes);
if(!mimeData)
return;
auto *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->exec(supportedActions);
}