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>
<y>0</y>
<width>100</width>
<height>16</height>
<height>30</height>
</rect>
</property>
<property name="sizePolicy">
@ -1777,7 +1777,7 @@
<x>0</x>
<y>0</y>
<width>100</width>
<height>16</height>
<height>30</height>
</rect>
</property>
<property name="sizePolicy">
@ -1871,7 +1871,7 @@
<x>0</x>
<y>0</y>
<width>100</width>
<height>16</height>
<height>30</height>
</rect>
</property>
<property name="sizePolicy">
@ -1971,7 +1971,7 @@
<x>0</x>
<y>0</y>
<width>100</width>
<height>16</height>
<height>30</height>
</rect>
</property>
<property name="sizePolicy">
@ -2065,7 +2065,7 @@
<x>0</x>
<y>0</y>
<width>100</width>
<height>16</height>
<height>30</height>
</rect>
</property>
<property name="sizePolicy">
@ -2383,117 +2383,37 @@
</widget>
</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">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Custom Fields</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10"/>
</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">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
<width>20</width>
<height>40</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>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_Connections">
<layout class="QGridLayout" name="gridLayout_12">
<property name="leftMargin">
@ -3403,6 +3323,12 @@
<extends>QWidget</extends>
<header>mapview.h</header>
</customwidget>
<customwidget>
<class>CustomAttributesTable</class>
<extends>QFrame</extends>
<header>customattributestable.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../resources/images.qrc"/>

View file

@ -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");

View file

@ -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();

View file

@ -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<QString, QJsonValue> 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<QString, QJsonValue> getAttributes() const;
void setAttributes(const QMap<QString, QJsonValue> 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();
};

View file

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

View file

@ -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();
}

View file

@ -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<double>(value - 30) / 30.0);

View file

@ -8,11 +8,11 @@
#include <QScrollBar>
#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)
{
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<QString, QJsonValue> 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);
QJsonValue value = this->pickType(&ok);
if (ok) {
CustomAttributesTable::addAttribute(this->table, "", value, true);
this->event->setCustomValues(CustomAttributesTable::getAttributes(this->table));
this->resizeVertically();
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->setMinimumHeight(totalHeight);
this->table->setMaximumHeight(totalHeight);
this->table->horizontalHeader()->setVisible(true);
height += this->table->horizontalHeader()->height() + 2;
}
const QMap<QString, QJsonValue> CustomAttributesTable::getAttributes(QTableWidget * table) {
this->table->setMinimumHeight(height);
this->table->setMaximumHeight(height);
this->updateGeometry();
}
QMap<QString, QJsonValue> CustomAttributesTable::getAttributes() const {
QMap<QString, QJsonValue> 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<QString, QJsonValue> CustomAttributesTable::getAttributes(QTableWidge
return fields;
}
QJsonValue CustomAttributesTable::pickType(QWidget * parent, bool * ok) {
const QMap<QString, QJsonValue> valueTypes = {
QJsonValue CustomAttributesTable::pickType(bool * ok) {
static const QMap<QString, QJsonValue> 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<QString, QJsonValue> 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<QJsonValue::Type, QString> typeToName = {
static const QHash<QJsonValue::Type, QString> 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<QPersistentModelIndex> 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;
}

View file

@ -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() {