Simplify handling of CustomAttributesTable

This commit is contained in:
GriffinR 2024-01-08 21:06:25 -05:00
parent 9538097a6f
commit 84d3b4c55e
9 changed files with 126 additions and 207 deletions

View file

@ -1683,7 +1683,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>100</width> <width>100</width>
<height>16</height> <height>30</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -1777,7 +1777,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>100</width> <width>100</width>
<height>16</height> <height>30</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -1871,7 +1871,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>100</width> <width>100</width>
<height>16</height> <height>30</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -1971,7 +1971,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>100</width> <width>100</width>
<height>16</height> <height>30</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -2065,7 +2065,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>100</width> <width>100</width>
<height>16</height> <height>30</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -2383,115 +2383,35 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QFrame" name="frame_keyValue"> <widget class="CustomAttributesTable" name="customAttributesTable">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape"> <property name="frameShape">
<enum>QFrame::StyledPanel</enum> <enum>QFrame::StyledPanel</enum>
</property> </property>
<property name="frameShadow"> <property name="frameShadow">
<enum>QFrame::Raised</enum> <enum>QFrame::Raised</enum>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_10"> <layout class="QVBoxLayout" name="verticalLayout_10"/>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Custom Fields</string>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_AddCustomHeaderField">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_DeleteCustomHeaderField">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTableWidget" name="tableWidget_CustomHeaderFields">
<property name="toolTip">
<string>Custom fields will be added to the map.json file for the current map.</string>
</property>
<attribute name="horizontalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderHighlightSections">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Type</string>
</property>
</column>
<column>
<property name="text">
<string>Key</string>
</property>
</column>
<column>
<property name="text">
<string>Value</string>
</property>
</column>
</widget>
</item>
</layout>
</widget> </widget>
</item> </item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_Connections"> <widget class="QWidget" name="tab_Connections">
@ -3403,6 +3323,12 @@
<extends>QWidget</extends> <extends>QWidget</extends>
<header>mapview.h</header> <header>mapview.h</header>
</customwidget> </customwidget>
<customwidget>
<class>CustomAttributesTable</class>
<extends>QFrame</extends>
<header>customattributestable.h</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources> <resources>
<include location="../resources/images.qrc"/> <include location="../resources/images.qrc"/>

View file

@ -89,7 +89,6 @@ public:
void updatePrimaryTileset(QString tilesetLabel, bool forceLoad = false); void updatePrimaryTileset(QString tilesetLabel, bool forceLoad = false);
void updateSecondaryTileset(QString tilesetLabel, bool forceLoad = false); void updateSecondaryTileset(QString tilesetLabel, bool forceLoad = false);
void toggleBorderVisibility(bool visible, bool enableScriptCallback = true); void toggleBorderVisibility(bool visible, bool enableScriptCallback = true);
void updateCustomMapHeaderValues(QTableWidget *);
void configureEncounterJSON(QWidget *); void configureEncounterJSON(QWidget *);
Tileset *getCurrentMapPrimaryTileset(); Tileset *getCurrentMapPrimaryTileset();
@ -162,6 +161,7 @@ public slots:
void maskNonVisibleConnectionTiles(); void maskNonVisibleConnectionTiles();
void onBorderMetatilesChanged(); void onBorderMetatilesChanged();
void selectedEventIndexChanged(int index, Event::Group eventGroup); void selectedEventIndexChanged(int index, Event::Group eventGroup);
void updateCustomMapHeaderValues();
private: private:
const QImage defaultCollisionImgSheet = QImage(":/images/collisions.png"); const QImage defaultCollisionImgSheet = QImage(":/images/collisions.png");

View file

@ -275,9 +275,6 @@ private slots:
void on_actionAbout_Porymap_triggered(); void on_actionAbout_Porymap_triggered();
void on_actionOpen_Log_File_triggered(); void on_actionOpen_Log_File_triggered();
void on_actionOpen_Config_Folder_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_MetatileZoom_valueChanged(int value);
void on_horizontalSlider_CollisionZoom_valueChanged(int value); void on_horizontalSlider_CollisionZoom_valueChanged(int value);
void on_pushButton_NewWildMonGroup_clicked(); void on_pushButton_NewWildMonGroup_clicked();

View file

@ -8,18 +8,23 @@
class CustomAttributesTable : public QFrame class CustomAttributesTable : public QFrame
{ {
Q_OBJECT
public: public:
explicit CustomAttributesTable(Event *event, QWidget *parent = nullptr); explicit CustomAttributesTable(QWidget *parent = nullptr);
~CustomAttributesTable(); ~CustomAttributesTable();
static const QMap<QString, QJsonValue> getAttributes(QTableWidget * table); QMap<QString, QJsonValue> getAttributes() const;
static QJsonValue pickType(QWidget * parent, bool * ok = nullptr); void setAttributes(const QMap<QString, QJsonValue> attributes);
static void addAttribute(QTableWidget * table, QString key, QJsonValue value, bool isNew = false); bool deleteSelectedAttributes();
static bool deleteSelectedAttributes(QTableWidget * table);
signals:
void edited();
private: private:
Event *event;
QTableWidget *table; QTableWidget *table;
QJsonValue pickType(bool * ok = nullptr);
void addAttribute(QString key, QJsonValue value, bool init);
void resizeVertically(); void resizeVertically();
}; };

View file

@ -50,6 +50,8 @@ public:
QFrame *frame_contents; QFrame *frame_contents;
QVBoxLayout *layout_contents; QVBoxLayout *layout_contents;
CustomAttributesTable *custom_attributes;
protected: protected:
bool populated = false; bool populated = false;
bool initialized = false; bool initialized = false;

View file

@ -1951,9 +1951,9 @@ void Editor::toggleBorderVisibility(bool visible, bool enableScriptCallback)
Scripting::cb_BorderVisibilityToggled(visible); Scripting::cb_BorderVisibilityToggled(visible);
} }
void Editor::updateCustomMapHeaderValues(QTableWidget *table) void Editor::updateCustomMapHeaderValues()
{ {
map->customHeaders = CustomAttributesTable::getAttributes(table); map->customHeaders = ui->customAttributesTable->getAttributes();
emit editedMapData(); emit editedMapData();
} }

View file

@ -256,6 +256,7 @@ void MainWindow::initEditor() {
connect(this->editor, &Editor::tilesetUpdated, this, &Scripting::cb_TilesetUpdated); connect(this->editor, &Editor::tilesetUpdated, this, &Scripting::cb_TilesetUpdated);
connect(ui->toolButton_Open_Scripts, &QToolButton::pressed, this->editor, &Editor::openMapScripts); 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->actionOpen_Project_in_Text_Editor, &QAction::triggered, this->editor, &Editor::openProjectInTextEditor);
connect(ui->customAttributesTable, &CustomAttributesTable::edited, this->editor, &Editor::updateCustomMapHeaderValues);
this->loadUserSettings(); this->loadUserSettings();
@ -846,13 +847,7 @@ void MainWindow::displayMapProperties() {
ui->checkBox_AllowEscaping->setChecked(map->allowEscaping); ui->checkBox_AllowEscaping->setChecked(map->allowEscaping);
ui->spinBox_FloorNumber->setValue(map->floorNumber); ui->spinBox_FloorNumber->setValue(map->floorNumber);
// Custom fields table. ui->customAttributesTable->setAttributes(map->customHeaders);
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);
} }
void MainWindow::on_comboBox_Song_currentTextChanged(const QString &song) void MainWindow::on_comboBox_Song_currentTextChanged(const QString &song)
@ -2800,27 +2795,6 @@ void MainWindow::reloadScriptEngine() {
Scripting::cb_MapOpened(editor->map->name); 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) { void MainWindow::on_horizontalSlider_MetatileZoom_valueChanged(int value) {
porymapConfig.setMetatilesZoom(value); porymapConfig.setMetatilesZoom(value);
double scale = pow(3.0, static_cast<double>(value - 30) / 30.0); double scale = pow(3.0, static_cast<double>(value - 30) / 30.0);

View file

@ -8,11 +8,11 @@
#include <QScrollBar> #include <QScrollBar>
#include <QInputDialog> #include <QInputDialog>
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) QFrame(parent)
{ {
this->event = event;
QVBoxLayout *layout = new QVBoxLayout(this); QVBoxLayout *layout = new QVBoxLayout(this);
QLabel *label = new QLabel("Custom Attributes"); QLabel *label = new QLabel("Custom Attributes");
layout->addWidget(label); layout->addWidget(label);
@ -33,34 +33,30 @@ CustomAttributesTable::CustomAttributesTable(Event *event, QWidget *parent) :
this->table->setColumnCount(3); this->table->setColumnCount(3);
this->table->setHorizontalHeaderLabels(QStringList({"Type", "Key", "Value"})); this->table->setHorizontalHeaderLabels(QStringList({"Type", "Key", "Value"}));
this->table->horizontalHeader()->setStretchLastSection(true); 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->addWidget(this->table);
layout->addStretch(1);
QMap<QString, QJsonValue> customValues = this->event->getCustomValues(); connect(addButton, &QPushButton::clicked, [this]() {
for (auto it = customValues.begin(); it != customValues.end(); it++)
CustomAttributesTable::addAttribute(this->table, it.key(), it.value());
connect(addButton, &QPushButton::clicked, [=]() {
bool ok; bool ok;
QJsonValue value = CustomAttributesTable::pickType(this, &ok); QJsonValue value = this->pickType(&ok);
if (ok){ if (ok) {
CustomAttributesTable::addAttribute(this->table, "", value, true); this->addAttribute("", value, false);
this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table)); emit this->edited();
this->resizeVertically();
} }
}); });
connect(deleteButton, &QPushButton::clicked, [=]() { connect(deleteButton, &QPushButton::clicked, [this]() {
if (CustomAttributesTable::deleteSelectedAttributes(this->table)) { if (this->deleteSelectedAttributes()) {
this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table)); emit this->edited();
this->resizeVertically();
} }
}); });
connect(this->table, &QTableWidget::cellChanged, [=]() { connect(this->table, &QTableWidget::cellChanged, [this]() {
this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table)); emit this->edited();
}); });
this->resizeVertically();
} }
CustomAttributesTable::~CustomAttributesTable() CustomAttributesTable::~CustomAttributesTable()
@ -68,30 +64,31 @@ CustomAttributesTable::~CustomAttributesTable()
} }
void CustomAttributesTable::resizeVertically() { void CustomAttributesTable::resizeVertically() {
int horizontalHeaderHeight = this->table->horizontalHeader()->height(); int height = 0;
int rowHeight = 0;
for (int i = 0; i < this->table->rowCount(); i++) { 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) { if (this->table->rowCount() == 0) {
totalHeight += 1; this->table->horizontalHeader()->setVisible(false);
} else { } 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<QString, QJsonValue> CustomAttributesTable::getAttributes(QTableWidget * table) { QMap<QString, QJsonValue> CustomAttributesTable::getAttributes() const {
QMap<QString, QJsonValue> fields; QMap<QString, QJsonValue> fields;
if (!table) return fields; for (int row = 0; row < this->table->rowCount(); row++) {
for (int row = 0; row < table->rowCount(); row++) {
QString key = ""; QString key = "";
QTableWidgetItem *typeItem = table->item(row, 0); QTableWidgetItem *typeItem = this->table->item(row, 0);
QTableWidgetItem *keyItem = table->item(row, 1); QTableWidgetItem *keyItem = this->table->item(row, 1);
QTableWidgetItem *valueItem = table->item(row, 2); QTableWidgetItem *valueItem = this->table->item(row, 2);
if (keyItem) key = keyItem->text(); if (keyItem) key = keyItem->text();
if (key.isEmpty() || !typeItem || !valueItem) if (key.isEmpty() || !typeItem || !valueItem)
@ -121,19 +118,32 @@ const QMap<QString, QJsonValue> CustomAttributesTable::getAttributes(QTableWidge
return fields; return fields;
} }
QJsonValue CustomAttributesTable::pickType(QWidget * parent, bool * ok) { QJsonValue CustomAttributesTable::pickType(bool * ok) {
const QMap<QString, QJsonValue> valueTypes = { static const QMap<QString, QJsonValue> valueTypes = {
{"String", QJsonValue(QString(""))}, {"String", QJsonValue(QString(""))},
{"Number", QJsonValue(0)}, {"Number", QJsonValue(0)},
{"Boolean", QJsonValue(false)}, {"Boolean", QJsonValue(false)},
}; };
QStringList typeNames = valueTypes.keys(); static const QStringList typeNames = valueTypes.keys();
QString selection = QInputDialog::getItem(parent, "", "Choose Value Type", typeNames, typeNames.indexOf("String"), false, ok); 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); return valueTypes.value(selection);
} }
void CustomAttributesTable::addAttribute(QTableWidget * table, QString key, QJsonValue value, bool isNew) { void CustomAttributesTable::setAttributes(const QMap<QString, QJsonValue> attributes) {
if (!table) return; 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; QTableWidgetItem * valueItem;
QJsonValue::Type type = value.type(); QJsonValue::Type type = value.type();
switch (type) switch (type)
@ -154,7 +164,7 @@ void CustomAttributesTable::addAttribute(QTableWidget * table, QString key, QJso
break; break;
} }
const QHash<QJsonValue::Type, QString> typeToName = { static const QHash<QJsonValue::Type, QString> typeToName = {
{QJsonValue::Bool, "Bool"}, {QJsonValue::Bool, "Bool"},
{QJsonValue::Double, "Number"}, {QJsonValue::Double, "Number"},
{QJsonValue::String, "String"}, {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->setData(Qt::UserRole, type); // Record the type for writing to the file
typeItem->setTextAlignment(Qt::AlignCenter); typeItem->setTextAlignment(Qt::AlignCenter);
int rowIndex = table->rowCount(); int rowIndex = this->table->rowCount();
table->insertRow(rowIndex); this->table->insertRow(rowIndex);
table->setItem(rowIndex, 0, typeItem); this->table->setItem(rowIndex, 0, typeItem);
table->setItem(rowIndex, 1, new QTableWidgetItem(key)); this->table->setItem(rowIndex, 1, new QTableWidgetItem(key));
table->setItem(rowIndex, 2, valueItem); this->table->setItem(rowIndex, 2, valueItem);
if (isNew) { if (!init) {
valueItem->setText(""); // Erase the "0" in new numbers valueItem->setText(""); // Erase the "0" in new numbers
table->selectRow(rowIndex); this->table->selectRow(rowIndex);
this->resizeVertically();
} }
} }
bool CustomAttributesTable::deleteSelectedAttributes(QTableWidget * table) { bool CustomAttributesTable::deleteSelectedAttributes() {
if (!table) int rowCount = this->table->rowCount();
return false;
int rowCount = table->rowCount();
if (rowCount <= 0) if (rowCount <= 0)
return false; return false;
QModelIndexList indexList = table->selectionModel()->selectedIndexes(); QModelIndexList indexList = this->table->selectionModel()->selectedIndexes();
QList<QPersistentModelIndex> persistentIndexes; QList<QPersistentModelIndex> persistentIndexes;
for (QModelIndex index : indexList) { for (QModelIndex index : indexList) {
QPersistentModelIndex persistentIndex(index); QPersistentModelIndex persistentIndex(index);
@ -199,11 +207,12 @@ bool CustomAttributesTable::deleteSelectedAttributes(QTableWidget * table) {
return false; return false;
for (QPersistentModelIndex index : persistentIndexes) { for (QPersistentModelIndex index : persistentIndexes) {
table->removeRow(index.row()); this->table->removeRow(index.row());
} }
if (table->rowCount() > 0) { if (this->table->rowCount() > 0) {
table->selectRow(0); this->table->selectRow(0);
} }
this->resizeVertically();
return true; return true;
} }

View file

@ -102,8 +102,9 @@ void EventFrame::setup() {
} }
void EventFrame::initCustomAttributesTable() { void EventFrame::initCustomAttributesTable() {
CustomAttributesTable *customAttributes = new CustomAttributesTable(this->event, this); this->custom_attributes = new CustomAttributesTable(this);
this->layout_contents->addWidget(customAttributes); this->custom_attributes->setAttributes(this->event->getCustomValues());
this->layout_contents->addWidget(this->custom_attributes);
} }
void EventFrame::connectSignals(MainWindow *) { void EventFrame::connectSignals(MainWindow *) {
@ -133,6 +134,11 @@ void EventFrame::connectSignals(MainWindow *) {
this->event->setZ(value); this->event->setZ(value);
this->event->modify(); this->event->modify();
}); });
this->custom_attributes->disconnect();
connect(this->custom_attributes, &CustomAttributesTable::edited, [this]() {
this->event->setCustomValues(this->custom_attributes->getAttributes());
});
} }
void EventFrame::initialize() { void EventFrame::initialize() {