1 /**************************************************************************
2  **                                                                      **
3  ** Copyright (C) 2018 Lukas Spies                                       **
4  ** Contact: http://photoqt.org                                          **
5  **                                                                      **
6  ** This file is part of PhotoQt.                                        **
7  **                                                                      **
8  ** PhotoQt is free software: you can redistribute it and/or modify      **
9  ** it under the terms of the GNU General Public License as published by **
10  ** the Free Software Foundation, either version 2 of the License, or    **
11  ** (at your option) any later version.                                  **
12  **                                                                      **
13  ** PhotoQt is distributed in the hope that it will be useful,           **
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of       **
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        **
16  ** GNU General Public License for more details.                         **
17  **                                                                      **
18  ** You should have received a copy of the GNU General Public License    **
19  ** along with PhotoQt. If not, see <http://www.gnu.org/licenses/>.      **
20  **                                                                      **
21  **************************************************************************/
22 
23 #include "tooltip.h"
24 #include <QQuickWindow>
25 #include <QQuickRenderControl>
26 
ToolTip(QObject * parent)27 ToolTip::ToolTip(QObject *parent) : QObject(parent) { }
28 
showText(QQuickItem * item,const QPointF & pos,const QString & str)29 void ToolTip::showText(QQuickItem *item, const QPointF &pos, const QString &str) {
30     QPoint quickWidgetOffsetInTlw;
31     QWindow *renderWindow = QQuickRenderControl::renderWindowFor(item->window(), &quickWidgetOffsetInTlw);
32     QWindow *window = renderWindow ? renderWindow : item->window();
33     const QPoint offsetInQuickWidget = item->mapToScene(pos).toPoint();
34     const QPoint mappedPos = window->mapToGlobal(offsetInQuickWidget + quickWidgetOffsetInTlw);
35     QToolTip::showText(mappedPos, str);
36 }
37 
hideText()38 void ToolTip::hideText() {
39     QToolTip::hideText();
40 }
41 
setBackgroundColor(int r,int g,int b,int a)42 void ToolTip::setBackgroundColor(int r, int g, int b, int a) {
43     _setBackgroundColor(QColor(r,g,b,a));
44 }
45 
setBackgroundColor(QString col)46 void ToolTip::setBackgroundColor(QString col) {
47     _setBackgroundColor(QColor(col));
48 }
49 
setTextColor(int r,int g,int b,int a)50 void ToolTip::setTextColor(int r, int g, int b, int a) {
51     _setTextColor(QColor(r,g,b,a));
52 }
53 
setTextColor(QString col)54 void ToolTip::setTextColor(QString col) {
55     _setTextColor(QColor(col));
56 }
57 
_setTextColor(QColor col)58 void ToolTip::_setTextColor(QColor col) {
59     QPalette pal = QToolTip::palette();
60     pal.setColor(QPalette::ToolTipText, col);
61     QToolTip::setPalette(pal);
62 }
63 
_setBackgroundColor(QColor col)64 void ToolTip::_setBackgroundColor(QColor col) {
65     QPalette pal = QToolTip::palette();
66     pal.setColor(QPalette::ToolTipBase, col);
67     QToolTip::setPalette(pal);
68 }
69