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) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de>
0004  * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "ktooltipwindow_p.h"
0022 
0023 #include <kcolorscheme.h>
0024 #include <kwindowsystem.h>
0025 
0026 #include <QPainter>
0027 #include <QPainterPath>
0028 #include <QVBoxLayout>
0029 
0030 KToolTipWindow::KToolTipWindow(QWidget *content) :
0031     QWidget(0)
0032 {
0033     setAttribute(Qt::WA_TranslucentBackground);
0034     setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
0035 
0036     QVBoxLayout* layout = new QVBoxLayout(this);
0037     layout->addWidget(content);
0038     adjustSize();
0039 
0040     QPalette p = palette();
0041     p.setColor(QPalette::WindowText, p.color(QPalette::ToolTipText));
0042     setPalette(p);
0043 }
0044 
0045 KToolTipWindow::~KToolTipWindow()
0046 {
0047 }
0048 
0049 void KToolTipWindow::paintEvent(QPaintEvent *event)
0050 {
0051     Q_UNUSED(event);
0052 
0053     QPainter painter(this);
0054 
0055     QColor toColor = palette().brush(QPalette::ToolTipBase).color();
0056     QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
0057 
0058     const bool haveAlphaChannel = KWindowSystem::compositingActive();
0059     if (haveAlphaChannel) {
0060         painter.setRenderHint(QPainter::Antialiasing);
0061         painter.translate(0.5, 0.5);
0062         toColor.setAlpha(220);
0063         fromColor.setAlpha(220);
0064     }
0065 
0066     QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(0.0, height()));
0067     gradient.setColorAt(0.0, fromColor);
0068     gradient.setColorAt(1.0, toColor);
0069     painter.setPen(Qt::NoPen);
0070     painter.setBrush(gradient);
0071 
0072     const QRect rect(0, 0, width(), height());
0073     if (haveAlphaChannel) {
0074         const qreal radius = 5.0;
0075 
0076         QPainterPath path;
0077         path.moveTo(rect.left(), rect.top() + radius);
0078         arc(path, rect.left()  + radius, rect.top()    + radius, radius, 180, -90);
0079         arc(path, rect.right() - radius, rect.top()    + radius, radius,  90, -90);
0080         arc(path, rect.right() - radius, rect.bottom() - radius, radius,   0, -90);
0081         arc(path, rect.left()  + radius, rect.bottom() - radius, radius, 270, -90);
0082         path.closeSubpath();
0083 
0084         painter.drawPath(path);
0085     } else {
0086         painter.drawRect(rect);
0087     }
0088 }
0089 
0090 void KToolTipWindow::arc(QPainterPath &path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength)
0091 {
0092     path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength);
0093 }