File indexing completed on 2022-11-23 11:29:39

0001 /*
0002  * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org>
0003  * Copyright (C) 2011 by Geoffry Song <goffrie@gmail.com>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include "ktooltip.h"
0021 #include "ktooltipwindow_p.h"
0022 
0023 #include <QLabel>
0024 #include <QPointer>
0025 #include <QPoint>
0026 #include <QWidget>
0027 #include <QtGlobal>
0028 
0029 
0030 class KToolTipManager
0031 {
0032 public:
0033     KToolTipManager();
0034     ~KToolTipManager();
0035 
0036     void showTip(const QPoint &pos, QWidget *content);
0037     KToolTipWindow *createTipWindow(QWidget *content);
0038     KToolTipWindow *currentTip();
0039     void hideTip();
0040 
0041 private:
0042     QPointer<KToolTipWindow> m_window;
0043 };
0044 
0045 Q_GLOBAL_STATIC(KToolTipManager, s_instance)
0046 
0047 KToolTipManager::KToolTipManager()
0048 {
0049 }
0050 
0051 KToolTipManager::~KToolTipManager()
0052 {
0053     if (!m_window.isNull()) {
0054         m_window.data()->deleteLater();
0055     }
0056 }
0057 
0058 void KToolTipManager::showTip(const QPoint &pos, QWidget *content)
0059 {
0060     hideTip();
0061     if (!m_window.isNull()) {
0062         delete m_window.data();
0063     }
0064     KToolTipWindow *tooltip = qobject_cast<KToolTipWindow*>(content);
0065     m_window = (tooltip ? tooltip : createTipWindow(content));
0066     m_window.data()->move(pos);
0067     m_window.data()->show();
0068 }
0069 
0070 KToolTipWindow *KToolTipManager::createTipWindow(QWidget* content)
0071 {
0072     return new KToolTipWindow(content);
0073 }
0074 
0075 KToolTipWindow *KToolTipManager::currentTip()
0076 {
0077     return m_window.data();
0078 }
0079 
0080 void KToolTipManager::hideTip()
0081 {
0082     if (!m_window.isNull()) {
0083         m_window.data()->hide();
0084         m_window.data()->deleteLater();
0085     }
0086 }
0087 
0088 namespace KToolTip
0089 {
0090     void showText(const QPoint &pos, const QString &text)
0091     {
0092         QLabel *label = new QLabel(text);
0093         label->setForegroundRole(QPalette::ToolTipText);
0094         showTip(pos, createTipWindow(label));
0095     }
0096 
0097     void showTip(const QPoint &pos, QWidget *content)
0098     {
0099         s_instance->showTip(pos, content);
0100     }
0101 
0102     QWidget *createTipWindow(QWidget *content)
0103     {
0104         return s_instance->createTipWindow(content);
0105     }
0106 
0107     QWidget *currentTip()
0108     {
0109         return s_instance->currentTip();
0110     }
0111 
0112     void hideTip()
0113     {
0114         s_instance->hideTip();
0115     }
0116 }
0117