diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 34ee86c6..b7aa89b7 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -1683,7 +1683,7 @@ 0 0 100 - 16 + 30 @@ -1777,7 +1777,7 @@ 0 0 100 - 16 + 30 @@ -1871,7 +1871,7 @@ 0 0 100 - 16 + 30 @@ -1971,7 +1971,7 @@ 0 0 100 - 16 + 30 @@ -2065,7 +2065,7 @@ 0 0 100 - 16 + 30 @@ -2383,115 +2383,35 @@ - + + + + 0 + 0 + + QFrame::StyledPanel QFrame::Raised - - - - - Custom Fields - - - - - - - QFrame::NoFrame - - - QFrame::Plain - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Add - - - - - - - Delete - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - Custom fields will be added to the map.json file for the current map. - - - false - - - false - - - true - - - false - - - true - - - false - - - - Type - - - - - Key - - - - - Value - - - - - + + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -3403,6 +3323,12 @@ QWidget
mapview.h
+ + CustomAttributesTable + QFrame +
customattributestable.h
+ 1 +
diff --git a/include/editor.h b/include/editor.h index f684b491..bad9a1c3 100644 --- a/include/editor.h +++ b/include/editor.h @@ -89,7 +89,6 @@ public: void updatePrimaryTileset(QString tilesetLabel, bool forceLoad = false); void updateSecondaryTileset(QString tilesetLabel, bool forceLoad = false); void toggleBorderVisibility(bool visible, bool enableScriptCallback = true); - void updateCustomMapHeaderValues(QTableWidget *); void configureEncounterJSON(QWidget *); Tileset *getCurrentMapPrimaryTileset(); @@ -162,6 +161,7 @@ public slots: void maskNonVisibleConnectionTiles(); void onBorderMetatilesChanged(); void selectedEventIndexChanged(int index, Event::Group eventGroup); + void updateCustomMapHeaderValues(); private: const QImage defaultCollisionImgSheet = QImage(":/images/collisions.png"); diff --git a/include/mainwindow.h b/include/mainwindow.h index bc6dface..197e4339 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -275,9 +275,6 @@ private slots: void on_actionAbout_Porymap_triggered(); void on_actionOpen_Log_File_triggered(); void on_actionOpen_Config_Folder_triggered(); - void on_pushButton_AddCustomHeaderField_clicked(); - void on_pushButton_DeleteCustomHeaderField_clicked(); - void on_tableWidget_CustomHeaderFields_cellChanged(int row, int column); void on_horizontalSlider_MetatileZoom_valueChanged(int value); void on_horizontalSlider_CollisionZoom_valueChanged(int value); void on_pushButton_NewWildMonGroup_clicked(); diff --git a/include/ui/customattributestable.h b/include/ui/customattributestable.h index f17f4877..421ac3bb 100644 --- a/include/ui/customattributestable.h +++ b/include/ui/customattributestable.h @@ -8,18 +8,23 @@ class CustomAttributesTable : public QFrame { + Q_OBJECT + public: - explicit CustomAttributesTable(Event *event, QWidget *parent = nullptr); + explicit CustomAttributesTable(QWidget *parent = nullptr); ~CustomAttributesTable(); - static const QMap getAttributes(QTableWidget * table); - static QJsonValue pickType(QWidget * parent, bool * ok = nullptr); - static void addAttribute(QTableWidget * table, QString key, QJsonValue value, bool isNew = false); - static bool deleteSelectedAttributes(QTableWidget * table); + QMap getAttributes() const; + void setAttributes(const QMap attributes); + bool deleteSelectedAttributes(); + +signals: + void edited(); private: - Event *event; QTableWidget *table; + QJsonValue pickType(bool * ok = nullptr); + void addAttribute(QString key, QJsonValue value, bool init); void resizeVertically(); }; diff --git a/include/ui/eventframes.h b/include/ui/eventframes.h index 9cabd585..8dbb76ea 100644 --- a/include/ui/eventframes.h +++ b/include/ui/eventframes.h @@ -50,6 +50,8 @@ public: QFrame *frame_contents; QVBoxLayout *layout_contents; + CustomAttributesTable *custom_attributes; + protected: bool populated = false; bool initialized = false; diff --git a/src/editor.cpp b/src/editor.cpp index 014fa489..7288474f 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1951,9 +1951,9 @@ void Editor::toggleBorderVisibility(bool visible, bool enableScriptCallback) Scripting::cb_BorderVisibilityToggled(visible); } -void Editor::updateCustomMapHeaderValues(QTableWidget *table) +void Editor::updateCustomMapHeaderValues() { - map->customHeaders = CustomAttributesTable::getAttributes(table); + map->customHeaders = ui->customAttributesTable->getAttributes(); emit editedMapData(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3917adf5..adc6ad76 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -256,6 +256,7 @@ void MainWindow::initEditor() { connect(this->editor, &Editor::tilesetUpdated, this, &Scripting::cb_TilesetUpdated); connect(ui->toolButton_Open_Scripts, &QToolButton::pressed, this->editor, &Editor::openMapScripts); connect(ui->actionOpen_Project_in_Text_Editor, &QAction::triggered, this->editor, &Editor::openProjectInTextEditor); + connect(ui->customAttributesTable, &CustomAttributesTable::edited, this->editor, &Editor::updateCustomMapHeaderValues); this->loadUserSettings(); @@ -846,13 +847,7 @@ void MainWindow::displayMapProperties() { ui->checkBox_AllowEscaping->setChecked(map->allowEscaping); ui->spinBox_FloorNumber->setValue(map->floorNumber); - // Custom fields table. - ui->tableWidget_CustomHeaderFields->blockSignals(true); - ui->tableWidget_CustomHeaderFields->setRowCount(0); - for (auto it = map->customHeaders.begin(); it != map->customHeaders.end(); it++) - CustomAttributesTable::addAttribute(ui->tableWidget_CustomHeaderFields, it.key(), it.value()); - ui->tableWidget_CustomHeaderFields->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - ui->tableWidget_CustomHeaderFields->blockSignals(false); + ui->customAttributesTable->setAttributes(map->customHeaders); } void MainWindow::on_comboBox_Song_currentTextChanged(const QString &song) @@ -2800,27 +2795,6 @@ void MainWindow::reloadScriptEngine() { Scripting::cb_MapOpened(editor->map->name); } -void MainWindow::on_pushButton_AddCustomHeaderField_clicked() -{ - bool ok; - QJsonValue value = CustomAttributesTable::pickType(this, &ok); - if (ok){ - CustomAttributesTable::addAttribute(this->ui->tableWidget_CustomHeaderFields, "", value, true); - this->editor->updateCustomMapHeaderValues(this->ui->tableWidget_CustomHeaderFields); - } -} - -void MainWindow::on_pushButton_DeleteCustomHeaderField_clicked() -{ - if (CustomAttributesTable::deleteSelectedAttributes(this->ui->tableWidget_CustomHeaderFields)) - this->editor->updateCustomMapHeaderValues(this->ui->tableWidget_CustomHeaderFields); -} - -void MainWindow::on_tableWidget_CustomHeaderFields_cellChanged(int, int) -{ - this->editor->updateCustomMapHeaderValues(this->ui->tableWidget_CustomHeaderFields); -} - void MainWindow::on_horizontalSlider_MetatileZoom_valueChanged(int value) { porymapConfig.setMetatilesZoom(value); double scale = pow(3.0, static_cast(value - 30) / 30.0); diff --git a/src/ui/customattributestable.cpp b/src/ui/customattributestable.cpp index d87b3f8a..a804c85b 100644 --- a/src/ui/customattributestable.cpp +++ b/src/ui/customattributestable.cpp @@ -8,11 +8,11 @@ #include #include -CustomAttributesTable::CustomAttributesTable(Event *event, QWidget *parent) : +// TODO: Tooltip-- "Custom fields will be added to the map.json file for the current map."? +// TODO: Fix squishing when first element is added +CustomAttributesTable::CustomAttributesTable(QWidget *parent) : QFrame(parent) { - this->event = event; - QVBoxLayout *layout = new QVBoxLayout(this); QLabel *label = new QLabel("Custom Attributes"); layout->addWidget(label); @@ -33,34 +33,30 @@ CustomAttributesTable::CustomAttributesTable(Event *event, QWidget *parent) : this->table->setColumnCount(3); this->table->setHorizontalHeaderLabels(QStringList({"Type", "Key", "Value"})); this->table->horizontalHeader()->setStretchLastSection(true); + this->table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + this->table->horizontalHeader()->setVisible(false); + this->table->verticalHeader()->setVisible(false); layout->addWidget(this->table); + layout->addStretch(1); - QMap customValues = this->event->getCustomValues(); - for (auto it = customValues.begin(); it != customValues.end(); it++) - CustomAttributesTable::addAttribute(this->table, it.key(), it.value()); - - connect(addButton, &QPushButton::clicked, [=]() { + connect(addButton, &QPushButton::clicked, [this]() { bool ok; - QJsonValue value = CustomAttributesTable::pickType(this, &ok); - if (ok){ - CustomAttributesTable::addAttribute(this->table, "", value, true); - this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table)); - this->resizeVertically(); + QJsonValue value = this->pickType(&ok); + if (ok) { + this->addAttribute("", value, false); + emit this->edited(); } }); - connect(deleteButton, &QPushButton::clicked, [=]() { - if (CustomAttributesTable::deleteSelectedAttributes(this->table)) { - this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table)); - this->resizeVertically(); + connect(deleteButton, &QPushButton::clicked, [this]() { + if (this->deleteSelectedAttributes()) { + emit this->edited(); } }); - connect(this->table, &QTableWidget::cellChanged, [=]() { - this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table)); + connect(this->table, &QTableWidget::cellChanged, [this]() { + emit this->edited(); }); - - this->resizeVertically(); } CustomAttributesTable::~CustomAttributesTable() @@ -68,30 +64,31 @@ CustomAttributesTable::~CustomAttributesTable() } void CustomAttributesTable::resizeVertically() { - int horizontalHeaderHeight = this->table->horizontalHeader()->height(); - int rowHeight = 0; + int height = 0; for (int i = 0; i < this->table->rowCount(); i++) { - rowHeight += this->table->rowHeight(0); + height += this->table->rowHeight(i); } - int totalHeight = horizontalHeaderHeight + rowHeight; + + // Header disappears if there are no entries if (this->table->rowCount() == 0) { - totalHeight += 1; + this->table->horizontalHeader()->setVisible(false); } else { - totalHeight += 2; + this->table->horizontalHeader()->setVisible(true); + height += this->table->horizontalHeader()->height() + 2; } - this->table->setMinimumHeight(totalHeight); - this->table->setMaximumHeight(totalHeight); + + this->table->setMinimumHeight(height); + this->table->setMaximumHeight(height); + this->updateGeometry(); } -const QMap CustomAttributesTable::getAttributes(QTableWidget * table) { +QMap CustomAttributesTable::getAttributes() const { QMap fields; - if (!table) return fields; - - for (int row = 0; row < table->rowCount(); row++) { + for (int row = 0; row < this->table->rowCount(); row++) { QString key = ""; - QTableWidgetItem *typeItem = table->item(row, 0); - QTableWidgetItem *keyItem = table->item(row, 1); - QTableWidgetItem *valueItem = table->item(row, 2); + QTableWidgetItem *typeItem = this->table->item(row, 0); + QTableWidgetItem *keyItem = this->table->item(row, 1); + QTableWidgetItem *valueItem = this->table->item(row, 2); if (keyItem) key = keyItem->text(); if (key.isEmpty() || !typeItem || !valueItem) @@ -121,19 +118,32 @@ const QMap CustomAttributesTable::getAttributes(QTableWidge return fields; } -QJsonValue CustomAttributesTable::pickType(QWidget * parent, bool * ok) { - const QMap valueTypes = { +QJsonValue CustomAttributesTable::pickType(bool * ok) { + static const QMap valueTypes = { {"String", QJsonValue(QString(""))}, {"Number", QJsonValue(0)}, {"Boolean", QJsonValue(false)}, }; - QStringList typeNames = valueTypes.keys(); - QString selection = QInputDialog::getItem(parent, "", "Choose Value Type", typeNames, typeNames.indexOf("String"), false, ok); + static const QStringList typeNames = valueTypes.keys(); + static int defaultIndex = typeNames.indexOf("String"); + QString selection = QInputDialog::getItem(this, "", "Choose Value Type", typeNames, defaultIndex, false, ok); + + // Preserve selection for next time + int index = typeNames.indexOf(selection); + if (index >= 0) defaultIndex = index; + return valueTypes.value(selection); } -void CustomAttributesTable::addAttribute(QTableWidget * table, QString key, QJsonValue value, bool isNew) { - if (!table) return; +void CustomAttributesTable::setAttributes(const QMap attributes) { + this->table->setRowCount(0); + for (auto it = attributes.cbegin(); it != attributes.cend(); it++) + this->addAttribute(it.key(), it.value(), true); + this->resizeVertically(); +} + +void CustomAttributesTable::addAttribute(QString key, QJsonValue value, bool init) { + const QSignalBlocker blocker(this->table); QTableWidgetItem * valueItem; QJsonValue::Type type = value.type(); switch (type) @@ -154,7 +164,7 @@ void CustomAttributesTable::addAttribute(QTableWidget * table, QString key, QJso break; } - const QHash typeToName = { + static const QHash typeToName = { {QJsonValue::Bool, "Bool"}, {QJsonValue::Double, "Number"}, {QJsonValue::String, "String"}, @@ -168,27 +178,25 @@ void CustomAttributesTable::addAttribute(QTableWidget * table, QString key, QJso typeItem->setData(Qt::UserRole, type); // Record the type for writing to the file typeItem->setTextAlignment(Qt::AlignCenter); - int rowIndex = table->rowCount(); - table->insertRow(rowIndex); - table->setItem(rowIndex, 0, typeItem); - table->setItem(rowIndex, 1, new QTableWidgetItem(key)); - table->setItem(rowIndex, 2, valueItem); + int rowIndex = this->table->rowCount(); + this->table->insertRow(rowIndex); + this->table->setItem(rowIndex, 0, typeItem); + this->table->setItem(rowIndex, 1, new QTableWidgetItem(key)); + this->table->setItem(rowIndex, 2, valueItem); - if (isNew) { + if (!init) { valueItem->setText(""); // Erase the "0" in new numbers - table->selectRow(rowIndex); + this->table->selectRow(rowIndex); + this->resizeVertically(); } } -bool CustomAttributesTable::deleteSelectedAttributes(QTableWidget * table) { - if (!table) - return false; - - int rowCount = table->rowCount(); +bool CustomAttributesTable::deleteSelectedAttributes() { + int rowCount = this->table->rowCount(); if (rowCount <= 0) return false; - QModelIndexList indexList = table->selectionModel()->selectedIndexes(); + QModelIndexList indexList = this->table->selectionModel()->selectedIndexes(); QList persistentIndexes; for (QModelIndex index : indexList) { QPersistentModelIndex persistentIndex(index); @@ -199,11 +207,12 @@ bool CustomAttributesTable::deleteSelectedAttributes(QTableWidget * table) { return false; for (QPersistentModelIndex index : persistentIndexes) { - table->removeRow(index.row()); + this->table->removeRow(index.row()); } - if (table->rowCount() > 0) { - table->selectRow(0); + if (this->table->rowCount() > 0) { + this->table->selectRow(0); } + this->resizeVertically(); return true; } diff --git a/src/ui/eventframes.cpp b/src/ui/eventframes.cpp index 348045d4..0b82d202 100644 --- a/src/ui/eventframes.cpp +++ b/src/ui/eventframes.cpp @@ -102,8 +102,9 @@ void EventFrame::setup() { } void EventFrame::initCustomAttributesTable() { - CustomAttributesTable *customAttributes = new CustomAttributesTable(this->event, this); - this->layout_contents->addWidget(customAttributes); + this->custom_attributes = new CustomAttributesTable(this); + this->custom_attributes->setAttributes(this->event->getCustomValues()); + this->layout_contents->addWidget(this->custom_attributes); } void EventFrame::connectSignals(MainWindow *) { @@ -133,6 +134,11 @@ void EventFrame::connectSignals(MainWindow *) { this->event->setZ(value); this->event->modify(); }); + + this->custom_attributes->disconnect(); + connect(this->custom_attributes, &CustomAttributesTable::edited, [this]() { + this->event->setCustomValues(this->custom_attributes->getAttributes()); + }); } void EventFrame::initialize() {