mirror of
https://github.com/OpenMW/openmw.git
synced 2025-03-03 15:09:39 +00:00
Merge branch 'screening' into 'master'
Editor: Fall back to the closest screen when necessary (#8354) Closes #8354 See merge request OpenMW/openmw!4542
This commit is contained in:
commit
5b788baa35
1 changed files with 19 additions and 12 deletions
|
@ -1164,23 +1164,30 @@ void CSVDoc::View::onRequestFocus(const std::string& id)
|
|||
QScreen* CSVDoc::View::getWidgetScreen(const QPoint& position)
|
||||
{
|
||||
QScreen* screen = QApplication::screenAt(position);
|
||||
if (screen == nullptr)
|
||||
if (screen)
|
||||
return screen;
|
||||
|
||||
const QList<QScreen*> screens = QApplication::screens();
|
||||
if (screens.isEmpty())
|
||||
throw std::runtime_error("No screens available");
|
||||
|
||||
int closestDistance = std::numeric_limits<int>::max();
|
||||
for (QScreen* candidate : screens)
|
||||
{
|
||||
QPoint clampedPosition = position;
|
||||
const QRect geometry = candidate->geometry();
|
||||
const int dx = position.x() - std::clamp(position.x(), geometry.left(), geometry.right());
|
||||
const int dy = position.y() - std::clamp(position.y(), geometry.top(), geometry.bottom());
|
||||
const int distance = dx * dx + dy * dy;
|
||||
|
||||
// If we failed to find the screen,
|
||||
// clamp negative positions and try again
|
||||
if (clampedPosition.x() <= 0)
|
||||
clampedPosition.setX(0);
|
||||
if (clampedPosition.y() <= 0)
|
||||
clampedPosition.setY(0);
|
||||
|
||||
screen = QApplication::screenAt(clampedPosition);
|
||||
if (distance < closestDistance)
|
||||
{
|
||||
closestDistance = distance;
|
||||
screen = candidate;
|
||||
}
|
||||
}
|
||||
|
||||
if (screen == nullptr)
|
||||
throw std::runtime_error(
|
||||
Misc::StringUtils::format("Can not detect the screen for position [%d, %d]", position.x(), position.y()));
|
||||
screen = screens.first();
|
||||
|
||||
return screen;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue