diff --git a/apps/opencs/view/doc/view.cpp b/apps/opencs/view/doc/view.cpp index f6ef02ad6d..7ee8092df6 100644 --- a/apps/opencs/view/doc/view.cpp +++ b/apps/opencs/view/doc/view.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -1112,14 +1113,8 @@ void CSVDoc::View::updateWidth(bool isGrowLimit, int minSubViewWidth) QRect rect; if (isGrowLimit) { - // Widget position can be negative, we should clamp it. - QPoint position = pos(); - if (position.x() <= 0) - position.setX(0); - if (position.y() <= 0) - position.setY(0); - - rect = QApplication::screenAt(position)->geometry(); + QScreen* screen = getWidgetScreen(pos()); + rect = screen->geometry(); } else rect = desktopRect(); @@ -1165,3 +1160,27 @@ void CSVDoc::View::onRequestFocus(const std::string& id) addSubView(CSMWorld::UniversalId(CSMWorld::UniversalId::Type_Reference, id)); } } + +QScreen* CSVDoc::View::getWidgetScreen(const QPoint& position) +{ + QScreen* screen = QApplication::screenAt(position); + if (screen == nullptr) + { + QPoint clampedPosition = position; + + // 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 (screen == nullptr) + throw std::runtime_error( + Misc::StringUtils::format("Can not detect the screen for position [%d, %d]", position.x(), position.y())); + + return screen; +} diff --git a/apps/opencs/view/doc/view.hpp b/apps/opencs/view/doc/view.hpp index 165cb0da8e..bdf051a80b 100644 --- a/apps/opencs/view/doc/view.hpp +++ b/apps/opencs/view/doc/view.hpp @@ -108,6 +108,8 @@ namespace CSVDoc View& operator=(const View&) = delete; ~View() override = default; + static QScreen* getWidgetScreen(const QPoint& position); + const CSMDoc::Document* getDocument() const; CSMDoc::Document* getDocument(); diff --git a/apps/opencs/view/world/tablesubview.cpp b/apps/opencs/view/world/tablesubview.cpp index e7701ee778..d2bf425ddd 100644 --- a/apps/opencs/view/world/tablesubview.cpp +++ b/apps/opencs/view/world/tablesubview.cpp @@ -23,6 +23,7 @@ #include "../../model/world/tablemimedata.hpp" #include "../doc/sizehint.hpp" +#include "../doc/view.hpp" #include "../filter/filterbox.hpp" #include "../filter/filterdata.hpp" #include "table.hpp" @@ -81,15 +82,10 @@ CSVWorld::TableSubView::TableSubView( setWidget(widget); - // Widget position can be negative, we should clamp it. - QPoint position = pos(); - if (position.x() <= 0) - position.setX(0); - if (position.y() <= 0) - position.setY(0); + QScreen* screen = CSVDoc::View::getWidgetScreen(pos()); // prefer height of the screen and full width of the table - const QRect rect = QApplication::screenAt(position)->geometry(); + const QRect rect = screen->geometry(); int frameHeight = 40; // set a reasonable default QWidget* topLevel = QApplication::topLevelAt(pos()); if (topLevel)