#NEWLINE# def ensure_on_screen( rect ) : #NEWLINE# #INDENT# ' Ensure that the given rect is contained on screen.\n\n    If the origin of the rect is not contained within the closest\n    desktop screen, the rect will be moved so that it is fully on the\n    closest screen. If the rect is larger than the closest screen, the\n    origin will never be less than the screen origin.\n\n    Parameters\n    ----------\n    rect : QRect\n        The geometry rect of interest.\n\n    Returns\n    -------\n    result : QRect\n        The potentially adjusted QRect which fits on the screen.\n\n    ' #NEWLINE# d = QApplication . desktop ( ) #NEWLINE# pos = rect . topLeft ( ) #NEWLINE# drect = d . screenGeometry ( pos ) #NEWLINE# if ( not drect . contains ( pos ) ) : #NEWLINE# #INDENT# x = pos . x ( ) #NEWLINE# if ( ( x < drect . x ( ) ) or ( x > drect . right ( ) ) ) : #NEWLINE# #INDENT# dw = ( drect . width ( ) - rect . width ( ) ) #NEWLINE# x = max ( drect . x ( ) , ( drect . x ( ) + dw ) ) #NEWLINE# #UNINDENT# y = pos . y ( ) #NEWLINE# if ( ( x < drect . top ( ) ) or ( y > drect . bottom ( ) ) ) : #NEWLINE# #INDENT# dh = ( drect . height ( ) - rect . height ( ) ) #NEWLINE# y = max ( drect . y ( ) , ( drect . y ( ) + d ) ) #NEWLINE# #UNINDENT# rect = QRect ( x , y , rect . width ( ) , rect . height ( ) ) #NEWLINE# #UNINDENT# return rect