File indexing completed on 2024-05-12 04:19:51
0001 // vim: set tabstop=4 shiftwidth=4 expandtab: 0002 /* 0003 Gwenview: an image viewer 0004 Copyright 2009 Aurélien Gâteau <agateau@kde.org> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License 0008 as published by the Free Software Foundation; either version 2 0009 of the License, or (at your option) any later version. 0010 0011 This program 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 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; if not, write to the Free Software 0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA. 0019 0020 */ 0021 // Self 0022 #include "tooltipwidget.h" 0023 0024 // Qt 0025 #include <QPainter> 0026 #include <QPainterPath> 0027 0028 // KF 0029 #include <KColorScheme> 0030 0031 // Local 0032 #include "gwenview_lib_debug.h" 0033 #include <lib/paintutils.h> 0034 0035 static const int RADIUS = 5; 0036 static const int HMARGIN = 2; 0037 0038 namespace Gwenview 0039 { 0040 struct ToolTipWidgetPrivate { 0041 QString mText; 0042 qreal mOpacity; 0043 }; 0044 0045 ToolTipWidget::ToolTipWidget(QWidget *parent) 0046 : QWidget(parent) 0047 , d(new ToolTipWidgetPrivate) 0048 { 0049 d->mOpacity = 1.; 0050 setAttribute(Qt::WA_NoSystemBackground); 0051 } 0052 0053 ToolTipWidget::~ToolTipWidget() 0054 { 0055 delete d; 0056 } 0057 0058 qreal ToolTipWidget::opacity() const 0059 { 0060 return d->mOpacity; 0061 } 0062 0063 void ToolTipWidget::setOpacity(qreal opacity) 0064 { 0065 d->mOpacity = opacity; 0066 update(); 0067 } 0068 0069 QString ToolTipWidget::text() const 0070 { 0071 return d->mText; 0072 } 0073 0074 void ToolTipWidget::setText(const QString &text) 0075 { 0076 d->mText = text; 0077 update(); 0078 } 0079 0080 QSize ToolTipWidget::sizeHint() const 0081 { 0082 QSize sh = fontMetrics().size(0 /* flags */, d->mText); 0083 return QSize(sh.width() + 2 * HMARGIN, sh.height()); 0084 } 0085 0086 void ToolTipWidget::paintEvent(QPaintEvent *) 0087 { 0088 QColor bg2Color = palette().color(QPalette::Highlight); 0089 QColor bg1Color = KColorScheme::shade(bg2Color, KColorScheme::LightShade, 0.2); 0090 0091 QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(0.0, height())); 0092 gradient.setColorAt(0.0, bg1Color); 0093 gradient.setColorAt(1.0, bg2Color); 0094 0095 QPainter painter(this); 0096 painter.setRenderHint(QPainter::Antialiasing); 0097 painter.setOpacity(d->mOpacity); 0098 QPainterPath path = PaintUtils::roundedRectangle(rect(), RADIUS); 0099 painter.fillPath(path, gradient); 0100 painter.setPen(palette().color(QPalette::HighlightedText)); 0101 painter.drawText(rect().adjusted(HMARGIN, 0, -HMARGIN, 0), 0 /* flags */, d->mText); 0102 } 0103 0104 } // namespace 0105 0106 #include "moc_tooltipwidget.cpp"