UI: Get correct coordinates for items in Scene Grid Mode

The x() and y() values of coordinates for events inside a scrollable
QWidget are relative to viewport's scrolled origin, the coordinates of
the upper left corner of the visible space, not the widget's true
origin. Since we do not allow horizontal scrolling, the value of x() is
okay. However, the value of y() needs to be adjusted by an offset of the
top()/y() value for the first widget in the SceneTree. When not
scrolled, this offset will be 0. When scrolled down, this offset will be
a negative value.
master
Ryan Foster 2022-08-28 00:19:09 -04:00 committed by Georges Basile Stavracas Neto
parent 5b0ae8c70d
commit 11bc39fe7c
1 changed files with 6 additions and 2 deletions

View File

@ -114,8 +114,10 @@ void SceneTree::dropEvent(QDropEvent *event)
if (gridMode) {
int scrollWid = verticalScrollBar()->sizeHint().width();
const QRect firstItem = visualItemRect(item(0));
const QRect lastItem = visualItemRect(item(count() - 1));
const int h = lastItem.y() + lastItem.height();
const int firstItemY = abs(firstItem.y());
if (h < height()) {
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -133,7 +135,7 @@ void SceneTree::dropEvent(QDropEvent *event)
#endif
int x = (float)point.x() / wid * ceil(wid / maxWidth);
int y = point.y() / itemHeight;
int y = (point.y() + firstItemY) / itemHeight;
int r = x + y * ceil(wid / maxWidth);
@ -156,8 +158,10 @@ void SceneTree::dropEvent(QDropEvent *event)
void SceneTree::RepositionGrid(QDragMoveEvent *event)
{
int scrollWid = verticalScrollBar()->sizeHint().width();
const QRect firstItem = visualItemRect(item(0));
const QRect lastItem = visualItemRect(item(count() - 1));
const int h = lastItem.y() + lastItem.height();
const int firstItemY = abs(firstItem.y());
if (h < height()) {
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -176,7 +180,7 @@ void SceneTree::RepositionGrid(QDragMoveEvent *event)
#endif
int x = (float)point.x() / wid * ceil(wid / maxWidth);
int y = point.y() / itemHeight;
int y = (point.y() + firstItemY) / itemHeight;
int r = x + y * ceil(wid / maxWidth);
int orig = selectedIndexes()[0].row();