]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kballoontipdelegate.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kballoontipdelegate.h"
21 #include <QPainterPath>
25 QSize
KBalloonTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
28 size
.rwidth() = option
->fontMetrics
.width(item
->text());
29 size
.rheight() = option
->fontMetrics
.lineSpacing();
31 QIcon icon
= item
->icon();
33 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
34 size
.rwidth() += iconSize
.width() + 4;
35 size
.rheight() = qMax(size
.height(), iconSize
.height());
38 int margin
= 2 * 10 + (option
->activeCorner
!= KStyleOptionToolTip::NoCorner
? 10 : 0);
39 return size
+ QSize(margin
, margin
);
42 static inline void arc(QPainterPath
&path
, qreal cx
, qreal cy
, qreal radius
, qreal angle
, qreal sweeplength
)
44 path
.arcTo(cx
-radius
, cy
-radius
, radius
* 2, radius
* 2, angle
, sweeplength
);
47 QPainterPath
KBalloonTipDelegate::createPath(const KStyleOptionToolTip
*option
, QRect
*contents
) const
50 QRect rect
= option
->rect
.adjusted(0, 0, -1, -1);
53 switch (option
->activeCorner
)
55 case KStyleOptionToolTip::TopLeftCorner
:
56 rect
.adjust(10, 10, 0, 0);
57 path
.moveTo(option
->rect
.topLeft());
58 path
.lineTo(rect
.left() + radius
, rect
.top());
59 arc(path
, rect
.right() - radius
, rect
.top() + radius
, radius
, 90, -90);
60 arc(path
, rect
.right() - radius
, rect
.bottom() - radius
, radius
, 0, -90);
61 arc(path
, rect
.left() + radius
, rect
.bottom() - radius
, radius
, 270, -90);
62 path
.lineTo(rect
.left(), rect
.top() + radius
);
66 case KStyleOptionToolTip::TopRightCorner
:
67 rect
.adjust(0, 10, -10, 0);
68 path
.moveTo(option
->rect
.topRight());
69 path
.lineTo(rect
.right(), rect
.top() + radius
);
70 arc(path
, rect
.right() - radius
, rect
.bottom() - radius
, radius
, 0, -90);
71 arc(path
, rect
.left() + radius
, rect
.bottom() - radius
, radius
, 270, -90);
72 arc(path
, rect
.left() + radius
, rect
.top() + radius
, radius
, 180, -90);
73 path
.lineTo(rect
.right() - radius
, rect
.top());
77 case KStyleOptionToolTip::BottomLeftCorner
:
78 rect
.adjust(10, 0, 0, -10);
79 path
.moveTo(option
->rect
.bottomLeft());
80 path
.lineTo(rect
.left(), rect
.bottom() - radius
);
81 arc(path
, rect
.left() + radius
, rect
.top() + radius
, radius
, 180, -90);
82 arc(path
, rect
.right() - radius
, rect
.top() + radius
, radius
, 90, -90);
83 arc(path
, rect
.right() - radius
, rect
.bottom() - radius
, radius
, 0, -90);
84 path
.lineTo(rect
.left() + radius
, rect
.bottom());
88 case KStyleOptionToolTip::BottomRightCorner
:
89 rect
.adjust(0, 0, -10, -10);
90 path
.moveTo(option
->rect
.bottomRight());
91 path
.lineTo(rect
.right() - radius
, rect
.bottom());
92 arc(path
, rect
.left() + radius
, rect
.bottom() - radius
, radius
, 270, -90);
93 arc(path
, rect
.left() + radius
, rect
.top() + radius
, radius
, 180, -90);
94 arc(path
, rect
.right() - radius
, rect
.top() + radius
, radius
, 90, -90);
95 path
.lineTo(rect
.right(), rect
.bottom() - radius
);
100 path
.moveTo(rect
.left(), rect
.top() + radius
);
101 arc(path
, rect
.left() + radius
, rect
.top() + radius
, radius
, 180, -90);
102 arc(path
, rect
.right() - radius
, rect
.top() + radius
, radius
, 90, -90);
103 arc(path
, rect
.right() - radius
, rect
.bottom() - radius
, radius
, 0, -90);
104 arc(path
, rect
.left() + radius
, rect
.bottom() - radius
, radius
, 270, -90);
110 *contents
= rect
.adjusted(10, 10, -10, -10);
115 void KBalloonTipDelegate::paint(QPainter
*painter
, const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
118 QPainterPath path
= createPath(option
, &contents
);
119 bool alpha
= haveAlphaChannel();
122 painter
->setRenderHint(QPainter::Antialiasing
);
123 painter
->translate(.5, .5);
126 #if QT_VERSION >= 0x040400
127 painter
->setBrush(option
->palette
.brush(QPalette::ToolTipBase
));
129 painter
->setBrush(option
->palette
.brush(QPalette::Base
));
131 painter
->drawPath(path
);
134 painter
->translate(-.5, -.5);
136 QIcon icon
= item
->icon();
137 if (!icon
.isNull()) {
138 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
139 painter
->drawPixmap(contents
.topLeft(), icon
.pixmap(iconSize
));
140 contents
.adjust(iconSize
.width() + 4, 0, 0, 0);
143 painter
->drawText(contents
, Qt::AlignLeft
| Qt::AlignVCenter
, item
->text());
147 QRegion
KBalloonTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
149 QBitmap
bitmap(option
->rect
.size());
150 bitmap
.fill(Qt::color0
);
153 p
.setPen(QPen(Qt::color1
, 1));
154 p
.setBrush(Qt::color1
);
155 p
.drawPath(createPath(option
));
157 return QRegion(bitmap
);
160 QRegion
KBalloonTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
162 return inputShape(option
);