Skip to content

Commit

Permalink
Try to use visual rect and row index
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Feb 11, 2025
1 parent 5394c3b commit 31a179e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
27 changes: 10 additions & 17 deletions src/tiled/tilesetmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ int TilesetModel::rowCount(const QModelIndex &parent) const
if (parent.isValid())
return 0;

if (tileset()->isAtlas()) {
// Atlas mode: single row containing whole tileset
if (tileset()->isAtlas())
return 1;
}

const int tileCount = mTileIds.size();
const int columns = columnCount();
Expand All @@ -71,12 +69,10 @@ int TilesetModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
if (tileset()->isAtlas()) {
// Atlas mode: single column containing whole tileset
return 1;
}
if (mColumnCountOverride > 0)
return mColumnCountOverride;
if (tileset()->isAtlas())
return 1;
if (tileset()->columnCount())
return tileset()->columnCount();
// TODO: Non-table tilesets should use a different model.
Expand Down Expand Up @@ -210,13 +206,10 @@ bool TilesetModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
Tile *TilesetModel::tileAt(const QModelIndex &index) const
{
if (tileset()->isAtlas()) {
// Convert index coordinates to tileset position
const QPoint pos(index.column() - 1, index.row() - 1);

// Find tile that contains this position
for (Tile *tile : tileset()->tiles()) {
if (tile->imageRect().contains(pos))
return tile;
const int tileIndex = index.column() - 1;
if (tileIndex < mTileIds.size() && tileIndex >= 0) {
const int tileId = mTileIds.at(tileIndex);
return tileset()->findTile(tileId);
}
return nullptr;
}
Expand All @@ -236,9 +229,9 @@ QModelIndex TilesetModel::tileIndex(const Tile *tile) const
{
Q_ASSERT(tile->tileset() == tileset());
if (tileset()->isAtlas()) {
// For atlas mode, create index from tile's position in the image
const QRect rect = tile->imageRect();
return index(rect.y() + 1, rect.x() + 1);
const int tileIndex = mTileIds.indexOf(tile->id());
Q_ASSERT(tileIndex != -1);
return index(0, tileIndex + 1);
}

const int columnCount = TilesetModel::columnCount();
Expand Down
28 changes: 22 additions & 6 deletions src/tiled/tilesetview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,12 @@ void TilesetView::mousePressEvent(QMouseEvent *event)
}
}

mAtlasSelecting = true;
mSelectionStart = viewToTile(event->pos());
updateAtlasSelection(mSelectionStart);
event->accept();
viewport()->update();
return;
// mAtlasSelecting = true;
// mSelectionStart = viewToTile(event->pos());
// updateAtlasSelection(mSelectionStart);
// event->accept();
// viewport()->update();
// return;
}
}

Expand Down Expand Up @@ -869,6 +869,22 @@ QModelIndex TilesetView::indexAt(const QPoint &pos) const
return QTableView::indexAt(pos);
}

QRect TilesetView::visualRect(const QModelIndex &index) const
{
if (!index.isValid())
return QRect();

const TilesetModel *m = tilesetModel();
if (m && m->tileset()->isAtlas()) {
if (Tile *tile = m->tileAt(index)) {
return tileToView(tile->imageRect());
}
return QRect();
}

return QTableView::visualRect(index);
}

void TilesetView::leaveEvent(QEvent *event)
{
if (mHoveredIndex.isValid()) {
Expand Down
1 change: 1 addition & 0 deletions src/tiled/tilesetview.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class TilesetView : public QTableView
void resizeEvent(QResizeEvent *event) override;
void paintEvent(QPaintEvent *event) override;
QModelIndex indexAt(const QPoint &pos) const override;
QRect visualRect(const QModelIndex &index) const override;

private:
void onChange(const ChangeEvent &change);
Expand Down

0 comments on commit 31a179e

Please sign in to comment.