Skip to content

Commit

Permalink
Merge branch 'display_fix' into 'master'
Browse files Browse the repository at this point in the history
Improve screen detection for editor's widgets

Closes #8146

See merge request OpenMW/openmw!4364
  • Loading branch information
Assumeru committed Oct 29, 2024
2 parents 1349fdb + 8dc16f0 commit 4c11dcd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
35 changes: 27 additions & 8 deletions apps/opencs/view/doc/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <components/files/conversion.hpp>
#include <components/misc/helpviewer.hpp>
#include <components/misc/scalableicon.hpp>
#include <components/misc/strings/format.hpp>
#include <components/misc/timeconvert.hpp>
#include <components/version/version.hpp>

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions apps/opencs/view/doc/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 3 additions & 7 deletions apps/opencs/view/world/tablesubview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4c11dcd

Please sign in to comment.