| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /*
- * objectselector.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 "objectselector.h"
- #include "ui_objectselector.h"
- #include "../mainwindow.h"
- #include "../mapcontroller.h"
- #include "../../lib/mapping/CMap.h"
- #include "../../lib/rmg/ObjectConfig.h"
- #include "../../lib/GameLibrary.h"
- #include "../../lib/mapObjects/CGObjectInstance.h"
- #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
- #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
- ObjectSelector::ObjectSelector(ObjectConfig & obj) :
- ui(new Ui::ObjectSelector),
- obj(obj),
- advObjects(getAdventureMapItems())
- {
- ui->setupUi(this);
- setWindowTitle(tr("Object Selector"));
-
- setWindowModality(Qt::ApplicationModal);
- fillBannedObjectCategories();
- fillBannedObjects();
- fillCustomObjects();
- show();
- }
- QMainWindow* getMainWindow()
- {
- foreach (QWidget *w, qApp->topLevelWidgets())
- if (QMainWindow* mainWin = qobject_cast<QMainWindow*>(w))
- return mainWin;
- return nullptr;
- }
- std::map<CompoundMapObjectID, QString> ObjectSelector::getAdventureMapItems()
- {
- std::map<CompoundMapObjectID, QString> objects;
- auto& controller = qobject_cast<MainWindow *>(getMainWindow())->controller;
- auto knownObjects = LIBRARY->objtypeh->knownObjects();
- for(auto & id : knownObjects)
- {
- auto knownSubObjects = LIBRARY->objtypeh->knownSubObjects(id);
- for(auto & subId : knownSubObjects)
- {
- auto objId = CompoundMapObjectID(id, subId);
- auto factory = LIBRARY->objtypeh->getHandlerFor(id, subId);
- auto templates = factory->getTemplates();
-
- QString name{};
- try
- {
- auto templ = templates.at(0);
- auto temporaryObj(factory->create(controller.getCallback(), templ));
- QString translated = QString::fromStdString(temporaryObj->getObjectName().c_str());
- name = translated;
- }
- catch(...) {}
- if(name.isEmpty())
- {
- auto subGroupName = QString::fromStdString(LIBRARY->objtypeh->getObjectName(id, subId));
- name = subGroupName;
- }
- if(!name.isEmpty())
- objects[objId] = name;
- }
- }
- return objects;
- }
- void ObjectSelector::fillBannedObjectCategories()
- {
- ui->tableWidgetBannedObjectCategories->setColumnCount(2);
- ui->tableWidgetBannedObjectCategories->setRowCount(obj.bannedObjectCategories.size() + 1);
- ui->tableWidgetBannedObjectCategories->setHorizontalHeaderLabels({tr("Category"), tr("Action")});
- auto addRow = [this](ObjectConfig::EObjectCategory category, int row){
- std::map<ObjectConfig::EObjectCategory, QString> values = {
- { ObjectConfig::EObjectCategory::OTHER, tr("Other") },
- { ObjectConfig::EObjectCategory::ALL, tr("All") },
- { ObjectConfig::EObjectCategory::NONE, tr("None") },
- { ObjectConfig::EObjectCategory::CREATURE_BANK, tr("Creature bank") },
- { ObjectConfig::EObjectCategory::BONUS, tr("Bonus") },
- { ObjectConfig::EObjectCategory::DWELLING, tr("Dwelling") },
- { ObjectConfig::EObjectCategory::RESOURCE, tr("Resource") },
- { ObjectConfig::EObjectCategory::RESOURCE_GENERATOR, tr("Resource generator") },
- { ObjectConfig::EObjectCategory::SPELL_SCROLL, tr("Spell scroll") },
- { ObjectConfig::EObjectCategory::RANDOM_ARTIFACT, tr("Random artifact") },
- { ObjectConfig::EObjectCategory::PANDORAS_BOX, tr("Pandoras box") },
- { ObjectConfig::EObjectCategory::QUEST_ARTIFACT, tr("Quest artifact") },
- { ObjectConfig::EObjectCategory::SEER_HUT, tr("Seer hut") }
- };
- QComboBox *combo = new QComboBox();
- for(auto & item : values)
- combo->addItem(item.second, QVariant(static_cast<int>(item.first)));
- int index = combo->findData(static_cast<int>(category));
- if (index != -1)
- combo->setCurrentIndex(index);
- ui->tableWidgetBannedObjectCategories->setCellWidget(row, 0, combo);
- auto deleteButton = new QPushButton("Delete");
- ui->tableWidgetBannedObjectCategories->setCellWidget(row, 1, deleteButton);
- connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
- for (int r = 0; r < ui->tableWidgetBannedObjectCategories->rowCount(); ++r) {
- if (ui->tableWidgetBannedObjectCategories->cellWidget(r, 1) == deleteButton) {
- ui->tableWidgetBannedObjectCategories->removeRow(r);
- break;
- }
- }
- });
- };
- for (int row = 0; row < obj.bannedObjectCategories.size(); ++row)
- addRow(obj.bannedObjectCategories[row], row);
- auto addButton = new QPushButton("Add");
- ui->tableWidgetBannedObjectCategories->setCellWidget(ui->tableWidgetBannedObjectCategories->rowCount() - 1, 1, addButton);
- connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
- ui->tableWidgetBannedObjectCategories->insertRow(ui->tableWidgetBannedObjectCategories->rowCount() - 1);
- addRow(ObjectConfig::EObjectCategory::ALL, ui->tableWidgetBannedObjectCategories->rowCount() - 2);
- });
- ui->tableWidgetBannedObjectCategories->resizeColumnsToContents();
- ui->tableWidgetBannedObjectCategories->setColumnWidth(0, 300);
- }
- void ObjectSelector::getBannedObjectCategories()
- {
- obj.bannedObjectCategories.clear();
- for (int row = 0; row < ui->tableWidgetBannedObjectCategories->rowCount() - 1; ++row)
- {
- auto val = static_cast<ObjectConfig::EObjectCategory>(static_cast<QComboBox *>(ui->tableWidgetBannedObjectCategories->cellWidget(row, 0))->currentData().toInt());
- if(std::find(obj.bannedObjectCategories.begin(), obj.bannedObjectCategories.end(), val) == obj.bannedObjectCategories.end())
- obj.bannedObjectCategories.push_back(val);
- }
- }
- void ObjectSelector::fillBannedObjects()
- {
- ui->tableWidgetBannedObjects->setColumnCount(2);
- ui->tableWidgetBannedObjects->setRowCount(obj.bannedObjects.size() + 1);
- ui->tableWidgetBannedObjects->setHorizontalHeaderLabels({tr("Object"), tr("Action")});
- auto addRow = [this](CompoundMapObjectID obj, int row){
- QComboBox *combo = new QComboBox();
- for(auto & item : advObjects)
- combo->addItem(item.second, QVariant::fromValue(item.first));
- int index = combo->findData(QVariant::fromValue(obj));
- if (index != -1)
- combo->setCurrentIndex(index);
-
- combo->setEditable(true);
- QCompleter* completer = new QCompleter(combo);
- completer->setModel(combo->model());
- completer->setCompletionMode(QCompleter::PopupCompletion);
- completer->setFilterMode(Qt::MatchContains);
- combo->setCompleter(completer);
- ui->tableWidgetBannedObjects->setCellWidget(row, 0, combo);
- auto deleteButton = new QPushButton("Delete");
- ui->tableWidgetBannedObjects->setCellWidget(row, 1, deleteButton);
- connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
- for (int r = 0; r < ui->tableWidgetBannedObjects->rowCount(); ++r) {
- if (ui->tableWidgetBannedObjects->cellWidget(r, 1) == deleteButton) {
- ui->tableWidgetBannedObjects->removeRow(r);
- break;
- }
- }
- });
- };
- for (int row = 0; row < obj.bannedObjects.size(); ++row)
- addRow(obj.bannedObjects[row], row);
- auto addButton = new QPushButton("Add");
- ui->tableWidgetBannedObjects->setCellWidget(ui->tableWidgetBannedObjects->rowCount() - 1, 1, addButton);
- connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
- ui->tableWidgetBannedObjects->insertRow(ui->tableWidgetBannedObjects->rowCount() - 1);
- addRow((*advObjects.begin()).first, ui->tableWidgetBannedObjects->rowCount() - 2);
- });
- ui->tableWidgetBannedObjects->resizeColumnsToContents();
- ui->tableWidgetBannedObjects->setColumnWidth(0, 300);
- }
- void ObjectSelector::getBannedObjects()
- {
- obj.bannedObjects.clear();
- for (int row = 0; row < ui->tableWidgetBannedObjects->rowCount() - 1; ++row)
- {
- auto val = static_cast<QComboBox *>(ui->tableWidgetBannedObjects->cellWidget(row, 0))->currentData().value<CompoundMapObjectID>();
- obj.bannedObjects.push_back(val);
- }
- }
- void ObjectSelector::fillCustomObjects()
- {
- }
- void ObjectSelector::getCustomObjects()
- {
- }
- void ObjectSelector::showObjectSelector(ObjectConfig & obj)
- {
- auto * dialog = new ObjectSelector(obj);
- dialog->setAttribute(Qt::WA_DeleteOnClose);
- dialog->exec();
- }
- void ObjectSelector::on_buttonBoxResult_accepted()
- {
- getBannedObjectCategories();
- getBannedObjects();
- getCustomObjects();
- close();
- }
- void ObjectSelector::on_buttonBoxResult_rejected()
- {
- close();
- }
|