Consider window frame size within centerWidgetAvoidingOverflow()
This commit is contained in:
parent
3da940fe5b
commit
0c07bdb10c
|
@ -115,6 +115,15 @@ QRect availableScreenGeometryAtPoint(const QPoint &point)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \cond
|
/// \cond
|
||||||
|
static QRect shrinkRectByMargins(QRect rect, const QMargins &margins)
|
||||||
|
{
|
||||||
|
rect.setLeft(rect.left() + margins.left());
|
||||||
|
rect.setTop(rect.top() + margins.top());
|
||||||
|
rect.setRight(rect.right() - margins.right());
|
||||||
|
rect.setBottom(rect.bottom() - margins.bottom());
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
static QRect limitRect(QRect rect, const QRect &bounds)
|
static QRect limitRect(QRect rect, const QRect &bounds)
|
||||||
{
|
{
|
||||||
if (rect.left() < bounds.left()) {
|
if (rect.left() < bounds.left()) {
|
||||||
|
@ -132,6 +141,18 @@ static QRect limitRect(QRect rect, const QRect &bounds)
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QMargins widgetFrame(QWidget *widget, const QMargins &defaultAssumption = QMargins(10, 25, 10, 10))
|
||||||
|
{
|
||||||
|
if (!widget->isWindow()) {
|
||||||
|
return QMargins();
|
||||||
|
}
|
||||||
|
const auto widgetGeometry = widget->geometry();
|
||||||
|
const auto frameGeometry = widget->frameGeometry();
|
||||||
|
const auto frame = QMargins(widgetGeometry.left() - frameGeometry.left(), widgetGeometry.top() - frameGeometry.top(),
|
||||||
|
frameGeometry.right() - widgetGeometry.right(), frameGeometry.bottom() - widgetGeometry.bottom());
|
||||||
|
return frame.isNull() ? defaultAssumption : frame;
|
||||||
|
}
|
||||||
|
|
||||||
static bool centerWidgetInternal(QWidget *widget, const QWidget *parent, const QPoint *position, bool avoidOverflow)
|
static bool centerWidgetInternal(QWidget *widget, const QWidget *parent, const QPoint *position, bool avoidOverflow)
|
||||||
{
|
{
|
||||||
const auto availableGeometry = parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos());
|
const auto availableGeometry = parent ? parent->geometry() : availableScreenGeometryAtPoint(position ? *position : QCursor::pos());
|
||||||
|
@ -140,7 +161,7 @@ static bool centerWidgetInternal(QWidget *widget, const QWidget *parent, const Q
|
||||||
widget->setGeometry(alignedRect);
|
widget->setGeometry(alignedRect);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const auto limitedRect = limitRect(alignedRect, availableGeometry);
|
const auto limitedRect = limitRect(alignedRect, shrinkRectByMargins(availableGeometry, widgetFrame(widget)));
|
||||||
widget->setGeometry(limitedRect);
|
widget->setGeometry(limitedRect);
|
||||||
return alignedRect != limitedRect;
|
return alignedRect != limitedRect;
|
||||||
}
|
}
|
||||||
|
@ -160,10 +181,10 @@ void centerWidget(QWidget *widget, const QWidget *parent, const QPoint *position
|
||||||
* \brief Moves the specified \a widget to be centered within the (available) screen area or \a parent if specified.
|
* \brief Moves the specified \a widget to be centered within the (available) screen area or \a parent if specified.
|
||||||
* \returns Returns whether an overflow occurred.
|
* \returns Returns whether an overflow occurred.
|
||||||
* \remarks
|
* \remarks
|
||||||
* - If the widget overflows it is resized to take the whole available space in the dimention(s) that overflow. Note that
|
* - If the widget overflows it is resized to take the whole available space in the dimention(s) that overflow.
|
||||||
* this does *not* take the window frame into account. So if \a widget is a window it makes sense to show it using
|
* If the widget is a window, its frame is attempted to be taken into account. If the window frame can not be determined
|
||||||
* QWidget::showMaximized() to make it fill the entire screen to avoid clipped window frames. It makes also sense to
|
* a generous assumption is made. It can nevertheless make sense to simply show \a widget using QWidget::showMaximized()
|
||||||
* assign a smaller size to avoid clipped window frames once the window is "de-maximized" again.
|
* to make it simply fill the entire screen after all.
|
||||||
* - The screen containing the current cursor position is used unless \a position is specified.
|
* - The screen containing the current cursor position is used unless \a position is specified.
|
||||||
*/
|
*/
|
||||||
bool centerWidgetAvoidingOverflow(QWidget *widget, const QWidget *parent, const QPoint *position)
|
bool centerWidgetAvoidingOverflow(QWidget *widget, const QWidget *parent, const QPoint *position)
|
||||||
|
|
Loading…
Reference in New Issue