Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/manual/keyboard-shortcuts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ General
- ``Ctrl + O`` - Open any file or project
- ``Ctrl + P`` - Open a file in the current project
- ``Ctrl + Shift + P`` - Search for available actions
- ``Ctrl + Shift + G`` - Jump to tile coordinates
- ``Ctrl + Shift + T`` - Reopen a recently closed file
- ``Ctrl + S`` - Save current document
- ``Ctrl + Shift + S`` - Save current document to another file
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/libtilededitor.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ DynamicLibrary {
"layeroffsettool.h",
"locatorwidget.cpp",
"locatorwidget.h",
"tilelocator.cpp",
"tilelocator.h",
"magicwandtool.h",
"magicwandtool.cpp",
"maintoolbar.cpp",
Expand Down
8 changes: 8 additions & 0 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "issuesdock.h"
#include "layer.h"
#include "locatorwidget.h"
#include "tilelocator.h"
#include "map.h"
#include "mapdocument.h"
#include "mapdocumentactionhandler.h"
Expand Down Expand Up @@ -289,6 +290,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
ActionManager::registerAction(mUi->actionOpen, "Open");
ActionManager::registerAction(mUi->actionOpenFileInProject, "OpenFileInProject");
ActionManager::registerAction(mUi->actionSearchActions, "SearchActions");
ActionManager::registerAction(mUi->actionJumpToTile, "JumpToTile");
ActionManager::registerAction(mUi->actionPaste, "Paste");
ActionManager::registerAction(mUi->actionPasteInPlace, "PasteInPlace");
ActionManager::registerAction(mUi->actionPreferences, "Preferences");
Expand Down Expand Up @@ -530,6 +532,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
connect(mUi->actionOpen, &QAction::triggered, this, &MainWindow::openFileDialog);
connect(mUi->actionOpenFileInProject, &QAction::triggered, this, &MainWindow::openFileInProject);
connect(mUi->actionSearchActions, &QAction::triggered, this, &MainWindow::searchActions);
connect(mUi->actionJumpToTile, &QAction::triggered, this, &MainWindow::jumpToTile);
connect(mUi->actionReopenClosedFile, &QAction::triggered, this, &MainWindow::reopenClosedFile);
connect(mUi->actionClearRecentFiles, &QAction::triggered, preferences, &Preferences::clearRecentFiles);
connect(mUi->actionSave, &QAction::triggered, this, &MainWindow::saveFile);
Expand Down Expand Up @@ -1146,6 +1149,11 @@ void MainWindow::searchActions()
showLocatorWidget(new ActionLocatorSource);
}

void MainWindow::jumpToTile()
{
showLocatorWidget(new TileLocatorSource);
}

void MainWindow::showLocatorWidget(LocatorSource *source)
{
if (mLocatorWidget)
Expand Down
1 change: 1 addition & 0 deletions src/tiled/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class TILED_EDITOR_EXPORT MainWindow : public QMainWindow
void openFileDialog();
void openFileInProject();
void searchActions();
void jumpToTile();
void showLocatorWidget(LocatorSource *source);
bool saveFile();
bool saveFileAs();
Expand Down
12 changes: 12 additions & 0 deletions src/tiled/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
<addaction name="actionSnapToPixels"/>
</widget>
<addaction name="actionSearchActions"/>
<addaction name="actionJumpToTile"/>
<addaction name="actionShowGrid"/>
<addaction name="actionShowTileObjectOutlines"/>
<addaction name="actionShowObjectReferences"/>
Expand Down Expand Up @@ -803,6 +804,17 @@
<string notr="true">Ctrl+Shift+P</string>
</property>
</action>
<action name="actionJumpToTile">
<property name="text">
<string>Jump to Tile...</string>
</property>
<property name="toolTip">
<string>Jump to a specific tile coordinate</string>
</property>
<property name="shortcut">
<string notr="true">Ctrl+Shift+G</string>
</property>
</action>
<action name="actionUnloadAllWorlds">
<property name="text">
<string>Unload All Worlds</string>
Expand Down
310 changes: 310 additions & 0 deletions src/tiled/tilelocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
/*
* tilelocator.cpp
* Copyright 2025, Siraj Ahmadzai
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "tilelocator.h"

#include "documentmanager.h"
#include "mapdocument.h"
#include "mapview.h"
#include "maprenderer.h"
#include "layer.h"
#include "utils.h"
#include "locatorwidget.h"

#include <QApplication>
#include <QPainter>
#include <QRegularExpression>
#include <QStyledItemDelegate>
#include <QTreeView>
#include <QStaticText>
#include <QTextOption>

namespace Tiled {

///////////////////////////////////////////////////////////////////////////////

class TileMatchDelegate : public QStyledItemDelegate
{
public:
explicit TileMatchDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent)
{}

void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override
{
painter->save();

const QString displayText = index.data().toString();
const QString description = index.data(Qt::UserRole).toString();

const Fonts fonts(option.font);
const QFontMetrics bigFontMetrics(fonts.big);

const int margin = Utils::dpiScaled(2);
const auto displayRect = option.rect.adjusted(margin, margin, -margin, 0);
const auto descriptionRect = option.rect.adjusted(margin, margin + bigFontMetrics.lineSpacing(), -margin, 0);

// draw the background (covers selection)
QStyle *style = QApplication::style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);

// adjust text color to state
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
cg = QPalette::Inactive;
if (option.state & QStyle::State_Selected) {
painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setPen(option.palette.color(cg, QPalette::Text));
}

QTextOption textOption;
textOption.setWrapMode(QTextOption::NoWrap);

QStaticText staticText(displayText);
staticText.setTextOption(textOption);

painter->setFont(fonts.big);
painter->drawStaticText(displayRect.topLeft(), staticText);

if (!description.isEmpty()) {
staticText.setText(description);
painter->setOpacity(0.75);
painter->setFont(fonts.small);
painter->drawStaticText(descriptionRect.topLeft(), staticText);
}

// draw the focus rect
if (option.state & QStyle::State_HasFocus) {
QStyleOptionFocusRect focusOption;
focusOption.initFrom(option.widget);
focusOption.rect = option.rect;
focusOption.state = option.state;
style->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOption, painter);
}

painter->restore();
}

QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override
{
const Fonts fonts(option.font);
const QFontMetrics bigFontMetrics(fonts.big);
const QFontMetrics smallFontMetrics(fonts.small);

const QString displayText = index.data().toString();
const QString description = index.data(Qt::UserRole).toString();

const int margin = Utils::dpiScaled(2);
const int height = margin * 2 + bigFontMetrics.lineSpacing();

if (!description.isEmpty())
return QSize(option.rect.width(), height + smallFontMetrics.lineSpacing());

return QSize(option.rect.width(), height);
}

private:
class Fonts {
public:
Fonts(const QFont &base)
: small(scaledFont(base, 0.9))
, big(scaledFont(base, 1.1))
{}

const QFont small;
const QFont big;
};

static QFont scaledFont(const QFont &font, qreal scale)
{
QFont scaled(font);
if (font.pixelSize() > 0)
scaled.setPixelSize(font.pixelSize() * scale);
else
scaled.setPointSizeF(font.pointSizeF() * scale);
return scaled;
}
};

///////////////////////////////////////////////////////////////////////////////

TileLocatorSource::TileLocatorSource(QObject *parent)
: LocatorSource(parent)
, mDelegate(new TileMatchDelegate(this))
{}

int TileLocatorSource::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : mMatches.size();
}

QVariant TileLocatorSource::data(const QModelIndex &index, int role) const
{
switch (role) {
case Qt::DisplayRole: {
const Match &match = mMatches.at(index.row());
return match.displayText;
}
case Qt::UserRole: {
const Match &match = mMatches.at(index.row());
return match.description;
}
}
return QVariant();
}

QAbstractItemDelegate *TileLocatorSource::delegate() const
{
return mDelegate;
}

QString TileLocatorSource::placeholderText() const
{
return QCoreApplication::translate("Tiled::LocatorWidget", "Enter tile coordinates (e.g., 10,20 or x:10 y:20)");
}

void TileLocatorSource::activate(const QModelIndex &index)
{
const Match &match = mMatches.at(index.row());

auto documentManager = DocumentManager::instance();
auto mapDocument = qobject_cast<MapDocument*>(documentManager->currentDocument());

if (!mapDocument) {
// Try to find any open map document
const auto documents = documentManager->documents();
for (auto document : documents) {
if (auto mapDoc = qobject_cast<MapDocument*>(document.data())) {
mapDocument = mapDoc;
break;
}
}
Comment on lines +196 to +203
Copy link

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The fallback logic for finding a map document when no current document exists could be extracted into a helper method to improve readability.

Suggested change
// Try to find any open map document
const auto documents = documentManager->documents();
for (auto document : documents) {
if (auto mapDoc = qobject_cast<MapDocument*>(document.data())) {
mapDocument = mapDoc;
break;
}
}
mapDocument = findFallbackMapDocument(documentManager);

Copilot uses AI. Check for mistakes.

}

if (!mapDocument)
return;

auto mapView = documentManager->viewForDocument(mapDocument);

if (!mapView)
return;

// Convert tile coordinates to screen coordinates
auto renderer = mapDocument->renderer();
auto screenPos = renderer->tileToScreenCoords(match.tilePos);

// Center the view on the specified tile position
mapView->forceCenterOn(screenPos);
}

void TileLocatorSource::setFilterWords(const QStringList &words)
{
auto matches = parseCoordinates(words);

beginResetModel();
mMatches = std::move(matches);
endResetModel();
}

QVector<TileLocatorSource::Match> TileLocatorSource::parseCoordinates(const QStringList &words)
{
QVector<Match> result;

if (words.isEmpty())
return result;

// Join all words to handle cases like "10,20" or "x:10 y:20"
QString input = words.join(QStringLiteral(" "));

QPoint tilePos;
if (parseCoordinate(input, tilePos)) {
Match match;
match.tilePos = tilePos;
match.displayText = QCoreApplication::translate("Tiled::TileLocatorSource", "Jump to tile (%1, %2)")
.arg(tilePos.x()).arg(tilePos.y());
match.description = QCoreApplication::translate("Tiled::TileLocatorSource", "Press Enter to jump to this tile");
result.append(match);
}

return result;
}

bool TileLocatorSource::parseCoordinate(const QString &text, QPoint &tilePos)
{
// Try different coordinate formats

// Format 1: "x,y" (e.g., "10,20")
QRegularExpression commaPattern(QStringLiteral(R"((\d+)\s*,\s*(\d+))"));
auto match = commaPattern.match(text);
if (match.hasMatch()) {
tilePos.setX(match.captured(1).toInt());
tilePos.setY(match.captured(2).toInt());
return true;
}

// Format 2: "x:10 y:20" or "x:10, y:20"
Copy link

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regular expression pattern is complex and hard to understand. Consider breaking it into smaller, more readable patterns or adding a comment explaining the expected format.

Suggested change
// Format 2: "x:10 y:20" or "x:10, y:20"
// Format 2: "x:10 y:20" or "x:10, y:20"
// Matches formats like "x:10 y:20" or "x:10, y:20".
// - "x:10" captures the x-coordinate.
// - "y:20" (optional) captures the y-coordinate, separated by a comma or whitespace.

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjorn this is not a regular expression, it is a comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it is definitely talking about a regular expression, it just placed its comment off by one line. It appears to apply to the line below:

QRegularExpression xyPattern(QStringLiteral(R"(x\s*:\s*(\d+)(?:\s*[,\s]\s*y\s*:\s*(\d+))?)"));

QRegularExpression xyPattern(QStringLiteral(R"(x\s*:\s*(\d+)(?:\s*[,\s]\s*y\s*:\s*(\d+))?)"));
match = xyPattern.match(text);
if (match.hasMatch()) {
tilePos.setX(match.captured(1).toInt());
if (match.captured(2).isEmpty()) {
// Only x coordinate provided, try to find y in the rest of the text
QString remaining = text.mid(match.capturedEnd());
QRegularExpression yPattern(QStringLiteral(R"(y\s*:\s*(\d+))"));
auto yMatch = yPattern.match(remaining);
if (yMatch.hasMatch()) {
tilePos.setY(yMatch.captured(1).toInt());
Comment on lines +274 to +278
Copy link

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The nested logic for handling partial x:y coordinate matches is complex and could be simplified. Consider extracting this into a separate helper method.

Suggested change
QString remaining = text.mid(match.capturedEnd());
QRegularExpression yPattern(QStringLiteral(R"(y\s*:\s*(\d+))"));
auto yMatch = yPattern.match(remaining);
if (yMatch.hasMatch()) {
tilePos.setY(yMatch.captured(1).toInt());
if (parseYCoordinateFromRemainingText(text.mid(match.capturedEnd()), tilePos)) {

Copilot uses AI. Check for mistakes.

return true;
}
} else {
tilePos.setY(match.captured(2).toInt());
return true;
}
}

// Format 3: Just two numbers separated by space (e.g., "10 20")
QRegularExpression spacePattern(QStringLiteral(R"((\d+)\s+(\d+))"));
match = spacePattern.match(text);
if (match.hasMatch()) {
tilePos.setX(match.captured(1).toInt());
tilePos.setY(match.captured(2).toInt());
return true;
}

// Format 4: Single number (assume it's x coordinate)
QRegularExpression singlePattern(QStringLiteral(R"((\d+))"));
match = singlePattern.match(text);
if (match.hasMatch()) {
tilePos.setX(match.captured(1).toInt());
tilePos.setY(0); // Default y to 0
return true;
}

return false;
}

} // namespace Tiled

#include "moc_tilelocator.cpp"
Loading
Loading