diff --git a/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.cpp b/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.cpp index fb2f816bf1a..a7b2c6df357 100644 --- a/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.cpp +++ b/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.cpp @@ -1,7 +1,9 @@ #include "ShipAltShipClassModel.h" #include "ship/ship.h" + namespace fso::fred::dialogs { + ShipAltShipClassModel::ShipAltShipClassModel(QObject* parent, EditorViewport* viewport) : AbstractDialogModel(parent, viewport) { @@ -11,7 +13,7 @@ ShipAltShipClassModel::ShipAltShipClassModel(QObject* parent, EditorViewport* vi bool ShipAltShipClassModel::apply() { // TODO: Add extra validation here - for (auto& pool_class : alt_class_pool) { + for (auto& pool_class : _altClassPool) { if (pool_class.ship_class == -1 && pool_class.variable_index == -1) { _viewport->dialogProvider->showButtonDialog(DialogType::Warning, "Warning", @@ -20,48 +22,48 @@ bool ShipAltShipClassModel::apply() return false; } } - for (int i = 0; i < _num_selected_ships; i++) { - Ships[_m_selected_ships[i]].s_alt_classes = alt_class_pool; + for (int i = 0; i < _numSelectedShips; i++) { + Ships[_selectedShips[i]].s_alt_classes = _altClassPool; } return true; } void ShipAltShipClassModel::reject() {} -SCP_vector ShipAltShipClassModel::get_pool() const +SCP_vector ShipAltShipClassModel::getPool() const { - return alt_class_pool; + return _altClassPool; } -SCP_vector> ShipAltShipClassModel::get_classes() +SCP_vector> ShipAltShipClassModel::getClasses() { // Fill the ship classes combo box - SCP_vector> _m_set_from_ship_class; + SCP_vector> classPool; std::pair classData; // Add the default entry if we need one followed by all the ship classes - classData.first = "Set From Variable"; - classData.second = -1; - _m_set_from_ship_class.push_back(classData); + classData.first = "Set From Variable"; + classData.second = -1; + classPool.push_back(classData); for (auto it = Ship_info.cbegin(); it != Ship_info.cend(); ++it) { - if (!(it->flags[Ship::Info_Flags::Player_ship])) { + if (!(it->flags[Ship::Info_Flags::Player_ship])) { continue; } classData.first = it->name; classData.second = std::distance(Ship_info.cbegin(), it); - _m_set_from_ship_class.push_back(classData); + classPool.push_back(classData); } - return _m_set_from_ship_class; + return classPool; } -SCP_vector> ShipAltShipClassModel::get_variables() +SCP_vector> ShipAltShipClassModel::getVariables() { // Fill the variable combo box - SCP_vector> _m_set_from_variables; + SCP_vector> variablePool; std::pair variableData; variableData.first = "Set From Ship Class"; variableData.second = -1; - _m_set_from_variables.push_back(variableData); + variablePool.push_back(variableData); for (int i = 0; i < MAX_SEXP_VARIABLES; i++) { if (Sexp_variables[i].type & SEXP_VARIABLE_STRING) { std::ostringstream oss; @@ -70,46 +72,46 @@ SCP_vector> ShipAltShipClassModel::get_variables() buff = oss.str(); variableData.first = buff; variableData.second = i; - _m_set_from_variables.push_back(variableData); - //_string_variables.push_back(variable); - // _string_variables[0].get().type = 1234; + variablePool.push_back(variableData); } } - return _m_set_from_variables; + return variablePool; } -void ShipAltShipClassModel::sync_data(const SCP_vector& new_pool) { - if (new_pool == alt_class_pool) { + +void ShipAltShipClassModel::syncData(const SCP_vector& newPool) +{ + if (newPool == _altClassPool) { return; } else { - alt_class_pool = new_pool; + _altClassPool = newPool; set_modified(); } } + void ShipAltShipClassModel::initializeData() { - _num_selected_ships = 0; - _m_selected_ships.clear(); + _numSelectedShips = 0; + _selectedShips.clear(); // have we got multiple selected ships? object* objp = GET_FIRST(&obj_used_list); while (objp != END_OF_LIST(&obj_used_list)) { if ((objp->type == OBJ_START) || (objp->type == OBJ_SHIP)) { if (objp->flags[Object::Object_Flags::Marked]) { - _m_selected_ships.push_back(objp->instance); - _num_selected_ships++; + _selectedShips.push_back(objp->instance); + _numSelectedShips++; } } objp = GET_NEXT(objp); } - Assertion(_num_selected_ships > 0, "No Ships Selected"); - // Assert(Objects[cur_object_index].flags[Object::Object_Flags::Marked]); + Assertion(_numSelectedShips > 0, "No Ships Selected"); - alt_class_pool.clear(); + _altClassPool.clear(); objp = GET_FIRST(&obj_used_list); while (objp != END_OF_LIST(&obj_used_list)) { if ((objp->type == OBJ_START) || (objp->type == OBJ_SHIP)) { if (objp->flags[Object::Object_Flags::Marked]) { - alt_class_pool = Ships[objp->instance].s_alt_classes; + _altClassPool = Ships[objp->instance].s_alt_classes; break; } } @@ -118,4 +120,4 @@ void ShipAltShipClassModel::initializeData() _modified = false; } -} // namespace fso::fred::dialogs \ No newline at end of file +} // namespace fso::fred::dialogs diff --git a/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.h b/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.h index b9ac7f1c4fb..7831dc58ecd 100644 --- a/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.h +++ b/qtfred/src/mission/dialogs/ShipEditor/ShipAltShipClassModel.h @@ -1,38 +1,29 @@ #pragma once -#include "../AbstractDialogModel.h" -namespace fso::fred::dialogs { -/** - * @brief Model for QtFRED's Alt Ship Class dialog - */ -class ShipAltShipClassModel : public AbstractDialogModel { - private: - /** - * @brief Initialises data for the model - */ - void initializeData(); - SCP_vector alt_class_pool; - - int _num_selected_ships = 0; - - SCP_vector _m_selected_ships; +#include "../AbstractDialogModel.h" - SCP_vector _m_alt_class_list; +namespace fso::fred::dialogs { +class ShipAltShipClassModel : public AbstractDialogModel { + Q_OBJECT public: - /** - * @brief Constructor - * @param [in] parent The parent dialog. - * @param [in] viewport The viewport this dialog is attacted to. - */ ShipAltShipClassModel(QObject* parent, EditorViewport* viewport); bool apply() override; void reject() override; - SCP_vector get_pool() const; + SCP_vector getPool() const; + + static SCP_vector> getClasses(); + static SCP_vector> getVariables(); + void syncData(const SCP_vector& newPool); - static SCP_vector> get_classes(); - static SCP_vector> get_variables(); - void sync_data(const SCP_vector&); + private: // NOLINT(readability-redundant-access-specifiers) + void initializeData(); + + SCP_vector _altClassPool; + int _numSelectedShips = 0; + SCP_vector _selectedShips; + SCP_vector _altClassList; }; -} // namespace fso::fred::dialogs \ No newline at end of file + +} // namespace fso::fred::dialogs diff --git a/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.cpp b/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.cpp index 59d886dd8d6..2e3bfc60547 100644 --- a/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.cpp +++ b/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.cpp @@ -9,6 +9,7 @@ #include namespace fso::fred::dialogs { + ShipAltShipClass::ShipAltShipClass(QDialog* parent, EditorViewport* viewport) : QDialog(parent), ui(new Ui::ShipAltShipClass()), _model(new ShipAltShipClassModel(this, viewport)), _viewport(viewport) @@ -21,8 +22,9 @@ ShipAltShipClass::ShipAltShipClass(QDialog* parent, EditorViewport* viewport) ShipAltShipClass::~ShipAltShipClass() = default; void ShipAltShipClass::accept() -{ // If apply() returns true, close the dialog - sync_data(); +{ + // If apply() returns true, close the dialog + syncData(); if (_model->apply()) { QDialog::accept(); } @@ -30,10 +32,11 @@ void ShipAltShipClass::accept() } void ShipAltShipClass::reject() -{ // Asks the user if they want to save changes, if any +{ + // Asks the user if they want to save changes, if any // If they do, it runs _model->apply() and returns the success value // If they don't, it runs _model->reject() and returns true - sync_data(); + syncData(); if (rejectOrCloseHandler(this, _model.get(), _viewport)) { QDialog::reject(); // actually close } @@ -50,6 +53,7 @@ void ShipAltShipClass::on_buttonBox_accepted() { accept(); } + void ShipAltShipClass::on_buttonBox_rejected() { reject(); @@ -57,7 +61,7 @@ void ShipAltShipClass::on_buttonBox_rejected() void ShipAltShipClass::on_addButton_clicked() { - auto item = generate_item(ui->shipCombo->currentData(Qt::UserRole).toInt(), + auto item = generateItem(ui->shipCombo->currentData(Qt::UserRole).toInt(), ui->variableCombo->currentData(Qt::UserRole).toInt(), ui->defaultCheckbox->isChecked()); if (item != nullptr) { @@ -69,7 +73,7 @@ void ShipAltShipClass::on_insertButton_clicked() { auto current = ui->classList->currentIndex(); if (current.isValid()) { - auto item = generate_item(ui->shipCombo->currentData(Qt::UserRole).toInt(), + auto item = generateItem(ui->shipCombo->currentData(Qt::UserRole).toInt(), ui->variableCombo->currentData(Qt::UserRole).toInt(), ui->defaultCheckbox->isChecked()); if (item != nullptr) { @@ -129,7 +133,7 @@ void ShipAltShipClass::on_shipCombo_currentIndexChanged(int index) on_variableCombo_currentIndexChanged(1); } } else { - QString classname = generate_name(ui->shipCombo->itemData(index, Qt::UserRole).toInt(), -1); + QString classname = generateName(ui->shipCombo->itemData(index, Qt::UserRole).toInt(), -1); ui->classList->model()->setData(current, classname, Qt::DisplayRole); ui->classList->model()->setData(current, ui->shipCombo->itemData(index, Qt::UserRole), Qt::UserRole + 1); ui->classList->model()->setData(current, -1, Qt::UserRole + 2); @@ -152,7 +156,7 @@ void ShipAltShipClass::on_variableCombo_currentIndexChanged(int index) } else { int ship_class = ship_info_lookup(Sexp_variables[ui->variableCombo->itemData(index, Qt::UserRole).toInt()].text); - QString classname = generate_name(ship_class, ui->variableCombo->itemData(index, Qt::UserRole).toInt()); + QString classname = generateName(ship_class, ui->variableCombo->itemData(index, Qt::UserRole).toInt()); ui->classList->model()->setData(current, classname, Qt::DisplayRole); ui->classList->model()->setData(current, ship_class, Qt::UserRole + 1); ui->classList->model()->setData(current, @@ -171,34 +175,35 @@ void ShipAltShipClass::on_defaultCheckbox_toggled(bool toggled) ui->classList->model()->setData(current, toggled, Qt::UserRole); } } + void ShipAltShipClass::initUI() { - alt_pool = new QStandardItemModel(); - for (auto& alt_class : _model->get_pool()) { - auto item = generate_item(alt_class.ship_class, alt_class.variable_index, alt_class.default_to_this_class); + _altPool = new QStandardItemModel(); + for (auto& alt_class : _model->getPool()) { + auto item = generateItem(alt_class.ship_class, alt_class.variable_index, alt_class.default_to_this_class); if (item != nullptr) { - alt_pool->appendRow(item); + _altPool->appendRow(item); } } - ui->classList->setModel(alt_pool); + ui->classList->setModel(_altPool); connect(ui->classList->selectionModel(), &QItemSelectionModel::currentChanged, this, &ShipAltShipClass::classListChanged); auto ship_pool = new QStandardItemModel(); - for (auto& ship : _model->get_classes()) { - QString classname = ship.first.c_str(); + for (auto& shipClass : _model->getClasses()) { + QString classname = shipClass.first.c_str(); auto item = new QStandardItem(classname); - item->setData(ship.second, Qt::UserRole); + item->setData(shipClass.second, Qt::UserRole); ship_pool->appendRow(item); } auto shipproxyModel = new InverseSortFilterProxyModel(this); shipproxyModel->setSourceModel(ship_pool); ui->shipCombo->setModel(shipproxyModel); auto variable_pool = new QStandardItemModel(); - for (auto& variable : _model->get_variables()) { + for (auto& variable : _model->getVariables()) { QString classname = variable.first.c_str(); auto item = new QStandardItem(classname); item->setData(variable.second, Qt::UserRole); @@ -214,10 +219,10 @@ void ShipAltShipClass::initUI() ui->downButton->setText(QString()); ui->downButton->setToolTip(tr("Move selected class down")); - updateUI(); + updateUi(); } -void ShipAltShipClass::updateUI() +void ShipAltShipClass::updateUi() { util::SignalBlockers blockers(this); // block signals while we set up the UI auto current = ui->classList->currentIndex(); @@ -279,17 +284,19 @@ void ShipAltShipClass::updateUI() } ui->defaultCheckbox->setChecked(default_ship); } + void ShipAltShipClass::classListChanged(const QModelIndex& current) { SCP_UNUSED(current); - updateUI(); + updateUi(); } -QStandardItem* ShipAltShipClass::generate_item(const int classid, const int variable, const bool default_ship) + +QStandardItem* ShipAltShipClass::generateItem(int classid, int variable, bool defaultShip) { - QString classname = generate_name(classid, variable); + QString classname = generateName(classid, variable); if (!classname.isEmpty()) { auto item = new QStandardItem(classname); - item->setData(default_ship, Qt::UserRole); + item->setData(defaultShip, Qt::UserRole); item->setData(classid, Qt::UserRole + 1); item->setData(variable, Qt::UserRole + 2); return item; @@ -301,7 +308,8 @@ QStandardItem* ShipAltShipClass::generate_item(const int classid, const int vari return nullptr; } } -QString ShipAltShipClass::generate_name(const int classid, const int variable) + +QString ShipAltShipClass::generateName(int classid, int variable) { QString classname; if (variable != -1) { @@ -322,7 +330,9 @@ QString ShipAltShipClass::generate_name(const int classid, const int variable) } return classname; } -void ShipAltShipClass::sync_data() { + +void ShipAltShipClass::syncData() +{ SCP_vector new_pool; int n = ui->classList->model()->rowCount(); for (int i = 0; i < n; i++) { @@ -335,12 +345,15 @@ void ShipAltShipClass::sync_data() { dynamic_cast(ui->classList->model())->index(i, 0).data(Qt::UserRole + 2).toInt(); new_pool.push_back(new_list_item); } - _model->sync_data(new_pool); + _model->syncData(new_pool); } + InverseSortFilterProxyModel::InverseSortFilterProxyModel(QObject* parent) : QSortFilterProxyModel(parent) {} + bool InverseSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const { bool accept = QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); return !accept; } + } // namespace fso::fred::dialogs diff --git a/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.h b/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.h index 3def433be6b..5d04ed106e6 100644 --- a/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.h +++ b/qtfred/src/ui/dialogs/ShipEditor/ShipAltShipClass.h @@ -1,16 +1,16 @@ #pragma once + #include #include #include -#include +#include + namespace fso::fred::dialogs { + namespace Ui { class ShipAltShipClass; } -/** - * @brief QtFRED's Alternate Ship Class Editor - */ class InverseSortFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT @@ -19,16 +19,11 @@ class InverseSortFilterProxyModel : public QSortFilterProxyModel { protected: bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; - }; + class ShipAltShipClass : public QDialog { Q_OBJECT public: - /** - * @brief Constructor - * @param [in] parent The parent dialog. - * @param [in] viewport The viewport this dialog is attatched to. - */ explicit ShipAltShipClass(QDialog* parent, EditorViewport* viewport); ~ShipAltShipClass() override; @@ -36,10 +31,6 @@ class ShipAltShipClass : public QDialog { void reject() override; protected: - /** - * @brief Overides the Dialogs Close event to add a confermation dialog - * @param [in] *e The event. - */ void closeEvent(QCloseEvent*) override; private: @@ -48,19 +39,15 @@ class ShipAltShipClass : public QDialog { EditorViewport* _viewport; void initUI(); + void updateUi(); - /** - * @brief Populates the UI - */ - void updateUI(); - - QStandardItemModel* alt_pool; + QStandardItemModel* _altPool; void classListChanged(const QModelIndex& current); - static QStandardItem* generate_item(const int classid, const int variable, const bool default_ship); - static QString generate_name(const int classid, const int variable); + static QStandardItem* generateItem(int classid, int variable, bool defaultShip); + static QString generateName(int classid, int variable); - void sync_data(); + void syncData(); private slots: // NOLINT(readability-redundant-access-specifiers) void on_buttonBox_accepted(); @@ -74,4 +61,5 @@ class ShipAltShipClass : public QDialog { void on_variableCombo_currentIndexChanged(int); void on_defaultCheckbox_toggled(bool); }; -} // namespace fso::fred::dialogs \ No newline at end of file + +} // namespace fso::fred::dialogs