diff --git a/containment.cpp b/containment.cpp index 12599f106..7ee9f51ad 100644 --- a/containment.cpp +++ b/containment.cpp @@ -307,6 +307,10 @@ void Containment::save(KConfigGroup &g) const group.writeEntry("location", (int)d->location); group.writeEntry("activity", d->context()->currentActivity()); + if (d->toolBox) { + d->toolBox->save(group); + } + if (d->wallpaper) { group.writeEntry("wallpaperplugin", d->wallpaper->pluginName()); group.writeEntry("wallpaperpluginmode", d->wallpaper->renderingMode().name()); @@ -1476,6 +1480,7 @@ ToolBox *ContainmentPrivate::createToolBox() if (toolBox) { QObject::connect(toolBox, SIGNAL(toggled()), q, SIGNAL(toolBoxToggled())); + toolBox->load(); positionToolBox(); } } @@ -1485,73 +1490,8 @@ ToolBox *ContainmentPrivate::createToolBox() void ContainmentPrivate::positionToolBox() { - if (!toolBox) { - return; - } - - kDebug(); - //The placement assumes that the geometry width/height is no more than the screen - if (type == Containment::PanelContainment) { - if (q->formFactor() == Vertical) { - toolBox->setCorner(ToolBox::Bottom); - toolBox->setPos(q->geometry().width() / 2 - toolBox->boundingRect().width() / 2, - q->geometry().height()); - } else { - //defaulting to Horizontal right now - if (QApplication::layoutDirection() == Qt::RightToLeft) { - toolBox->setPos(q->geometry().left(), - q->geometry().height() / 2 - toolBox->boundingRect().height() / 2); - toolBox->setCorner(ToolBox::Left); - } else { - toolBox->setPos(q->geometry().width(), - q->geometry().height() / 2 - toolBox->boundingRect().height() / 2); - toolBox->setCorner(ToolBox::Right); - } - } - } else if (q->corona()) { - kDebug() << "desktop"; - - QRectF avail = q->corona()->availableScreenRegion(screen).boundingRect(); - QRectF screenGeom = q->corona()->screenGeometry(screen); - - // Transform to the containment's coordinate system. - avail.translate(-screenGeom.topLeft()); - screenGeom.moveTo(0, 0); - const int toolBoxSize = toolBox->size(); - - if (!q->view() || !q->view()->transform().isScaling()) { - if (QApplication::layoutDirection() == Qt::RightToLeft) { - if (avail.top() > screenGeom.top()) { - toolBox->setPos(avail.topLeft() - QPoint(0, toolBoxSize)); - toolBox->setCorner(ToolBox::Left); - } else if (avail.left() > screenGeom.left()) { - toolBox->setPos(avail.topLeft() - QPoint(toolBoxSize, 0)); - toolBox->setCorner(ToolBox::Top); - } else { - toolBox->setPos(avail.topLeft()); - toolBox->setCorner(ToolBox::TopLeft); - } - } else { - if (avail.top() > screenGeom.top()) { - toolBox->setPos(avail.topRight() - QPoint(0, toolBoxSize)); - toolBox->setCorner(ToolBox::Right); - } else if (avail.right() < screenGeom.right()) { - toolBox->setPos(avail.topRight() - QPoint(toolBoxSize, 0)); - toolBox->setCorner(ToolBox::Top); - } else { - toolBox->setPos(avail.topRight() - QPoint(toolBoxSize, 0)); - toolBox->setCorner(ToolBox::TopRight); - } - } - } else { - if (QApplication::layoutDirection() == Qt::RightToLeft) { - toolBox->setPos(q->mapFromScene(QPointF(q->geometry().topLeft()))); - toolBox->setCorner(ToolBox::TopLeft); - } else { - toolBox->setPos(q->mapFromScene(QPointF(q->geometry().topRight()))); - toolBox->setCorner(ToolBox::TopRight); - } - } + if (toolBox) { + toolBox->reposition(); } } diff --git a/private/desktoptoolbox.cpp b/private/desktoptoolbox.cpp index dcde231a7..ce0b1c444 100644 --- a/private/desktoptoolbox.cpp +++ b/private/desktoptoolbox.cpp @@ -103,7 +103,7 @@ public: bool hovering : 1; }; -DesktopToolBox::DesktopToolBox(QGraphicsItem *parent) +DesktopToolBox::DesktopToolBox(Containment *parent) : ToolBox(parent), d(new DesktopToolBoxPrivate) { diff --git a/private/desktoptoolbox_p.h b/private/desktoptoolbox_p.h index ae2815680..8b1a96711 100644 --- a/private/desktoptoolbox_p.h +++ b/private/desktoptoolbox_p.h @@ -43,7 +43,7 @@ class DesktopToolBox : public ToolBox Q_OBJECT public: - explicit DesktopToolBox(QGraphicsItem *parent = 0); + explicit DesktopToolBox(Containment *parent = 0); ~DesktopToolBox(); QRectF boundingRect() const; QPainterPath shape() const; diff --git a/private/paneltoolbox.cpp b/private/paneltoolbox.cpp index f3b57e876..90db54e08 100644 --- a/private/paneltoolbox.cpp +++ b/private/paneltoolbox.cpp @@ -95,7 +95,7 @@ public: bool toggled; }; -PanelToolBox::PanelToolBox(QGraphicsItem *parent) +PanelToolBox::PanelToolBox(Containment *parent) : ToolBox(parent), d(new PanelToolBoxPrivate) { diff --git a/private/paneltoolbox_p.h b/private/paneltoolbox_p.h index 2e96413b9..9751f1bfc 100644 --- a/private/paneltoolbox_p.h +++ b/private/paneltoolbox_p.h @@ -43,7 +43,7 @@ class PanelToolBox : public ToolBox Q_OBJECT public: - explicit PanelToolBox(QGraphicsItem *parent = 0); + explicit PanelToolBox(Containment *parent); ~PanelToolBox(); QRectF boundingRect() const; QPainterPath shape() const; diff --git a/private/toolbox.cpp b/private/toolbox.cpp index 215cbf04c..92420f655 100644 --- a/private/toolbox.cpp +++ b/private/toolbox.cpp @@ -21,14 +21,18 @@ #include "toolbox_p.h" #include +#include #include +#include #include #include #include +#include #include -#include +#include "corona.h" +#include "theme.h" #include "widgets/iconwidget.h" namespace Plasma @@ -37,16 +41,18 @@ namespace Plasma class ToolBoxPrivate { public: - ToolBoxPrivate() - : size(50), - iconSize(32, 32), - corner(ToolBox::TopRight), - hidden(false), - showing(false), - movable(false), - dragging(false) + ToolBoxPrivate(Containment *c) + : containment(c), + size(50), + iconSize(32, 32), + corner(ToolBox::TopRight), + hidden(false), + showing(false), + movable(false), + dragging(false) {} + Containment *containment; int size; QSize iconSize; ToolBox::Corner corner; @@ -55,11 +61,12 @@ public: bool showing : 1; bool movable : 1; bool dragging : 1; + bool userMoved : 1; }; -ToolBox::ToolBox(QGraphicsItem *parent) +ToolBox::ToolBox(Containment *parent) : QGraphicsItem(parent), - d(new ToolBoxPrivate) + d(new ToolBoxPrivate(parent)) { setAcceptsHoverEvents(true); } @@ -194,6 +201,7 @@ void ToolBox::mouseMoveEvent(QGraphicsSceneMouseEvent *event) // sticky points at midpoints // change how buttons appear depending on the location of the box d->dragging = true; + d->userMoved = true; const QPoint newPos = mapToParent(event->pos()).toPoint(); const QPoint curPos = pos().toPoint(); const int h = abs(boundingRect().height()); @@ -284,6 +292,131 @@ void ToolBox::setIsMovable(bool movable) d->movable = movable; } +void ToolBox::save(KConfigGroup &cg) const +{ + if (!d->movable) { + return; + } + + KConfigGroup group(&cg, "ToolBox"); + if (!d->userMoved) { + group.deleteGroup(); + return; + } + + int offset = 0; + if (d->corner == ToolBox::Left || + d->corner == ToolBox::Right) { + offset = y(); + } else if (d->corner == ToolBox::Left || + d->corner == ToolBox::Right) { + offset = x(); + } + + group.writeEntry("corner", int(d->corner)); + group.writeEntry("offset", offset); +} + +void ToolBox::load() +{ + if (!d->movable) { + return; + } + + KConfigGroup group = d->containment->config(); + group = KConfigGroup(&group, "ToolBox"); + + if (!group.hasKey("corner")) { + return; + } + + d->userMoved = true; + d->corner = Corner(group.readEntry("corner", int(d->corner))); + + int offset = group.readEntry("offset", 0); + if (d->corner == ToolBox::Left) { + setPos(0, offset); + } else if (d->corner == ToolBox::Right) { + setPos(d->containment->size().width() - d->size, offset); + } else if (d->corner == ToolBox::Top) { + setPos(offset, 0); + } else if (d->corner == ToolBox::Bottom) { + setPos(offset, d->containment->size().height() - d->size); + } +} + +void ToolBox::reposition() +{ + if (d->userMoved) { + //FIXME: adjust for situations like changing of the available space + load(); + return; + } + + if (d->containment->containmentType() == Containment::PanelContainment) { + if (d->containment->formFactor() == Vertical) { + setCorner(ToolBox::Bottom); + setPos(d->containment->geometry().width() / 2 - boundingRect().width() / 2, + d->containment->geometry().height()); + } else { + //defaulting to Horizontal right now + if (QApplication::layoutDirection() == Qt::RightToLeft) { + setPos(d->containment->geometry().left(), + d->containment->geometry().height() / 2 - boundingRect().height() / 2); + setCorner(ToolBox::Left); + } else { + setPos(d->containment->geometry().width(), + d->containment->geometry().height() / 2 - boundingRect().height() / 2); + setCorner(ToolBox::Right); + } + } + } else if (d->containment->corona()) { + //kDebug() << "desktop"; + + int screen = d->containment->screen(); + QRectF avail = d->containment->corona()->availableScreenRegion(screen).boundingRect(); + QRectF screenGeom = d->containment->corona()->screenGeometry(screen); + + // Transform to the containment's coordinate system. + avail.translate(-screenGeom.topLeft()); + screenGeom.moveTo(0, 0); + + if (!d->containment->view() || !d->containment->view()->transform().isScaling()) { + if (QApplication::layoutDirection() == Qt::RightToLeft) { + if (avail.top() > screenGeom.top()) { + setPos(avail.topLeft() - QPoint(0, d->size)); + setCorner(ToolBox::Left); + } else if (avail.left() > screenGeom.left()) { + setPos(avail.topLeft() - QPoint(d->size, 0)); + setCorner(ToolBox::Top); + } else { + setPos(avail.topLeft()); + setCorner(ToolBox::TopLeft); + } + } else { + if (avail.top() > screenGeom.top()) { + setPos(avail.topRight() - QPoint(0, d->size)); + setCorner(ToolBox::Right); + } else if (avail.right() < screenGeom.right()) { + setPos(avail.topRight() - QPoint(d->size, 0)); + setCorner(ToolBox::Top); + } else { + setPos(avail.topRight() - QPoint(d->size, 0)); + setCorner(ToolBox::TopRight); + } + } + } else { + if (QApplication::layoutDirection() == Qt::RightToLeft) { + setPos(d->containment->mapFromScene(QPointF(d->containment->geometry().topLeft()))); + setCorner(ToolBox::TopLeft); + } else { + setPos(d->containment->mapFromScene(QPointF(d->containment->geometry().topRight()))); + setCorner(ToolBox::TopRight); + } + } + } +} + } // plasma namespace #include "toolbox_p.moc" diff --git a/private/toolbox_p.h b/private/toolbox_p.h index 0763c2d9f..1a2488856 100644 --- a/private/toolbox_p.h +++ b/private/toolbox_p.h @@ -24,10 +24,12 @@ #include #include -#include "animator.h" +#include "containment.h" class QAction; +class KConfigGroup; + namespace Plasma { @@ -54,7 +56,7 @@ public: BottomLeft }; - explicit ToolBox(QGraphicsItem *parent = 0); + explicit ToolBox(Containment *parent); ~ToolBox(); /** @@ -78,6 +80,10 @@ public: bool isMovable() const; void setIsMovable(bool movable); + void save(KConfigGroup &cg) const; + void load(); + void reposition(); + virtual void showToolBox() = 0; virtual void hideToolBox() = 0; public Q_SLOTS: