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 #include <plasma/svg.h>
00035
00036 namespace Plasma
00037 {
00038
00039 class PanelToolBoxPrivate
00040 {
00041 public:
00042 PanelToolBoxPrivate()
00043 : icon("plasma"),
00044 animId(0),
00045 animFrame(0),
00046 highlighting(false)
00047 {
00048 }
00049
00050
00051 KIcon icon;
00052 int animId;
00053 qreal animFrame;
00054 QColor fgColor;
00055 QColor bgColor;
00056 Plasma::Svg *background;
00057 bool highlighting;
00058 };
00059
00060 PanelToolBox::PanelToolBox(Containment *parent)
00061 : ToolBox(parent),
00062 d(new PanelToolBoxPrivate)
00063 {
00064 connect(this, SIGNAL(toggled()), this, SLOT(toggle()));
00065
00066 setZValue(10000000);
00067 setFlag(ItemClipsChildrenToShape, false);
00068
00069
00070 setFlag(ItemIgnoresTransformations, false);
00071 assignColors();
00072 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
00073 this, SLOT(assignColors()));
00074
00075 d->background = new Plasma::Svg(this);
00076 d->background->setImagePath("widgets/toolbox");
00077 d->background->setContainsMultipleImages(true);
00078 }
00079
00080 PanelToolBox::~PanelToolBox()
00081 {
00082 delete d;
00083 }
00084
00085 void PanelToolBox::assignColors()
00086 {
00087 d->bgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
00088 d->fgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
00089 }
00090
00091 QRectF PanelToolBox::boundingRect() const
00092 {
00093 QRectF r;
00094
00095
00096 if (corner() == ToolBox::Bottom) {
00097 r = QRectF(0, 0, size() * 2, size());
00098 } else if (corner() == ToolBox::Left) {
00099 r = QRectF(0, 0, size(), size() * 2);
00100 } else {
00101 r = QRectF(0, 0, size(), size() * 2);
00102 }
00103
00104 if (parentItem()) {
00105 QSizeF s = parentItem()->boundingRect().size();
00106
00107 if (r.height() > s.height()) {
00108 r.setHeight(s.height());
00109 }
00110
00111 if (r.width() > s.width()) {
00112 r.setWidth(s.width());
00113 }
00114 }
00115
00116 return r;
00117 }
00118
00119 void PanelToolBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00120 {
00121 Q_UNUSED(option)
00122 Q_UNUSED(widget)
00123
00124 const qreal progress = d->animFrame / size();
00125
00126 QRect backgroundRect;
00127 QPoint gradientCenter;
00128 QRectF rect = boundingRect();
00129 QString cornerElement;
00130
00131 if (corner() == ToolBox::Bottom) {
00132 gradientCenter = QPoint(rect.center().x(), rect.bottom());
00133 cornerElement = "panel-south";
00134
00135 backgroundRect = d->background->elementRect(cornerElement).toRect();
00136 backgroundRect.moveBottomLeft(shape().boundingRect().bottomLeft().toPoint());
00137 } else if (corner() == ToolBox::Right) {
00138 gradientCenter = QPoint(rect.right(), rect.center().y());
00139 cornerElement = "panel-east";
00140
00141 backgroundRect = d->background->elementRect(cornerElement).toRect();
00142 backgroundRect.moveTopRight(shape().boundingRect().topRight().toPoint());
00143 } else {
00144 gradientCenter = QPoint(rect.right(), rect.center().y());
00145 cornerElement = "panel-west";
00146
00147 backgroundRect = d->background->elementRect(cornerElement).toRect();
00148 backgroundRect.moveTopLeft(shape().boundingRect().topLeft().toPoint());
00149 }
00150
00151
00152 d->background->paint(painter, backgroundRect, cornerElement);
00153
00154
00155 QRect iconRect;
00156
00157
00158 if (corner() == ToolBox::Bottom) {
00159 iconRect = QRect(QPoint(gradientCenter.x() - iconSize().width() / 2,
00160 (int)rect.bottom() - iconSize().height() - 2), iconSize());
00161 } else if (corner() == ToolBox::Left) {
00162 iconRect = QRect(QPoint(2, gradientCenter.y() - iconSize().height() / 2), iconSize());
00163 } else {
00164 iconRect = QRect(QPoint((int)rect.right() - iconSize().width() + 1,
00165 gradientCenter.y() - iconSize().height() / 2), iconSize());
00166 }
00167
00168 if (qFuzzyCompare(qreal(1.0), progress)) {
00169 d->icon.paint(painter, iconRect);
00170 } else if (qFuzzyCompare(qreal(1.0), 1 + progress)) {
00171 d->icon.paint(painter, iconRect, Qt::AlignCenter, QIcon::Disabled, QIcon::Off);
00172 } else {
00173 QPixmap disabled = d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off);
00174 QPixmap enabled = d->icon.pixmap(iconSize());
00175 QPixmap result = PaintUtils::transition(
00176 d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off),
00177 d->icon.pixmap(iconSize()), progress);
00178 painter->drawPixmap(iconRect, result);
00179 }
00180 }
00181
00182 QPainterPath PanelToolBox::shape() const
00183 {
00184 QPainterPath path;
00185 int toolSize = size();
00186 QRectF rect = boundingRect();
00187
00188
00189 if (corner() == ToolBox::Bottom) {
00190 path.moveTo(rect.bottomLeft());
00191 path.arcTo(QRectF(rect.center().x() - toolSize,
00192 rect.bottom() - toolSize,
00193 toolSize * 2,
00194 toolSize * 2), 0, 180);
00195 } else if (corner() == ToolBox::Left) {
00196 path.arcTo(QRectF(rect.left(),
00197 rect.center().y() - toolSize,
00198 toolSize * 2,
00199 toolSize * 2), 90, -180);
00200 } else {
00201 path.moveTo(rect.topRight());
00202 path.arcTo(QRectF(rect.left(),
00203 rect.center().y() - toolSize,
00204 toolSize * 2,
00205 toolSize * 2), 90, 180);
00206 }
00207
00208 return path;
00209 }
00210
00211 void PanelToolBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
00212 {
00213 highlight(true);
00214 QGraphicsItem::hoverEnterEvent(event);
00215 }
00216
00217 void PanelToolBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
00218 {
00219
00220 if (!showing()) {
00221 highlight(false);
00222 }
00223
00224 QGraphicsItem::hoverLeaveEvent(event);
00225 }
00226
00227 void PanelToolBox::showToolBox()
00228 {
00229 setShowing(true);
00230 highlight(true);
00231 }
00232
00233 void PanelToolBox::hideToolBox()
00234 {
00235 setShowing(false);
00236 highlight(false);
00237 }
00238
00239 void PanelToolBox::highlight(bool highlighting)
00240 {
00241 if (d->highlighting == highlighting) {
00242 return;
00243 }
00244
00245 Plasma::Animator *animdriver = Plasma::Animator::self();
00246
00247 if (d->animId) {
00248 animdriver->stopCustomAnimation(d->animId);
00249 }
00250
00251 d->highlighting = highlighting;
00252 d->animId = animdriver->customAnimation(10, 240,
00253 highlighting ? Plasma::Animator::EaseInCurve
00254 : Plasma::Animator::EaseOutCurve,
00255 this, "animate");
00256 }
00257
00258 void PanelToolBox::animate(qreal progress)
00259 {
00260 if (d->highlighting) {
00261 d->animFrame = size() * progress;
00262 } else {
00263 d->animFrame = size() * (1.0 - progress);
00264 }
00265
00266
00267
00268 if (progress >= 1) {
00269 d->animId = 0;
00270 }
00271
00272 update();
00273 }
00274
00275 void PanelToolBox::toggle()
00276 {
00277 setShowing(!showing());
00278 highlight(showing());
00279 }
00280
00281 }
00282
00283 #include "paneltoolbox_p.moc"
00284