Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 176 additions & 1 deletion src/tiled/propertiesdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,91 @@

#include "propertiesdock.h"

#include "mapdocument.h"
#include "mapobject.h"
#include "propertiesview.h"
#include "propertieswidget.h"
#include "tilesetdocument.h"

#include <QKeyEvent>
#include <QTabBar>
#include <QVBoxLayout>

namespace Tiled {

PropertiesDock::PropertiesDock(QWidget *parent)
: QDockWidget(parent)
, mTabBar(new QTabBar(this))
, mPropertiesWidget(new PropertiesWidget(this))
{
setObjectName(QLatin1String("propertiesDock"));
setWidget(mPropertiesWidget);

mTabBar->setDocumentMode(true);
mTabBar->setUsesScrollButtons(true); // defaults to false on macOS

mTabBar->insertTab(MapTab, tr("Map"));
mTabBar->insertTab(LayerTab, tr("Layer"));
mTabBar->insertTab(MapObjectTab, tr("Object"));
mTabBar->insertTab(TilesetTab, tr("Tileset"));
mTabBar->insertTab(TileTab, tr("Tile"));
mTabBar->insertTab(WangSetTab, tr("Terrain Set"));
mTabBar->insertTab(WangColorTab, tr("Terrain"));

auto widget = new QWidget(this);
auto layout = new QVBoxLayout(widget);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(mTabBar);
layout->addWidget(mPropertiesWidget);

setWidget(widget);

connect(mTabBar, &QTabBar::currentChanged,
this, &PropertiesDock::tabChanged);

connect(mPropertiesWidget, &PropertiesWidget::bringToFront,
this, &PropertiesDock::bringToFront);

updateTabs();
retranslateUi();
}

void PropertiesDock::setDocument(Document *document)
{
if (mDocument == document)
return;

if (mDocument)
mDocument->disconnect(this);

if (document) {
connect(document, &Document::currentObjectChanged,
this, &PropertiesDock::updateTabs);

switch (document->type()) {
case Document::MapDocumentType: {
auto mapDocument = static_cast<MapDocument *>(document);
connect(mapDocument, &MapDocument::selectedLayersChanged,
this, &PropertiesDock::updateTabs);
connect(mapDocument, &MapDocument::selectedObjectsChanged,
this, &PropertiesDock::updateTabs);
break;
}
case Document::TilesetDocumentType: {
auto tilesetDocument = static_cast<TilesetDocument *>(document);
connect(tilesetDocument, &TilesetDocument::selectedTilesChanged,
this, &PropertiesDock::updateTabs);
break;
}
case Document::WorldDocumentType:
case Document::ProjectDocumentType:
break;
}
}

mDocument = document;
mPropertiesWidget->setDocument(document);
updateTabs();
}

void PropertiesDock::selectCustomProperty(const QString &name)
Expand Down Expand Up @@ -82,6 +144,119 @@ void PropertiesDock::bringToFront()
void PropertiesDock::retranslateUi()
{
setWindowTitle(tr("Properties"));

mTabBar->setTabText(MapTab, tr("Map"));
mTabBar->setTabText(LayerTab, tr("Layer"));
mTabBar->setTabText(MapObjectTab, tr("Object"));
mTabBar->setTabText(TilesetTab, tr("Tileset"));
mTabBar->setTabText(TileTab, tr("Tile"));
mTabBar->setTabText(WangSetTab, tr("Terrain Set"));
mTabBar->setTabText(WangColorTab, tr("Terrain"));
}

void PropertiesDock::updateTabs()
{
const bool isMap = mDocument && mDocument->type() == Document::MapDocumentType;
const bool isTileset = mDocument && mDocument->type() == Document::TilesetDocumentType;
const auto currentObject = mDocument ? mDocument->currentObject() : nullptr;

bool layersSelected = false;
bool objectsSelected = false;
bool tilesSelected = false;
bool wangSetSelected = false;
bool wangColorSelected = false;

if (isMap) {
const auto mapDocument = static_cast<MapDocument *>(mDocument);
layersSelected = !mapDocument->selectedLayers().isEmpty();
objectsSelected = !mapDocument->selectedObjects().isEmpty();
}

if (isTileset) {
const auto tilesetDocument = static_cast<TilesetDocument *>(mDocument);
tilesSelected = !tilesetDocument->selectedTiles().isEmpty();

// todo: keep track of selected wang set and color in TilesetDocument
wangSetSelected = currentObject && currentObject->typeId() == Object::WangSetType;
wangColorSelected = currentObject && currentObject->typeId() == Object::WangColorType;
}

mTabBar->setTabVisible(MapTab, isMap);
mTabBar->setTabVisible(LayerTab, layersSelected);
mTabBar->setTabVisible(MapObjectTab, objectsSelected);
mTabBar->setTabVisible(TilesetTab, isTileset);
mTabBar->setTabVisible(TileTab, tilesSelected);
mTabBar->setTabVisible(WangSetTab, wangSetSelected);
mTabBar->setTabVisible(WangColorTab, wangColorSelected || wangSetSelected);
mTabBar->tabRect(0); // Force recalculation of tab sizes

int currentIndex = -1;
if (currentObject) {
switch (currentObject->typeId()) {
case Object::LayerType: currentIndex = LayerTab; break;
case Object::MapObjectType: currentIndex = MapObjectTab; break;
case Object::MapType: currentIndex = MapTab; break;
case Object::TilesetType: currentIndex = TilesetTab; break;
case Object::TileType: currentIndex = TileTab; break;
case Object::WangSetType: currentIndex = WangSetTab; break;
case Object::WangColorType: currentIndex = WangColorTab; break;

case Object::ProjectType:
case Object::WorldType:
currentIndex = -1; break;
break;
}
}
if (currentIndex == -1) {
if (isMap)
currentIndex = MapTab;
else if (isTileset)
currentIndex = TilesetTab;
}
if (currentIndex != -1 && !mTabBar->isTabVisible(currentIndex))
currentIndex = -1;
mTabBar->setCurrentIndex(currentIndex);
}

void PropertiesDock::tabChanged(int index)
{
switch (static_cast<TabType>(index)) {
case MapTab: {
if (auto mapDocument = qobject_cast<MapDocument *>(mDocument))
mapDocument->setCurrentObject(mapDocument->map());
break;
}
case LayerTab:
if (auto mapDocument = qobject_cast<MapDocument *>(mDocument)) {
if (auto layer = mapDocument->currentLayer())
mapDocument->setCurrentObject(layer);
}
break;
case MapObjectTab:
// todo: keep track of last focused object
if (auto mapDocument = qobject_cast<MapDocument *>(mDocument)) {
auto selectedObjects = mapDocument->selectedObjects();
if (!selectedObjects.isEmpty())
mapDocument->setCurrentObject(selectedObjects.first());
}
break;
case TilesetTab: {
if (auto tilesetDocument = qobject_cast<TilesetDocument *>(mDocument))
tilesetDocument->setCurrentObject(tilesetDocument->tileset().data());
break;
}
case TileTab:
// todo: keep track of last focused tile
if (auto tilesetDocument = qobject_cast<TilesetDocument *>(mDocument)) {
auto selectedTiles = tilesetDocument->selectedTiles();
if (!selectedTiles.isEmpty())
tilesetDocument->setCurrentObject(selectedTiles.first());
}
case WangSetTab:
case WangColorTab:
// todo: keep track of selected wang set and color
break;
}
}

} // namespace Tiled
Expand Down
17 changes: 17 additions & 0 deletions src/tiled/propertiesdock.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <QDockWidget>

class QTabBar;

namespace Tiled {

class Document;
Expand All @@ -47,10 +49,25 @@ public slots:
bool event(QEvent *event) override;

private:
enum TabType {
MapTab,
LayerTab,
MapObjectTab,
TilesetTab,
TileTab,
WangSetTab,
WangColorTab,
};

void bringToFront();
void retranslateUi();

void updateTabs();
void tabChanged(int index);

QTabBar *mTabBar;
PropertiesWidget *mPropertiesWidget;
Document *mDocument = nullptr;
};

} // namespace Tiled
Loading