Skip to content

Commit

Permalink
Used the new properties framework in the Project Properties dialog
Browse files Browse the repository at this point in the history
A bit of a hacky solution to hiding the "Custom Properties" header in
this context, because it is now inside a group box with that title.
  • Loading branch information
bjorn committed Nov 8, 2024
1 parent 2b710d4 commit 8bf312e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 70 deletions.
106 changes: 66 additions & 40 deletions src/tiled/projectpropertiesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@
#include "mapformat.h"
#include "project.h"
#include "projectdocument.h"
#include "varianteditorfactory.h"
#include "variantpropertymanager.h"
#include "tiled.h"
#include "utils.h"
#include "varianteditor.h"

#include <QtGroupPropertyManager>
#include <QFormLayout>
#include <QGroupBox>

namespace Tiled {

template<> EnumData enumData<CompatibilityVersion>()
{
return {{
QCoreApplication::translate("Tiled::ProjectPropertiesDialog", "Tiled 1.8"),
QCoreApplication::translate("Tiled::ProjectPropertiesDialog", "Tiled 1.9"),
QCoreApplication::translate("Tiled::ProjectPropertiesDialog", "Tiled 1.10"),
QCoreApplication::translate("Tiled::ProjectPropertiesDialog", "Latest"),
}, {
Tiled_1_8,
Tiled_1_9,
Tiled_1_10,
Tiled_Latest,
}};
}

ProjectPropertiesDialog::ProjectPropertiesDialog(Project &project, QWidget *parent)
: QDialog(parent)
, ui(new Ui::ProjectPropertiesDialog)
Expand All @@ -41,47 +58,56 @@ ProjectPropertiesDialog::ProjectPropertiesDialog(Project &project, QWidget *pare

mPropertiesProjectDocument->project().setProperties(project.properties());

auto variantPropertyManager = new VariantPropertyManager(this);
auto variantEditorFactory = new VariantEditorFactory(this);
auto groupPropertyManager = new QtGroupPropertyManager(this);

ui->propertyBrowser->setFactoryForManager<QtVariantPropertyManager>(variantPropertyManager,
variantEditorFactory);

const QMap<CompatibilityVersion, QString> versionToName {
{ Tiled_1_8, tr("Tiled 1.8") },
{ Tiled_1_9, tr("Tiled 1.9") },
{ Tiled_1_10, tr("Tiled 1.10") },
{ Tiled_Latest, tr("Latest") },
};
mVersions = versionToName.keys();

mCompatibilityVersionProperty = variantPropertyManager->addProperty(QtVariantPropertyManager::enumTypeId(),
tr("Compatibility Version"));
mCompatibilityVersionProperty->setAttribute(QLatin1String("enumNames"),
QVariant::fromValue<QStringList>(versionToName.values()));
mCompatibilityVersionProperty->setValue(mVersions.indexOf(project.mCompatibilityVersion));

mExtensionPathProperty = variantPropertyManager->addProperty(filePathTypeId(), tr("Extensions Directory"));
mExtensionPathProperty->setValue(project.mExtensionsPath);
mExtensionPathProperty->setAttribute(QStringLiteral("directory"), true);
mCompatibilityVersionProperty = new EnumProperty<CompatibilityVersion>(
tr("Compatibility Version"),
[=] {
return mProject.mCompatibilityVersion;
},
[=](CompatibilityVersion value) {
mProject.mCompatibilityVersion = value;
});

mExtensionPathProperty = new UrlProperty(
tr("Extensions Directory"),
[=] {
return QUrl::fromLocalFile(mProject.mExtensionsPath);
},
[=](const QUrl &value) {
mProject.mExtensionsPath = value.toLocalFile();
});
mExtensionPathProperty->setIsDirectory(true);

QString ruleFileFilter = QCoreApplication::translate("File Types", "Automapping Rules files (*.txt)");
FormatHelper<MapFormat> helper(FileFormat::ReadWrite, std::move(ruleFileFilter));

mAutomappingRulesFileProperty = variantPropertyManager->addProperty(filePathTypeId(), tr("Automapping rules"));
mAutomappingRulesFileProperty->setValue(project.mAutomappingRulesFile);
mAutomappingRulesFileProperty->setAttribute(QStringLiteral("filter"), helper.filter());
mAutomappingRulesFileProperty = new UrlProperty(
tr("Automapping rules"),
[=] {
return QUrl::fromLocalFile(mProject.mAutomappingRulesFile);
},
[=](const QUrl &value) {
mProject.mAutomappingRulesFile = value.toLocalFile();
});
mAutomappingRulesFileProperty->setFilter(helper.filter());

auto generalGroup = new QGroupBox(tr("General"));
auto generalLayout = new QFormLayout(generalGroup);
generalLayout->addRow(mCompatibilityVersionProperty->name(), mCompatibilityVersionProperty->createEditor(generalGroup));

auto filesGroup = new QGroupBox(tr("Paths && Files"));
auto filesLayout = new QFormLayout(filesGroup);
filesLayout->addRow(mExtensionPathProperty->name(), mExtensionPathProperty->createEditor(filesGroup));
filesLayout->addRow(mAutomappingRulesFileProperty->name(), mAutomappingRulesFileProperty->createEditor(filesGroup));

auto generalGroupProperty = groupPropertyManager->addProperty(tr("General"));
generalGroupProperty->addSubProperty(mCompatibilityVersionProperty);
ui->dialogLayout->insertWidget(0, filesGroup);
ui->dialogLayout->insertWidget(0, generalGroup);

auto filesGroupProperty = groupPropertyManager->addProperty(tr("Paths && Files"));
filesGroupProperty->addSubProperty(mExtensionPathProperty);
filesGroupProperty->addSubProperty(mAutomappingRulesFileProperty);
// Don't display the "Custom Properties" header
ui->propertiesWidget->customPropertiesGroup()->setName(QString());

ui->propertyBrowser->addProperty(generalGroupProperty);
ui->propertyBrowser->addProperty(filesGroupProperty);
// Tweak margins
const auto margin = Utils::dpiScaled(3);
ui->propertiesWidget->propertiesView()->widget()->setContentsMargins(0, margin, 0, margin);

ui->propertiesWidget->setDocument(mPropertiesProjectDocument);
}
Expand All @@ -94,9 +120,9 @@ ProjectPropertiesDialog::~ProjectPropertiesDialog()
void ProjectPropertiesDialog::accept()
{
mProject.setProperties(mPropertiesProjectDocument->project().properties());
mProject.mCompatibilityVersion = mVersions.at(mCompatibilityVersionProperty->value().toInt());
mProject.mExtensionsPath = mExtensionPathProperty->value().toString();
mProject.mAutomappingRulesFile = mAutomappingRulesFileProperty->value().toString();
mProject.mCompatibilityVersion = static_cast<CompatibilityVersion>(mCompatibilityVersionProperty->value());
mProject.mExtensionsPath = mExtensionPathProperty->value().toLocalFile();
mProject.mAutomappingRulesFile = mAutomappingRulesFileProperty->value().toLocalFile();

QDialog::accept();
}
Expand Down
13 changes: 5 additions & 8 deletions src/tiled/projectpropertiesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@

#pragma once

#include "tiled.h"

#include <QDialog>

class QtVariantProperty;

namespace Ui {
class ProjectPropertiesDialog;
}

namespace Tiled {

class IntProperty;
class Project;
class ProjectDocument;
class UrlProperty;

class ProjectPropertiesDialog : public QDialog
{
Expand All @@ -50,10 +48,9 @@ class ProjectPropertiesDialog : public QDialog

Project &mProject;
ProjectDocument *mPropertiesProjectDocument;
QList<CompatibilityVersion> mVersions;
QtVariantProperty *mCompatibilityVersionProperty;
QtVariantProperty *mExtensionPathProperty;
QtVariantProperty *mAutomappingRulesFileProperty;
IntProperty *mCompatibilityVersionProperty;
UrlProperty *mExtensionPathProperty;
UrlProperty *mAutomappingRulesFileProperty;
};

} // namespace Tiled
28 changes: 8 additions & 20 deletions src/tiled/projectpropertiesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@
<x>0</x>
<y>0</y>
<width>586</width>
<height>356</height>
<height>489</height>
</rect>
</property>
<property name="windowTitle">
<string>Project Properties</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="dialogLayout">
<item>
<widget class="QtGroupBoxPropertyBrowser" name="propertyBrowser" native="true"/>
</item>
<item>
<widget class="QWidget" name="container" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Custom Properties</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="Tiled::PropertiesWidget" name="propertiesWidget"/>
</item>
Expand All @@ -35,22 +29,16 @@
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QtGroupBoxPropertyBrowser</class>
<extends>QWidget</extends>
<header location="global">QtGroupBoxPropertyBrowser</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Tiled::PropertiesWidget</class>
<extends>QTreeView</extends>
Expand Down
7 changes: 6 additions & 1 deletion src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ class WangColorProperties : public ObjectProperties

PropertiesWidget::PropertiesWidget(QWidget *parent)
: QWidget{parent}
, mCustomProperties(new CustomProperties)
, mCustomProperties(new CustomProperties(this))
, mPropertyBrowser(new VariantEditorView(this))
{
mActionAddProperty = new QAction(this);
Expand Down Expand Up @@ -2251,6 +2251,11 @@ void PropertiesWidget::setDocument(Document *document)
}
}

GroupProperty *PropertiesWidget::customPropertiesGroup() const
{
return mCustomProperties;
}

void PropertiesWidget::selectCustomProperty(const QString &name)
{
// mPropertyBrowser->selectCustomProperty(name);
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/propertieswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Object;

class CustomProperties;
class Document;
class GroupProperty;
class ObjectProperties;
class VariantEditorView;

Expand All @@ -52,6 +53,9 @@ class PropertiesWidget : public QWidget
*/
void setDocument(Document *document);

GroupProperty *customPropertiesGroup() const;
VariantEditorView *propertiesView() const { return mPropertyBrowser; }

signals:
void bringToFront();

Expand Down
18 changes: 18 additions & 0 deletions src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@

namespace Tiled {

void Property::setName(const QString &name)
{
if (m_name != name) {
m_name = name;
emit nameChanged(name);
}
}

void Property::setToolTip(const QString &toolTip)
{
if (m_toolTip != toolTip) {
Expand Down Expand Up @@ -158,6 +166,7 @@ QWidget *UrlProperty::createEditor(QWidget *parent)
{
auto editor = new FileEdit(parent);
editor->setFilter(m_filter);
editor->setIsDirectory(m_isDirectory);

auto syncEditor = [=] {
editor->setFileUrl(value());
Expand Down Expand Up @@ -737,6 +746,9 @@ QLayout *VariantEditor::createPropertyLayout(Property *property)
updatePropertyToolTip(widgets, property->toolTip());
updatePropertyActions(widgets, property->actions());

connect(property, &Property::nameChanged, this, [=] (const QString &name) {
updatePropertyName(m_propertyWidgets[property], name);
});
connect(property, &Property::enabledChanged, this, [=] (bool enabled) {
updatePropertyEnabled(m_propertyWidgets[property], enabled);
});
Expand Down Expand Up @@ -787,6 +799,12 @@ void VariantEditor::setPropertyChildrenExpanded(GroupProperty *groupProperty, bo
}
}

void VariantEditor::updatePropertyName(const PropertyWidgets &widgets, const QString &name)
{
if (widgets.label)
widgets.label->setText(name);
}

void VariantEditor::updatePropertyEnabled(const PropertyWidgets &widgets, bool enabled)
{
if (widgets.label)
Expand Down
7 changes: 6 additions & 1 deletion src/tiled/varianteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PropertyLabel;
class Property : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip NOTIFY toolTipChanged)
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(bool modified READ isModified WRITE setModified NOTIFY modifiedChanged)
Expand Down Expand Up @@ -72,6 +72,7 @@ class Property : public QObject
{}

const QString &name() const { return m_name; }
void setName(const QString &name);

const QString &toolTip() const { return m_toolTip; }
void setToolTip(const QString &toolTip);
Expand All @@ -90,6 +91,7 @@ class Property : public QObject
virtual QWidget *createEditor(QWidget *parent) = 0;

signals:
void nameChanged(const QString &name);
void toolTipChanged(const QString &toolTip);
void valueChanged();
void enabledChanged(bool enabled);
Expand Down Expand Up @@ -256,8 +258,10 @@ struct UrlProperty : PropertyTemplate<QUrl>
using PropertyTemplate::PropertyTemplate;
QWidget *createEditor(QWidget *parent) override;
void setFilter(const QString &filter) { m_filter = filter; }
void setIsDirectory(bool isDirectory) { m_isDirectory = isDirectory; }
private:
QString m_filter;
bool m_isDirectory = false;
};

struct IntProperty : PropertyTemplate<int>
Expand Down Expand Up @@ -523,6 +527,7 @@ class VariantEditor : public QWidget

void setPropertyChildrenExpanded(GroupProperty *groupProperty, bool expanded);

void updatePropertyName(const PropertyWidgets &widgets, const QString &name);
void updatePropertyEnabled(const PropertyWidgets &widgets, bool enabled);
void updatePropertyToolTip(const PropertyWidgets &widgets, const QString &toolTip);
void updatePropertyActions(const PropertyWidgets &widgets, Property::Actions actions);
Expand Down

0 comments on commit 8bf312e

Please sign in to comment.