1 /*******************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
3 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 *******************************************************************************/
21 #include "kformattedballoontipdelegate.h"
23 #include <QLinearGradient>
24 #include <QTextDocument>
25 #include <kcolorscheme.h>
27 KFormattedBalloonTipDelegate::KFormattedBalloonTipDelegate()
31 KFormattedBalloonTipDelegate::~KFormattedBalloonTipDelegate()
35 QSize
KFormattedBalloonTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
38 doc
.setHtml(item
->text());
39 const QIcon icon
= item
->icon();
41 const QSize iconSize
= icon
.isNull() ? QSize(0, 0) : icon
.actualSize(option
->decorationSize
);
42 const QSize docSize
= doc
.size().toSize();
43 QSize contentSize
= iconSize
+ docSize
;
45 // assure that the content height is large enough for the icon and the document
46 contentSize
.setHeight(iconSize
.height() > doc
.size().height() ? iconSize
.height() : doc
.size().height());
47 return contentSize
+ QSize(Border
* 3, Border
* 2);
50 void KFormattedBalloonTipDelegate::paint(QPainter
*painter
,
51 const KStyleOptionToolTip
*option
,
52 const KToolTipItem
*item
) const
54 QPainterPath path
= createPath(*option
);
55 if (haveAlphaChannel()) {
56 painter
->setRenderHint(QPainter::Antialiasing
);
57 painter
->translate(.5, .5);
60 const QColor toColor
= option
->palette
.brush(QPalette::ToolTipBase
).color();
61 const QColor fromColor
= KColorScheme::shade(toColor
, KColorScheme::LightShade
, 0.2);
63 QLinearGradient
gradient(option
->rect
.topLeft(), option
->rect
.bottomLeft());
64 gradient
.setColorAt(0.0, fromColor
);
65 gradient
.setColorAt(1.0, toColor
);
66 painter
->setPen(Qt::NoPen
);
67 painter
->setBrush(gradient
);
69 painter
->drawPath(path
);
71 const QIcon icon
= item
->icon();
75 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
76 const QPoint
pos(x
+ option
->rect
.x(), y
+ option
->rect
.y());
77 painter
->drawPixmap(pos
, icon
.pixmap(iconSize
));
78 x
+= iconSize
.width() + Border
;
82 doc
.setHtml(item
->text());
83 QPixmap
bitmap(doc
.size().toSize());
84 bitmap
.fill(Qt::transparent
);
88 const QRect
docRect(QPoint(x
, y
), doc
.size().toSize());
89 painter
->drawPixmap(docRect
, bitmap
);
92 QRegion
KFormattedBalloonTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
94 QBitmap
bitmap(option
->rect
.size());
95 bitmap
.fill(Qt::color0
);
98 p
.setPen(QPen(Qt::color1
, 1));
99 p
.setBrush(Qt::color1
);
100 p
.drawPath(createPath(*option
));
102 return QRegion(bitmap
);
105 QRegion
KFormattedBalloonTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
107 return inputShape(option
);
110 static inline void arc(QPainterPath
&path
, qreal cx
, qreal cy
, qreal radius
, qreal angle
, qreal sweeplength
)
112 path
.arcTo(cx
-radius
, cy
-radius
, radius
* 2, radius
* 2, angle
, sweeplength
);
115 QPainterPath
KFormattedBalloonTipDelegate::createPath(const KStyleOptionToolTip
& option
) const
117 const QRect rect
= option
.rect
;
118 const qreal radius
= 5;
121 path
.moveTo(rect
.left(), rect
.top() + radius
);
122 arc(path
, rect
.left() + radius
, rect
.top() + radius
, radius
, 180, -90);
123 arc(path
, rect
.right() - radius
, rect
.top() + radius
, radius
, 90, -90);
124 arc(path
, rect
.right() - radius
, rect
.bottom() - radius
, radius
, 0, -90);
125 arc(path
, rect
.left() + radius
, rect
.bottom() - radius
, radius
, 270, -90);