File indexing completed on 2024-04-28 04:21:23

0001 // SPDX-FileCopyrightText: 2003-2019 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #include "ThumbnailToolTip.h"
0007 
0008 #include "ThumbnailWidget.h"
0009 
0010 #include <DB/ImageDB.h>
0011 #include <DB/ImageInfo.h>
0012 #include <Utilities/FileUtil.h>
0013 #include <kpabase/SettingsData.h>
0014 
0015 #include <QApplication>
0016 #include <QCursor>
0017 #include <QDesktopWidget>
0018 #include <QScreen>
0019 
0020 /**
0021    \class ThumbnailToolTip
0022    This class takes care of showing tooltips for the individual items in the thumbnail view.
0023    I tried implementing this with QToolTip::maybeTip() on the iconview, but it had the
0024    disadvantages that either the tooltip would not follow the
0025    mouse( and would therefore stand on top of the image), or it flickered.
0026 */
0027 
0028 ThumbnailView::ThumbnailToolTip::ThumbnailToolTip(ThumbnailWidget *view)
0029     : Utilities::ToolTip(view, Qt::FramelessWindowHint | Qt::Window | Qt::X11BypassWindowManagerHint | Qt::Tool)
0030     , m_view(view)
0031     , m_widthInverse(false)
0032     , m_heightInverse(false)
0033 {
0034 }
0035 
0036 bool ThumbnailView::ThumbnailToolTip::eventFilter(QObject *o, QEvent *event)
0037 {
0038     if (o == m_view->viewport() && event->type() == QEvent::Leave)
0039         hide();
0040 
0041     else if (event->type() == QEvent::MouseMove || event->type() == QEvent::Wheel) {
0042         // We need this to be done through a timer, so the thumbnail view gets the wheel even first,
0043         // otherwise the fileName reported by mediaIdUnderCursor is wrong.
0044         QTimer::singleShot(0, this, SLOT(requestToolTip()));
0045     }
0046 
0047     return false;
0048 }
0049 
0050 void ThumbnailView::ThumbnailToolTip::requestToolTip()
0051 {
0052     const DB::FileName fileName = m_view->mediaIdUnderCursor();
0053     ToolTip::requestToolTip(fileName);
0054 }
0055 
0056 void ThumbnailView::ThumbnailToolTip::setActive(bool b)
0057 {
0058     if (b) {
0059         requestToolTip();
0060         m_view->viewport()->installEventFilter(this);
0061     } else {
0062         m_view->viewport()->removeEventFilter(this);
0063         hide();
0064     }
0065 }
0066 
0067 void ThumbnailView::ThumbnailToolTip::placeWindow()
0068 {
0069     // First try to set the position.
0070     QPoint pos = QCursor::pos() + QPoint(20, 20);
0071     if (m_widthInverse)
0072         pos.setX(pos.x() - 30 - width());
0073     if (m_heightInverse)
0074         pos.setY(pos.y() - 30 - height());
0075 
0076     QScreen *screen = qApp->screenAt(QCursor::pos());
0077     if (!screen)
0078         return;
0079     QRect geom = screen->geometry();
0080 
0081     // Now test whether the window moved outside the screen
0082     if (m_widthInverse) {
0083         if (pos.x() < geom.x()) {
0084             pos.setX(QCursor::pos().x() + 20);
0085             m_widthInverse = false;
0086         }
0087     } else {
0088         if (pos.x() + width() > geom.right()) {
0089             pos.setX(QCursor::pos().x() - width());
0090             m_widthInverse = true;
0091         }
0092     }
0093 
0094     if (m_heightInverse) {
0095         if (pos.y() < geom.y()) {
0096             pos.setY(QCursor::pos().y() + 10);
0097             m_heightInverse = false;
0098         }
0099     } else {
0100         if (pos.y() + height() > geom.bottom()) {
0101             pos.setY(QCursor::pos().y() - 10 - height());
0102             m_heightInverse = true;
0103         }
0104     }
0105 
0106     move(pos);
0107 }
0108 
0109 // vi:expandtab:tabstop=4 shiftwidth=4:
0110 
0111 #include "moc_ThumbnailToolTip.cpp"