00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "paneltoolbox_p.h"
00022
00023 #include <QGraphicsSceneHoverEvent>
00024 #include <QPainter>
00025 #include <QRadialGradient>
00026 #include <QApplication>
00027
00028 #include <kcolorscheme.h>
00029 #include <kdebug.h>
00030
00031 #include <plasma/applet.h>
00032 #include <plasma/paintutils.h>
00033 #include <plasma/theme.h>
00034
00035 namespace Plasma
00036 {
00037
00038 class PanelToolBoxPrivate
00039 {
00040 public:
00041 PanelToolBoxPrivate()
00042 : icon("plasma"),
00043 animId(0),
00044 animFrame(0),
00045 toggled(false)
00046 {
00047 }
00048
00049
00050 KIcon icon;
00051 int animId;
00052 qreal animFrame;
00053 bool toggled;
00054 QColor fgColor;
00055 QColor bgColor;
00056 };
00057
00058 PanelToolBox::PanelToolBox(Containment *parent)
00059 : ToolBox(parent),
00060 d(new PanelToolBoxPrivate)
00061 {
00062 connect(this, SIGNAL(toggled()), this, SLOT(toggle()));
00063
00064 setZValue(10000000);
00065 setFlag(ItemClipsToShape, true);
00066 setFlag(ItemClipsChildrenToShape, false);
00067
00068
00069 setFlag(ItemIgnoresTransformations, false);
00070 assignColors();
00071 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
00072 this, SLOT(assignColors()));
00073 }
00074
00075 PanelToolBox::~PanelToolBox()
00076 {
00077 delete d;
00078 }
00079
00080 void PanelToolBox::assignColors()
00081 {
00082 d->bgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
00083 d->fgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
00084 }
00085
00086 QRectF PanelToolBox::boundingRect() const
00087 {
00088 QRectF r;
00089
00090
00091 if (corner() == ToolBox::Bottom) {
00092 r = QRectF(0, 0, size() * 2, size());
00093 } else if (corner() == ToolBox::Left) {
00094 r = QRectF(0, 0, size(), size() * 2);
00095 } else {
00096 r = QRectF(0, 0, size(), size() * 2);
00097 }
00098
00099 if (parentItem()) {
00100 QSizeF s = parentItem()->boundingRect().size();
00101
00102 if (r.height() > s.height()) {
00103 r.setHeight(s.height());
00104 }
00105
00106 if (r.width() > s.width()) {
00107 r.setWidth(s.width());
00108 }
00109 }
00110
00111 return r;
00112 }
00113
00114 void PanelToolBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00115 {
00116 Q_UNUSED(option)
00117 Q_UNUSED(widget)
00118
00119 const qreal progress = d->animFrame / size();
00120
00121 QPoint gradientCenter;
00122 QRectF rect = boundingRect();
00123 if (corner() == ToolBox::Bottom) {
00124 gradientCenter = QPoint(rect.center().x(), rect.bottom());
00125 } else {
00126 gradientCenter = QPoint(rect.right(), rect.center().y());
00127 }
00128
00129 {
00130 QRadialGradient gradient(gradientCenter, size() - 2);
00131 gradient.setFocalPoint(gradientCenter);
00132 d->bgColor.setAlpha(64);
00133 d->fgColor.setAlpha(64);
00134 gradient.setColorAt(0, d->bgColor);
00135 gradient.setColorAt(.85, d->bgColor);
00136 gradient.setColorAt(.95, d->fgColor);
00137 d->fgColor.setAlpha(0);
00138 gradient.setColorAt(1, d->fgColor);
00139
00140 painter->save();
00141 painter->setPen(Qt::NoPen);
00142 painter->setRenderHint(QPainter::Antialiasing, true);
00143 painter->setBrush(gradient);
00144 QPainterPath p = shape();
00145 painter->drawPath(p);
00146 painter->restore();
00147 }
00148
00149 QRect iconRect;
00150
00151
00152 if (corner() == ToolBox::Bottom) {
00153 iconRect = QRect(QPoint(gradientCenter.x() - iconSize().width() / 2,
00154 (int)rect.bottom() - iconSize().height() - 2), iconSize());
00155 } else if (corner() == ToolBox::Left) {
00156 iconRect = QRect(QPoint(2, gradientCenter.y() - iconSize().height() / 2), iconSize());
00157 } else {
00158 iconRect = QRect(QPoint((int)rect.right() - iconSize().width() + 1,
00159 gradientCenter.y() - iconSize().height() / 2), iconSize());
00160 }
00161
00162 if (qFuzzyCompare(qreal(1.0), progress)) {
00163 d->icon.paint(painter, iconRect);
00164 } else if (qFuzzyCompare(qreal(1.0), 1 + progress)) {
00165 d->icon.paint(painter, iconRect, Qt::AlignCenter, QIcon::Disabled, QIcon::Off);
00166 } else {
00167 QPixmap disabled = d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off);
00168 QPixmap enabled = d->icon.pixmap(iconSize());
00169 QPixmap result = PaintUtils::transition(
00170 d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off),
00171 d->icon.pixmap(iconSize()), progress);
00172 painter->drawPixmap(iconRect, result);
00173 }
00174 }
00175
00176 QPainterPath PanelToolBox::shape() const
00177 {
00178 QPainterPath path;
00179 int toolSize = size();
00180 QRectF rect = boundingRect();
00181
00182
00183 if (corner() == ToolBox::Bottom) {
00184 path.moveTo(rect.bottomLeft());
00185 path.arcTo(QRectF(rect.center().x() - toolSize,
00186 rect.bottom() - toolSize,
00187 toolSize * 2,
00188 toolSize * 2), 0, 180);
00189 } else if (corner() == ToolBox::Left) {
00190 path.arcTo(QRectF(rect.left(),
00191 rect.center().y() - toolSize,
00192 toolSize * 2,
00193 toolSize * 2), 90, -180);
00194 } else {
00195 path.moveTo(rect.topRight());
00196 path.arcTo(QRectF(rect.left(),
00197 rect.center().y() - toolSize,
00198 toolSize * 2,
00199 toolSize * 2), 90, 180);
00200 }
00201
00202 return path;
00203 }
00204
00205 void PanelToolBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
00206 {
00207 if (showing()) {
00208 QGraphicsItem::hoverEnterEvent(event);
00209 return;
00210 }
00211
00212 showToolBox();
00213 QGraphicsItem::hoverEnterEvent(event);
00214 }
00215
00216 void PanelToolBox::showToolBox()
00217 {
00218 if (showing()) {
00219 return;
00220 }
00221
00222 int maxwidth = 0;
00223 foreach (QGraphicsItem *tool, QGraphicsItem::children()) {
00224 if (!tool->isEnabled()) {
00225 continue;
00226 }
00227 maxwidth = qMax(static_cast<int>(tool->boundingRect().width()), maxwidth);
00228 }
00229
00230
00231 Plasma::Animator *animdriver = Plasma::Animator::self();
00232
00233 if (d->animId) {
00234 animdriver->stopCustomAnimation(d->animId);
00235 }
00236
00237 setShowing(true);
00238
00239
00240 d->animId = animdriver->customAnimation(
00241 10, 240, Plasma::Animator::EaseInCurve, this, "animate");
00242 }
00243
00244 void PanelToolBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
00245 {
00246
00247 if (!d->toggled) {
00248 hideToolBox();
00249 }
00250
00251 QGraphicsItem::hoverLeaveEvent(event);
00252 }
00253
00254 void PanelToolBox::hideToolBox()
00255 {
00256 if (!showing()) {
00257 return;
00258 }
00259
00260 d->toggled = false;
00261 Plasma::Animator *animdriver = Plasma::Animator::self();
00262
00263 if (d->animId) {
00264 animdriver->stopCustomAnimation(d->animId);
00265 }
00266
00267 setShowing(false);
00268 d->animId = animdriver->customAnimation(
00269 10, 240, Plasma::Animator::EaseOutCurve, this, "animate");
00270 }
00271
00272 void PanelToolBox::animate(qreal progress)
00273 {
00274 if (showing()) {
00275 d->animFrame = size() * progress;
00276 } else {
00277 d->animFrame = size() * (1.0 - progress);
00278 }
00279
00280
00281
00282 if (progress >= 1) {
00283 d->animId = 0;
00284 }
00285
00286 update();
00287 }
00288
00289 void PanelToolBox::toggle()
00290 {
00291 d->toggled = !d->toggled;
00292 if (showing() && !d->toggled) {
00293 hideToolBox();
00294 }
00295 }
00296
00297 }
00298
00299 #include "paneltoolbox_p.moc"
00300