- const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
-
- // Restrict tooltip size to current screen size when needed.
- // Because layout controlling widget doesn't respect widget's maximumSize property
- // (correct me if I'm wrong), we need to let layout do its work, then manually change
- // geometry if resulting widget doesn't fit the screen.
-
- // Step #1 - make sizeHint return calculated tooltip size
- m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetFixedSize);
- m_fileMetaDataToolTip->adjustSize();
- QSize size = m_fileMetaDataToolTip->sizeHint();
-
- // Step #2 - correct tooltip size when needed
- if (size.width() > screen.width()) {
- size.setWidth(screen.width());
- }
- if (size.height() > screen.height()) {
- size.setHeight(screen.height());
- }
-
- // m_itemRect defines the area of the item, where the tooltip should be
- // shown. Per default the tooltip is shown in the bottom right corner.
- // It must be assured that:
- // - the content is fully visible
- // - the content is not drawn inside m_itemRect
- const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= screen.left());
- const bool hasRoomToRight = (m_itemRect.right() + size.width() <= screen.right());
- const bool hasRoomAbove = (m_itemRect.top() - size.height() >= screen.top());
- const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= screen.bottom());
- if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
- return;
- }
-
- int x, y;
- if (hasRoomBelow || hasRoomAbove) {
- x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
- if (x + size.width() >= screen.right()) {
- x = screen.right() - size.width() + 1;
- }
- if (hasRoomBelow) {
- y = m_itemRect.bottom() + 1;
- } else {
- y = m_itemRect.top() - size.height();
- }
- } else {
- Q_ASSERT(hasRoomToLeft || hasRoomToRight);
- if (hasRoomToRight) {
- x = m_itemRect.right() + 1;
- } else {
- x = m_itemRect.left() - size.width();
- }
- // Put the tooltip at the bottom of the screen. The x-coordinate has already
- // been adjusted, so that no overlapping with m_itemRect occurs.
- y = screen.bottom() - size.height() + 1;