From 0da36b3dd6e23bbfe19a3bd9a96f20f83d4dd1bb 2012-03-09 11:13:41 From: Puneeth Chaganti Date: 2012-03-09 11:13:41 Subject: [PATCH] BUG: Improve placement of CallTipWidget Only the vertical placement of the CallTipWidget was being checked. Now add checks for horizontal placement as well. --- diff --git a/IPython/frontend/qt/console/call_tip_widget.py b/IPython/frontend/qt/console/call_tip_widget.py index 6c2857f..c918737 100644 --- a/IPython/frontend/qt/console/call_tip_widget.py +++ b/IPython/frontend/qt/console/call_tip_widget.py @@ -157,17 +157,49 @@ class CallTipWidget(QtGui.QLabel): self.resize(self.sizeHint()) # Locate and show the widget. Place the tip below the current line - # unless it would be off the screen. In that case, place it above - # the current line. + # unless it would be off the screen. In that case, decide the best + # location based trying to minimize the area that goes off-screen. padding = 3 # Distance in pixels between cursor bounds and tip box. cursor_rect = text_edit.cursorRect(cursor) screen_rect = QtGui.qApp.desktop().screenGeometry(text_edit) point = text_edit.mapToGlobal(cursor_rect.bottomRight()) point.setY(point.y() + padding) tip_height = self.size().height() + tip_width = self.size().width() + + vertical = 'bottom' + horizontal = 'Right' if point.y() + tip_height > screen_rect.height(): - point = text_edit.mapToGlobal(cursor_rect.topRight()) + point_ = text_edit.mapToGlobal(cursor_rect.topRight()) + # If tip is still off screen, check if point is in top or bottom + # half of screen. + if point_.y() - tip_height < padding: + # If point is in upper half of screen, show tip below it. + # otherwise above it. + if 2*point.y() < screen_rect.height(): + vertical = 'bottom' + else: + vertical = 'top' + else: + vertical = 'top' + if point.x() + tip_width > screen_rect.width(): + point_ = text_edit.mapToGlobal(cursor_rect.topRight()) + # If tip is still off-screen, check if point is in the right or + # left half of the screen. + if point_.x() - tip_width < padding: + if 2*point.x() < screen_rect.width(): + horizontal = 'Right' + else: + horizontal = 'Left' + else: + horizontal = 'Left' + pos = getattr(cursor_rect, '%s%s' %(vertical, horizontal)) + point = text_edit.mapToGlobal(pos()) + if vertical == 'top': point.setY(point.y() - tip_height - padding) + if horizontal == 'Left': + point.setX(point.x() - tip_width - padding) + self.move(point) self.show() return True